Skip to content

Commit

Permalink
fix: retrict withCalls functions to only allow up to one param
Browse files Browse the repository at this point in the history
This adds a type restriction to only allow one param in functions declared in the wihCalls config,
necessary because we transfom the fucntion inside an rxMethod which only supports having one param

Fix #78
  • Loading branch information
Gabriel Guerrero authored and gabrielguerrero committed May 21, 2024
1 parent 5d3cc51 commit abc1111
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.model.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Signal } from '@angular/core';
import { Observable } from 'rxjs';

export type Call<Params extends readonly any[] = any[], Result = any> = (
...args: Params
) => Observable<Result> | Promise<Result>;
export type Call<Param extends any = any, Result = any> =
| (() => Observable<Result> | Promise<Result>)
| ((arg: Param) => Observable<Result> | Promise<Result>);
export type CallConfig<
Params extends readonly any[] = any[],
Param = any,
Result = any,
PropName extends string = string,
Error = any,
> = {
call: Call<Params, Result>;
call: Call<Param, Result>;
resultProp: PropName;
mapPipe?: 'switchMap' | 'concatMap' | 'exhaustMap';
storeResult?: boolean;
onSuccess?: (result: Result, param: Params[0]) => void;
mapError?: (error: unknown, param: Params[0]) => Error;
onError?: (error: Error, param: Params[0]) => void;
onSuccess?: (result: Result, param: Param) => void;
mapError?: (error: unknown, param: Param) => Error;
onError?: (error: Error, param: Param) => void;
};
export type ExtractCallResultType<T extends Call | CallConfig> =
T extends Call<any, infer R>
Expand All @@ -25,7 +25,7 @@ export type ExtractCallResultType<T extends Call | CallConfig> =
? R
: never;
export type ExtractCallParams<T extends Call | CallConfig> =
T extends Call<infer P> ? P : T extends CallConfig<infer P> ? P : [];
T extends Call<infer P> ? P : T extends CallConfig<infer P> ? P : undefined;

export type NamedCallsStatusComputed<Prop extends string> = {
[K in Prop as `is${Capitalize<string & K>}Loading`]: Signal<boolean>;
Expand Down
16 changes: 8 additions & 8 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ export function withCalls<
signals: NamedCallsStatusComputed<keyof Calls & string> &
NamedCallsStatusErrorComputed<Calls>;
methods: {
[K in keyof Calls]: ExtractCallParams<Calls[K]> extends []
[K in keyof Calls]: Calls[K] extends (() => any) | CallConfig<undefined>
? { (): void }
: {
(
param:
| ExtractCallParams<Calls[K]>[0]
| Observable<ExtractCallParams<Calls[K]>[0]>
| Signal<ExtractCallParams<Calls[K]>[0]>,
| ExtractCallParams<Calls[K]>
| Observable<ExtractCallParams<Calls[K]>>
| Signal<ExtractCallParams<Calls[K]>>,
): void;
};
};
Expand Down Expand Up @@ -286,18 +286,18 @@ const mapPipes = {
};

export function typedCallConfig<
Params extends readonly any[] = any[],
Param extends any = undefined,
Result = any,
PropName extends string = '',
Error = unknown,
C extends CallConfig<Params, Result, PropName, Error> = CallConfig<
Params,
C extends CallConfig<Param, Result, PropName, Error> = CallConfig<
Param,
Result,
PropName,
Error
>,
>(
config: Omit<CallConfig<Params, Result, PropName, Error>, 'resultProp'> & {
config: Omit<CallConfig<Param, Result, PropName, Error>, 'resultProp'> & {
resultProp?: PropName;
},
) {
Expand Down

0 comments on commit abc1111

Please sign in to comment.