UNPKG

@powership/server

Version:
1 lines 2.79 kB
{"version":3,"file":"createRouteHandler.cjs","names":["createRouteHandler","path","handler","data","matcher","createRouteMatcher","createHandler","onRequest","request","response","app","close","pathname","request_path","params","match","context","call"],"sources":["../src/createRouteHandler.ts"],"sourcesContent":["import type { CloseResponseFunction, Handler, Server } from './Server';\nimport type { ServerRequest } from './ServerRequest';\nimport type { ServerResponse } from './ServerResponse';\nimport { createHandler } from './createHandler';\nimport type { GetRouteParams } from './routeMatch';\nimport { createRouteMatcher } from './routeMatch';\n\nexport type RouteHandlerContext<Path extends string = string> = {\n path: Path;\n request: ServerRequest;\n response: ServerResponse;\n close: CloseResponseFunction;\n app: Server;\n params: GetRouteParams<Path>;\n};\n\nexport interface RouteHandlerCallback<Path extends string> {\n (this: RouteHandlerContext<Path>, context: RouteHandlerContext<Path>): any;\n}\n\nexport function createRouteHandler<\n Path extends string,\n Callback extends RouteHandlerCallback<Path>\n>(path: Path, handler: RouteHandlerCallback<Path>): Handler<undefined>;\n\nexport function createRouteHandler<\n Path extends string,\n Callback extends RouteHandlerCallback<Path>,\n StaticData extends Record<string, any>\n>(\n path: Path,\n handler: RouteHandlerCallback<Path>,\n data: StaticData\n): Handler<StaticData>;\n\nexport function createRouteHandler(\n path: string,\n handler: RouteHandlerCallback<string>,\n data?: Record<string, any>\n) {\n //\n const matcher = createRouteMatcher(path);\n\n return createHandler(\n path,\n {\n async onRequest(request, { response, app, close }): Promise<any> {\n const { pathname: request_path } = request;\n\n const params = matcher.match(request_path);\n if (!params) return;\n\n const context: RouteHandlerContext = {\n path: request_path,\n request,\n response,\n close,\n app,\n params,\n };\n\n await handler.call(context, context);\n },\n },\n data as any\n );\n}\n"],"mappings":";;;;;;;;AAmCO,SAASA,kBAAkBA,CAChCC,IAAY,EACZC,OAAqC,EACrCC,IAA0B,EAC1B;EACA;EACA,MAAMC,OAAO,GAAG,IAAAC,8BAAkB,EAACJ,IAAI,CAAC;EAExC,OAAO,IAAAK,4BAAa,EAClBL,IAAI,EACJ;IACE,MAAMM,SAASA,CAACC,OAAO,EAAE;MAAEC,QAAQ;MAAEC,GAAG;MAAEC;IAAM,CAAC,EAAgB;MAC/D,MAAM;QAAEC,QAAQ,EAAEC;MAAa,CAAC,GAAGL,OAAO;MAE1C,MAAMM,MAAM,GAAGV,OAAO,CAACW,KAAK,CAACF,YAAY,CAAC;MAC1C,IAAI,CAACC,MAAM,EAAE;MAEb,MAAME,OAA4B,GAAG;QACnCf,IAAI,EAAEY,YAAY;QAClBL,OAAO;QACPC,QAAQ;QACRE,KAAK;QACLD,GAAG;QACHI;MACF,CAAC;MAED,MAAMZ,OAAO,CAACe,IAAI,CAACD,OAAO,EAAEA,OAAO,CAAC;IACtC;EACF,CAAC,EACDb,IACF,CAAC;AACH","ignoreList":[]}