Skip to content

Commit

Permalink
feat(typescript): improved definitions (#60)
Browse files Browse the repository at this point in the history
* 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
MunifTanjim authored and gr2m committed Jul 12, 2019
1 parent a3ad872 commit e57c123
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 142 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ hookCollection('add', function (record) {

### hookCollection.before()

Add before hook for given name. Returns `hookCollection` instance for chaining.
Add before hook for given name.

```js
hookCollection.before(name, method)
Expand Down Expand Up @@ -316,7 +316,7 @@ hookCollection.before('save', function validate (record) {

### hookCollection.error()

Add error hook for given name. Returns `hookCollection` instance for chaining.
Add error hook for given name.

```js
hookCollection.error(name, method)
Expand Down Expand Up @@ -362,7 +362,7 @@ hookCollection.error('save', function (error, options) {

### hookCollection.after()

Add after hook for given name. Returns `hook` instance for chaining.
Add after hook for given name.

```js
hookCollection.after(name, method)
Expand Down Expand Up @@ -408,7 +408,7 @@ hookCollection.after('save', function (result, options) {

### hookCollection.wrap()

Add wrap hook for given name. Returns `hookCollection` instance for chaining.
Add wrap hook for given name.

```js
hookCollection.wrap(name, method)
Expand Down Expand Up @@ -468,7 +468,7 @@ See also: [Test mock example](examples/test-mock-example.md)

### hookCollection.remove()

Removes hook for given name. Returns `hookCollection` instance for chaining.
Removes hook for given name.

```js
hookCollection.remove(name, hookMethod)
Expand Down
211 changes: 86 additions & 125 deletions index.d.ts
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
Loading

0 comments on commit e57c123

Please sign in to comment.