Skip to content

Commit 7815be8

Browse files
committed
add wrapPromise
1 parent 228528c commit 7815be8

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

houston-common-ui/lib/composables/wrapActions.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export type Action<TParams extends Array<any>, TOk, TErr extends Error> = (
77
..._: TParams
88
) => ResultAsync<TOk, TErr>;
99

10-
export type WrappedAction<
11-
TParams extends Array<any>,
10+
export type WrappedAction<TParams extends Array<any>, TOk, TErr extends Error> = Action<
11+
TParams,
1212
TOk,
13-
TErr extends Error,
14-
> = Action<TParams, TOk, TErr> & {
13+
TErr
14+
> & {
1515
/**
1616
* Tracks whether the action is executing
1717
*/
@@ -23,11 +23,7 @@ export type WrappedActions<
2323
[action: string]: Action<any, any, any>;
2424
},
2525
> = {
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>
3127
? WrappedAction<TParams, TOk, TErr>
3228
: never;
3329
};
@@ -109,8 +105,15 @@ export const wrapActions = <
109105
actions: Actions
110106
) =>
111107
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)])
116109
) 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

Comments
 (0)