Skip to content

Commit

Permalink
fix: fix CallConfig onSuccess Result type, and bug using patchState i…
Browse files Browse the repository at this point in the history
…nside onSuccess

Fixed bug using patchState inside onSuccess, and added typedConfigCall helper method to get the
result param in onSuccess typed

Fixes #64 #65
  • Loading branch information
Gabriel Guerrero authored and gabrielguerrero committed May 7, 2024
1 parent cc92c5b commit 8b9d5cf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type CallConfig<
mapPipe?: 'switchMap' | 'concatMap' | 'exhaustMap';
storeResult?: boolean;
onSuccess?: (result: Result) => void;
onError?: (error: any) => void;
onError?: (error: unknown) => void;
};
export type ExtractCallResultType<T extends Call | CallConfig> =
T extends Call<any, infer R>
Expand Down
24 changes: 16 additions & 8 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TestBed } from '@angular/core/testing';
import { signalStore, withState } from '@ngrx/signals';
import { Subject, throwError } from 'rxjs';
import { patchState, signalStore, withState } from '@ngrx/signals';
import { Subject, tap, throwError } from 'rxjs';

import { withCalls } from '../index';
import { typedCallConfig, withCalls } from '../index';

describe('withCalls', () => {
const apiResponse = new Subject<string>();
Expand Down Expand Up @@ -72,15 +72,22 @@ describe('withCalls', () => {
TestBed.runInInjectionContext(() => {
const Store = signalStore(
withState({ foo: 'bar' }),
withCalls(() => ({
testCall: {
withCalls((store) => ({
testCall: typedCallConfig({
call: ({ ok }: { ok: boolean }) => {
return ok ? apiResponse : throwError(() => new Error('fail'));
return ok
? apiResponse
: apiResponse.pipe(
tap(() => throwError(() => new Error('fail'))),
);
},
storeResult: false,
onSuccess,
onSuccess: (result) => {
// patchState should be able to update the store inside onSuccess
patchState(store, { foo: result });
},
onError,
},
}),
})),
);
const store = new Store();
Expand All @@ -91,6 +98,7 @@ describe('withCalls', () => {
expect(store.isTestCallLoaded()).toBeTruthy();
expect((store as any).testCallResult).toBeUndefined();
expect(onSuccess).toHaveBeenCalledWith('test');
expect(store.foo()).toBe('test');
});
});
});
Expand Down
22 changes: 16 additions & 6 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ import { getWithCallKeys } from './with-calls.util';
/**
* Generates necessary state, computed and methods to track the progress of the call
* and store the result of the call
* @param callsFactory
* @param {callsFactory} callsFactory - a factory function that receives the store and returns an object of type {Record<string, Call | CallConfig>} with the calls to be made
*
* @example
* withCalls(({ productsSelectedEntity }) => ({
* loadProductDetail: {
* loadProductDetail: typedCallConfig({
* call: ({ id }: { id: string }) =>
* inject(ProductService).getProductDetail(id),
* resultProp: 'productDetail',
Expand All @@ -65,7 +65,7 @@ import { getWithCallKeys } from './with-calls.util';
* onError: (error) => {
* // do something with the error
* },
* },
* }),
* checkout: () =>
* inject(OrderService).checkout({
* productId: productsSelectedEntity()!.id,
Expand Down Expand Up @@ -121,10 +121,12 @@ export function withCalls<
}
> {
return (store) => {
const { slices, methods, signals, hooks, ...rest } = store;
const calls = callsFactory({
...store.slices,
...store.signals,
...store.methods,
...slices,
...signals,
...methods,
...rest,
} as Prettify<
SignalStoreSlices<Input['state']> &
Input['signals'] &
Expand Down Expand Up @@ -256,3 +258,11 @@ const mapPipes = {
concatMap: concatMap,
exhaustMap: exhaustMap,
};

export function typedCallConfig<
Params extends readonly any[] = any[],
Result = any,
PropName extends string = string,
>(config: CallConfig<Params, Result, PropName>) {
return config;
}

0 comments on commit 8b9d5cf

Please sign in to comment.