UNPKG

@akala/core

Version:
102 lines (101 loc) 4.2 kB
import { MiddlewareCompositeAsync } from "./middlewares/composite-async.js"; import type { MiddlewareAsync, MiddlewarePromise } from "./middlewares/shared.js"; import type { Routable } from "./router/route.js"; import { RouterAsync } from "./router/router-async.js"; type MiddlewareError = 'break' | 'loop' | undefined; /** * Handles URL routing and middleware processing for different protocol, host, and path components * @template T - Tuple type representing middleware context parameters [URL, ...unknown[], Partial<TResult>] * @template TResult - Result type for middleware processing (default: object) */ export declare class UrlHandler<T extends [URL, ...unknown[], Partial<TResult> | void], TResult = object> implements MiddlewareAsync<T> { private readonly noAssign; /** * Composite middleware stack for protocol-specific processing */ protocol: MiddlewareCompositeAsync<T, MiddlewareError>; /** * Composite middleware stack for host-specific processing */ host: MiddlewareCompositeAsync<T>; /** * Router for path-based routing */ router: RouterAsync<[Routable, ...T]>; /** * Creates a new URL handler instance */ constructor(noAssign?: boolean); /** * warning ! the second parameter needs to be not null as we will assign properties to it. * Processes the URL through protocol, host, and path routing middleware * @param context - Middleware context parameters * @returns Promise resolving to the final TResult object */ /** * Processes the URL through protocol, host, and path routing middleware * @param context - Middleware context parameters * @returns Promise resolving to the final TResult object */ process(...context: T): Promise<TResult>; /** * Adds a protocol handler middleware * @param protocol Protocol to handle (colon will be automatically stripped if present) * @param action Async handler function for protocol processing * @returns Registered protocol middleware instance */ useProtocol(protocol: string, action: (...args: T) => Promise<TResult>): UrlHandler.Protocol<T>; /** * Adds a host handler middleware * @param host Hostname to match * @param action Async handler function for host processing * @returns Registered host middleware instance */ useHost(host: string, action: (...args: T) => Promise<TResult>): UrlHandler.Host<T>; /** * Handles the URL processing pipeline * @param context - Middleware context parameters * @returns Promise that resolves when handling fails or rejects with the final result */ handle(...context: T): MiddlewarePromise; } /** * Namespace containing Protocol and Host middleware classes */ export declare namespace UrlHandler { /** * Middleware for handling specific protocols * @template T - Middleware context type extending [URL, ...unknown[]] */ class Protocol<T extends [URL, ...unknown[]]> extends MiddlewareCompositeAsync<T, MiddlewareError> { readonly protocol: string; /** * @param protocol - The protocol to handle (automatically strips trailing colon if present) */ constructor(protocol: string); /** * Handles protocol matching and processing * @param context - Middleware context parameters * @returns Promise resolving to middleware error status or undefined */ handle(...context: T): MiddlewarePromise<MiddlewareError>; } /** * Middleware for handling specific hosts * @template T - Middleware context type extending [URL, ...unknown[]] */ class Host<T extends [URL, ...unknown[]]> extends MiddlewareCompositeAsync<T, MiddlewareError> { private host; /** * @param host - The host name to match */ constructor(host: string); /** * Handles host matching * @param context - Middleware context parameters * @returns Promise resolving to middleware error status or undefined */ handle(...context: T): MiddlewarePromise<MiddlewareError>; } } export {};