(
callback: NoInfer<(
state: OnFinishState<
Awaited<ReturnType<TOptions['next']>>,
ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError
>,
options: TOptions,
...rest: TRest
) => Promisable<void>>,
)
| 69 | * Can be used for interceptors or middlewares |
| 70 | */ |
| 71 | export function onFinish<T, TOptions extends { next(): any }, TRest extends any[]>( |
| 72 | callback: NoInfer<( |
| 73 | state: OnFinishState< |
| 74 | Awaited<ReturnType<TOptions['next']>>, |
| 75 | ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError |
| 76 | >, |
| 77 | options: TOptions, |
| 78 | ...rest: TRest |
| 79 | ) => Promisable<void>>, |
| 80 | ): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> { |
| 81 | let state: any |
| 82 | |
| 83 | return async (options, ...rest) => { |
| 84 | try { |
| 85 | const result = await options.next() |
| 86 | state = [null, result, true] |
| 87 | return result |
| 88 | } |
| 89 | catch (error) { |
| 90 | state = [error, undefined, false] |
| 91 | throw error |
| 92 | } |
| 93 | finally { |
| 94 | await callback(state, options, ...rest) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | export function intercept<TOptions extends InterceptableOptions, TResult>( |
| 100 | interceptors: Interceptor<TOptions, TResult>[], |
no test coverage detected