Created
August 7, 2019 14:02
-
-
Save savagematt/109b9274f5987c1c168b766beecbe860 to your computer and use it in GitHub Desktop.
scenario withX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type ReturnValue<T> = T extends (...args: any[]) => infer RETURN | |
? RETURN | |
: T; | |
export type Args<T> = T extends (...args: infer ARGS) => any | |
? ARGS | |
: []; | |
export type CallAsStep<C, T> = (...args: Args<T>) => Step<C, Promise<Resolve<ReturnValue<T>>>>; | |
export type StepGenerator<C, T> = { [K in keyof T]: CallAsStep<C, T[K]> }; | |
export type Resolve<T> = T extends Promise<infer U> ? U :T; | |
export function withX<C, T>(f: (c: C) => T, tPrototype: object): StepGenerator<C, Resolve<T>> { | |
function asStep<K extends keyof T>(k: K): CallAsStep<C, T[K]> { | |
return (...args: Args<T[K]>): Step<C, Promise<Resolve<ReturnValue<T[K]>>>> => { | |
return async (c: C): Promise<Resolve<ReturnValue<T[K]>>> => { | |
const target = await f(c); | |
const value = target[k]; | |
return typeof value === 'function' | |
? await (value as any).call(target, ...args) as Resolve<ReturnValue<T[K]>> | |
: await value as Resolve<ReturnValue<T[K]>>; | |
} | |
}; | |
} | |
return Object.getOwnPropertyNames(tPrototype).reduce( | |
(ret: StepGenerator<C, T>, k: any) => { | |
ret[k as keyof T] = asStep(k); | |
return ret; | |
}, | |
{}) as any; | |
} |
Author
savagematt
commented
Aug 7, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment