/// import type { ServerResponse } from 'worktop/response'; import type { ServerRequest, Params, Method } from 'worktop/request'; type Promisable = Promise | T; type CronHandler = (event: CronEvent) => void; export type ResponseHandler = (event: FetchEvent) => Promisable; export type FetchHandler = (event: FetchEvent, request?: Request | string) => void; interface CronEvent { type: 'scheduled'; /** * The CRON trigger * @example "23 59 LW * *" */ cron: string; /** * Milliseconds since UNIX epoch. * @example new Date(evt.scheduledTime) */ scheduledTime: number; /** * Method wrapper for event's action handler. */ waitUntil(f: Promisable): void; } declare global { function addEventListener(type: 'fetch', handler: FetchHandler): void; function addEventListener(type: 'scheduled', handler: CronHandler): void; interface FetchEvent { passThroughOnException(): void; } } /** * Return the `handler` with an `event.respondWith` wrapper. * @param {ResponseHandler} handler */ export function reply(handler: ResponseHandler): FetchHandler; /** * Assign the `handler` to the "fetch" event. * @note Your `handler` will be wrapped by `reply` automatically. * @param {ResponseHandler} handler */ export function listen(handler: ResponseHandler): void; export type Handler

= (req: ServerRequest

, res: ServerResponse) => Promisable; export type RouteParams = T extends `${infer Prev}/*/${infer Rest}` ? RouteParams & { wild: string } & RouteParams : T extends `${string}:${infer P}?/${infer Rest}` ? { [K in P]?: string } & RouteParams : T extends `${string}:${infer P}/${infer Rest}` ? { [K in P]: string } & RouteParams : T extends `${string}:${infer P}?` ? { [K in P]?: string } : T extends `${string}:${infer P}` ? { [K in P]: string } : T extends `${string}*` ? { wild: string } : {}; export declare class Router { add(method: Method, route: T, handler: Handler): void; add(method: Method, route: T, handler: Handler>): void; run(event: FetchEvent): Promise; onerror(req: ServerRequest, res: ServerResponse, status?: number, error?: Error): Promisable; prepare?(req: Omit, res: ServerResponse): Promisable; } // TODO?: worktop/status | worktop/errors export declare var STATUS_CODES: Record; /** * Compose multiple `Handler` functions together, creating a final handler. */ export function compose

(...handlers: Handler

[]): Handler

;