ios-dev/comments/Home/node_modules/@tinyhttp/router/dist/index.js.map

1 line
10 KiB
Plaintext
Raw Permalink Normal View History

2024-03-11 14:47:28 +03:00
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n/* HELPER TYPES */\n\nexport type NextFunction = (err?: any) => void\n\nexport type SyncHandler<Request = any, Response = any> = (req: Request, res: Response, next: NextFunction) => void\n\nexport type AsyncHandler<Request = any, Response = any> = (\n req: Request,\n res: Response,\n next: NextFunction\n) => Promise<void>\n\nexport type Handler<Request = any, Response = any> = AsyncHandler<Request, Response> | SyncHandler<Request, Response>\n\nconst METHODS = [\n 'ACL',\n 'BIND',\n 'CHECKOUT',\n 'CONNECT',\n 'COPY',\n 'DELETE',\n 'GET',\n 'HEAD',\n 'LINK',\n 'LOCK',\n 'M-SEARCH',\n 'MERGE',\n 'MKACTIVITY',\n 'MKCALENDAR',\n 'MKCOL',\n 'MOVE',\n 'NOTIFY',\n 'OPTIONS',\n 'PATCH',\n 'POST',\n 'PRI',\n 'PROPFIND',\n 'PROPPATCH',\n 'PURGE',\n 'PUT',\n 'REBIND',\n 'REPORT',\n 'SEARCH',\n 'SOURCE',\n 'SUBSCRIBE',\n 'TRACE',\n 'UNBIND',\n 'UNLINK',\n 'UNLOCK',\n 'UNSUBSCRIBE'\n] as const\n\nexport type Method = (typeof METHODS)[number]\n\nexport type MiddlewareType = 'mw' | 'route'\n\ntype RegexParams = {\n keys: string[] | false\n pattern: RegExp\n}\n\ntype RIM<Req, Res, App> = (...args: RouterMethodParams<Req, Res>) => App\n\nexport interface Middleware<Req = any, Res = any> {\n method?: Method\n handler: Handler<Req, Res>\n path?: string\n type: MiddlewareType\n regex?: RegexParams\n fullPath?: string\n}\n\nexport type MethodHandler<Req = any, Res = any> = {\n path?: string | string[] | Handler<Req, Res>\n handler?: Handler<Req, Res>\n type: MiddlewareType\n regex?: RegexParams\n fullPath?: string\n}\n\nexport type RouterHandler<Req = any, Res = any> = Handler<Req, Res> | Handler<Req, Res>[] | string[]\n\nexport type RouterPathOrHandler<Req = any, Res = any> = string | RouterHandler<Req, Res>\n\nexport type RouterMethod<Req = any, Res = any> = (\n path: string | string[] | Handler<Req, Res>,\n handler?: RouterHandler<Req, Res>,\n ...handlers: RouterHandler<Req, Res>[]\n) => any\n\ntype RouterMethodParams<Req = any, Res = any> = Parameters<RouterMethod<Req, Res>>\n\nexport type UseMethod<Req = any, Res = any, App extends Router = any> = (\n path: RouterPathOrHandler<Req, Res> | App,\n handler?: RouterHandler<Req, Res> | App,\n ...handlers: (RouterHandler<Req, Res> | App)[]\n) => any\n\nexport type UseMethodParams<Req = any, Res = any, App extends Router = any> = Parameters<UseMethod<Req, Res, App>>\n\n/** HELPER METHODS */\n\nconst createMiddlewareFromRoute = <Req = any, Res = any>({\n path,\n handler,\n fullPath,\n method\n}: MethodHandler<Req, Res> & {\n method?: Method\n}) => ({\n method,\n handler: handler || (path as Handler),\n path: typeof path === 'string' ? path : '/',\n fullPath: typeof path === 'string' ? fullPath : path\n})\n\n/**\n * Push wares to a middleware array\n * @param mw Middleware arrays\n */\nexport const pushMiddleware =\n <Req = any, Res = any>(mw: Middleware[]) =>\n ({\n path,\n handler,\n method,\n handlers,\n type,\n fullPaths\n }: MethodHandler<Req, Res> & {\n method?: Method\n handlers?: RouterHandler<Req, Res>[]\n fullPaths?: string[]\n }): void => {\n const m = createMiddlewareFromRoute<Req, Res>({ path, handler, method, type, fullPath: fullPaths?.[0] })\n\n let waresFromHandlers: { handler: Handler<Req, Res> }[] = []\n let idx = 1\n\n if (handlers) {\n waresFromHandlers = handlers.flat().map((handler) =>\n createMiddlewareFromRoute<Req, Res>({\n path,\n handler: handler as Handler,\n method,\n type,\n fullPath: fullPaths == null ? null : fullPaths[idx++]\n })\n )\n }\n\n for (const mdw of [m, ...waresFromHandlers]) mw.push({ ...mdw, type })\n }\n\n/**\n * tinyhttp Router. Manages middleware and has HTTP methods aliases, e.g. `app.get`, `app.put`\n */\nexport class Router<App extends Router = any, Req = any, Res = any> {\n middleware: Middleware[] = []\n mountpath = '/'\n pa