phecda-server 4.0.4 → 4.0.6

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.
Files changed (65) hide show
  1. package/dist/{chunk-YU7ROHTO.js → chunk-7OB6K45T.js} +78 -66
  2. package/dist/chunk-7OB6K45T.js.map +1 -0
  3. package/dist/{chunk-LZAU5FUS.js → chunk-AAF47ZC6.js} +9 -3
  4. package/dist/chunk-AAF47ZC6.js.map +1 -0
  5. package/dist/{chunk-AJ2HYENF.mjs → chunk-AYJWOIAW.mjs} +10 -4
  6. package/dist/chunk-AYJWOIAW.mjs.map +1 -0
  7. package/dist/{chunk-Q6T7HNEO.mjs → chunk-GSJ5A2MA.mjs} +43 -31
  8. package/dist/chunk-GSJ5A2MA.mjs.map +1 -0
  9. package/dist/{chunk-JUXEUWOU.mjs → chunk-K32VRTXB.mjs} +32 -18
  10. package/dist/chunk-K32VRTXB.mjs.map +1 -0
  11. package/dist/{chunk-W64EUEDU.js → chunk-TZ27DHWL.js} +51 -37
  12. package/dist/chunk-TZ27DHWL.js.map +1 -0
  13. package/dist/{chunk-JQPX546Z.mjs → chunk-V7SC5EVJ.mjs} +2 -2
  14. package/dist/{chunk-FAHHXHL5.js → chunk-ZPMAM4LQ.js} +3 -3
  15. package/dist/{core-7c2dd0c7.d.ts → core-c27b4cf4.d.ts} +3 -2
  16. package/dist/index.d.ts +45 -26
  17. package/dist/index.js +119 -54
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +98 -33
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/rpc/rabbitmq/index.d.ts +2 -2
  22. package/dist/rpc/rabbitmq/index.js +19 -13
  23. package/dist/rpc/rabbitmq/index.js.map +1 -1
  24. package/dist/rpc/rabbitmq/index.mjs +13 -7
  25. package/dist/rpc/rabbitmq/index.mjs.map +1 -1
  26. package/dist/rpc/redis/index.d.ts +2 -2
  27. package/dist/rpc/redis/index.js +19 -13
  28. package/dist/rpc/redis/index.js.map +1 -1
  29. package/dist/rpc/redis/index.mjs +13 -7
  30. package/dist/rpc/redis/index.mjs.map +1 -1
  31. package/dist/server/express/index.d.ts +4 -3
  32. package/dist/server/express/index.js +37 -29
  33. package/dist/server/express/index.js.map +1 -1
  34. package/dist/server/express/index.mjs +19 -11
  35. package/dist/server/express/index.mjs.map +1 -1
  36. package/dist/server/fastify/index.d.ts +3 -2
  37. package/dist/server/fastify/index.js +33 -25
  38. package/dist/server/fastify/index.js.map +1 -1
  39. package/dist/server/fastify/index.mjs +18 -10
  40. package/dist/server/fastify/index.mjs.map +1 -1
  41. package/dist/server/h3/index.d.ts +6 -5
  42. package/dist/server/h3/index.js +34 -28
  43. package/dist/server/h3/index.js.map +1 -1
  44. package/dist/server/h3/index.mjs +16 -10
  45. package/dist/server/h3/index.mjs.map +1 -1
  46. package/dist/server/koa/index.d.ts +3 -2
  47. package/dist/server/koa/index.js +36 -28
  48. package/dist/server/koa/index.js.map +1 -1
  49. package/dist/server/koa/index.mjs +18 -10
  50. package/dist/server/koa/index.mjs.map +1 -1
  51. package/dist/test.d.ts +1 -1
  52. package/dist/test.js +6 -6
  53. package/dist/test.mjs +2 -2
  54. package/dist/{types-ead02c5d.d.ts → types-8415bd5d.d.ts} +16 -17
  55. package/package.json +1 -1
  56. package/register/compile.mjs +3 -7
  57. package/register/loader.mjs +0 -1
  58. package/dist/chunk-AJ2HYENF.mjs.map +0 -1
  59. package/dist/chunk-JUXEUWOU.mjs.map +0 -1
  60. package/dist/chunk-LZAU5FUS.js.map +0 -1
  61. package/dist/chunk-Q6T7HNEO.mjs.map +0 -1
  62. package/dist/chunk-W64EUEDU.js.map +0 -1
  63. package/dist/chunk-YU7ROHTO.js.map +0 -1
  64. /package/dist/{chunk-JQPX546Z.mjs.map → chunk-V7SC5EVJ.mjs.map} +0 -0
  65. /package/dist/{chunk-FAHHXHL5.js.map → chunk-ZPMAM4LQ.js.map} +0 -0
