phecda-server 4.0.0 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-FPN7NODB.mjs → chunk-AJ2HYENF.mjs} +13 -5
- package/dist/chunk-AJ2HYENF.mjs.map +1 -0
- package/dist/{chunk-UEH6FW5I.js → chunk-D3VS53JQ.js} +26 -23
- package/dist/chunk-D3VS53JQ.js.map +1 -0
- package/dist/{chunk-JGSJIKGN.js → chunk-FAHHXHL5.js} +3 -3
- package/dist/{chunk-HY6T4ZWQ.mjs → chunk-JQPX546Z.mjs} +2 -2
- package/dist/{chunk-62QDU5FJ.js → chunk-LZAU5FUS.js} +14 -6
- package/dist/chunk-LZAU5FUS.js.map +1 -0
- package/dist/{chunk-3PXRNECS.mjs → chunk-Q6T7HNEO.mjs} +69 -20
- package/dist/chunk-Q6T7HNEO.mjs.map +1 -0
- package/dist/{chunk-MFGAP3PV.mjs → chunk-WRMLZ7NU.mjs} +8 -5
- package/dist/{chunk-MFGAP3PV.mjs.map → chunk-WRMLZ7NU.mjs.map} +1 -1
- package/dist/{chunk-BP7CKHPK.js → chunk-YU7ROHTO.js} +96 -47
- package/dist/chunk-YU7ROHTO.js.map +1 -0
- package/dist/index.d.ts +30 -17
- package/dist/index.js +84 -47
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +55 -18
- package/dist/index.mjs.map +1 -1
- package/dist/rpc/rabbitmq/index.js +12 -9
- package/dist/rpc/rabbitmq/index.js.map +1 -1
- package/dist/rpc/rabbitmq/index.mjs +9 -6
- package/dist/rpc/rabbitmq/index.mjs.map +1 -1
- package/dist/rpc/redis/index.js +12 -9
- package/dist/rpc/redis/index.js.map +1 -1
- package/dist/rpc/redis/index.mjs +9 -6
- package/dist/rpc/redis/index.mjs.map +1 -1
- package/dist/server/express/index.js +27 -23
- package/dist/server/express/index.js.map +1 -1
- package/dist/server/express/index.mjs +11 -7
- package/dist/server/express/index.mjs.map +1 -1
- package/dist/server/fastify/index.js +24 -20
- package/dist/server/fastify/index.js.map +1 -1
- package/dist/server/fastify/index.mjs +11 -7
- package/dist/server/fastify/index.mjs.map +1 -1
- package/dist/server/h3/index.js +27 -23
- package/dist/server/h3/index.js.map +1 -1
- package/dist/server/h3/index.mjs +11 -7
- package/dist/server/h3/index.mjs.map +1 -1
- package/dist/server/koa/index.d.ts +22 -0
- package/dist/server/koa/index.js +193 -0
- package/dist/server/koa/index.js.map +1 -0
- package/dist/server/koa/index.mjs +193 -0
- package/dist/server/koa/index.mjs.map +1 -0
- package/dist/test.js +7 -9
- package/dist/test.js.map +1 -1
- package/dist/test.mjs +4 -6
- package/dist/test.mjs.map +1 -1
- package/package.json +10 -2
- package/register/index.mjs +1 -1
- package/register/loader.mjs +0 -1
- package/dist/chunk-3PXRNECS.mjs.map +0 -1
- package/dist/chunk-62QDU5FJ.js.map +0 -1
- package/dist/chunk-BP7CKHPK.js.map +0 -1
- package/dist/chunk-FPN7NODB.mjs.map +0 -1
- package/dist/chunk-IIGNGFDC.js +0 -47
- package/dist/chunk-IIGNGFDC.js.map +0 -1
- package/dist/chunk-Q4FA4TAV.mjs +0 -47
- package/dist/chunk-Q4FA4TAV.mjs.map +0 -1
- package/dist/chunk-UEH6FW5I.js.map +0 -1
- package/dist/supertest-G6LTYS5C.js +0 -21969
- package/dist/supertest-G6LTYS5C.js.map +0 -1
- package/dist/supertest-NLMK5OIC.mjs +0 -21969
- package/dist/supertest-NLMK5OIC.mjs.map +0 -1
- /package/dist/{chunk-JGSJIKGN.js.map → chunk-FAHHXHL5.js.map} +0 -0
- /package/dist/{chunk-HY6T4ZWQ.mjs.map → chunk-JQPX546Z.mjs.map} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/express/bind.ts"],"names":["bindApp","app","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","APP_SYMBOL","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","createRoute","post","req","_res","next","MERGE_SYMBOL","MODULE_SYMBOL","META_SYMBOL","Context","usePlugin","res","body","errorHandler","e","error","filter","status","json","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","guards","interceptors","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipeOpts","pipe","index","arg","reflect","funcData","usePostInterceptor","forEach","handler","useFilter","then","ret","i","header","send","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__","stack"],"mappings":";;;;;;;;;;;;;;;;;;;AAqCO,SAASA,QAAQC,KAAa,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAAG;AACpH,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ;EAAQ;AAC7JH,MAAYQ,cAAc;IAAEP;IAAWC;EAAK;AAE7C,QAAMO,UAAU,oBAAIC,IAAAA;AACpB,WAASC,aAAa;AACpBF,YAAQG,MAAK;AACb,eAAWC,QAAQX,MAAM;AACvB,YAAM,EAAEY,KAAKC,QAAQC,KAAI,IAAKH,KAAKI;AACnC,UAAI,CAACD,MAAME;AACT;AACF,YAAMC,YAAY,GAAGL,OAAOC;AAC5BN,cAAQW,IAAID,WAAWN,IAAAA;IACzB;EACF;AATSF;AAWT,iBAAeU,cAAc;AAC1BrB,QAAgBsB,KAAKhB,OAAO,CAACiB,KAAKC,MAAMC,SAAS;AAC/CF,UAAYG,gBAAgB;AAC5BH,UAAYI,iBAAiB1B;AAC7BsB,UAAYK,eAAe1B;AAE5BuB,WAAAA;IACF,GAAA,GAAMI,QAAQC,UAAUvB,OAAAA,GAAU,OAAOgB,KAAKQ,QAAQ;AACpD,YAAM,EAAEC,KAAI,IAAKT;AAEjB,qBAAeU,aAAaC,GAAQ;AAClC,cAAMC,QAAQ,MAAMN,QAAQO,OAAO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../../helper'\nimport { APP_SYMBOL, IS_DEV, MERGE_SYMBOL, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context } from '../../context'\n\nexport interface ExpressCtx {\n type: 'express'\n request: Request\n response: Response\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp(app: Router, { moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}) {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n async function createRoute() {\n (app as Express).post(route, (req, _res, next) => {\n (req as any)[MERGE_SYMBOL] = true;\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const { body } = req\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).json(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'express' as const,\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n handlers,\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n return resolve(cache)\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, pipe, index }) => {\n return { arg: item.args[index], type, key, pipeOpts, pipe, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.json(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!;\n\n (app as Express)[http.type](http.route, (req, _res, next) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'express' as const,\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.set(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined) {\n if (typeof cache === 'string')\n res.send(cache)\n\n else\n res.json(cache)\n\n return\n }\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, index, pipe }) => {\n return { arg: resolveDep((req as any)[type], key), pipeOpts, pipe, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n if (typeof ret === 'string')\n res.send(ret)\n\n else\n res.json(ret)\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).json(err)\n }\n })\n }\n }\n\n handleMeta()\n createRoute()\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\n rawMetaHmr?.()\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/express/bind.ts"],"names":["bindApp","app","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","isAopDepInject","guards","interceptors","APP_SYMBOL","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","createRoute","post","req","_res","next","MERGE_SYMBOL","MODULE_SYMBOL","META_SYMBOL","Context","usePlugin","res","body","errorHandler","e","error","filter","status","json","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipeOpts","pipe","index","arg","reflect","funcData","usePostInterceptor","forEach","handler","useFilter","then","ret","i","header","send","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__","stack"],"mappings":";;;;;;;;;;;;;;;;;;AAqCO,SAASA,QAAQC,KAAa,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAAG;AACpH,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ;EAAQ;AAC9JK,iBAAeN,MAAM;IACnBK;IACAE,QAAQL;IACRM,cAAcL;EAChB,CAAA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,IAAAA;AAC3C,gBAAMc,MAAM,MAAMzB,QAAQoB,mBAAmBD,QAAAA;AAE7C,cAAI,OAAOM,QAAQ;AACjB5C,gBAAI+C,KAAKH,GAAAA;;AAGT5C,gBAAIO,KAAKqC,GAAAA;QACb,SACOzC,GAAP;AACEoB,mBAASiB,QAAQC,CAAAA,YAAWA,QAAQrC,QAAQD,CAAAA,CAAAA;AAC5C,gBAAM8C,MAAM,MAAM9B,QAAQuB,UAAUvC,CAAAA;AACpCH,cAAIM,OAAO2C,IAAI3C,MAAM,EAAEC,KAAK0C,GAAAA;QAC9B;MACF,CAAA;IACF;EACF;AA7Ie3D;AA+IfV,aAAAA;AACAU,cAAAA;AACA,MAAI4D,QAAQ;AAEV,UAAMC,aAAaC,WAAWC;AAG9BD,eAAWC,mBAAmB,MAAM;AAClCvF,UAAIwF,QAAQ,CAAA;AACZ1E,iBAAAA;AACAU,kBAAAA;AACA6D,mBAAAA;IACF;EACF;AACF;AAnLgBtF","sourcesContent":["import type { Express, Router } from 'express'\nimport { resolveDep } from '../../helper'\nimport { APP_SYMBOL, IS_DEV, MERGE_SYMBOL, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context, isAopDepInject } from '../../context'\n\nexport interface ExpressCtx {\n type: 'express'\n request: Request\n response: Response\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp(app: Router, { moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}) {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n isAopDepInject(meta, {\n plugins,\n guards: globalGuards,\n interceptors: globalInterceptors,\n });\n\n (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n async function createRoute() {\n (app as Express).post(route, (req, _res, next) => {\n (req as any)[MERGE_SYMBOL] = true;\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const { body } = req\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).json(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'express' as const,\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n handlers,\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n return resolve(cache)\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, pipe, index }) => {\n return { arg: item.args[index], type, key, pipeOpts, pipe, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.json(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!;\n\n (app as Express)[http.type](http.route, (req, _res, next) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'express' as const,\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.set(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined) {\n if (typeof cache === 'string')\n res.send(cache)\n\n else\n res.json(cache)\n\n return\n }\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, index, pipe }) => {\n return { arg: resolveDep((req as any)[type], key), pipeOpts, pipe, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n if (typeof ret === 'string')\n res.send(ret)\n\n else\n res.json(ret)\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).json(err)\n }\n })\n }\n }\n\n handleMeta()\n createRoute()\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\n rawMetaHmr?.()\n }\n }\n}\n"]}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
resolveDep
|
|
3
|
-
} from "../../chunk-
|
|
3
|
+
} from "../../chunk-JQPX546Z.mjs";
|
|
4
4
|
import {
|
|
5
5
|
BadRequestException,
|
|
6
|
-
Context
|
|
7
|
-
|
|
6
|
+
Context,
|
|
7
|
+
isAopDepInject
|
|
8
|
+
} from "../../chunk-Q6T7HNEO.mjs";
|
|
8
9
|
import {
|
|
9
10
|
APP_SYMBOL,
|
|
10
11
|
IS_DEV,
|
|
11
12
|
MERGE_SYMBOL,
|
|
12
13
|
META_SYMBOL,
|
|
13
|
-
MODULE_SYMBOL
|
|
14
|
-
} from "../../chunk-FPN7NODB.mjs";
|
|
15
|
-
import {
|
|
14
|
+
MODULE_SYMBOL,
|
|
16
15
|
__name
|
|
17
|
-
} from "../../chunk-
|
|
16
|
+
} from "../../chunk-AJ2HYENF.mjs";
|
|
18
17
|
|
|
19
18
|
// src/server/express/bind.ts
|
|
20
19
|
function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
@@ -25,6 +24,11 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
25
24
|
plugins: [],
|
|
26
25
|
...options
|
|
27
26
|
};
|
|
27
|
+
isAopDepInject(meta, {
|
|
28
|
+
plugins,
|
|
29
|
+
guards: globalGuards,
|
|
30
|
+
interceptors: globalInterceptors
|
|
31
|
+
});
|
|
28
32
|
app[APP_SYMBOL] = {
|
|
29
33
|
moduleMap,
|
|
30
34
|
meta
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/express/bind.ts"],"sourcesContent":["import type { Express, Router } from 'express'\nimport { resolveDep } from '../../helper'\nimport { APP_SYMBOL, IS_DEV, MERGE_SYMBOL, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context } from '../../context'\n\nexport interface ExpressCtx {\n type: 'express'\n request: Request\n response: Response\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp(app: Router, { moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}) {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n async function createRoute() {\n (app as Express).post(route, (req, _res, next) => {\n (req as any)[MERGE_SYMBOL] = true;\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const { body } = req\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).json(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'express' as const,\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n handlers,\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n return resolve(cache)\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, pipe, index }) => {\n return { arg: item.args[index], type, key, pipeOpts, pipe, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.json(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!;\n\n (app as Express)[http.type](http.route, (req, _res, next) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'express' as const,\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.set(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined) {\n if (typeof cache === 'string')\n res.send(cache)\n\n else\n res.json(cache)\n\n return\n }\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, index, pipe }) => {\n return { arg: resolveDep((req as any)[type], key), pipeOpts, pipe, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n if (typeof ret === 'string')\n res.send(ret)\n\n else\n res.json(ret)\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).json(err)\n }\n })\n }\n }\n\n handleMeta()\n createRoute()\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\n rawMetaHmr?.()\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAqCO,SAASA,QAAQC,KAAa,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAAG;AACpH,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ;EAAQ;AAC7JH,MAAYQ,cAAc;IAAEP;IAAWC;EAAK;AAE7C,QAAMO,UAAU,oBAAIC,IAAAA;AACpB,WAASC,aAAa;AACpBF,YAAQG,MAAK;AACb,eAAWC,QAAQX,MAAM;AACvB,YAAM,EAAEY,KAAKC,QAAQC,KAAI,IAAKH,KAAKI;AACnC,UAAI,CAACD,MAAME;AACT;AACF,YAAMC,YAAY,GAAGL,OAAOC;AAC5BN,cAAQW,IAAID,WAAWN,IAAAA;IACzB;EACF;AATSF;AAWT,iBAAeU,cAAc;AAC1BrB,QAAgBsB,KAAKhB,OAAO,CAACiB,KAAKC,MAAMC,SAAS;AAC/CF,UAAYG,gBAAgB;AAC5BH,UAAYI,iBAAiB1B;AAC7BsB,UAAYK,eAAe1B;AAE5BuB,WAAAA;IACF,GAAA,GAAMI,QAAQC,UAAUvB,OAAAA,GAAU,OAAOgB,KAAKQ,QAAQ;AACpD,YAAM,EAAEC,KAAI,IAAKT;AAEjB,qBAAeU,aAAaC,GAAQ;AAClC,cAAMC,QAAQ,MAAMN,QAAQO,OAAOF,CAAAA;AACnC,eAAOH,IAAIM,OAAOF,MAAME,MAAM,EAAEC,KAAKH,KAAAA;MACvC;AAHeF;AAKf,UAAI,CAACM,MAAMC,QAAQR,IAAAA;AACjB,eAAOC,aAAa,IAAIQ,oBAAoB,gCAAA,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AAOF,UAAU;AACnB7B,kBAAIiD,KAAKpB,KAAAA;;AAGT7B,kBAAIO,KAAKsB,KAAAA;AAEX;UACF;AACA,gBAAMG,OAAO,MAAMb,QAAQc,QAAQT,OAAOX,IAAI,CAAC,EAAE1B,MAAM+C,KAAKC,UAAUE,OAAOD,KAAI,MAAO;AACtF,mBAAO;cAAEE,KAAKY,WAAY1D,IAAYL,OAAO+C,GAAAA;cAAMC;cAAUC;cAAMF;cAAK/C;cAAMkD;cAAOE,SAASjB,WAAWe;YAAO;UAClH,CAAA,CAAA;AAEAV,mBAASR,UAAUH;AACnB,gBAAMwB,WAAW,MAAMb,SAAS3C,QAAO,GAAIgD,IAAAA;AAC3C,gBAAMc,MAAM,MAAM3B,QAAQsB,mBAAmBD,QAAAA;AAE7C,cAAI,OAAOM,QAAQ;AACjB9C,gBAAIiD,KAAKH,GAAAA;;AAGT9C,gBAAIO,KAAKuC,GAAAA;QACb,SACO3C,GAAP;AACEoB,mBAASmB,QAAQC,CAAAA,YAAWA,QAAQvC,QAAQD,CAAAA,CAAAA;AAC5C,gBAAMgD,MAAM,MAAMhC,QAAQyB,UAAUzC,CAAAA;AACpCH,cAAIM,OAAO6C,IAAI7C,MAAM,EAAEC,KAAK4C,GAAAA;QAC9B;MACF,CAAA;IACF;EACF;AA7Ie7D;AA+IfV,aAAAA;AACAU,cAAAA;AACA,MAAI8D,QAAQ;AAEV,UAAMC,aAAaC,WAAWC;AAG9BD,eAAWC,mBAAmB,MAAM;AAClCtF,UAAIuF,QAAQ,CAAA;AACZ5E,iBAAAA;AACAU,kBAAAA;AACA+D,mBAAAA;IACF;EACF;AACF;AA7KgBrF;","names":["bindApp","app","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","APP_SYMBOL","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","createRoute","post","req","_res","next","MERGE_SYMBOL","MODULE_SYMBOL","META_SYMBOL","Context","usePlugin","res","body","errorHandler","e","error","filter","status","json","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","guards","interceptors","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipeOpts","pipe","index","arg","reflect","funcData","usePostInterceptor","forEach","handler","useFilter","then","ret","i","header","send","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__","stack"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/express/bind.ts"],"sourcesContent":["import type { Express, Router } from 'express'\nimport { resolveDep } from '../../helper'\nimport { APP_SYMBOL, IS_DEV, MERGE_SYMBOL, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context, isAopDepInject } from '../../context'\n\nexport interface ExpressCtx {\n type: 'express'\n request: Request\n response: Response\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp(app: Router, { moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}) {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n isAopDepInject(meta, {\n plugins,\n guards: globalGuards,\n interceptors: globalInterceptors,\n });\n\n (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n async function createRoute() {\n (app as Express).post(route, (req, _res, next) => {\n (req as any)[MERGE_SYMBOL] = true;\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const { body } = req\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).json(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'express' as const,\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n handlers,\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n return resolve(cache)\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, pipe, index }) => {\n return { arg: item.args[index], type, key, pipeOpts, pipe, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.json(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!;\n\n (app as Express)[http.type](http.route, (req, _res, next) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n next()\n }, ...Context.usePlugin(plugins), async (req, res) => {\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'express' as const,\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.set(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined) {\n if (typeof cache === 'string')\n res.send(cache)\n\n else\n res.json(cache)\n\n return\n }\n const args = await context.usePipe(params.map(({ type, key, pipeOpts, index, pipe }) => {\n return { arg: resolveDep((req as any)[type], key), pipeOpts, pipe, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n if (typeof ret === 'string')\n res.send(ret)\n\n else\n res.json(ret)\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).json(err)\n }\n })\n }\n }\n\n handleMeta()\n createRoute()\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\n rawMetaHmr?.()\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqCO,SAASA,QAAQC,KAAa,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAAG;AACpH,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ;EAAQ;AAC9JK,iBAAeN,MAAM;IACnBK;IACAE,QAAQL;IACRM,cAAcL;EAChB,CAAA;AAECL,MAAYW,cAAc;IAAEV;IAAWC;EAAK;AAE7C,QAAMU,UAAU,oBAAIC,IAAAA;AACpB,WAASC,aAAa;AACpBF,YA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I+C,KAAKH,GAAAA;;AAGT5C,gBAAIO,KAAKqC,GAAAA;QACb,SACOzC,GAAP;AACEoB,mBAASiB,QAAQC,CAAAA,YAAWA,QAAQrC,QAAQD,CAAAA,CAAAA;AAC5C,gBAAM8C,MAAM,MAAM9B,QAAQuB,UAAUvC,CAAAA;AACpCH,cAAIM,OAAO2C,IAAI3C,MAAM,EAAEC,KAAK0C,GAAAA;QAC9B;MACF,CAAA;IACF;EACF;AA7Ie3D;AA+IfV,aAAAA;AACAU,cAAAA;AACA,MAAI4D,QAAQ;AAEV,UAAMC,aAAaC,WAAWC;AAG9BD,eAAWC,mBAAmB,MAAM;AAClCvF,UAAIwF,QAAQ,CAAA;AACZ1E,iBAAAA;AACAU,kBAAAA;AACA6D,mBAAAA;IACF;EACF;AACF;AAnLgBtF;","names":["bindApp","app","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","isAopDepInject","guards","interceptors","APP_SYMBOL","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","createRoute","post","req","_res","next","MERGE_SYMBOL","MODULE_SYMBOL","META_SYMBOL","Context","usePlugin","res","body","errorHandler","e","error","filter","status","json","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipeOpts","pipe","index","arg","reflect","funcData","usePostInterceptor","forEach","handler","useFilter","then","ret","i","header","send","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__","stack"]}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkFAHHXHL5js = require('../../chunk-FAHHXHL5.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var _chunkBP7CKHPKjs = require('../../chunk-BP7CKHPK.js');
|
|
8
7
|
|
|
8
|
+
var _chunkYU7ROHTOjs = require('../../chunk-YU7ROHTO.js');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
var _chunk62QDU5FJjs = require('../../chunk-62QDU5FJ.js');
|
|
14
13
|
|
|
15
14
|
|
|
16
|
-
var
|
|
15
|
+
var _chunkLZAU5FUSjs = require('../../chunk-LZAU5FUS.js');
|
|
17
16
|
|
|
18
17
|
// src/server/fastify/bind.ts
|
|
19
18
|
function bindApp({ moduleMap, meta }, options = {}) {
|
|
@@ -24,6 +23,11 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
24
23
|
plugins: [],
|
|
25
24
|
...options
|
|
26
25
|
};
|
|
26
|
+
_chunkYU7ROHTOjs.isAopDepInject.call(void 0, meta, {
|
|
27
|
+
plugins,
|
|
28
|
+
guards: globalGuards,
|
|
29
|
+
interceptors: globalInterceptors
|
|
30
|
+
});
|
|
27
31
|
const metaMap = /* @__PURE__ */ new Map();
|
|
28
32
|
function handleMeta() {
|
|
29
33
|
metaMap.clear();
|
|
@@ -35,16 +39,16 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
35
39
|
metaMap.set(methodTag, item);
|
|
36
40
|
}
|
|
37
41
|
}
|
|
38
|
-
|
|
42
|
+
_chunkLZAU5FUSjs.__name.call(void 0, handleMeta, "handleMeta");
|
|
39
43
|
handleMeta();
|
|
40
44
|
return (fastify, _, done) => {
|
|
41
|
-
fastify[
|
|
45
|
+
fastify[_chunkLZAU5FUSjs.APP_SYMBOL] = {
|
|
42
46
|
moduleMap,
|
|
43
47
|
meta
|
|
44
48
|
};
|
|
45
49
|
fastify.register((fastify2, _opts, done2) => {
|
|
46
50
|
plugins.forEach((p) => {
|
|
47
|
-
const plugin =
|
|
51
|
+
const plugin = _chunkYU7ROHTOjs.Context.usePlugin([
|
|
48
52
|
p
|
|
49
53
|
])[0];
|
|
50
54
|
if (plugin) {
|
|
@@ -55,19 +59,19 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
55
59
|
fastify2.post(route, async (req, res) => {
|
|
56
60
|
const { body } = req;
|
|
57
61
|
async function errorHandler(e) {
|
|
58
|
-
const error = await
|
|
62
|
+
const error = await _chunkYU7ROHTOjs.Context.filter(e);
|
|
59
63
|
return res.status(error.status).send(error);
|
|
60
64
|
}
|
|
61
|
-
|
|
65
|
+
_chunkLZAU5FUSjs.__name.call(void 0, errorHandler, "errorHandler");
|
|
62
66
|
if (!Array.isArray(body))
|
|
63
|
-
return errorHandler(new (0,
|
|
67
|
+
return errorHandler(new (0, _chunkYU7ROHTOjs.BadRequestException)("data format should be an array"));
|
|
64
68
|
try {
|
|
65
69
|
return Promise.all(body.map((item) => {
|
|
66
70
|
return new Promise(async (resolve) => {
|
|
67
71
|
const { tag } = item;
|
|
68
72
|
const meta2 = metaMap.get(tag);
|
|
69
73
|
if (!meta2)
|
|
70
|
-
return resolve(await
|
|
74
|
+
return resolve(await _chunkYU7ROHTOjs.Context.filter(new (0, _chunkYU7ROHTOjs.BadRequestException)(`"${tag}" doesn't exist`)));
|
|
71
75
|
const contextData = {
|
|
72
76
|
type: "fastify",
|
|
73
77
|
request: req,
|
|
@@ -75,13 +79,13 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
75
79
|
response: res,
|
|
76
80
|
moduleMap
|
|
77
81
|
};
|
|
78
|
-
const context = new (0,
|
|
82
|
+
const context = new (0, _chunkYU7ROHTOjs.Context)(tag, contextData);
|
|
79
83
|
const [name, method] = tag.split("-");
|
|
80
84
|
const { paramsType, handlers, data: { params, guards, interceptors } } = meta2;
|
|
81
85
|
const instance = moduleMap.get(name);
|
|
82
86
|
try {
|
|
83
87
|
if (!params)
|
|
84
|
-
throw new (0,
|
|
88
|
+
throw new (0, _chunkYU7ROHTOjs.BadRequestException)(`"${tag}" doesn't exist`);
|
|
85
89
|
await context.useGuard([
|
|
86
90
|
...globalGuards,
|
|
87
91
|
...guards
|
|
@@ -127,13 +131,13 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
127
131
|
const methodTag = `${tag}-${method}`;
|
|
128
132
|
const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2 } } = metaMap.get(methodTag);
|
|
129
133
|
fastify.register((fastify2, _opts, done2) => {
|
|
130
|
-
|
|
134
|
+
_chunkYU7ROHTOjs.Context.usePlugin(plugins2).forEach((p) => {
|
|
131
135
|
p[Symbol.for("skip-override")] = true;
|
|
132
136
|
fastify2.register(p);
|
|
133
137
|
});
|
|
134
138
|
fastify2[http.type](http.route, async (req, res) => {
|
|
135
|
-
req[
|
|
136
|
-
req[
|
|
139
|
+
req[_chunkLZAU5FUSjs.MODULE_SYMBOL] = moduleMap;
|
|
140
|
+
req[_chunkLZAU5FUSjs.META_SYMBOL] = meta;
|
|
137
141
|
const instance = moduleMap.get(tag);
|
|
138
142
|
const contextData = {
|
|
139
143
|
type: "fastify",
|
|
@@ -142,7 +146,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
142
146
|
response: res,
|
|
143
147
|
moduleMap
|
|
144
148
|
};
|
|
145
|
-
const context = new (0,
|
|
149
|
+
const context = new (0, _chunkYU7ROHTOjs.Context)(methodTag, contextData);
|
|
146
150
|
try {
|
|
147
151
|
for (const name in header)
|
|
148
152
|
res.header(name, header[name]);
|
|
@@ -158,7 +162,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
158
162
|
return cache;
|
|
159
163
|
const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {
|
|
160
164
|
return {
|
|
161
|
-
arg:
|
|
165
|
+
arg: _chunkFAHHXHL5js.resolveDep.call(void 0, req[type], key),
|
|
162
166
|
pipe,
|
|
163
167
|
pipeOpts,
|
|
164
168
|
key,
|
|
@@ -181,7 +185,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
181
185
|
});
|
|
182
186
|
}
|
|
183
187
|
done();
|
|
184
|
-
if (
|
|
188
|
+
if (_chunkLZAU5FUSjs.IS_DEV) {
|
|
185
189
|
const rawMetaHmr = globalThis.__PS_WRITEMETA__;
|
|
186
190
|
globalThis.__PS_WRITEMETA__ = () => {
|
|
187
191
|
handleMeta();
|
|
@@ -190,7 +194,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
190
194
|
}
|
|
191
195
|
};
|
|
192
196
|
}
|
|
193
|
-
|
|
197
|
+
_chunkLZAU5FUSjs.__name.call(void 0, bindApp, "bindApp");
|
|
194
198
|
|
|
195
199
|
|
|
196
200
|
exports.bindApp = bindApp;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/fastify/bind.ts"],"names":["bindApp","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","fastify","_","done","APP_SYMBOL","register","_opts","forEach","p","plugin","Context","usePlugin","Symbol","for","post","req","res","body","errorHandler","e","error","filter","status","send","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","guards","interceptors","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipe","pipeOpts","index","arg","reflect","funcData","usePostInterceptor","handler","useFilter","then","ret","i","header","MODULE_SYMBOL","META_SYMBOL","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__"],"mappings":";;;;;;;;;;;;;;;;;;AAsCO,SAASA,QAAQ,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAA0B;AAC9H,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;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aAAa;AACnF,gBAAIG,UAAUE;AAEZ,qBAAOF;AAET,kBAAMG,OAAO,MAAMb,QAAQc,QAAQT,OAAOX,IAAI,CAAC,EAAE/B,MAAMoD,KAAKC,MAAMC,UAAUC,MAAK,MAAO;AACtF,qBAAO;gBAAEC,KAAKY,WAAYnD,IAAYjB,OAAOoD,GAAAA;gBAAMC;gBAAMC;gBAAUF;gBAAKpD;gBAAMuD;gBAAOE,SAASjB,WAAWe;cAAO;YAClH,CAAA,CAAA;AAEAV,qBAASR,UAAUH;AACnB,kBAAMwB,WAAW,MAAMb,SAAShD,QAAO,GAAIqD,IAAAA;AAC3C,kBAAMa,MAAM,MAAM1B,QAAQsB,mBAAmBD,QAAAA;AAE7C,mBAAOK;UACT,SACO1C,GAAP;AACEoB,qBAAShC,QAAQmD,CAAAA,YAAWA,QAAQtC,QAAQD,CAAAA,CAAAA;AAC5C,kBAAMgD,MAAM,MAAMhC,QAAQwB,UAAUxC,CAAAA;AACpCH,gBAAIM,OAAO6C,IAAI7C,MAAM,EAAEC,KAAK4C,GAAAA;UAC9B;QACF,CAAA;AACAhE,QAAAA,MAAAA;MACF,CAAA;IACF;AAEAA,SAAAA;AAEA,QAAIiE,QAAQ;AAEV,YAAMC,aAAaC,WAAWC;AAG9BD,iBAAWC,mBAAmB,MAAM;AAClChF,mBAAAA;AAEA8E,qBAAAA;MACF;IACF;EACF;AACF;AAhMgBxF","sourcesContent":["import type { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify'\nimport { resolveDep } from '../../helper'\nimport { APP_SYMBOL, IS_DEV, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context } from '../../context'\n\nexport interface FastifyCtx {\n type: 'fastify'\n request: FastifyRequest\n response: FastifyReply\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}): FastifyPluginCallback {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n // (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n handleMeta()\n\n return (fastify, _, done) => {\n (fastify as any)[APP_SYMBOL] = {\n moduleMap, meta,\n }\n // fastify.decorateRequest(MODULE_SYMBOL, null)\n // fastify.decorateRequest(META_SYMBOL, null)\n // fastify.decorateRequest(MERGE_SYMBOL, false)\n\n fastify.register((fastify, _opts, done) => {\n plugins.forEach((p) => {\n const plugin = Context.usePlugin([p])[0]\n if (plugin) {\n plugin[Symbol.for('skip-override')] = true\n\n fastify.register(plugin)\n }\n })\n fastify.post(route, async (req, res) => {\n const { body } = req as any\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).send(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'fastify',\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n\n handlers,\n\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n if (!params)\n throw new BadRequestException(`\"${tag}\" doesn't exist`)\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return resolve(cache)\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: item.args[index], type, key, pipe, pipeOpts, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.send(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n\n done()\n })\n\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!\n\n fastify.register((fastify, _opts, done) => {\n Context.usePlugin(plugins).forEach((p) => {\n p[Symbol.for('skip-override')] = true\n\n fastify.register(p)\n })\n fastify[http.type](http.route, async (req, res) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'fastify',\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.header(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return cache\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: resolveDep((req as any)[type], key), pipe, pipeOpts, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n return ret\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).send(err)\n }\n })\n done()\n })\n }\n\n done()\n\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n handleMeta()\n\n rawMetaHmr?.()\n }\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/fastify/bind.ts"],"names":["bindApp","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","isAopDepInject","guards","interceptors","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","fastify","_","done","APP_SYMBOL","register","_opts","forEach","p","plugin","Context","usePlugin","Symbol","for","post","req","res","body","errorHandler","e","error","filter","status","send","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipe","pipeOpts","index","arg","reflect","funcData","usePostInterceptor","handler","useFilter","then","ret","i","header","MODULE_SYMBOL","META_SYMBOL","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__"],"mappings":";;;;;;;;;;;;;;;;;AAsCO,SAASA,QAAQ,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAA0B;AAC9H,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ;EAAQ;AAG9JK,iBAAeN,MAAM;IACnBK;IACAE,QAAQL;IACRM,cAAcL;EAChB,CAAA;AAEA,QAAMM,UAAU,oBAAIC,IAAAA;AACpB,WAASC,aAAa;AACpBF,YAAQG,MAAK;AACb,eAAWC,QAAQb,MAAM;AACvB,YAAM,EAAEc,KAAKC,QAA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AMC;gBAAUF;gBAAKlD;gBAAMqD;gBAAOE,SAASf,WAAWa;cAAO;YAClH,CAAA,CAAA;AAEAV,qBAASN,UAAUH;AACnB,kBAAMsB,WAAW,MAAMb,SAAS9C,QAAO,GAAImD,IAAAA;AAC3C,kBAAMa,MAAM,MAAMxB,QAAQoB,mBAAmBD,QAAAA;AAE7C,mBAAOK;UACT,SACOxC,GAAP;AACEoB,qBAAShC,QAAQiD,CAAAA,YAAWA,QAAQpC,QAAQD,CAAAA,CAAAA;AAC5C,kBAAM8C,MAAM,MAAM9B,QAAQsB,UAAUtC,CAAAA;AACpCH,gBAAIM,OAAO2C,IAAI3C,MAAM,EAAEC,KAAK0C,GAAAA;UAC9B;QACF,CAAA;AACA9D,QAAAA,MAAAA;MACF,CAAA;IACF;AAEAA,SAAAA;AAEA,QAAI+D,QAAQ;AAEV,YAAMC,aAAaC,WAAWC;AAG9BD,iBAAWC,mBAAmB,MAAM;AAClC9E,mBAAAA;AAEA4E,qBAAAA;MACF;IACF;EACF;AACF;AAtMgBzF","sourcesContent":["import type { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify'\nimport { resolveDep } from '../../helper'\nimport { APP_SYMBOL, IS_DEV, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context, isAopDepInject } from '../../context'\n\nexport interface FastifyCtx {\n type: 'fastify'\n request: FastifyRequest\n response: FastifyReply\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}): FastifyPluginCallback {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n // (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n isAopDepInject(meta, {\n plugins,\n guards: globalGuards,\n interceptors: globalInterceptors,\n })\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n handleMeta()\n\n return (fastify, _, done) => {\n (fastify as any)[APP_SYMBOL] = {\n moduleMap, meta,\n }\n // fastify.decorateRequest(MODULE_SYMBOL, null)\n // fastify.decorateRequest(META_SYMBOL, null)\n // fastify.decorateRequest(MERGE_SYMBOL, false)\n\n fastify.register((fastify, _opts, done) => {\n plugins.forEach((p) => {\n const plugin = Context.usePlugin([p])[0]\n if (plugin) {\n plugin[Symbol.for('skip-override')] = true\n\n fastify.register(plugin)\n }\n })\n fastify.post(route, async (req, res) => {\n const { body } = req as any\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).send(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'fastify',\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n\n handlers,\n\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n if (!params)\n throw new BadRequestException(`\"${tag}\" doesn't exist`)\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return resolve(cache)\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: item.args[index], type, key, pipe, pipeOpts, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.send(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n\n done()\n })\n\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!\n\n fastify.register((fastify, _opts, done) => {\n Context.usePlugin(plugins).forEach((p) => {\n p[Symbol.for('skip-override')] = true\n\n fastify.register(p)\n })\n fastify[http.type](http.route, async (req, res) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'fastify',\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.header(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return cache\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: resolveDep((req as any)[type], key), pipe, pipeOpts, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n return ret\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).send(err)\n }\n })\n done()\n })\n }\n\n done()\n\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n handleMeta()\n\n rawMetaHmr?.()\n }\n }\n }\n}\n"]}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
2
|
resolveDep
|
|
3
|
-
} from "../../chunk-
|
|
3
|
+
} from "../../chunk-JQPX546Z.mjs";
|
|
4
4
|
import {
|
|
5
5
|
BadRequestException,
|
|
6
|
-
Context
|
|
7
|
-
|
|
6
|
+
Context,
|
|
7
|
+
isAopDepInject
|
|
8
|
+
} from "../../chunk-Q6T7HNEO.mjs";
|
|
8
9
|
import {
|
|
9
10
|
APP_SYMBOL,
|
|
10
11
|
IS_DEV,
|
|
11
12
|
META_SYMBOL,
|
|
12
|
-
MODULE_SYMBOL
|
|
13
|
-
} from "../../chunk-FPN7NODB.mjs";
|
|
14
|
-
import {
|
|
13
|
+
MODULE_SYMBOL,
|
|
15
14
|
__name
|
|
16
|
-
} from "../../chunk-
|
|
15
|
+
} from "../../chunk-AJ2HYENF.mjs";
|
|
17
16
|
|
|
18
17
|
// src/server/fastify/bind.ts
|
|
19
18
|
function bindApp({ moduleMap, meta }, options = {}) {
|
|
@@ -24,6 +23,11 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
24
23
|
plugins: [],
|
|
25
24
|
...options
|
|
26
25
|
};
|
|
26
|
+
isAopDepInject(meta, {
|
|
27
|
+
plugins,
|
|
28
|
+
guards: globalGuards,
|
|
29
|
+
interceptors: globalInterceptors
|
|
30
|
+
});
|
|
27
31
|
const metaMap = /* @__PURE__ */ new Map();
|
|
28
32
|
function handleMeta() {
|
|
29
33
|
metaMap.clear();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/fastify/bind.ts"],"sourcesContent":["import type { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify'\nimport { resolveDep } from '../../helper'\nimport { APP_SYMBOL, IS_DEV, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context } from '../../context'\n\nexport interface FastifyCtx {\n type: 'fastify'\n request: FastifyRequest\n response: FastifyReply\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}): FastifyPluginCallback {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n // (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n handleMeta()\n\n return (fastify, _, done) => {\n (fastify as any)[APP_SYMBOL] = {\n moduleMap, meta,\n }\n // fastify.decorateRequest(MODULE_SYMBOL, null)\n // fastify.decorateRequest(META_SYMBOL, null)\n // fastify.decorateRequest(MERGE_SYMBOL, false)\n\n fastify.register((fastify, _opts, done) => {\n plugins.forEach((p) => {\n const plugin = Context.usePlugin([p])[0]\n if (plugin) {\n plugin[Symbol.for('skip-override')] = true\n\n fastify.register(plugin)\n }\n })\n fastify.post(route, async (req, res) => {\n const { body } = req as any\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).send(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'fastify',\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n\n handlers,\n\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n if (!params)\n throw new BadRequestException(`\"${tag}\" doesn't exist`)\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return resolve(cache)\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: item.args[index], type, key, pipe, pipeOpts, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.send(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n\n done()\n })\n\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!\n\n fastify.register((fastify, _opts, done) => {\n Context.usePlugin(plugins).forEach((p) => {\n p[Symbol.for('skip-override')] = true\n\n fastify.register(p)\n })\n fastify[http.type](http.route, async (req, res) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'fastify',\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.header(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return cache\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: resolveDep((req as any)[type], key), pipe, pipeOpts, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n return ret\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).send(err)\n }\n })\n done()\n })\n }\n\n done()\n\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n handleMeta()\n\n rawMetaHmr?.()\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsCO,SAASA,QAAQ,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAA0B;AAC9H,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ;EAAQ;AAG9J,QAAMK,UAAU,oBAAIC,IAAAA;AACpB,WAASC,aAAa;AACpBF,YAAQG,MAAK;AACb,eAAWC,QAAQV,MAAM;AACvB,YAAM,EAAEW,KAAKC,QAAQC,KAAI,IAAKH,KAAKI;AACnC,UAAI,CAACD,MAAME;AACT;AACF,YAAMC,YAAY,GAAGL,OAAOC;AAC5BN,cAAQW,IAAID,WAAWN,IAAAA;IACzB;EACF;AATSF;AAWTA,aAAAA;AAEA,SAAO,CAACU,SAASC,GAAGC,SAAS;AAC1BF,YAAgBG,cAAc;MAC7BtB;MAAWC;IACb;AAKAkB,YAAQI,SAAS,CAACJ,UAASK,OAAOH,UAAS;AACzCf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QAAQtC,QAAQD,CAAAA,CAAAA;AAC5C,kBAAMgD,MAAM,MAAMhC,QAAQwB,UAAUxC,CAAAA;AACpCH,gBAAIM,OAAO6C,IAAI7C,MAAM,EAAEC,KAAK4C,GAAAA;UAC9B;QACF,CAAA;AACAhE,QAAAA,MAAAA;MACF,CAAA;IACF;AAEAA,SAAAA;AAEA,QAAIiE,QAAQ;AAEV,YAAMC,aAAaC,WAAWC;AAG9BD,iBAAWC,mBAAmB,MAAM;AAClChF,mBAAAA;AAEA8E,qBAAAA;MACF;IACF;EACF;AACF;AAhMgBxF;","names":["bindApp","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","fastify","_","done","APP_SYMBOL","register","_opts","forEach","p","plugin","Context","usePlugin","Symbol","for","post","req","res","body","errorHandler","e","error","filter","status","send","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","guards","interceptors","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipe","pipeOpts","index","arg","reflect","funcData","usePostInterceptor","handler","useFilter","then","ret","i","header","MODULE_SYMBOL","META_SYMBOL","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/fastify/bind.ts"],"sourcesContent":["import type { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify'\nimport { resolveDep } from '../../helper'\nimport { APP_SYMBOL, IS_DEV, META_SYMBOL, MODULE_SYMBOL } from '../../common'\nimport type { Factory } from '../../core'\nimport { BadRequestException } from '../../exception'\nimport type { Meta } from '../../meta'\nimport { Context, isAopDepInject } from '../../context'\n\nexport interface FastifyCtx {\n type: 'fastify'\n request: FastifyRequest\n response: FastifyReply\n meta: Meta\n moduleMap: Record<string, any>\n [key: string]: any\n}\nexport interface Options {\n\n /**\n * 专用路由的值,默认为/__PHECDA_SERVER__,处理phecda-client发出的合并请求\n */\n route?: string\n /**\n * 全局守卫\n */\n globalGuards?: string[]\n /**\n * 全局拦截器\n */\n globalInterceptors?: string[]\n\n /**\n * 专用路由的插件(work for merge request),\n */\n plugins?: string[]\n\n}\n\nexport function bindApp({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options: Options = {}): FastifyPluginCallback {\n const { globalGuards, globalInterceptors, route, plugins } = { route: '/__PHECDA_SERVER__', globalGuards: [], globalInterceptors: [], plugins: [], ...options } as Required<Options>\n // (app as any)[APP_SYMBOL] = { moduleMap, meta }\n\n isAopDepInject(meta, {\n plugins,\n guards: globalGuards,\n interceptors: globalInterceptors,\n })\n\n const metaMap = new Map<string, Meta>()\n function handleMeta() {\n metaMap.clear()\n for (const item of meta) {\n const { tag, method, http } = item.data\n if (!http?.type)\n continue\n const methodTag = `${tag}-${method}`\n metaMap.set(methodTag, item)\n }\n }\n\n handleMeta()\n\n return (fastify, _, done) => {\n (fastify as any)[APP_SYMBOL] = {\n moduleMap, meta,\n }\n // fastify.decorateRequest(MODULE_SYMBOL, null)\n // fastify.decorateRequest(META_SYMBOL, null)\n // fastify.decorateRequest(MERGE_SYMBOL, false)\n\n fastify.register((fastify, _opts, done) => {\n plugins.forEach((p) => {\n const plugin = Context.usePlugin([p])[0]\n if (plugin) {\n plugin[Symbol.for('skip-override')] = true\n\n fastify.register(plugin)\n }\n })\n fastify.post(route, async (req, res) => {\n const { body } = req as any\n\n async function errorHandler(e: any) {\n const error = await Context.filter(e)\n return res.status(error.status).send(error)\n }\n\n if (!Array.isArray(body))\n return errorHandler(new BadRequestException('data format should be an array'))\n\n try {\n return Promise.all(body.map((item: any) => {\n // eslint-disable-next-line no-async-promise-executor\n return new Promise(async (resolve) => {\n const { tag } = item\n const meta = metaMap.get(tag)\n\n if (!meta)\n return resolve(await Context.filter(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'fastify',\n request: req,\n meta,\n response: res,\n moduleMap,\n }\n const context = new Context(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n\n handlers,\n\n data: {\n params,\n guards, interceptors,\n },\n } = meta\n\n const instance = moduleMap.get(name)\n\n try {\n if (!params)\n throw new BadRequestException(`\"${tag}\" doesn't exist`)\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return resolve(cache)\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: item.args[index], type, key, pipe, pipeOpts, index, reflect: paramsType[index] }\n })) as any\n instance.context = contextData\n const funcData = await moduleMap.get(name)[method](...args)\n resolve(await context.usePostInterceptor(funcData))\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n resolve(await context.useFilter(e))\n }\n })\n })).then((ret) => {\n res.send(ret)\n })\n }\n catch (e) {\n return errorHandler(e)\n }\n })\n\n done()\n })\n\n for (const i of meta) {\n const { method, http, header, tag } = i.data\n\n if (!http?.type)\n continue\n\n const methodTag = `${tag}-${method}`\n\n const {\n paramsType,\n handlers,\n data: {\n interceptors,\n guards,\n params,\n plugins,\n },\n } = metaMap.get(methodTag)!\n\n fastify.register((fastify, _opts, done) => {\n Context.usePlugin(plugins).forEach((p) => {\n p[Symbol.for('skip-override')] = true\n\n fastify.register(p)\n })\n fastify[http.type](http.route, async (req, res) => {\n (req as any)[MODULE_SYMBOL] = moduleMap;\n (req as any)[META_SYMBOL] = meta\n const instance = moduleMap.get(tag)!\n const contextData = {\n type: 'fastify',\n request: req,\n meta: i,\n response: res,\n moduleMap,\n }\n const context = new Context(methodTag, contextData)\n\n try {\n for (const name in header)\n res.header(name, header[name])\n await context.useGuard([...globalGuards, ...guards])\n const cache = await context.useInterceptor([...globalInterceptors, ...interceptors])\n if (cache !== undefined)\n\n return cache\n\n const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {\n return { arg: resolveDep((req as any)[type], key), pipe, pipeOpts, key, type, index, reflect: paramsType[index] }\n }))\n\n instance.context = contextData\n const funcData = await instance[method](...args)\n const ret = await context.usePostInterceptor(funcData)\n\n return ret\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e)\n res.status(err.status).send(err)\n }\n })\n done()\n })\n }\n\n done()\n\n if (IS_DEV) {\n // @ts-expect-error globalThis\n const rawMetaHmr = globalThis.__PS_WRITEMETA__\n // @ts-expect-error globalThis\n\n globalThis.__PS_WRITEMETA__ = () => {\n handleMeta()\n\n rawMetaHmr?.()\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAsCO,SAASA,QAAQ,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAA0B;AAC9H,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ;EAAQ;AAG9JK,iBAAeN,MAAM;IACnBK;IACAE,QAAQL;IACRM,cAAcL;EAChB,CAAA;AAEA,QAAMM,UAAU,oBAAIC,IAAAA;AACpB,WAASC,aAAa;AACpBF,YAAQG,MAAK;AACb,eAAWC,QAAQb,MAAM;AACvB,YAAM,EAAEc,KAAKC,QAAQC,KAAI,IAAKH,KAAKI;AACnC,UAAI,CAACD,MAAME;AACT;AACF,YAAMC,YAAY,GAAGL,OAAOC;AAC5BN,cAAQW,IAAID,WAAWN,IAAAA;IACzB;EACF;AATSF;AAWTA,aAAAA;AAEA,SAAO,CAACU,SAASC,GAAGC,SAAS;AAC1BF,YAAgBG,cAAc;MAC7BzB;MAAWC;IACb;AAKAqB,YAAQI,SAAS,CAACJ,UAASK,OAAOH,UAAS;AACzClB,cAAQsB,QAAQ,CAACC,MAAM;AACrB,cAAMC,SAASC,QAAQC,UAAU;UAACH;SAAE,EAAE;AACtC,YAAIC,QAAQ;AACVA,iBAAOG,OAAOC,IAAI,eAAA,KAAoB;AAEtCZ,UAAAA,SAAQ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ACA9D,QAAAA,MAAAA;MACF,CAAA;IACF;AAEAA,SAAAA;AAEA,QAAI+D,QAAQ;AAEV,YAAMC,aAAaC,WAAWC;AAG9BD,iBAAWC,mBAAmB,MAAM;AAClC9E,mBAAAA;AAEA4E,qBAAAA;MACF;IACF;EACF;AACF;AAtMgBzF;","names":["bindApp","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","isAopDepInject","guards","interceptors","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","fastify","_","done","APP_SYMBOL","register","_opts","forEach","p","plugin","Context","usePlugin","Symbol","for","post","req","res","body","errorHandler","e","error","filter","status","send","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","context","name","split","paramsType","handlers","params","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipe","pipeOpts","index","arg","reflect","funcData","usePostInterceptor","handler","useFilter","then","ret","i","header","MODULE_SYMBOL","META_SYMBOL","resolveDep","err","IS_DEV","rawMetaHmr","globalThis","__PS_WRITEMETA__"]}
|
package/dist/server/h3/index.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkFAHHXHL5js = require('../../chunk-FAHHXHL5.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var _chunkBP7CKHPKjs = require('../../chunk-BP7CKHPK.js');
|
|
8
7
|
|
|
8
|
+
var _chunkYU7ROHTOjs = require('../../chunk-YU7ROHTO.js');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
var _chunk62QDU5FJjs = require('../../chunk-62QDU5FJ.js');
|
|
15
14
|
|
|
16
15
|
|
|
17
|
-
var
|
|
16
|
+
var _chunkLZAU5FUSjs = require('../../chunk-LZAU5FUS.js');
|
|
18
17
|
|
|
19
18
|
// src/server/h3/bind.ts
|
|
20
19
|
var _h3 = require('h3');
|
|
@@ -26,7 +25,12 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
26
25
|
plugins: [],
|
|
27
26
|
...options
|
|
28
27
|
};
|
|
29
|
-
|
|
28
|
+
_chunkYU7ROHTOjs.isAopDepInject.call(void 0, meta, {
|
|
29
|
+
plugins,
|
|
30
|
+
guards: globalGuards,
|
|
31
|
+
interceptors: globalInterceptors
|
|
32
|
+
});
|
|
33
|
+
router[_chunkLZAU5FUSjs.APP_SYMBOL] = {
|
|
30
34
|
moduleMap,
|
|
31
35
|
meta
|
|
32
36
|
};
|
|
@@ -41,16 +45,16 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
41
45
|
metaMap.set(methodTag, item);
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
|
-
|
|
48
|
+
_chunkLZAU5FUSjs.__name.call(void 0, handleMeta, "handleMeta");
|
|
45
49
|
async function createRoute() {
|
|
46
50
|
router.post(route, _h3.fromNodeMiddleware.call(void 0, (req, _res, next) => {
|
|
47
|
-
req[
|
|
48
|
-
req[
|
|
49
|
-
req[
|
|
51
|
+
req[_chunkLZAU5FUSjs.MERGE_SYMBOL] = true;
|
|
52
|
+
req[_chunkLZAU5FUSjs.MODULE_SYMBOL] = moduleMap;
|
|
53
|
+
req[_chunkLZAU5FUSjs.META_SYMBOL] = meta;
|
|
50
54
|
next();
|
|
51
55
|
}));
|
|
52
56
|
plugins.forEach((p) => {
|
|
53
|
-
router.post(route, _h3.fromNodeMiddleware.call(void 0,
|
|
57
|
+
router.post(route, _h3.fromNodeMiddleware.call(void 0, _chunkYU7ROHTOjs.Context.usePlugin([
|
|
54
58
|
p
|
|
55
59
|
])[0]));
|
|
56
60
|
});
|
|
@@ -59,27 +63,27 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
59
63
|
strict: true
|
|
60
64
|
});
|
|
61
65
|
async function errorHandler(e) {
|
|
62
|
-
const error = await
|
|
66
|
+
const error = await _chunkYU7ROHTOjs.Context.filter(e);
|
|
63
67
|
_h3.setResponseStatus.call(void 0, event, error.status);
|
|
64
68
|
return error;
|
|
65
69
|
}
|
|
66
|
-
|
|
70
|
+
_chunkLZAU5FUSjs.__name.call(void 0, errorHandler, "errorHandler");
|
|
67
71
|
if (!Array.isArray(body))
|
|
68
|
-
return errorHandler(new (0,
|
|
72
|
+
return errorHandler(new (0, _chunkYU7ROHTOjs.BadRequestException)("data format should be an array"));
|
|
69
73
|
try {
|
|
70
74
|
return Promise.all(body.map((item) => {
|
|
71
75
|
return new Promise(async (resolve) => {
|
|
72
76
|
const { tag } = item;
|
|
73
77
|
const meta2 = metaMap.get(tag);
|
|
74
78
|
if (!meta2)
|
|
75
|
-
return resolve(await
|
|
79
|
+
return resolve(await _chunkYU7ROHTOjs.Context.filter(new (0, _chunkYU7ROHTOjs.BadRequestException)(`"${tag}" doesn't exist`)));
|
|
76
80
|
const contextData = {
|
|
77
81
|
type: "h3",
|
|
78
82
|
event,
|
|
79
83
|
meta: meta2,
|
|
80
84
|
moduleMap
|
|
81
85
|
};
|
|
82
|
-
const context = new (0,
|
|
86
|
+
const context = new (0, _chunkYU7ROHTOjs.Context)(tag, contextData);
|
|
83
87
|
const [name, method] = tag.split("-");
|
|
84
88
|
const { paramsType, handlers, data: { params, guards, interceptors } } = metaMap.get(tag);
|
|
85
89
|
const instance = moduleMap.get(name);
|
|
@@ -125,12 +129,12 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
125
129
|
const methodTag = `${tag}-${method}`;
|
|
126
130
|
const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2 } } = metaMap.get(methodTag);
|
|
127
131
|
router[http.type](http.route, _h3.fromNodeMiddleware.call(void 0, (req, _res, next) => {
|
|
128
|
-
req[
|
|
129
|
-
req[
|
|
132
|
+
req[_chunkLZAU5FUSjs.MODULE_SYMBOL] = moduleMap;
|
|
133
|
+
req[_chunkLZAU5FUSjs.META_SYMBOL] = meta;
|
|
130
134
|
next();
|
|
131
135
|
}));
|
|
132
136
|
for (const p of plugins2)
|
|
133
|
-
router[http.type](http.route, _h3.fromNodeMiddleware.call(void 0,
|
|
137
|
+
router[http.type](http.route, _h3.fromNodeMiddleware.call(void 0, _chunkYU7ROHTOjs.Context.usePlugin([
|
|
134
138
|
p
|
|
135
139
|
])[0]));
|
|
136
140
|
router[http.type](http.route, _h3.eventHandler.call(void 0, async (event) => {
|
|
@@ -141,7 +145,7 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
141
145
|
event,
|
|
142
146
|
moduleMap
|
|
143
147
|
};
|
|
144
|
-
const context = new (0,
|
|
148
|
+
const context = new (0, _chunkYU7ROHTOjs.Context)(methodTag, contextData);
|
|
145
149
|
try {
|
|
146
150
|
_h3.setHeaders.call(void 0, event, header);
|
|
147
151
|
await context.useGuard([
|
|
@@ -173,7 +177,7 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
173
177
|
arg = body;
|
|
174
178
|
}
|
|
175
179
|
return {
|
|
176
|
-
arg:
|
|
180
|
+
arg: _chunkFAHHXHL5js.resolveDep.call(void 0, arg, key),
|
|
177
181
|
pipe,
|
|
178
182
|
pipeOpts,
|
|
179
183
|
key,
|
|
@@ -195,10 +199,10 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
195
199
|
}));
|
|
196
200
|
}
|
|
197
201
|
}
|
|
198
|
-
|
|
202
|
+
_chunkLZAU5FUSjs.__name.call(void 0, createRoute, "createRoute");
|
|
199
203
|
handleMeta();
|
|
200
204
|
createRoute();
|
|
201
|
-
if (
|
|
205
|
+
if (_chunkLZAU5FUSjs.IS_DEV) {
|
|
202
206
|
const rawMetaHmr = globalThis.__PS_WRITEMETA__;
|
|
203
207
|
globalThis.__PS_WRITEMETA__ = () => {
|
|
204
208
|
handleMeta();
|
|
@@ -206,7 +210,7 @@ function bindApp(router, { moduleMap, meta }, options = {}) {
|
|
|
206
210
|
};
|
|
207
211
|
}
|
|
208
212
|
}
|
|
209
|
-
|
|
213
|
+
_chunkLZAU5FUSjs.__name.call(void 0, bindApp, "bindApp");
|
|
210
214
|
|
|
211
215
|
|
|
212
216
|
exports.bindApp = bindApp;
|