import { Prettify } from "./helper.cjs"; import { InferBodyInput, InferHeaders, InferHeadersInput, InferMiddlewareBody, InferMiddlewareQuery, InferQueryInput, InferRequest, InferRequestInput, InferUse } from "./context.cjs"; import { EndpointContext, EndpointOptions } from "./endpoint.cjs"; //#region src/middleware.d.ts interface MiddlewareOptions extends Omit {} type MiddlewareResponse = null | void | undefined | Record; type MiddlewareContext = EndpointContext & { /** * Method * * The request method */ method: string; /** * Path * * The path of the endpoint */ path: string; /** * Body * * The body object will be the parsed JSON from the request and validated * against the body schema if it exists */ body: InferMiddlewareBody; /** * Query * * The query object will be the parsed query string from the request * and validated against the query schema if it exists */ query: InferMiddlewareQuery; /** * Params * * If the path is `/user/:id` and the request is `/user/1` then the * params will * be `{ id: "1" }` and if the path includes a wildcard like `/user/*` * then the * params will be `{ _: "1" }` where `_` is the wildcard key. If the * wildcard * is named like `/user/**:name` then the params will be `{ name: string }` */ params: string; /** * Request object * * If `requireRequest` is set to true in the endpoint options this will be * required */ request: InferRequest; /** * Headers * * If `requireHeaders` is set to true in the endpoint options this will be * required */ headers: InferHeaders; /** * Set header * * If it's called outside of a request it will just be ignored. */ setHeader: (key: string, value: string) => void; /** * Get header * * If it's called outside of a request it will just return null * * @param key - The key of the header * @returns */ getHeader: (key: string) => string | null; /** * JSON * * a helper function to create a JSON response with * the correct headers * and status code. If `asResponse` is set to true in * the context then * it will return a Response object instead of the * JSON object. * * @param json - The JSON object to return * @param routerResponse - The response object to * return if `asResponse` is * true in the context this will take precedence */ json: | null>(json: R, routerResponse?: { status?: number; headers?: Record; response?: Response; } | Response) => Promise; /** * Middleware context */ context: Prettify; }; declare function createMiddleware(options: Options, handler: (context: MiddlewareContext) => Promise): >(inputContext: InputCtx) => Promise; declare function createMiddleware(handler: (context: MiddlewareContext) => Promise): >(inputContext: InputCtx) => Promise; declare namespace createMiddleware { var create: (opts?: E) => { (options: Options, handler: (ctx: MiddlewareContext>) => Promise): (inputContext: MiddlewareInputContext) => Promise; (handler: (ctx: MiddlewareContext>) => Promise): (inputContext: MiddlewareInputContext) => Promise; }; } type MiddlewareInputContext = InferBodyInput & InferQueryInput & InferRequestInput & InferHeadersInput & { asResponse?: boolean; returnHeaders?: boolean; use?: Middleware[]; }; type Middleware Promise = any> = Handler & { options: Options; }; //#endregion export { Middleware, MiddlewareContext, MiddlewareInputContext, MiddlewareOptions, MiddlewareResponse, createMiddleware }; //# sourceMappingURL=middleware.d.cts.map