phecda-server 4.0.6 → 4.1.0
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-7OB6K45T.js → chunk-32E425F6.js} +55 -55
- package/dist/chunk-32E425F6.js.map +1 -0
- package/dist/{chunk-AYJWOIAW.mjs → chunk-5KOB2WHH.mjs} +20 -2
- package/dist/chunk-5KOB2WHH.mjs.map +1 -0
- package/dist/{chunk-TZ27DHWL.js → chunk-5L4K7XEB.js} +33 -31
- package/dist/chunk-5L4K7XEB.js.map +1 -0
- package/dist/{chunk-ZPMAM4LQ.js → chunk-63KBHOSB.js} +3 -3
- package/dist/{chunk-AAF47ZC6.js → chunk-BDIQWWHN.js} +20 -2
- package/dist/chunk-BDIQWWHN.js.map +1 -0
- package/dist/{chunk-V7SC5EVJ.mjs → chunk-Q3V3ERU3.mjs} +2 -2
- package/dist/{chunk-K32VRTXB.mjs → chunk-WQBXOVND.mjs} +16 -14
- package/dist/chunk-WQBXOVND.mjs.map +1 -0
- package/dist/{chunk-GSJ5A2MA.mjs → chunk-Z6LSJTOF.mjs} +11 -11
- package/dist/chunk-Z6LSJTOF.mjs.map +1 -0
- package/dist/{types-8415bd5d.d.ts → core-d080d872.d.ts} +41 -9
- package/dist/index.d.ts +28 -29
- package/dist/index.js +61 -57
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -6
- package/dist/index.mjs.map +1 -1
- package/dist/rpc/rabbitmq/index.d.ts +3 -7
- package/dist/rpc/rabbitmq/index.js +22 -20
- package/dist/rpc/rabbitmq/index.js.map +1 -1
- package/dist/rpc/rabbitmq/index.mjs +17 -15
- package/dist/rpc/rabbitmq/index.mjs.map +1 -1
- package/dist/rpc/redis/index.d.ts +3 -7
- package/dist/rpc/redis/index.js +23 -21
- package/dist/rpc/redis/index.js.map +1 -1
- package/dist/rpc/redis/index.mjs +18 -16
- package/dist/rpc/redis/index.mjs.map +1 -1
- package/dist/server/express/index.d.ts +2 -6
- package/dist/server/express/index.js +28 -26
- package/dist/server/express/index.js.map +1 -1
- package/dist/server/express/index.mjs +9 -7
- package/dist/server/express/index.mjs.map +1 -1
- package/dist/server/fastify/index.d.ts +4 -8
- package/dist/server/fastify/index.js +30 -24
- package/dist/server/fastify/index.js.map +1 -1
- package/dist/server/fastify/index.mjs +14 -8
- package/dist/server/fastify/index.mjs.map +1 -1
- package/dist/server/h3/index.d.ts +2 -6
- package/dist/server/h3/index.js +28 -26
- package/dist/server/h3/index.js.map +1 -1
- package/dist/server/h3/index.mjs +9 -7
- package/dist/server/h3/index.mjs.map +1 -1
- package/dist/server/koa/index.d.ts +2 -6
- package/dist/server/koa/index.js +28 -26
- package/dist/server/koa/index.js.map +1 -1
- package/dist/server/koa/index.mjs +9 -7
- package/dist/server/koa/index.mjs.map +1 -1
- package/dist/test.d.ts +9 -5
- package/dist/test.js +10 -21
- package/dist/test.js.map +1 -1
- package/dist/test.mjs +7 -18
- package/dist/test.mjs.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-7OB6K45T.js.map +0 -1
- package/dist/chunk-AAF47ZC6.js.map +0 -1
- package/dist/chunk-AYJWOIAW.mjs.map +0 -1
- package/dist/chunk-GSJ5A2MA.mjs.map +0 -1
- package/dist/chunk-K32VRTXB.mjs.map +0 -1
- package/dist/chunk-TZ27DHWL.js.map +0 -1
- package/dist/core-c27b4cf4.d.ts +0 -23
- /package/dist/{chunk-ZPMAM4LQ.js.map → chunk-63KBHOSB.js.map} +0 -0
- /package/dist/{chunk-V7SC5EVJ.mjs.map → chunk-Q3V3ERU3.mjs.map} +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
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 _chunk63KBHOSBjs = require('../../chunk-63KBHOSB.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _chunk32E425F6js = require('../../chunk-32E425F6.js');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
@@ -13,7 +13,7 @@ var _chunk7OB6K45Tjs = require('../../chunk-7OB6K45T.js');
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
var
|
|
16
|
+
var _chunkBDIQWWHNjs = require('../../chunk-BDIQWWHN.js');
|
|
17
17
|
|
|
18
18
|
// src/server/express/bind.ts
|
|
19
19
|
function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
@@ -24,12 +24,12 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
24
24
|
plugins: [],
|
|
25
25
|
...options
|
|
26
26
|
};
|
|
27
|
-
|
|
27
|
+
_chunk32E425F6js.isAopDepInject.call(void 0, meta, {
|
|
28
28
|
plugins,
|
|
29
29
|
guards: globalGuards,
|
|
30
30
|
interceptors: globalInterceptors
|
|
31
31
|
});
|
|
32
|
-
app[
|
|
32
|
+
app[_chunkBDIQWWHNjs.APP_SYMBOL] = {
|
|
33
33
|
moduleMap,
|
|
34
34
|
meta
|
|
35
35
|
};
|
|
@@ -44,38 +44,39 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
44
44
|
metaMap.set(methodTag, item);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
_chunkBDIQWWHNjs.__name.call(void 0, handleMeta, "handleMeta");
|
|
48
48
|
async function createRoute() {
|
|
49
49
|
app.post(route, (req, _res, next) => {
|
|
50
|
-
req[
|
|
51
|
-
req[
|
|
52
|
-
req[
|
|
50
|
+
req[_chunkBDIQWWHNjs.MERGE_SYMBOL] = true;
|
|
51
|
+
req[_chunkBDIQWWHNjs.MODULE_SYMBOL] = moduleMap;
|
|
52
|
+
req[_chunkBDIQWWHNjs.META_SYMBOL] = meta;
|
|
53
53
|
next();
|
|
54
|
-
}, ...
|
|
54
|
+
}, ..._chunk32E425F6js.Context.usePlugin(plugins), async (req, res) => {
|
|
55
55
|
const { body } = req;
|
|
56
56
|
async function errorHandler(e) {
|
|
57
|
-
const error = await
|
|
57
|
+
const error = await _chunk32E425F6js.Context.filterRecord.default(e);
|
|
58
58
|
return res.status(error.status).json(error);
|
|
59
59
|
}
|
|
60
|
-
|
|
60
|
+
_chunkBDIQWWHNjs.__name.call(void 0, errorHandler, "errorHandler");
|
|
61
61
|
if (!Array.isArray(body))
|
|
62
|
-
return errorHandler(new (0,
|
|
62
|
+
return errorHandler(new (0, _chunk32E425F6js.BadRequestException)("data format should be an array"));
|
|
63
63
|
try {
|
|
64
64
|
return Promise.all(body.map((item) => {
|
|
65
65
|
return new Promise(async (resolve) => {
|
|
66
66
|
const { tag } = item;
|
|
67
67
|
const meta2 = metaMap.get(tag);
|
|
68
68
|
if (!meta2)
|
|
69
|
-
return resolve(await
|
|
69
|
+
return resolve(await _chunk32E425F6js.Context.filterRecord.default(new (0, _chunk32E425F6js.BadRequestException)(`"${tag}" doesn't exist`)));
|
|
70
70
|
const contextData = {
|
|
71
71
|
type: "express",
|
|
72
72
|
request: req,
|
|
73
73
|
meta: meta2,
|
|
74
74
|
response: res,
|
|
75
75
|
moduleMap,
|
|
76
|
-
parallel: true
|
|
76
|
+
parallel: true,
|
|
77
|
+
tag
|
|
77
78
|
};
|
|
78
|
-
const context = new (0,
|
|
79
|
+
const context = new (0, _chunk32E425F6js.Context)(contextData);
|
|
79
80
|
const [name, method] = tag.split("-");
|
|
80
81
|
const { paramsType, handlers, data: { params, guards, interceptors, filter } } = meta2;
|
|
81
82
|
const instance = moduleMap.get(name);
|
|
@@ -123,10 +124,10 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
123
124
|
const methodTag = `${tag}-${method}`;
|
|
124
125
|
const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2, filter } } = metaMap.get(methodTag);
|
|
125
126
|
app[http.type](http.route, (req, _res, next) => {
|
|
126
|
-
req[
|
|
127
|
-
req[
|
|
127
|
+
req[_chunkBDIQWWHNjs.MODULE_SYMBOL] = moduleMap;
|
|
128
|
+
req[_chunkBDIQWWHNjs.META_SYMBOL] = meta;
|
|
128
129
|
next();
|
|
129
|
-
}, ...
|
|
130
|
+
}, ..._chunk32E425F6js.Context.usePlugin(plugins2), async (req, res) => {
|
|
130
131
|
const instance = moduleMap.get(tag);
|
|
131
132
|
const contextData = {
|
|
132
133
|
type: "express",
|
|
@@ -134,9 +135,10 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
134
135
|
meta: i,
|
|
135
136
|
response: res,
|
|
136
137
|
moduleMap,
|
|
137
|
-
parallel: false
|
|
138
|
+
parallel: false,
|
|
139
|
+
tag: methodTag
|
|
138
140
|
};
|
|
139
|
-
const context = new (0,
|
|
141
|
+
const context = new (0, _chunk32E425F6js.Context)(contextData);
|
|
140
142
|
try {
|
|
141
143
|
for (const name in header)
|
|
142
144
|
res.set(name, header[name]);
|
|
@@ -157,7 +159,7 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
157
159
|
}
|
|
158
160
|
const args = await context.usePipe(params.map(({ type, key, pipeOpts, index, pipe }) => {
|
|
159
161
|
return {
|
|
160
|
-
arg:
|
|
162
|
+
arg: _chunk63KBHOSBjs.resolveDep.call(void 0, req[type], key),
|
|
161
163
|
pipeOpts,
|
|
162
164
|
pipe,
|
|
163
165
|
key,
|
|
@@ -185,12 +187,12 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
185
187
|
});
|
|
186
188
|
}
|
|
187
189
|
}
|
|
188
|
-
|
|
190
|
+
_chunkBDIQWWHNjs.__name.call(void 0, createRoute, "createRoute");
|
|
189
191
|
handleMeta();
|
|
190
192
|
createRoute();
|
|
191
|
-
if (
|
|
193
|
+
if (_chunkBDIQWWHNjs.IS_DEV) {
|
|
192
194
|
_optionalChain([globalThis, 'access', _7 => _7.__PS_HMR__, 'optionalAccess', _8 => _8.push, 'call', _9 => _9(async () => {
|
|
193
|
-
|
|
195
|
+
_chunk32E425F6js.isAopDepInject.call(void 0, meta, {
|
|
194
196
|
plugins,
|
|
195
197
|
guards: globalGuards,
|
|
196
198
|
interceptors: globalInterceptors
|
|
@@ -201,7 +203,7 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
201
203
|
})]);
|
|
202
204
|
}
|
|
203
205
|
}
|
|
204
|
-
|
|
206
|
+
_chunkBDIQWWHNjs.__name.call(void 0, bindApp, "bindApp");
|
|
205
207
|
|
|
206
208
|
|
|
207
209
|
exports.bindApp = bindApp;
|
|
@@ -1 +1 @@
|
|
|
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","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"],"mappings":";;;;;;;;;;;;;;;;;;AAsCO,SAASA,QAAQC,KAAa,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAAG;AACpH,QAAM,EAAEC,cAAcC,oBAAoBC,OA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AC,EAAE3B,MAAMgD,KAAKC,UAAUE,OAAOD,KAAI,MAAO;AACtF,mBAAO;cAAEE,KAAKY,WAAY3D,IAAYL,OAAOgD,GAAAA;cAAMC;cAAUC;cAAMF;cAAKhD;cAAMmD;cAAOE,SAAShB,WAAWc;YAAO;UAClH,CAAA,CAAA;AAEAV,mBAASP,UAAUJ;AACnB,gBAAMwB,WAAW,MAAMb,SAAS5C,QAAO,GAAIiD,IAAAA;AAC3C,gBAAMc,MAAM,MAAM1B,QAAQqB,mBAAmBD,QAAAA;AAE7C,cAAIzC,IAAIoD;AACN;AAEF,cAAI,OAAOL,QAAQ;AACjB/C,gBAAIkD,KAAKH,GAAAA;;AAGT/C,gBAAIQ,KAAKuC,GAAAA;QACb,SACO5C,GAAP;AACEsB,mBAASkB,QAAQC,CAAAA,YAAWA,QAAQxC,QAAQD,CAAAA,CAAAA;AAC5C,gBAAMkD,MAAM,MAAMhC,QAAQwB,UAAU1C,GAAGwB,MAAAA;AACvC,cAAI3B,IAAIoD;AACN;AACFpD,cAAIO,OAAO8C,IAAI9C,MAAM,EAAEC,KAAK6C,GAAAA;QAC9B;MACF,CAAA;IACF;EACF;AAtJe/D;AAwJfV,aAAAA;AACAU,cAAAA;AACA,MAAIgE,QAAQ;AACVC,eAAWC,YAAYC,KAAK,YAAY;AACtCnF,qBAAeN,MAAM;QACnBK;QACAE,QAAQL;QACRM,cAAcL;MAChB,CAAA;AACAL,UAAI4F,QAAQ,CAAA;AACZ9E,iBAAAA;AACAU,kBAAAA;IACF,CAAA;EACF;AACF;AA5LgBzB","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,\n interceptors: globalInterceptors,\n })\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\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","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"],"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;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AUJ;AACnB,gBAAMwB,WAAW,MAAMb,SAAS5C,QAAO,GAAIiD,IAAAA;AAC3C,gBAAMc,MAAM,MAAM1B,QAAQqB,mBAAmBD,QAAAA;AAE7C,cAAIzC,IAAIoD;AACN;AAEF,cAAI,OAAOL,QAAQ;AACjB/C,gBAAIkD,KAAKH,GAAAA;;AAGT/C,gBAAIQ,KAAKuC,GAAAA;QACb,SACO5C,GAAP;AACEsB,mBAASkB,QAAQC,CAAAA,YAAWA,QAAQxC,QAAQD,CAAAA,CAAAA;AAC5C,gBAAMkD,MAAM,MAAMhC,QAAQwB,UAAU1C,GAAGwB,MAAAA;AACvC,cAAI3B,IAAIoD;AACN;AACFpD,cAAIO,OAAO8C,IAAI9C,MAAM,EAAEC,KAAK6C,GAAAA;QAC9B;MACF,CAAA;IACF;EACF;AAzJe/D;AA2JfV,aAAAA;AACAU,cAAAA;AACA,MAAIgE,QAAQ;AACVC,eAAWC,YAAYC,KAAK,YAAY;AACtCnF,qBAAeN,MAAM;QACnBK;QACAE,QAAQL;QACRM,cAAcL;MAChB,CAAA;AACAL,UAAI4F,QAAQ,CAAA;AACZ9E,iBAAAA;AACAU,kBAAAA;IACF,CAAA;EACF;AACF;AA/LgBzB","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'\nimport { P } from '../../types'\n\nexport interface ExpressCtx extends P.BaseContext{\n type: 'express'\n request: Request\n response: Response\n parallel: boolean\n\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 tag\n }\n const context = new Context<ExpressCtx>( 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 tag:methodTag\n }\n \n const context = new Context<ExpressCtx>(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,\n interceptors: globalInterceptors,\n })\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\n })\n }\n}\n"]}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
resolveDep
|
|
3
|
-
} from "../../chunk-
|
|
3
|
+
} from "../../chunk-Q3V3ERU3.mjs";
|
|
4
4
|
import {
|
|
5
5
|
BadRequestException,
|
|
6
6
|
Context,
|
|
7
7
|
isAopDepInject
|
|
8
|
-
} from "../../chunk-
|
|
8
|
+
} from "../../chunk-Z6LSJTOF.mjs";
|
|
9
9
|
import {
|
|
10
10
|
APP_SYMBOL,
|
|
11
11
|
IS_DEV,
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
META_SYMBOL,
|
|
14
14
|
MODULE_SYMBOL,
|
|
15
15
|
__name
|
|
16
|
-
} from "../../chunk-
|
|
16
|
+
} from "../../chunk-5KOB2WHH.mjs";
|
|
17
17
|
|
|
18
18
|
// src/server/express/bind.ts
|
|
19
19
|
function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
@@ -73,9 +73,10 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
73
73
|
meta: meta2,
|
|
74
74
|
response: res,
|
|
75
75
|
moduleMap,
|
|
76
|
-
parallel: true
|
|
76
|
+
parallel: true,
|
|
77
|
+
tag
|
|
77
78
|
};
|
|
78
|
-
const context = new Context(
|
|
79
|
+
const context = new Context(contextData);
|
|
79
80
|
const [name, method] = tag.split("-");
|
|
80
81
|
const { paramsType, handlers, data: { params, guards, interceptors, filter } } = meta2;
|
|
81
82
|
const instance = moduleMap.get(name);
|
|
@@ -134,9 +135,10 @@ function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
|
134
135
|
meta: i,
|
|
135
136
|
response: res,
|
|
136
137
|
moduleMap,
|
|
137
|
-
parallel: false
|
|
138
|
+
parallel: false,
|
|
139
|
+
tag: methodTag
|
|
138
140
|
};
|
|
139
|
-
const context = new Context(
|
|
141
|
+
const context = new Context(contextData);
|
|
140
142
|
try {
|
|
141
143
|
for (const name in header)
|
|
142
144
|
res.set(name, header[name]);
|
|
@@ -1 +1 @@
|
|
|
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,\n interceptors: globalInterceptors,\n })\n app.stack = []// app.stack.slice(0, 1)\n handleMeta()\n createRoute()\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsCO,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,UA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AAW,MAAMb,SAAS5C,QAAO,GAAIiD,IAAAA;AAC3C,gBAAMc,MAAM,MAAM1B,QAAQqB,mBAAmBD,QAAAA;AAE7C,cAAIzC,IAAIoD;AACN;AAEF,cAAI,OAAOL,QAAQ;AACjB/C,gBAAIkD,KAAKH,GAAAA;;AAGT/C,gBAAIQ,KAAKuC,GAAAA;QACb,SACO5C,GAAP;AACEsB,mBAASkB,QAAQC,CAAAA,YAAWA,QAAQxC,QAAQD,CAAAA,CAAAA;AAC5C,gBAAMkD,MAAM,MAAMhC,QAAQwB,UAAU1C,GAAGwB,MAAAA;AACvC,cAAI3B,IAAIoD;AACN;AACFpD,cAAIO,OAAO8C,IAAI9C,MAAM,EAAEC,KAAK6C,GAAAA;QAC9B;MACF,CAAA;IACF;EACF;AAtJe/D;AAwJfV,aAAAA;AACAU,cAAAA;AACA,MAAIgE,QAAQ;AACVC,eAAWC,YAAYC,KAAK,YAAY;AACtCnF,qBAAeN,MAAM;QACnBK;QACAE,QAAQL;QACRM,cAAcL;MAChB,CAAA;AACAL,UAAI4F,QAAQ,CAAA;AACZ9E,iBAAAA;AACAU,kBAAAA;IACF,CAAA;EACF;AACF;AA5LgBzB;","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","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
|
+
{"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'\nimport { P } from '../../types'\n\nexport interface ExpressCtx extends P.BaseContext{\n type: 'express'\n request: Request\n response: Response\n parallel: boolean\n\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 tag\n }\n const context = new Context<ExpressCtx>( 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 tag:methodTag\n }\n \n const context = new Context<ExpressCtx>(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,\n interceptors: globalInterceptors,\n })\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,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AAAA;QACb,SACO5C,GAAP;AACEsB,mBAASkB,QAAQC,CAAAA,YAAWA,QAAQxC,QAAQD,CAAAA,CAAAA;AAC5C,gBAAMkD,MAAM,MAAMhC,QAAQwB,UAAU1C,GAAGwB,MAAAA;AACvC,cAAI3B,IAAIoD;AACN;AACFpD,cAAIO,OAAO8C,IAAI9C,MAAM,EAAEC,KAAK6C,GAAAA;QAC9B;MACF,CAAA;IACF;EACF;AAzJe/D;AA2JfV,aAAAA;AACAU,cAAAA;AACA,MAAIgE,QAAQ;AACVC,eAAWC,YAAYC,KAAK,YAAY;AACtCnF,qBAAeN,MAAM;QACnBK;QACAE,QAAQL;QACRM,cAAcL;MAChB,CAAA;AACAL,UAAI4F,QAAQ,CAAA;AACZ9E,iBAAAA;AACAU,kBAAAA;IACF,CAAA;EACF;AACF;AA/LgBzB;","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","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,16 +1,12 @@
|
|
|
1
|
-
import { FastifyRequest, FastifyReply, FastifyPluginCallback } from 'fastify';
|
|
2
|
-
import {
|
|
3
|
-
import '../../types-8415bd5d.js';
|
|
1
|
+
import { FastifyRequest, FastifyReply, FastifyInstance, FastifyPluginCallback } from 'fastify';
|
|
2
|
+
import { P, F as Factory } from '../../core-d080d872.js';
|
|
4
3
|
import 'phecda-core';
|
|
5
4
|
|
|
6
|
-
interface FastifyCtx {
|
|
5
|
+
interface FastifyCtx extends P.BaseContext {
|
|
7
6
|
type: 'fastify';
|
|
8
7
|
request: FastifyRequest;
|
|
9
8
|
response: FastifyReply;
|
|
10
|
-
meta: Meta;
|
|
11
|
-
moduleMap: Record<string, any>;
|
|
12
9
|
parallel: boolean;
|
|
13
|
-
[key: string]: any;
|
|
14
10
|
}
|
|
15
11
|
interface Options {
|
|
16
12
|
route?: string;
|
|
@@ -18,6 +14,6 @@ interface Options {
|
|
|
18
14
|
globalInterceptors?: string[];
|
|
19
15
|
plugins?: string[];
|
|
20
16
|
}
|
|
21
|
-
declare function bindApp({ moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options?: Options): FastifyPluginCallback;
|
|
17
|
+
declare function bindApp(app: FastifyInstance, { moduleMap, meta }: Awaited<ReturnType<typeof Factory>>, options?: Options): FastifyPluginCallback;
|
|
22
18
|
|
|
23
19
|
export { FastifyCtx, Options, bindApp };
|
|
@@ -1,21 +1,21 @@
|
|
|
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 _chunk63KBHOSBjs = require('../../chunk-63KBHOSB.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _chunk32E425F6js = require('../../chunk-32E425F6.js');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
var
|
|
15
|
+
var _chunkBDIQWWHNjs = require('../../chunk-BDIQWWHN.js');
|
|
16
16
|
|
|
17
17
|
// src/server/fastify/bind.ts
|
|
18
|
-
function bindApp({ moduleMap, meta }, options = {}) {
|
|
18
|
+
function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
19
19
|
const { globalGuards, globalInterceptors, route, plugins } = {
|
|
20
20
|
route: "/__PHECDA_SERVER__",
|
|
21
21
|
globalGuards: [],
|
|
@@ -23,7 +23,11 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
23
23
|
plugins: [],
|
|
24
24
|
...options
|
|
25
25
|
};
|
|
26
|
-
|
|
26
|
+
app.server[_chunkBDIQWWHNjs.APP_SYMBOL] = {
|
|
27
|
+
moduleMap,
|
|
28
|
+
meta
|
|
29
|
+
};
|
|
30
|
+
_chunk32E425F6js.isAopDepInject.call(void 0, meta, {
|
|
27
31
|
plugins,
|
|
28
32
|
guards: globalGuards,
|
|
29
33
|
interceptors: globalInterceptors
|
|
@@ -39,16 +43,16 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
39
43
|
metaMap.set(methodTag, item);
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
|
-
|
|
46
|
+
_chunkBDIQWWHNjs.__name.call(void 0, handleMeta, "handleMeta");
|
|
43
47
|
handleMeta();
|
|
44
48
|
return (fastify, _, done) => {
|
|
45
|
-
fastify[
|
|
49
|
+
fastify[_chunkBDIQWWHNjs.APP_SYMBOL] = {
|
|
46
50
|
moduleMap,
|
|
47
51
|
meta
|
|
48
52
|
};
|
|
49
53
|
fastify.register((fastify2, _opts, done2) => {
|
|
50
54
|
plugins.forEach((p) => {
|
|
51
|
-
const plugin =
|
|
55
|
+
const plugin = _chunk32E425F6js.Context.usePlugin([
|
|
52
56
|
p
|
|
53
57
|
])[0];
|
|
54
58
|
if (plugin) {
|
|
@@ -59,34 +63,35 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
59
63
|
fastify2.post(route, async (req, res) => {
|
|
60
64
|
const { body } = req;
|
|
61
65
|
async function errorHandler(e) {
|
|
62
|
-
const error = await
|
|
66
|
+
const error = await _chunk32E425F6js.Context.filterRecord.default(e);
|
|
63
67
|
return res.status(error.status).send(error);
|
|
64
68
|
}
|
|
65
|
-
|
|
69
|
+
_chunkBDIQWWHNjs.__name.call(void 0, errorHandler, "errorHandler");
|
|
66
70
|
if (!Array.isArray(body))
|
|
67
|
-
return errorHandler(new (0,
|
|
71
|
+
return errorHandler(new (0, _chunk32E425F6js.BadRequestException)("data format should be an array"));
|
|
68
72
|
try {
|
|
69
73
|
return Promise.all(body.map((item) => {
|
|
70
74
|
return new Promise(async (resolve) => {
|
|
71
75
|
const { tag } = item;
|
|
72
76
|
const meta2 = metaMap.get(tag);
|
|
73
77
|
if (!meta2)
|
|
74
|
-
return resolve(await
|
|
78
|
+
return resolve(await _chunk32E425F6js.Context.filterRecord.default(new (0, _chunk32E425F6js.BadRequestException)(`"${tag}" doesn't exist`)));
|
|
75
79
|
const contextData = {
|
|
76
80
|
type: "fastify",
|
|
77
81
|
request: req,
|
|
78
82
|
meta: meta2,
|
|
79
83
|
response: res,
|
|
80
84
|
moduleMap,
|
|
81
|
-
parallel: true
|
|
85
|
+
parallel: true,
|
|
86
|
+
tag
|
|
82
87
|
};
|
|
83
|
-
const context = new (0,
|
|
88
|
+
const context = new (0, _chunk32E425F6js.Context)(contextData);
|
|
84
89
|
const [name, method] = tag.split("-");
|
|
85
90
|
const { paramsType, handlers, data: { params, guards, interceptors, filter } } = meta2;
|
|
86
91
|
const instance = moduleMap.get(name);
|
|
87
92
|
try {
|
|
88
93
|
if (!params)
|
|
89
|
-
throw new (0,
|
|
94
|
+
throw new (0, _chunk32E425F6js.BadRequestException)(`"${tag}" doesn't exist`);
|
|
90
95
|
await context.useGuard([
|
|
91
96
|
...globalGuards,
|
|
92
97
|
...guards
|
|
@@ -132,13 +137,13 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
132
137
|
const methodTag = `${tag}-${method}`;
|
|
133
138
|
const { paramsType, handlers, data: { interceptors, guards, params, plugins: plugins2, filter } } = metaMap.get(methodTag);
|
|
134
139
|
fastify.register((fastify2, _opts, done2) => {
|
|
135
|
-
|
|
140
|
+
_chunk32E425F6js.Context.usePlugin(plugins2).forEach((p) => {
|
|
136
141
|
p[Symbol.for("skip-override")] = true;
|
|
137
142
|
fastify2.register(p);
|
|
138
143
|
});
|
|
139
144
|
fastify2[http.type](http.route, async (req, res) => {
|
|
140
|
-
req[
|
|
141
|
-
req[
|
|
145
|
+
req[_chunkBDIQWWHNjs.MODULE_SYMBOL] = moduleMap;
|
|
146
|
+
req[_chunkBDIQWWHNjs.META_SYMBOL] = meta;
|
|
142
147
|
const instance = moduleMap.get(tag);
|
|
143
148
|
const contextData = {
|
|
144
149
|
type: "fastify",
|
|
@@ -146,9 +151,10 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
146
151
|
meta: i,
|
|
147
152
|
response: res,
|
|
148
153
|
moduleMap,
|
|
149
|
-
parallel: false
|
|
154
|
+
parallel: false,
|
|
155
|
+
tag: methodTag
|
|
150
156
|
};
|
|
151
|
-
const context = new (0,
|
|
157
|
+
const context = new (0, _chunk32E425F6js.Context)(contextData);
|
|
152
158
|
try {
|
|
153
159
|
for (const name in header)
|
|
154
160
|
res.header(name, header[name]);
|
|
@@ -164,7 +170,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
164
170
|
return cache;
|
|
165
171
|
const args = await context.usePipe(params.map(({ type, key, pipe, pipeOpts, index }) => {
|
|
166
172
|
return {
|
|
167
|
-
arg:
|
|
173
|
+
arg: _chunk63KBHOSBjs.resolveDep.call(void 0, req[type], key),
|
|
168
174
|
pipe,
|
|
169
175
|
pipeOpts,
|
|
170
176
|
key,
|
|
@@ -191,9 +197,9 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
191
197
|
});
|
|
192
198
|
}
|
|
193
199
|
done();
|
|
194
|
-
if (
|
|
200
|
+
if (_chunkBDIQWWHNjs.IS_DEV) {
|
|
195
201
|
_optionalChain([globalThis, 'access', _8 => _8.__PS_HMR__, 'optionalAccess', _9 => _9.push, 'call', _10 => _10(async () => {
|
|
196
|
-
|
|
202
|
+
_chunk32E425F6js.isAopDepInject.call(void 0, meta, {
|
|
197
203
|
plugins,
|
|
198
204
|
guards: globalGuards,
|
|
199
205
|
interceptors: globalInterceptors
|
|
@@ -203,7 +209,7 @@ function bindApp({ moduleMap, meta }, options = {}) {
|
|
|
203
209
|
}
|
|
204
210
|
};
|
|
205
211
|
}
|
|
206
|
-
|
|
212
|
+
_chunkBDIQWWHNjs.__name.call(void 0, bindApp, "bindApp");
|
|
207
213
|
|
|
208
214
|
|
|
209
215
|
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","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,UAAmB,CAAC,GAA0B;AAC9H,QAAM,EAAEC,cAAcC,oBAAoBC,OAAOC,QAAO,IAAK;IAAED,OAAO;IAAsBF,cAAc,CAAA;IAAIC,oBAAoB,CAAA;IAAIE,SAAS,CAAA;IAAI,GAAGJ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D,kBAAM2D,QAAQ,MAAMT,QAAQU,eAAe;iBAAIhE;iBAAuBK;aAAa;AACnF,gBAAI0D,UAAUE;AAEZ,qBAAOF;AAET,kBAAMG,OAAO,MAAMZ,QAAQa,QAAQR,OAAOZ,IAAI,CAAC,EAAEhC,MAAMqD,KAAKC,MAAMC,UAAUC,MAAK,MAAO;AACtF,qBAAO;gBAAEC,KAAKY,WAAYpD,IAAYjB,OAAOqD,GAAAA;gBAAMC;gBAAMC;gBAAUF;gBAAKrD;gBAAMwD;gBAAOE,SAAShB,WAAWc;cAAO;YAClH,CAAA,CAAA;AAEAV,qBAASP,UAAUJ;AACnB,kBAAMwB,WAAW,MAAMb,SAASjD,QAAO,GAAIsD,IAAAA;AAC3C,kBAAMa,MAAM,MAAMzB,QAAQqB,mBAAmBD,QAAAA;AAC7C,gBAAIzC,IAAIoD;AACN;AAEF,mBAAON;UACT,SACO3C,GAAP;AACEsB,qBAASlC,QAAQoD,CAAAA,YAAWA,QAAQvC,QAAQD,CAAAA,CAAAA;AAC5C,kBAAMkD,MAAM,MAAMhC,QAAQuB,UAAUzC,GAAGwB,MAAAA;AAEvC,gBAAI3B,IAAIoD;AACN;AACFpD,gBAAIO,OAAO8C,IAAI9C,MAAM,EAAEC,KAAK6C,GAAAA;UAC9B;QACF,CAAA;AACAlE,QAAAA,MAAAA;MACF,CAAA;IACF;AAEAA,SAAAA;AAEA,QAAImE,QAAQ;AACVC,iBAAWC,YAAYC,KAAK,YAAY;AACtCvF,uBAAeN,MAAM;UACnBK;UACAE,QAAQL;UACRM,cAAcL;QAChB,CAAA;AACAQ,mBAAAA;MACF,CAAA;IACF;EACF;AACF;AAhNgBb","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 })\n handleMeta()\n })\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/fastify/bind.ts"],"names":["bindApp","app","moduleMap","meta","options","globalGuards","globalInterceptors","route","plugins","server","APP_SYMBOL","isAopDepInject","guards","interceptors","metaMap","Map","handleMeta","clear","item","tag","method","http","data","type","methodTag","set","fastify","_","done","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,QAAQC,KAAsB,EAAEC,WAAWC,KAAI,GAAyCC,UAAmB,CAAC,GAA0B;AACpJ,Q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","sourcesContent":["import type { FastifyInstance, 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'\nimport { P } from '../../types'\n\nexport interface FastifyCtx extends P.BaseContext {\n type: 'fastify'\n request: FastifyRequest\n response: FastifyReply\n\n parallel: boolean\n\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(app: FastifyInstance, { 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).server[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,\ntag\n }\n const context = new Context<FastifyCtx>( 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 tag:methodTag\n }\n const context = new Context<FastifyCtx>( 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"]}
|