@@ -7,11 +7,11 @@ export type Action<TParams extends Array<any>, TOk, TErr extends Error> = (
7
7
..._ : TParams
8
8
) => ResultAsync < TOk , TErr > ;
9
9
10
- export type WrappedAction <
11
- TParams extends Array < any > ,
10
+ export type WrappedAction < TParams extends Array < any > , TOk , TErr extends Error > = Action <
11
+ TParams ,
12
12
TOk ,
13
- TErr extends Error ,
14
- > = Action < TParams , TOk , TErr > & {
13
+ TErr
14
+ > & {
15
15
/**
16
16
* Tracks whether the action is executing
17
17
*/
@@ -23,11 +23,7 @@ export type WrappedActions<
23
23
[ action : string ] : Action < any , any , any > ;
24
24
} ,
25
25
> = {
26
- [ Prop in keyof Actions ] : Actions [ Prop ] extends Action <
27
- infer TParams ,
28
- infer TOk ,
29
- infer TErr
30
- >
26
+ [ Prop in keyof Actions ] : Actions [ Prop ] extends Action < infer TParams , infer TOk , infer TErr >
31
27
? WrappedAction < TParams , TOk , TErr >
32
28
: never ;
33
29
} ;
@@ -109,8 +105,15 @@ export const wrapActions = <
109
105
actions : Actions
110
106
) =>
111
107
Object . fromEntries (
112
- Object . entries ( actions ) . map ( ( [ funcName , func ] ) => [
113
- funcName ,
114
- wrapAction ( func ) ,
115
- ] )
108
+ Object . entries ( actions ) . map ( ( [ funcName , func ] ) => [ funcName , wrapAction ( func ) ] )
116
109
) as WrappedActions < Actions > ;
110
+
111
+ export function wrapPromise < TParams extends Array < any > , TResult > (
112
+ func : ( ..._ : TParams ) => PromiseLike < TResult >
113
+ ) : WrappedAction < TParams , TResult , Error > {
114
+ return wrapAction ( ( ...args : TParams ) =>
115
+ ResultAsync . fromPromise ( func ( ...args ) , ( e ) =>
116
+ e instanceof Error ? e : new Error ( `unknown error: ${ e } ` )
117
+ )
118
+ ) ;
119
+ }
0 commit comments