@akala/core
Version:
81 lines (80 loc) • 4.94 kB
TypeScript
import { type SpecialNextParam } from '../middlewares/shared.js';
import { MiddlewareCompositeAsync } from '../middlewares/composite-async.js';
import { MiddlewareRouteAsync } from './route-async.js';
import type { RouterOptions, Routes } from './router.js';
import type { Routable, RouteBuilderArguments } from './route.js';
import type { MiddlewareAsync } from '../middlewares/shared.js';
import type { UriTemplate } from '../uri-template/index.js';
export type RouteBuilderAsync<T extends [Routable, ...unknown[]], TSpecialNextParam extends SpecialNextParam = SpecialNextParam> = (...args: RouteBuilderArguments) => MiddlewareRouteAsync<T, TSpecialNextParam>;
/**
* Dynamically applies asynchronous routes to a middleware composite.
*
* This function recursively processes route definitions and attaches them to the parent middleware chain.
*
* @template T - The context type containing the routable request and parameters.
* @template TSpecialNextParam - The type for special next parameters (defaults to {@link SpecialNextParam})
* @param {Routes<T, Promise<unknown>, TSpecialNextParam>} routes - Route definitions mapping URI templates to handler functions or nested route objects
* @param {MiddlewareCompositeAsync<T, TSpecialNextParam> & { route: RouteBuilderAsync<T, TSpecialNextParam> }} [parent] - Parent middleware container to attach routes to
* @returns {MiddlewareCompositeAsync<T, TSpecialNextParam>} Configured middleware composite with attached routes
*/
export declare function useRoutesAsync<T extends [Routable, ...unknown[]], TSpecialNextParam extends SpecialNextParam = SpecialNextParam>(routes: Routes<T, Promise<unknown>, TSpecialNextParam>, parent?: MiddlewareCompositeAsync<T, TSpecialNextParam> & {
route: RouteBuilderAsync<T, TSpecialNextParam>;
}): MiddlewareCompositeAsync<T, TSpecialNextParam>;
/**
* Base class for defining asynchronous routing middleware chains.
*
* Extends the middleware composite to provide route configuration capabilities for asynchronous handlers.
*
* @template T - The context type containing the routable request and parameters
* @template TSpecialNextParam - The type for special next parameters (defaults to {@link SpecialNextParam})
* @extends {MiddlewareCompositeAsync<T, TSpecialNextParam>}
* @implements {MiddlewareAsync<T, TSpecialNextParam>}
*/
export declare class RouterAsync<T extends [{
path: string;
params?: Record<string, unknown>;
}, ...unknown[]], TSpecialNextParam extends SpecialNextParam = SpecialNextParam> extends MiddlewareCompositeAsync<T, TSpecialNextParam> implements MiddlewareAsync<T, TSpecialNextParam> {
/**
* Creates a new RouterAsync instance with optional configuration.
*
* @param {RouterOptions} [options] - Configuration options including middleware priority and name
*/
constructor(options?: RouterOptions);
/**
* Creates a new route configuration chain for defining route handlers.
*
* @function route
* @param {...RouteBuilderArguments} args - Route configuration parameters (path template, HTTP method, etc.)
* @returns {MiddlewareRouteAsync<T, TSpecialNextParam>} Newly created route configuration instance
*/
route(...args: RouteBuilderArguments): MiddlewareRouteAsync<T, TSpecialNextParam>;
/**
* Attaches a collection of route definitions to the router.
*
* @param {Routes<T, Promise<unknown>, TSpecialNextParam>} routes - Route definitions to be applied
* @returns {this} Current router instance for method chaining
*/
useRoutes(routes: Routes<T, Promise<unknown>, TSpecialNextParam>): this;
/**
* Adds middleware to the router's chain either globally or for a specific route.
*
* @function useMiddleware
* @param {string | UriTemplate | MiddlewareAsync<T, TSpecialNextParam>} routeOrMiddleware -
* Route definition (string/URI template) or middleware function
* @param {...MiddlewareAsync<T, TSpecialNextParam>} middlewares - Additional middleware functions
* @returns {this} Current router instance for method chaining
*/
useMiddleware(...middlewares: MiddlewareAsync<T, TSpecialNextParam>[]): this;
useMiddleware(routeOrMiddleware: string | UriTemplate | MiddlewareAsync<T, TSpecialNextParam>, ...middlewares: MiddlewareAsync<T, TSpecialNextParam>[]): this;
/**
* Registers route handlers or middleware functions.
*
* @function use
* @param {(string | UriTemplate | ((...args: T) => Promise<unknown>))} routeOrHandler -
* Route definition (string/URI template) or handler function
* @param {...((...args: T) => Promise<unknown>)} handlers - Additional handler functions
* @returns {this} Current router instance for method chaining
*/
use(routeOrHandler: string | UriTemplate, ...handlers: ((...args: T) => Promise<unknown>)[]): this;
use(...handlers: ((...args: T) => Promise<unknown>)[]): this;
}