UNPKG

coap-fast-router

Version:
31 lines (30 loc) 1.38 kB
import { LRUCache } from "lru-cache"; import type { IncomingMessage, OutgoingMessage } from "@ref/coap"; declare module "@ref/coap" { interface IncomingMessage { params: Record<string, any>; query: Record<string, any>; } } export interface IncomingMessageWithParams<P> extends IncomingMessage { params: Readonly<P>; } export type RouteMethods = "all" | "get" | "post" | "put" | "delete" | "observe"; export type RouteCallback<P = any> = (req: IncomingMessageWithParams<P>, res: OutgoingMessage) => void; interface Router { (req: IncomingMessage, res: OutgoingMessage): void; cache: LRUCache<string, CachedHandler, unknown>; method<P = any>(method: RouteMethods, path: string, callback: RouteCallback<P>): Router; all<P = any>(path: string, callback: RouteCallback<P>): Router; get<P = any>(path: string, callback: RouteCallback<P>): Router; post<P = any>(path: string, callback: RouteCallback<P>): Router; put<P = any>(path: string, callback: RouteCallback<P>): Router; delete<P = any>(path: string, callback: RouteCallback<P>): Router; observe<P = any>(path: string, callback: RouteCallback<P>): Router; } interface CachedHandler { params: Record<string, any>; callback: RouteCallback; } export declare function createRouter(cacheOptions?: LRUCache.Options<string, CachedHandler, unknown>): Router; export {};