@universal-middleware/fastify
Version:
fastify adapter for universal middlewares
31 lines (27 loc) • 2.02 kB
TypeScript
import { contextSymbol, Get, UniversalHandler, UniversalFn, UniversalMiddleware, UniversalRouter, UniversalRouterInterface, EnhancedMiddleware } from '@universal-middleware/core';
import { RouteHandlerMethod, FastifyPluginAsync, FastifyRequest, FastifyInstance } from 'fastify';
declare const pendingMiddlewaresSymbol: unique symbol;
declare const wrappedResponseSymbol: unique symbol;
type FastifyHandler<In extends Universal.Context> = UniversalFn<UniversalHandler<In>, RouteHandlerMethod>;
type FastifyMiddleware<In extends Universal.Context, Out extends Universal.Context> = UniversalFn<UniversalMiddleware<In, Out>, FastifyPluginAsync>;
declare module "fastify" {
interface FastifyRequest {
[pendingMiddlewaresSymbol]?: ((response: Response) => Response | Promise<Response | undefined> | undefined | Promise<undefined>)[];
[wrappedResponseSymbol]?: boolean;
}
interface FastifyContextConfig {
[contextSymbol]: unknown;
}
}
declare function createHandler<T extends unknown[], InContext extends Universal.Context>(handlerFactory: Get<T, UniversalHandler<InContext>>): Get<T, FastifyHandler<InContext>>;
declare function createMiddleware<T extends unknown[], InContext extends Universal.Context, OutContext extends Universal.Context>(middlewareFactory: Get<T, UniversalMiddleware<InContext, OutContext>>): Get<T, FastifyMiddleware<InContext, OutContext>>;
declare function getContext<InContext extends Universal.Context = Universal.Context>(req: FastifyRequest): InContext;
type App = FastifyInstance;
declare class UniversalFastifyRouter extends UniversalRouter implements UniversalRouterInterface<"async"> {
#private;
constructor(app: App);
use(middleware: EnhancedMiddleware): Promise<this>;
applyCatchAll(): Promise<this>;
}
declare function apply(app: App, middlewares: EnhancedMiddleware[]): Promise<void>;
export { type App, type FastifyHandler, type FastifyMiddleware, UniversalFastifyRouter, apply, createHandler, createMiddleware, getContext };