```sh npm install --save extra-aspect # or yarn add extra-aspect ```
npm install extra-aspectsh
npm install --save extra-aspect
or
yarn add extra-aspect
`Usage
`ts
import { aspect } from 'extra-aspect'const fn = aspect(
() => {
console.log('called')
return 'return'
}
, {
before: () => console.log('before')
, returning: () => console.log('returning')
}
)
console.log(fn())
// before
// called
// returning
// return
`API
`ts
type IHooks = {
before?: Arrayable<() => unknown>
returning?: Arrayable<(result: T) => unknown>
throwing?: Arrayable<(err: unknown) => unknown>
finally?: Arrayable<() => unknown>
}type IAsyncHooks = {
before?: Arrayable<() => unknown | PromiseLike>
returning?: Arrayable<(result: T) => unknown | PromiseLike>
throwing?: Arrayable<(err: unknown) => unknown | PromiseLike>
finally?: Arrayable<() => unknown | PromiseLike>
}
`$3
`ts
function aspect(
fn: (...args: Args) => T
, hooks: IHooks
): (...args: Args) => T
function aspect(
hooks: IHooks
, fn: (...args: Args) => T
): (...args: Args) => T
`$3
`ts
function aspectAsync(
fn: (...args: Args) => PromiseLike
, hooks: IAsyncHooks
, concurrency: number = 1
): (...args: Args) => Promise
function aspectAsync(
hooks: IAsyncHooks
, fn: (...args: Args) => PromiseLike
, concurrency: number = 1
): (...args: Args) => Promise
`$3
`ts
function compose(...hooks: Array>): IHooks
function compose(...hooks: Array>): IAsyncHooks
function compose(...hooks: Array | IAsyncHooks>): IAsyncHooks
``