wishthis/node_modules/before-after-hook/index.d.ts

187 lines
4.4 KiB
TypeScript
Raw Normal View History

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