UNPKG

kequapp

Version:

DEPRECATED: renamed to @kequtech/arbor

175 lines (174 loc) 4.79 kB
import type { IncomingMessage, ServerResponse } from 'node:http'; import type { FakeReq, FakeRes } from './utils/fake-http.ts'; export type LoggerFn = (...params: unknown[]) => void; export interface Logger { error: LoggerFn; warn: LoggerFn; info: LoggerFn; } export type Action<T extends BundleContext = BundleContext> = (bundle: Bundle<T>) => Promise<unknown> | unknown; export type Renderer = (payload: unknown, bundle: Bundle) => Promise<void> | void; export type ErrorHandler = (ex: ServerEx, bundle: Bundle) => Promise<unknown> | unknown; export type Pathname = `/${string}`; export type PathnameWild = Pathname & `${string}/**`; export type Header = string | number | string[] | undefined; export type Headers = Record<string, Header>; export type Params = Record<string, string>; export interface Bundle<T extends BundleContext = BundleContext> { req: IncomingMessage; res: ServerResponse; url: URL; context: T; params: Params; methods: string[]; cookies: Cookies; getBody: GetBody; } export interface BundleContext { [k: string]: unknown; } export interface CookieOptions { domain?: string; expires?: Date; maxAge?: number; secure?: boolean; httpOnly?: boolean; sameSite?: 'Strict' | 'Lax' | 'None'; } export interface Cookies { get: (key: string) => string | undefined; set: (key: string, value: string, options?: CookieOptions) => void; remove: (key: string) => void; } export interface GetBody { (format: GetBodyOptions & { raw: true; multipart: true; }): Promise<RawPart[]>; (format: GetBodyOptions & { raw: true; }): Promise<Buffer>; <T>(format: GetBodyOptions<T> & { multipart: true; throws: false; }): Promise<[Expand<BodyResult<T>>, FilePart[]]>; <T>(format: GetBodyOptions<T> & { multipart: true; }): Promise<[T, FilePart[]]>; <T>(format: GetBodyOptions<T> & { throws: false; }): Promise<Expand<BodyResult<T>>>; <T>(format?: GetBodyOptions<T>): Promise<T>; } type Expand<T> = T extends infer O ? { [K in keyof O]: O[K]; } : never; type BodyResult<T> = (T & { ok: true; }) | { ok: false; errors: { [K in keyof T]?: string; }; }; export interface GetBodyOptions<T = BodyJson> { raw?: boolean; multipart?: boolean; maxPayloadSize?: number; skipNormalize?: boolean; arrays?: string[]; numbers?: string[]; booleans?: string[]; required?: string[]; trim?: boolean; validate?: { [K in keyof T]?: (value: T[K]) => string | undefined; }; throws?: boolean; } export interface GetResponse { (format: GetResponseOptions & { raw: true; }): Promise<Buffer>; (format?: GetResponseOptions): Promise<any>; } export interface ReqOptions extends Record<string, any> { method?: string; url?: string; headers?: Params; rawHeaders?: string[]; body?: unknown; } export interface TestBundleOptions extends ReqOptions { params?: Record<string, string>; context?: BundleContext; } export interface GetResponseOptions { raw?: boolean; } export interface RawPart { headers: Params; data: Buffer; } export interface FilePart extends RawPart { contentType?: string; name?: string; filename?: string; } export type BodyJsonValue = string | number | boolean | Date | null | BodyJson | BodyJsonValue[]; export interface BodyJson { [k: string]: BodyJsonValue; } export interface ServerEx extends Error { statusCode: number; info: Record<string, unknown>; } export interface Inject { req: FakeReq; res: FakeRes; getResponse: GetResponse; } export type Router = (method: string, url: string) => [Route, Params, string[]]; export interface RouteData { method: string; url?: Pathname; actions?: Action[]; logger?: Logger; autoHead?: boolean; } export interface BranchData extends Omit<RouteData, 'method'> { routes?: RouteData[]; branches?: BranchData[]; errorHandlers?: ErrorHandlerData[]; renderers?: RendererData[]; } export interface RendererData { contentType: string; action: Renderer; } export interface ErrorHandlerData { contentType: string; action: ErrorHandler; } export interface CacheBranch { url: Pathname; actions: Action[]; errorHandlers: ErrorHandlerData[]; renderers: RendererData[]; autoHead?: boolean; logger?: Logger; } export interface CacheRoute extends CacheBranch { method: string; } export interface Branch extends Omit<CacheBranch, 'url'> { regexp: RegExp; autoHead: boolean; logger: Logger; } export interface Route extends Omit<CacheRoute, 'url'> { regexp: RegExp; autoHead: boolean; logger: Logger; } export {};