@@ -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, 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 parallel: true,\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 (res.writableEnded)\n return true\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 globalThis.__PS_HMR__?.push(async () => {\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\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,YAAQG,MAAK;AACb,eAAWC,QAAQd,MAAM;AACvB,YAAM,EAAEe,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;AAC1BxB,QAAgByB,KAAKnB,OAAO,CAACoB,KAAKC,MAAMC,SAAS;AAC/CF,UAAYG,gBAAgB;AAC5BH,UAA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AAA;IACF;EACF;AAjJe7D;AAmJfV,aAAAA;AACAU,cAAAA;AACA,MAAI8D,QAAQ;AACVC,eAAWC,YAAYC,KAAK,YAAY;AACtCzF,UAAI0F,QAAQ,CAAA;AACZ5E,iBAAAA;AACAU,kBAAAA;IACF,CAAA;EACF;AACF;AAlLgBzB;","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","parallel","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","writableEnded","err","IS_DEV","globalThis","__PS_HMR__","push","stack"]}
1
+ {"version":3,"sources":["../../../src/server/express/bind.ts"],"sourcesContent":["import type { Express, Request, Response, 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 parallel: boolean\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.filterRecord.default(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.filterRecord.default(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 parallel: true,\n }\n const context = new Context<ExpressCtx>(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n handlers,\n data: {\n params,\n guards, interceptors,\n filter,\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, filter))\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 filter,\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 parallel: false,\n }\n const context = new Context<ExpressCtx>(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 (res.writableEnded)\n return\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, filter)\n if (res.writableEnded)\n return\n res.status(err.status).json(err)\n }\n })\n }\n }\n\n handleMeta()\n createRoute()\n if (IS_DEV) {\n globalThis.__PS_HMR__?.push(async () => {\n isAopDepInject(meta, {\n plugins,\n guards: globalGuards,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terceptors","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","filterRecord","default","status","json","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","parallel","context","name","split","paramsType","handlers","params","filter","instance","useGuard","cache","useInterceptor","undefined","args","usePipe","key","pipeOpts","pipe","index","arg","reflect","funcData","usePostInterceptor","forEach","handler","useFilter","then","ret","i","header","send","resolveDep","writableEnded","err","IS_DEV","globalThis","__PS_HMR__","push","stack"]}
@@ -1,6 +1,6 @@
1
1
  import { FastifyRequest, FastifyReply, FastifyPluginCallback } from 'fastify';
2
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
3
- import '../../types-ead02c5d.js';
2
+ import { M as Meta, F as Factory } from '../../core-c27b4cf4.js';
3
+ import '../../types-8415bd5d.js';
4
4
  import 'phecda-core';
5
5
 
6
6
  interface FastifyCtx {
@@ -9,6 +9,7 @@ interface FastifyCtx {
9
9
  response: FastifyReply;
10
10
  meta: Meta;
11
11
  moduleMap: Record<string, any>;
12
+ parallel: boolean;
12
13
  [key: string]: any;
13
14
  }
14
15
  interface Options {
@@ -1,18 +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 _chunkFAHHXHL5js = require('../../chunk-FAHHXHL5.js');
3
+ var _chunkZPMAM4LQjs = require('../../chunk-ZPMAM4LQ.js');
4
4
 
5
5
 
6
6
 
7
7
 
8
- var _chunkYU7ROHTOjs = require('../../chunk-YU7ROHTO.js');
8
+ var _chunk7OB6K45Tjs = require('../../chunk-7OB6K45T.js');
9
9
 
10
10
 
11
11
 
12
12
 
13
13
 
14
14
 
15
- var _chunkLZAU5FUSjs = require('../../chunk-LZAU5FUS.js');
15
+ var _chunkAAF47ZC6js = require('../../chunk-AAF47ZC6.js');
16
16
 
17
17
  // src/server/fastify/bind.ts
18
18
  function bindApp({ moduleMap, meta }, options = {}) {
@@ -23,7 +23,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
23
23
  plugins: [],
24
24
  ...options
25
25
  };
26
- _chunkYU7ROHTOjs.isAopDepInject.call(void 0, meta, {
26
+ _chunk7OB6K45Tjs.isAopDepInject.call(void 0, meta, {
27
27
  plugins,
28
28
  guards: globalGuards,
29
29
  interceptors: globalInterceptors
@@ -39,16 +39,16 @@ function bindApp({ moduleMap, meta }, options = {}) {
39
39
  metaMap.set(methodTag, item);
40
40
  }
41
41
  }
42
- _chunkLZAU5FUSjs.__name.call(void 0, handleMeta, "handleMeta");
42
+ _chunkAAF47ZC6js.__name.call(void 0, handleMeta, "handleMeta");
43
43
  handleMeta();
44
44
  return (fastify, _, done) => {
45
- fastify[_chunkLZAU5FUSjs.APP_SYMBOL] = {
45
+ fastify[_chunkAAF47ZC6js.APP_SYMBOL] = {
46
46
  moduleMap,
47
47
  meta
48
48
  };
49
49
  fastify.register((fastify2, _opts, done2) => {
50
50
  plugins.forEach((p) => {
51
- const plugin = _chunkYU7ROHTOjs.Context.usePlugin([
51
+ const plugin = _chunk7OB6K45Tjs.Context.usePlugin([
52
52
  p
53
53
  ])[0];
54
54
  if (plugin) {
@@ -59,19 +59,19 @@ function bindApp({ moduleMap, meta }, options = {}) {
59
59
  fastify2.post(route, async (req, res) => {
60
60
  const { body } = req;
61
61
  async function errorHandler(e) {
62
- const error = await _chunkYU7ROHTOjs.Context.filter(e);
62
+ const error = await _chunk7OB6K45Tjs.Context.filterRecord.default(e);
63
63
  return res.status(error.status).send(error);
64
64
  }
65
- _chunkLZAU5FUSjs.__name.call(void 0, errorHandler, "errorHandler");
65
+ _chunkAAF47ZC6js.__name.call(void 0, errorHandler, "errorHandler");
66
66
  if (!Array.isArray(body))
67
- return errorHandler(new (0, _chunkYU7ROHTOjs.BadRequestException)("data format should be an array"));
67
+ return errorHandler(new (0, _chunk7OB6K45Tjs.BadRequestException)("data format should be an array"));
68
68
  try {
69
69
  return Promise.all(body.map((item) => {
70
70
  return new Promise(async (resolve) => {
71
71
  const { tag } = item;
72
72
  const meta2 = metaMap.get(tag);
73
73
  if (!meta2)
74
- return resolve(await _chunkYU7ROHTOjs.Context.filter(new (0, _chunkYU7ROHTOjs.BadRequestException)(`"${tag}" doesn't exist`)));
74
+ return resolve(await _chunk7OB6K45Tjs.Context.filterRecord.default(new (0, _chunk7OB6K45Tjs.BadRequestException)(`"${tag}" doesn't exist`)));
75
75
  const contextData = {
76
76
  type: "fastify",
77
77
  request: req,
@@ -80,13 +80,13 @@ function bindApp({ moduleMap, meta }, options = {}) {
80
80
  moduleMap,
81
81
  parallel: true
82
82
  };
83
- const context = new (0, _chunkYU7ROHTOjs.Context)(tag, contextData);
83
+ const context = new (0, _chunk7OB6K45Tjs.Context)(tag, contextData);
84
84
  const [name, method] = tag.split("-");
85
- const { paramsType, handlers, data: { params, guards, interceptors } } = meta2;
85
+ const { paramsType, handlers, data: { params, guards, interceptors, filter } } = meta2;
86
86
  const instance = moduleMap.get(name);
87
87
  try {
88
88
  if (!params)
89
- throw new (0, _chunkYU7ROHTOjs.BadRequestException)(`"${tag}" doesn't exist`);
89
+ throw new (0, _chunk7OB6K45Tjs.BadRequestException)(`"${tag}" doesn't exist`);
90
90
  await context.useGuard([
91
91
  ...globalGuards,
92
92
  ...guards
@@ -113,7 +113,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
113
113
  resolve(await context.usePostInterceptor(funcData));
114
114
  } catch (e) {
115
115
  handlers.forEach((handler) => _optionalChain([handler, 'access', _3 => _3.error, 'optionalCall', _4 => _4(e)]));
116
- resolve(await context.useFilter(e));
116
+ resolve(await context.useFilter(e, filter));
117
117
  }
118
118
  });
119
119
  })).then((ret) => {
@@ -130,24 +130,25 @@ function bindApp({ moduleMap, meta }, options = {}) {
130
130
  if (!_optionalChain([http, 'optionalAccess', _5 => _5.type]))
131
131
  continue;
132
132
  const methodTag = `${tag}-${method}`;
133
- const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2 } } = metaMap.get(methodTag);
133
+ const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2, filter } } = metaMap.get(methodTag);
134
134
  fastify.register((fastify2, _opts, done2) => {
135
- _chunkYU7ROHTOjs.Context.usePlugin(plugins2).forEach((p) => {
135
+ _chunk7OB6K45Tjs.Context.usePlugin(plugins2).forEach((p) => {
136
136
  p[Symbol.for("skip-override")] = true;
137
137
  fastify2.register(p);
138
138
  });
139
139
  fastify2[http.type](http.route, async (req, res) => {
140
- req[_chunkLZAU5FUSjs.MODULE_SYMBOL] = moduleMap;
141
- req[_chunkLZAU5FUSjs.META_SYMBOL] = meta;
140
+ req[_chunkAAF47ZC6js.MODULE_SYMBOL] = moduleMap;
141
+ req[_chunkAAF47ZC6js.META_SYMBOL] = meta;
142
142
  const instance = moduleMap.get(tag);
143
143
  const contextData = {
144
144
  type: "fastify",
145
145
  request: req,
146
146
  meta: i,
147
147
  response: res,
148
- moduleMap
148
+ moduleMap,
149
+ parallel: false
149
150
  };
150
- const context = new (0, _chunkYU7ROHTOjs.Context)(methodTag, contextData);
151
+ const context = new (0, _chunk7OB6K45Tjs.Context)(methodTag, contextData);
151
152
  try {
152
153
  for (const name in header)
153
154
  res.header(name, header[name]);
@@ -163,7 +164,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
163
164
  return cache;
164
165
  const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {
165
166
  return {
166
- arg: _chunkFAHHXHL5js.resolveDep.call(void 0, req[type], key),
167
+ arg: _chunkZPMAM4LQjs.resolveDep.call(void 0, req[type], key),
167
168
  pipe,
168
169
  pipeOpts,
169
170
  key,
@@ -180,7 +181,9 @@ function bindApp({ moduleMap, meta }, options = {}) {
180
181
  return ret;
181
182
  } catch (e) {
182
183
  handlers.forEach((handler) => _optionalChain([handler, 'access', _6 => _6.error, 'optionalCall', _7 => _7(e)]));
183
- const err = await context.useFilter(e);
184
+ const err = await context.useFilter(e, filter);
185
+ if (res.sent)
186
+ return;
184
187
  res.status(err.status).send(err);
185
188
  }
186
189
  });
@@ -188,14 +191,19 @@ function bindApp({ moduleMap, meta }, options = {}) {
188
191
  });
189
192
  }
190
193
  done();
191
- if (_chunkLZAU5FUSjs.IS_DEV) {
194
+ if (_chunkAAF47ZC6js.IS_DEV) {
192
195
  _optionalChain([globalThis, 'access', _8 => _8.__PS_HMR__, 'optionalAccess', _9 => _9.push, 'call', _10 => _10(async () => {
196
+ _chunk7OB6K45Tjs.isAopDepInject.call(void 0, meta, {
197
+ plugins,
198
+ guards: globalGuards,
199
+ interceptors: globalInterceptors
200
+ });
193
201
  handleMeta();
194
202
  })]);
195
203
  }
196
204
  };
197
205
  }
198
- _chunkLZAU5FUSjs.__name.call(void 0, bindApp, "bindApp");
206
+ _chunkAAF47ZC6js.__name.call(void 0, bindApp, "bindApp");
199
207
 
200
208
 
201
209
  exports.bindApp = bindApp;
@@ -1 +1 @@
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","parallel","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","sent","err","IS_DEV","globalThis","__PS_HMR__","push"],"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,SAAQI,SAASI,MAAAA;QACnB;MACF,CAAA;AACAR,MAAAA,SAAQa,KAAK9B,OAAO,OAAO+B,KAAKC,QAAQ;AACtC,cAAM,EAAEC,KAAI,IAAKF;AAEjB,uBAAeG,aAAaC,GAAQ;AAClC,gBAAMC,QAAQ,MAAMV,QAAQW,OAAOF,CAAAA;A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gBb","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 parallel: true,\n\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 if (res.sent)\n return\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 globalThis.__PS_HMR__?.push(async () => {\n handleMeta()\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","filterRecord","default","status","send","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","parallel","context","name","split","paramsType","handlers","params","filter","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","sent","err","IS_DEV","globalThis","__PS_HMR__","push"],"mappings":";;;;;;;;;;;;;;;;;AAuCO,SAASA,QAAQ,EAAEC,WAAWC,KAAI,GAAyCC,UAA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yRequest } 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 parallel: boolean\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.filterRecord.default(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.filterRecord.default(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'fastify' as const,\n request: req,\n meta,\n response: res,\n moduleMap,\n parallel: true,\n\n }\n const context = new Context<FastifyCtx>(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n\n handlers,\n\n data: {\n params,\n guards,\n interceptors,\n filter,\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, filter))\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 filter,\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' as const,\n request: req,\n meta: i,\n response: res,\n moduleMap,\n parallel: false,\n }\n const context = new Context<FastifyCtx>(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 if (res.sent)\n return\n\n return ret\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e, filter)\n\n if (res.sent)\n return\n res.status(err.status).send(err)\n }\n })\n done()\n })\n }\n\n done()\n\n if (IS_DEV) {\n globalThis.__PS_HMR__?.push(async () => {\n isAopDepInject(meta, {\n plugins,\n guards: globalGuards,\n interceptors: globalInterceptors,\n })\n handleMeta()\n })\n }\n }\n}\n"]}
@@ -1,18 +1,18 @@
1
1
  import {
2
2
  resolveDep
3
- } from "../../chunk-JQPX546Z.mjs";
3
+ } from "../../chunk-V7SC5EVJ.mjs";
4
4
  import {
5
5
  BadRequestException,
6
6
  Context,
7
7
  isAopDepInject
8
- } from "../../chunk-Q6T7HNEO.mjs";
8
+ } from "../../chunk-GSJ5A2MA.mjs";
9
9
  import {
10
10
  APP_SYMBOL,
11
11
  IS_DEV,
12
12
  META_SYMBOL,
13
13
  MODULE_SYMBOL,
14
14
  __name
15
- } from "../../chunk-AJ2HYENF.mjs";
15
+ } from "../../chunk-AYJWOIAW.mjs";
16
16
 
17
17
  // src/server/fastify/bind.ts
18
18
  function bindApp({ moduleMap, meta }, options = {}) {
@@ -59,7 +59,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
59
59
  fastify2.post(route, async (req, res) => {
60
60
  const { body } = req;
61
61
  async function errorHandler(e) {
62
- const error = await Context.filter(e);
62
+ const error = await Context.filterRecord.default(e);
63
63
  return res.status(error.status).send(error);
64
64
  }
65
65
  __name(errorHandler, "errorHandler");
@@ -71,7 +71,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
71
71
  const { tag } = item;
72
72
  const meta2 = metaMap.get(tag);
73
73
  if (!meta2)
74
- return resolve(await Context.filter(new BadRequestException(`"${tag}" doesn't exist`)));
74
+ return resolve(await Context.filterRecord.default(new BadRequestException(`"${tag}" doesn't exist`)));
75
75
  const contextData = {
76
76
  type: "fastify",
77
77
  request: req,
@@ -82,7 +82,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
82
82
  };
83
83
  const context = new Context(tag, contextData);
84
84
  const [name, method] = tag.split("-");
85
- const { paramsType, handlers, data: { params, guards, interceptors } } = meta2;
85
+ const { paramsType, handlers, data: { params, guards, interceptors, filter } } = meta2;
86
86
  const instance = moduleMap.get(name);
87
87
  try {
88
88
  if (!params)
@@ -113,7 +113,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
113
113
  resolve(await context.usePostInterceptor(funcData));
114
114
  } catch (e) {
115
115
  handlers.forEach((handler) => handler.error?.(e));
116
- resolve(await context.useFilter(e));
116
+ resolve(await context.useFilter(e, filter));
117
117
  }
118
118
  });
119
119
  })).then((ret) => {
@@ -130,7 +130,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
130
130
  if (!http?.type)
131
131
  continue;
132
132
  const methodTag = `${tag}-${method}`;
133
- const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2 } } = metaMap.get(methodTag);
133
+ const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2, filter } } = metaMap.get(methodTag);
134
134
  fastify.register((fastify2, _opts, done2) => {
135
135
  Context.usePlugin(plugins2).forEach((p) => {
136
136
  p[Symbol.for("skip-override")] = true;
@@ -145,7 +145,8 @@ function bindApp({ moduleMap, meta }, options = {}) {
145
145
  request: req,
146
146
  meta: i,
147
147
  response: res,
148
- moduleMap
148
+ moduleMap,
149
+ parallel: false
149
150
  };
150
151
  const context = new Context(methodTag, contextData);
151
152
  try {
@@ -180,7 +181,9 @@ function bindApp({ moduleMap, meta }, options = {}) {
180
181
  return ret;
181
182
  } catch (e) {
182
183
  handlers.forEach((handler) => handler.error?.(e));
183
- const err = await context.useFilter(e);
184
+ const err = await context.useFilter(e, filter);
185
+ if (res.sent)
186
+ return;
184
187
  res.status(err.status).send(err);
185
188
  }
186
189
  });
@@ -190,6 +193,11 @@ function bindApp({ moduleMap, meta }, options = {}) {
190
193
  done();
191
194
  if (IS_DEV) {
192
195
  globalThis.__PS_HMR__?.push(async () => {
196
+ isAopDepInject(meta, {
197
+ plugins,
198
+ guards: globalGuards,
199
+ interceptors: globalInterceptors
200
+ });
193
201
  handleMeta();
194
202
  });
195
203
  }
@@ -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, 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 parallel: true,\n\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 if (res.sent)\n return\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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ceptors","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","parallel","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","sent","err","IS_DEV","globalThis","__PS_HMR__","push"]}
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 parallel: boolean\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.filterRecord.default(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.filterRecord.default(new BadRequestException(`\"${tag}\" doesn't exist`)))\n\n const contextData = {\n type: 'fastify' as const,\n request: req,\n meta,\n response: res,\n moduleMap,\n parallel: true,\n\n }\n const context = new Context<FastifyCtx>(tag, contextData)\n const [name, method] = tag.split('-')\n const {\n paramsType,\n\n handlers,\n\n data: {\n params,\n guards,\n interceptors,\n filter,\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, filter))\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 filter,\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' as const,\n request: req,\n meta: i,\n response: res,\n moduleMap,\n parallel: false,\n }\n const context = new Context<FastifyCtx>(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 if (res.sent)\n return\n\n return ret\n }\n catch (e: any) {\n handlers.forEach(handler => handler.error?.(e))\n const err = await context.useFilter(e, filter)\n\n if (res.sent)\n return\n res.status(err.status).send(err)\n }\n })\n done()\n })\n }\n\n done()\n\n if (IS_DEV) {\n globalThis.__PS_HMR__?.push(async () => {\n isAopDepInject(meta, {\n plugins,\n guards: globalGuards,\n interceptors: globalInterceptors,\n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,cAAcL;QAChB,CAAA;AACAQ,mBAAAA;MACF,CAAA;IACF;EACF;AACF;AAhNgBb;","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","filterRecord","default","status","send","Array","isArray","BadRequestException","Promise","all","map","resolve","get","contextData","request","response","parallel","context","name","split","paramsType","handlers","params","filter","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","sent","err","IS_DEV","globalThis","__PS_HMR__","push"]}
@@ -1,14 +1,15 @@
1
- import { Router } from 'h3';
2
- import { M as Meta, F as Factory } from '../../core-7c2dd0c7.js';
3
- import '../../types-ead02c5d.js';
1
+ import { H3Event, Router } from 'h3';
2
+ import { M as Meta, F as Factory } from '../../core-c27b4cf4.js';
3
+ import '../../types-8415bd5d.js';
4
4
  import 'phecda-core';
5
5
 
6
6
  interface H3Ctx {
7
7
  type: 'h3';
8
- request: Request;
9
- response: Response;
8
+ event: H3Event;
10
9
  meta: Meta;
11
10
  moduleMap: Record<string, any>;
11
+ parallel: boolean;
12
+ [key: string]: any;
12
13
  }
13
14
  interface Options {
14
15
  route?: string;