2021-06-04 22:19:14 +08:00
|
|
|
type HookMethod<Options, Result> = (
|
|
|
|
options: Options
|
|
|
|
) => Result | Promise<Result>
|
2021-02-08 12:58:32 +08:00
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
type BeforeHook<Options> = (options: Options) => void | Promise<void>
|
|
|
|
type ErrorHook<Options, Error> = (
|
|
|
|
error: Error,
|
|
|
|
options: Options
|
|
|
|
) => void | Promise<void>
|
|
|
|
type AfterHook<Options, Result> = (
|
|
|
|
result: Result,
|
|
|
|
options: Options
|
|
|
|
) => void | Promise<void>
|
|
|
|
type WrapHook<Options, Result> = (
|
|
|
|
hookMethod: HookMethod<Options, Result>,
|
|
|
|
options: Options
|
|
|
|
) => Result | Promise<Result>
|
2021-02-08 12:58:32 +08:00
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
type AnyHook<Options, Result, Error> =
|
|
|
|
| BeforeHook<Options>
|
|
|
|
| ErrorHook<Options, Error>
|
|
|
|
| AfterHook<Options, Result>
|
|
|
|
| WrapHook<Options, Result>
|
2021-02-08 12:58:32 +08:00
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
type TypeStoreKeyLong = 'Options' | 'Result' | 'Error'
|
|
|
|
type TypeStoreKeyShort = 'O' | 'R' | 'E'
|
|
|
|
type TypeStore =
|
|
|
|
| ({ [key in TypeStoreKeyLong]?: any } &
|
|
|
|
{ [key in TypeStoreKeyShort]?: never })
|
|
|
|
| ({ [key in TypeStoreKeyLong]?: never } &
|
|
|
|
{ [key in TypeStoreKeyShort]?: any })
|
|
|
|
type GetType<
|
|
|
|
Store extends TypeStore,
|
|
|
|
LongKey extends TypeStoreKeyLong,
|
|
|
|
ShortKey extends TypeStoreKeyShort
|
|
|
|
> = LongKey extends keyof Store
|
|
|
|
? Store[LongKey]
|
|
|
|
: ShortKey extends keyof Store
|
|
|
|
? Store[ShortKey]
|
|
|
|
: any
|
|
|
|
|
|
|
|
export interface HookCollection<
|
|
|
|
HooksType extends Record<string, TypeStore> = Record<
|
|
|
|
string,
|
|
|
|
{ Options: any; Result: any; Error: any }
|
|
|
|
>,
|
|
|
|
HookName extends keyof HooksType = keyof HooksType
|
|
|
|
> {
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Invoke before and after hooks
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
<Name extends HookName>(
|
|
|
|
name: Name | Name[],
|
|
|
|
hookMethod: HookMethod<
|
|
|
|
GetType<HooksType[Name], 'Options', 'O'>,
|
|
|
|
GetType<HooksType[Name], 'Result', 'R'>
|
|
|
|
>,
|
|
|
|
options?: GetType<HooksType[Name], 'Options', 'O'>
|
|
|
|
): Promise<GetType<HooksType[Name], 'Result', 'R'>>
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `before` hook for given `name`
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
before<Name extends HookName>(
|
|
|
|
name: Name,
|
|
|
|
beforeHook: BeforeHook<GetType<HooksType[Name], 'Options', 'O'>>
|
|
|
|
): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `error` hook for given `name`
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
error<Name extends HookName>(
|
|
|
|
name: Name,
|
|
|
|
errorHook: ErrorHook<
|
|
|
|
GetType<HooksType[Name], 'Options', 'O'>,
|
|
|
|
GetType<HooksType[Name], 'Error', 'E'>
|
|
|
|
>
|
|
|
|
): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `after` hook for given `name`
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
after<Name extends HookName>(
|
|
|
|
name: Name,
|
|
|
|
afterHook: AfterHook<
|
|
|
|
GetType<HooksType[Name], 'Options', 'O'>,
|
|
|
|
GetType<HooksType[Name], 'Result', 'R'>
|
|
|
|
>
|
|
|
|
): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `wrap` hook for given `name`
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
wrap<Name extends HookName>(
|
|
|
|
name: Name,
|
|
|
|
wrapHook: WrapHook<
|
|
|
|
GetType<HooksType[Name], 'Options', 'O'>,
|
|
|
|
GetType<HooksType[Name], 'Result', 'R'>
|
|
|
|
>
|
|
|
|
): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Remove added hook for given `name`
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
remove<Name extends HookName>(
|
|
|
|
name: Name,
|
|
|
|
hook: AnyHook<
|
|
|
|
GetType<HooksType[Name], 'Options', 'O'>,
|
|
|
|
GetType<HooksType[Name], 'Result', 'R'>,
|
|
|
|
GetType<HooksType[Name], 'Error', 'E'>
|
|
|
|
>
|
|
|
|
): void
|
|
|
|
/**
|
|
|
|
* Public API
|
|
|
|
*/
|
|
|
|
api: Pick<
|
|
|
|
HookCollection<HooksType>,
|
|
|
|
'before' | 'error' | 'after' | 'wrap' | 'remove'
|
|
|
|
>
|
2021-02-08 12:58:32 +08:00
|
|
|
}
|
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
export interface HookSingular<Options, Result, Error> {
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Invoke before and after hooks
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
(hookMethod: HookMethod<Options, Result>, options?: Options): Promise<Result>
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `before` hook
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
before(beforeHook: BeforeHook<Options>): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `error` hook
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
error(errorHook: ErrorHook<Options, Error>): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `after` hook
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
after(afterHook: AfterHook<Options, Result>): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Add `wrap` hook
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
wrap(wrapHook: WrapHook<Options, Result>): void
|
2021-02-08 12:58:32 +08:00
|
|
|
/**
|
|
|
|
* Remove added hook
|
|
|
|
*/
|
2021-06-04 22:19:14 +08:00
|
|
|
remove(hook: AnyHook<Options, Result, Error>): void
|
|
|
|
/**
|
|
|
|
* Public API
|
|
|
|
*/
|
|
|
|
api: Pick<
|
|
|
|
HookSingular<Options, Result, Error>,
|
|
|
|
'before' | 'error' | 'after' | 'wrap' | 'remove'
|
|
|
|
>
|
2021-02-08 12:58:32 +08:00
|
|
|
}
|
|
|
|
|
2021-06-04 22:19:14 +08:00
|
|
|
type Collection = new <
|
|
|
|
HooksType extends Record<string, TypeStore> = Record<
|
|
|
|
string,
|
|
|
|
{ Options: any; Result: any; Error: any }
|
|
|
|
>
|
|
|
|
>() => HookCollection<HooksType>
|
|
|
|
type Singular = new <
|
|
|
|
Options = any,
|
|
|
|
Result = any,
|
|
|
|
Error = any
|
|
|
|
>() => HookSingular<Options, Result, Error>
|
2021-02-08 12:58:32 +08:00
|
|
|
|
|
|
|
interface Hook {
|
2021-06-04 22:19:14 +08:00
|
|
|
new <
|
|
|
|
HooksType extends Record<string, TypeStore> = Record<
|
|
|
|
string,
|
|
|
|
{ Options: any; Result: any; Error: any }
|
|
|
|
>
|
|
|
|
>(): HookCollection<HooksType>
|
2021-02-08 12:58:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a collection of hooks
|
|
|
|
*/
|
|
|
|
Collection: Collection
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a nameless hook that supports strict typings
|
|
|
|
*/
|
|
|
|
Singular: Singular
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Hook: Hook
|
|
|
|
export const Collection: Collection
|
|
|
|
export const Singular: Singular
|
|
|
|
|
|
|
|
export default Hook
|