-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typescript): improved definitions (#60)
* add generic fallback: new Singular * remove redundant types * rename arguments & remove redundant types * complete HookSingular * fix wrong return types * complete HookCollection * add types validation * scripts: add validate:ts to posttest * export interfaces * add generic for error type
- Loading branch information
1 parent
a3ad872
commit e57c123
Showing
5 changed files
with
195 additions
and
142 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,96 @@ | ||
declare module "before-after-hook" { | ||
export interface HookCollection { | ||
/** | ||
* Invoke before and after hooks. | ||
*/ | ||
(name: string | string[], method: (options: any) => Promise<any> | any): Promise<any> | ||
/** | ||
* Invoke before and after hooks. | ||
*/ | ||
(name: string | string[], method: (options: any) => Promise<any> | any, options: any): Promise<any> | ||
/** | ||
* Add before hook for given name. Returns `hook` instance for chaining. | ||
*/ | ||
before (name: string, method: (options: any) => Promise<any> | any): HookCollection | ||
/** | ||
* Add error hook for given name. Returns `hook` instance for chaining. | ||
*/ | ||
error (name: string, method: (options: any) => Promise<any> | any): HookCollection | ||
/** | ||
* Add after hook for given name. Returns `hook` instance for chaining. | ||
*/ | ||
after (name: string, method: (options: any) => Promise<any> | any): HookCollection | ||
/** | ||
* Add wrap hook for given name. Returns `hook` instance for chaining. | ||
*/ | ||
wrap (name: string, method: (options: any) => Promise<any> | any): HookCollection | ||
/** | ||
* Removes hook for given name. Returns `hook` instance for chaining. | ||
*/ | ||
remove (name: string, beforeHookMethod: (options: any) => Promise<any> | any): HookCollection | ||
} | ||
type HookMethod<O, R> = (options: O) => R | Promise<R> | ||
|
||
export interface HookSingular<T> { | ||
/** | ||
* Invoke before and after hooks without options | ||
*/ | ||
(method: (options: T) => T | null | void): Promise<T> | ||
/** | ||
* Invoke before and after hooks without options | ||
*/ | ||
(method: (options: T) => Promise<T | null | void>): Promise<T> | ||
/** | ||
* Invoke before and after hooks with options | ||
*/ | ||
(method: (options: T) => T | null | void, options: T): Promise<T> | ||
/** | ||
* Invoke before and after hooks with options | ||
*/ | ||
(method: (options: T) => Promise<T | null | void>, options: T): Promise<T> | ||
type BeforeHook<O> = (options: O) => void | ||
type ErrorHook<O, E> = (error: E, options: O) => void | ||
type AfterHook<O, R> = (result: R, options: O) => void | ||
type WrapHook<O, R> = ( | ||
hookMethod: HookMethod<O, R>, | ||
options: O | ||
) => R | Promise<R> | ||
|
||
/** | ||
* Add before hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
before( | ||
beforeFn: (options: T) => T | null | void | ||
): HookSingular<T>; | ||
/** | ||
* Add before hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
before( | ||
beforeFn: (options: T) => Promise<T | null | void> | ||
): HookSingular<T>; | ||
type AnyHook<O, R, E> = | ||
| BeforeHook<O> | ||
| ErrorHook<O, E> | ||
| AfterHook<O, R> | ||
| WrapHook<O, R> | ||
|
||
/** | ||
* Add error hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
error( | ||
errorFn: (options: T) => T | null | void | ||
): HookSingular<T>; | ||
/** | ||
* Add error hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
error( | ||
errorFn: (options: T) => Promise<T | null | void> | ||
): HookSingular<T>; | ||
|
||
/** | ||
* Add after hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
after( | ||
afterFn: (options: T) => T | null | void | ||
): HookSingular<T>; | ||
/** | ||
* Add after hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
after( | ||
afterFn: (options: T) => Promise<T | null | void> | ||
): HookSingular<T>; | ||
export interface HookCollection { | ||
/** | ||
* Invoke before and after hooks | ||
*/ | ||
( | ||
name: string | string[], | ||
hookMethod: HookMethod<any, any>, | ||
options?: any | ||
): Promise<any> | ||
/** | ||
* Add `before` hook for given `name` | ||
*/ | ||
before(name: string, beforeHook: BeforeHook<any>): void | ||
/** | ||
* Add `error` hook for given `name` | ||
*/ | ||
error(name: string, errorHook: ErrorHook<any, any>): void | ||
/** | ||
* Add `after` hook for given `name` | ||
*/ | ||
after(name: string, afterHook: AfterHook<any, any>): void | ||
/** | ||
* Add `wrap` hook for given `name` | ||
*/ | ||
wrap(name: string, wrapHook: WrapHook<any, any>): void | ||
/** | ||
* Remove added hook for given `name` | ||
*/ | ||
remove(name: string, hook: AnyHook<any, any, any>): void | ||
} | ||
|
||
/** | ||
* Add wrap hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
wrap( | ||
wrapFn: (options: T) => T | null | void | ||
): HookSingular<T>; | ||
/** | ||
* Add wrap hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
wrap( | ||
wrapFn: (options: T) => Promise<T | null | void> | ||
): HookSingular<T>; | ||
export interface HookSingular<O, R, E> { | ||
/** | ||
* Invoke before and after hooks | ||
*/ | ||
(hookMethod: HookMethod<O, R>, options?: O): Promise<R> | ||
/** | ||
* Add `before` hook | ||
*/ | ||
before(beforeHook: BeforeHook<O>): void | ||
/** | ||
* Add `error` hook | ||
*/ | ||
error(errorHook: ErrorHook<O, E>): void | ||
/** | ||
* Add `after` hook | ||
*/ | ||
after(afterHook: AfterHook<O, R>): void | ||
/** | ||
* Add `wrap` hook | ||
*/ | ||
wrap(wrapHook: WrapHook<O, R>): void | ||
/** | ||
* Remove added hook | ||
*/ | ||
remove(hook: AnyHook<O, R, E>): void | ||
} | ||
|
||
/** | ||
* Removes hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
remove( | ||
beforeHookMethod: (options: T) => T | null | void | ||
): HookSingular<T>; | ||
/** | ||
* Removes hook. Returns `UnnamedHook` instance for chaining. | ||
*/ | ||
remove( | ||
beforeHookMethod: (options: T) => Promise<T | null | void> | ||
): HookSingular<T>; | ||
} | ||
type Collection = new () => HookCollection | ||
type Singular = new <O = any, R = any, E = any>() => HookSingular<O, R, E> | ||
|
||
export const Hook: { | ||
new (): HookCollection | ||
interface Hook { | ||
new (): HookCollection | ||
|
||
/** | ||
* Creates a nameless hook that allows passing down typings for the options | ||
*/ | ||
Singular: {new <T>(): HookSingular<T>} | ||
/** | ||
* Creates a collection of hooks | ||
*/ | ||
Collection: Collection | ||
|
||
/** | ||
* Creates a hook collection | ||
*/ | ||
Collection: {new (): HookCollection} | ||
} | ||
/** | ||
* Creates a nameless hook that supports strict typings | ||
*/ | ||
Singular: Singular | ||
} | ||
|
||
export const Singular: {new <T>(): HookSingular<T>} | ||
export const Collection: {new (): HookCollection} | ||
export const Hook: Hook | ||
export const Collection: Collection | ||
export const Singular: Singular | ||
|
||
export = Hook | ||
} | ||
export default Hook |
Oops, something went wrong.