Skip to content

Commit

Permalink
fix: add warning if api or store not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
SaulMoro committed Mar 13, 2024
1 parent 905c3ae commit d6f444f
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion projects/ngrx-rtk-query/src/lib/create-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,19 @@ export const createApi: CreateApi<typeof coreModuleName | typeof angularHooksMod

const getApiInjector = () =>
(api as unknown as Api<any, Record<string, any>, string, string, AngularHooksModule | CoreModule>).injector;
const getStore = () => getApiInjector().get(Store);
const getStore = () => {
const injector = getApiInjector();
if (!injector) {
throw new Error(
`Provide the API (${reducerPath}) is necessary to use the queries. Did you forget to provide the queries api?`,
);
}
const store = injector.get(Store, undefined, { optional: true });
if (!store) {
throw new Error(`Provide the Store is necessary to use the queries. Did you forget to provide the store?`);
}
return store;
};
const storeDispatch = (action: Action) => {
getStore().dispatch(action);
return action;
Expand Down

0 comments on commit d6f444f

Please sign in to comment.