UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

123 lines (122 loc) 4.92 kB
/** * @module Utilities */ import { type IInvokableObject, type Invokable, type InvokableFn, type OneOrMore, type Promisable } from "../../../utilities/_module-exports.js"; import type { HookContext } from "../../../utilities/classes/hooks/types.js"; /** * * IMPORT_PATH: `"@daiso-tech/core/utilities"` * @group Hooks */ export type AsyncNextFunc<TParameters extends unknown[] = unknown[], TReturn = unknown> = InvokableFn<TParameters, PromiseLike<TReturn>>; /** * * IMPORT_PATH: `"@daiso-tech/core/utilities"` * @group Hooks */ export type AsyncMiddlewareFn<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext = object> = InvokableFn<[ arguments_: TParameters, next: AsyncNextFunc<TParameters, TReturn>, context: TContext ], Promisable<TReturn>>; /** * * IMPORT_PATH: `"@daiso-tech/core/utilities"` * @group Hooks */ export type IAsyncMiddlewareObject<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext = object> = IInvokableObject<[ arguments_: TParameters, next: AsyncNextFunc<TParameters, TReturn>, context: TContext ], Promisable<TReturn>>; /** * * IMPORT_PATH: `"@daiso-tech/core/utilities"` * @group Hooks */ export type AsyncMiddleware<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext = object> = IAsyncMiddlewareObject<TParameters, TReturn, TContext> | AsyncMiddlewareFn<TParameters, TReturn, TContext>; /** * The `AsyncHooks` class provides a convenient way to change and inspect arguments and return value of both async and sync functions. * For example `AsyncHooks` class can be used to log function arguments and return values. Note this class will always return promise and is immutable. * * IMPORT_PATH: `"@daiso-tech/core/utilities"` * @group Hooks */ export declare class AsyncHooks<TParameters extends unknown[] = unknown[], TReturn = unknown, TContext extends HookContext = HookContext> implements IInvokableObject<TParameters, Promise<TReturn>> { private readonly invokable; private readonly middlewares; /** * You can pass in additional information that can be used by the middleware. */ private readonly context; private static init; private readonly func; /** * @example * ```ts * import { AsyncHooks, type AsyncMiddlewareFn } from "@daiso-tech/core/utilities"; * * function log<TParameters extends unknown[], TReturn>(): AsyncMiddlewareFn<TParameters, TReturn, { funcName: string; }> { * return async (args, next, { funcName }) => { * console.log("FUNCTION_NAME:", funcName); * console.log("ARGUMENTS:", args); * const value = await next(...args); * console.log("RETURN:", value); * return value; * } * } * * function time<TParameters extends unknown[], TReturn>(): AsyncMiddlewareFn<TParameters, TReturn> { * return async (args, next) => { * const start = performance.now(); * const value = await next(...args); * const end = performance.now(); * const time = end - start; * console.log("TIME:", `${String(time)}ms`); * return value; * } * } * * function add(a: number, b: number): number { * return a + b; * } * * const enhancedAdd = new AsyncHooks(add, [ * log(), * time() * ], * // You can provide additional information to `AsyncMiddleware` invokables. * { * funcName: add.name * }); * * // Will log the function name, arguments and return value. * // Will also log the execution time. * const result = await enhancedAdd.invoke(1, 2); * * // Will be 3. * console.log(result); * ``` */ constructor(invokable: Invokable<TParameters, Promisable<TReturn>>, middlewares: NoInfer<OneOrMore<AsyncMiddleware<TParameters, TReturn, TContext>>>, /** * You can pass in additional information that can be used by the middleware. */ context?: TContext); /** * The `pipe` method returns a new `AsyncHooks` instance with the additional `middlewares` applied. */ pipe(middlewares: OneOrMore<AsyncMiddleware<TParameters, TReturn, TContext>>): AsyncHooks<TParameters, TReturn, TContext>; /** * The `pipeWhen` method conditionally applies additional `middlewares`, returning a new `AsyncHooks` instance only if the specified condition is met. */ pipeWhen(condition: boolean, middlewares: OneOrMore<AsyncMiddleware<TParameters, TReturn, TContext>>): AsyncHooks<TParameters, TReturn, TContext>; /** * The `toFunc` will return the function with all middlewares applied. */ toFunc(): InvokableFn<TParameters, PromiseLike<TReturn>>; /** * The `invoke` method executes the constructor's input function, applying all middlewares. */ invoke(...arguments_: TParameters): Promise<TReturn>; }