File size: 4,534 Bytes
77731d1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
type HookMethod<Options, Result> = (
options: Options
) => Result | Promise<Result>;
type BeforeHook<Options> = (options: Options) => void | Promise<void>;
type ErrorHook<Options, Error> = (
error: Error,
options: Options
) => unknown | Promise<unknown>;
type AfterHook<Options, Result> = (
result: Result,
options: Options
) => void | Promise<void>;
type WrapHook<Options, Result> = (
hookMethod: HookMethod<Options, Result>,
options: Options
) => Result | Promise<Result>;
type AnyHook<Options, Result, Error> =
| BeforeHook<Options>
| ErrorHook<Options, Error>
| AfterHook<Options, Result>
| WrapHook<Options, Result>;
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
> {
/**
* Invoke before and after hooks
*/
<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">>;
/**
* Add `before` hook for given `name`
*/
before<Name extends HookName>(
name: Name,
beforeHook: BeforeHook<GetType<HooksType[Name], "Options", "O">>
): void;
/**
* Add `error` hook for given `name`
*/
error<Name extends HookName>(
name: Name,
errorHook: ErrorHook<
GetType<HooksType[Name], "Options", "O">,
GetType<HooksType[Name], "Error", "E">
>
): void;
/**
* Add `after` hook for given `name`
*/
after<Name extends HookName>(
name: Name,
afterHook: AfterHook<
GetType<HooksType[Name], "Options", "O">,
GetType<HooksType[Name], "Result", "R">
>
): void;
/**
* Add `wrap` hook for given `name`
*/
wrap<Name extends HookName>(
name: Name,
wrapHook: WrapHook<
GetType<HooksType[Name], "Options", "O">,
GetType<HooksType[Name], "Result", "R">
>
): void;
/**
* Remove added hook for given `name`
*/
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"
>;
}
export interface HookSingular<Options, Result, Error> {
/**
* Invoke before and after hooks
*/
(hookMethod: HookMethod<Options, Result>, options?: Options): Promise<Result>;
/**
* Add `before` hook
*/
before(beforeHook: BeforeHook<Options>): void;
/**
* Add `error` hook
*/
error(errorHook: ErrorHook<Options, Error>): void;
/**
* Add `after` hook
*/
after(afterHook: AfterHook<Options, Result>): void;
/**
* Add `wrap` hook
*/
wrap(wrapHook: WrapHook<Options, Result>): void;
/**
* Remove added hook
*/
remove(hook: AnyHook<Options, Result, Error>): void;
/**
* Public API
*/
api: Pick<
HookSingular<Options, Result, Error>,
"before" | "error" | "after" | "wrap" | "remove"
>;
}
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>;
interface Hook {
new <
HooksType extends Record<string, TypeStore> = Record<
string,
{ Options: any; Result: any; Error: any }
>
>(): HookCollection<HooksType>;
/**
* 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;
|