@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.
93 lines (92 loc) • 3.11 kB
TypeScript
/**
* @module Middleware
*/
import { type IExecutionContext } from "../../../execution-context/contracts/_module.js";
import { type Use } from "../../../middleware/contracts/_module.js";
/**
* Configuration options for creating a middleware application function.
*
* Allows customization of the execution context used during middleware execution
* and the default priority assigned to middlewares without an explicit priority.
*
* @example
* ```ts
* const customContext = new ExecutionContext(new CustomAdapter());
* const use = useFactory({
* executionContext: customContext,
* defaultPriority: 100
* });
* ```
*
* @see {@link useFactory | `useFactory`}
* @see {@link IExecutionContext | `IExecutionContext`}
*
* IMPORT_PATH: `@daiso-tech/core/middleware`
* @group Implementations
*/
export type UseFactorySettings = {
/**
* The execution context in which middleware will be executed.
*
* Used to manage state and context across middleware and invokable execution.
* Middleware can access this context via the `context` property in {@link MiddlewareArgs | `MiddlewareArgs`}.
*
* @default
* ```ts
* new ExecutionContext(
* new NoOpExecutionContextAdapter()
* )
* ```
*
* @see {@link IExecutionContext | `IExecutionContext`}
*/
executionContext?: IExecutionContext;
/**
* Default priority for middlewares that do not explicitly set a priority.
*
* Middlewares are executed in order of priority (lower values first).
* This setting applies to function-based middlewares or object middlewares
* that don't specify a priority.
*
* @default 0
*/
defaultPriority?: number;
};
/**
* Creates a middleware application function with optional custom settings.
*
* Returns a {@link Use | `Use`} function that can wrap invokables with middleware chains.
* The factory pattern allows reusing the same configuration across multiple invokables,
* including shared execution context and default middleware priority.
*
* @param settings - Configuration for the middleware application function
*
* @returns A {@link Use | `Use`} function configured with the provided settings
*
* @example
* ```ts
* import { useFactory } from '@daiso-tech/core/middleware';
*
* // Create with default settings
* const use = useFactory();
*
* // Create with custom execution context
* const customContext = new ExecutionContext(new MyAdapter());
* const useWithContext = useFactory({ executionContext: customContext });
*
* // Create with custom default priority
* const useWithPriority = useFactory({ defaultPriority: 50 });
*
* // Use the middleware application function
* const wrappedFunc = use(myFunction, [middleware1, middleware2]);
* const result = wrappedFunc(...args);
* ```
*
* @see {@link Use | `Use`}
* @see {@link UseFactorySettings | `UseFactorySettings`}
* @see {@link Middleware | `Middleware`}
*
* IMPORT_PATH: `@daiso-tech/core/middleware`
* @group Implementations
*/
export declare function useFactory(settings?: UseFactorySettings): Use;