phecda-server 1.0.1 → 1.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/client/index.d.ts +7 -5
- package/dist/client/index.js +2 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +2 -2
- package/dist/client/index.mjs.map +1 -1
- package/dist/index.d.ts +99 -57
- package/dist/index.js +309 -106
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +299 -106
- package/dist/index.mjs.map +1 -1
- package/dist/{types-6e2842b3.d.ts → types-acc60391.d.ts} +27 -4
- package/package.json +5 -3
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
21
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
22
|
var __publicField = (obj, key, value) => {
|
|
22
23
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
@@ -39,6 +40,7 @@ __export(src_exports, {
|
|
|
39
40
|
HttpException: () => HttpException,
|
|
40
41
|
Inject: () => Inject,
|
|
41
42
|
Interceptor: () => Interceptor,
|
|
43
|
+
MQ: () => MQ,
|
|
42
44
|
Param: () => Param,
|
|
43
45
|
Pcompiler: () => Pcompiler,
|
|
44
46
|
Pcontext: () => Pcontext,
|
|
@@ -46,19 +48,30 @@ __export(src_exports, {
|
|
|
46
48
|
Post: () => Post,
|
|
47
49
|
Put: () => Put,
|
|
48
50
|
Query: () => Query,
|
|
51
|
+
RabbitMqContext: () => RabbitMqContext,
|
|
49
52
|
Route: () => Route,
|
|
50
53
|
Server: () => Server,
|
|
54
|
+
ServerContext: () => ServerContext,
|
|
51
55
|
UndefinedException: () => UndefinedException,
|
|
52
56
|
ValidateException: () => ValidateException,
|
|
53
57
|
addGuard: () => addGuard,
|
|
54
58
|
addInterceptor: () => addInterceptor,
|
|
55
59
|
bindApp: () => bindApp,
|
|
60
|
+
bindMQ: () => bindMQ,
|
|
61
|
+
createPub: () => createPub,
|
|
56
62
|
defaultPipe: () => defaultPipe,
|
|
63
|
+
getInstance: () => getInstance,
|
|
57
64
|
parseMeta: () => parseMeta,
|
|
58
|
-
|
|
65
|
+
useMqFilter: () => useMqFilter,
|
|
66
|
+
useMqPipe: () => useMqPipe,
|
|
67
|
+
useServerFilter: () => useServerFilter,
|
|
68
|
+
useServerPipe: () => useServerPipe
|
|
59
69
|
});
|
|
60
70
|
module.exports = __toCommonJS(src_exports);
|
|
61
71
|
|
|
72
|
+
// src/pipe.ts
|
|
73
|
+
var import_phecda_core = require("phecda-core");
|
|
74
|
+
|
|
62
75
|
// src/exception/base.ts
|
|
63
76
|
var HttpException = class extends Error {
|
|
64
77
|
message;
|
|
@@ -81,27 +94,36 @@ var HttpException = class extends Error {
|
|
|
81
94
|
};
|
|
82
95
|
__name(HttpException, "HttpException");
|
|
83
96
|
|
|
84
|
-
// src/exception/
|
|
85
|
-
var
|
|
97
|
+
// src/exception/validate.ts
|
|
98
|
+
var ValidateException = class extends HttpException {
|
|
86
99
|
constructor(message) {
|
|
87
|
-
super(message,
|
|
100
|
+
super(message, 400, "Validate exception");
|
|
88
101
|
}
|
|
89
102
|
};
|
|
90
|
-
__name(
|
|
103
|
+
__name(ValidateException, "ValidateException");
|
|
91
104
|
|
|
92
|
-
// src/
|
|
93
|
-
var
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
105
|
+
// src/pipe.ts
|
|
106
|
+
var defaultPipe = {
|
|
107
|
+
async transform(args, reflect) {
|
|
108
|
+
for (const i in args) {
|
|
109
|
+
const { validate, arg } = args[i];
|
|
110
|
+
if (validate === false)
|
|
111
|
+
continue;
|
|
112
|
+
if (validate && !(arg?.constructor === reflect[i])) {
|
|
113
|
+
throw new ValidateException(`${arg} is not ${reflect[i].name}`);
|
|
114
|
+
} else {
|
|
115
|
+
if ((0, import_phecda_core.isPhecda)(reflect[i])) {
|
|
116
|
+
const ret = await (0, import_phecda_core.plainToClass)(reflect[i], arg, {
|
|
117
|
+
transform: false
|
|
118
|
+
});
|
|
119
|
+
if (ret.err.length > 0)
|
|
120
|
+
throw new ValidateException(ret.err[0]);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
100
123
|
}
|
|
101
|
-
return
|
|
124
|
+
return args.map((item) => item.arg);
|
|
102
125
|
}
|
|
103
126
|
};
|
|
104
|
-
__name(Phistroy, "Phistroy");
|
|
105
127
|
|
|
106
128
|
// src/exception/undefine.ts
|
|
107
129
|
var UndefinedException = class extends HttpException {
|
|
@@ -111,14 +133,6 @@ var UndefinedException = class extends HttpException {
|
|
|
111
133
|
};
|
|
112
134
|
__name(UndefinedException, "UndefinedException");
|
|
113
135
|
|
|
114
|
-
// src/exception/validate.ts
|
|
115
|
-
var ValidateException = class extends HttpException {
|
|
116
|
-
constructor(message) {
|
|
117
|
-
super(message, 400, "Validate exception");
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
__name(ValidateException, "ValidateException");
|
|
121
|
-
|
|
122
136
|
// src/exception/forbidden.ts
|
|
123
137
|
var ForbiddenException = class extends HttpException {
|
|
124
138
|
constructor(message) {
|
|
@@ -135,41 +149,50 @@ var BadRequestException = class extends HttpException {
|
|
|
135
149
|
};
|
|
136
150
|
__name(BadRequestException, "BadRequestException");
|
|
137
151
|
|
|
138
|
-
// src/
|
|
139
|
-
var
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
152
|
+
// src/filter.ts
|
|
153
|
+
var serverFilter = /* @__PURE__ */ __name((e) => {
|
|
154
|
+
if (!(e instanceof HttpException))
|
|
155
|
+
e = new UndefinedException(e.message || e);
|
|
156
|
+
return e.data;
|
|
157
|
+
}, "serverFilter");
|
|
158
|
+
var rabbitMqFilter = /* @__PURE__ */ __name((e, data) => {
|
|
159
|
+
const { channel, message } = data;
|
|
160
|
+
channel.reject(message, true);
|
|
161
|
+
}, "rabbitMqFilter");
|
|
162
|
+
|
|
163
|
+
// src/exception/wrong-meta.ts
|
|
164
|
+
var WrongMetaException = class extends HttpException {
|
|
165
|
+
constructor(message) {
|
|
166
|
+
super(message, 500, "Meta is not correct");
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
__name(WrongMetaException, "WrongMetaException");
|
|
170
|
+
|
|
171
|
+
// src/history.ts
|
|
172
|
+
var Phistroy = class {
|
|
173
|
+
guard = [];
|
|
174
|
+
interceptor = [];
|
|
175
|
+
record(name, type) {
|
|
176
|
+
if (!this[type].includes(name)) {
|
|
177
|
+
this[type].push(name);
|
|
178
|
+
return true;
|
|
157
179
|
}
|
|
158
|
-
return
|
|
180
|
+
return false;
|
|
159
181
|
}
|
|
160
182
|
};
|
|
183
|
+
__name(Phistroy, "Phistroy");
|
|
161
184
|
|
|
162
|
-
// src/context.ts
|
|
185
|
+
// src/context/base.ts
|
|
163
186
|
var _Pcontext = class {
|
|
164
187
|
key;
|
|
165
|
-
|
|
188
|
+
data;
|
|
166
189
|
method;
|
|
167
190
|
params;
|
|
168
191
|
post;
|
|
169
192
|
history;
|
|
170
|
-
constructor(key,
|
|
193
|
+
constructor(key, data) {
|
|
171
194
|
this.key = key;
|
|
172
|
-
this.
|
|
195
|
+
this.data = data;
|
|
173
196
|
this.history = new Phistroy();
|
|
174
197
|
}
|
|
175
198
|
static registerGuard(key, handler) {
|
|
@@ -178,19 +201,12 @@ var _Pcontext = class {
|
|
|
178
201
|
static registerInterceptor(key, handler) {
|
|
179
202
|
_Pcontext.interceptorsRecord[key] = handler;
|
|
180
203
|
}
|
|
181
|
-
static useMiddleware(middlewares) {
|
|
182
|
-
return middlewares.map((m) => {
|
|
183
|
-
if (!(m in _Pcontext.middlewareRecord))
|
|
184
|
-
throw new WrongMetaException(`can't find middleware named ${m}`);
|
|
185
|
-
return _Pcontext.middlewareRecord[m];
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
204
|
async useGuard(guards, isMerge = false) {
|
|
189
205
|
for (const guard of guards) {
|
|
190
206
|
if (this.history.record(guard, "guard")) {
|
|
191
207
|
if (!(guard in _Pcontext.guardsRecord))
|
|
192
208
|
throw new WrongMetaException(`can't find guard named ${guard}`);
|
|
193
|
-
if (!await _Pcontext.guardsRecord[guard](this.
|
|
209
|
+
if (!await _Pcontext.guardsRecord[guard](this.data, isMerge))
|
|
194
210
|
throw new ForbiddenException(`Guard exception--${guard}`);
|
|
195
211
|
}
|
|
196
212
|
}
|
|
@@ -201,7 +217,7 @@ var _Pcontext = class {
|
|
|
201
217
|
if (this.history.record(interceptor, "interceptor")) {
|
|
202
218
|
if (!(interceptor in _Pcontext.interceptorsRecord))
|
|
203
219
|
throw new WrongMetaException(`can't find guard named ${interceptor}`);
|
|
204
|
-
const post = await _Pcontext.interceptorsRecord[interceptor](this.
|
|
220
|
+
const post = await _Pcontext.interceptorsRecord[interceptor](this.data, isMerge);
|
|
205
221
|
if (post)
|
|
206
222
|
ret.push(post);
|
|
207
223
|
}
|
|
@@ -209,20 +225,19 @@ var _Pcontext = class {
|
|
|
209
225
|
this.post = ret;
|
|
210
226
|
}
|
|
211
227
|
async usePost(data) {
|
|
228
|
+
if (!this.post)
|
|
229
|
+
return data;
|
|
212
230
|
for (const cb of this.post)
|
|
213
231
|
data = await cb(data) | data;
|
|
214
232
|
return data;
|
|
215
233
|
}
|
|
216
|
-
async usePipe(args, reflect) {
|
|
217
|
-
return _Pcontext.pipe.transform(args, reflect);
|
|
218
|
-
}
|
|
219
234
|
};
|
|
220
235
|
var Pcontext = _Pcontext;
|
|
221
236
|
__name(Pcontext, "Pcontext");
|
|
222
|
-
__publicField(Pcontext, "pipe", defaultPipe);
|
|
223
237
|
__publicField(Pcontext, "metaRecord", {});
|
|
238
|
+
__publicField(Pcontext, "metaDataRecord", {});
|
|
239
|
+
__publicField(Pcontext, "instanceRecord", {});
|
|
224
240
|
__publicField(Pcontext, "guardsRecord", {});
|
|
225
|
-
__publicField(Pcontext, "middlewareRecord", {});
|
|
226
241
|
__publicField(Pcontext, "interceptorsRecord", {});
|
|
227
242
|
function addGuard(key, handler) {
|
|
228
243
|
Pcontext.registerGuard(key, handler);
|
|
@@ -232,10 +247,10 @@ function addInterceptor(key, handler) {
|
|
|
232
247
|
Pcontext.registerInterceptor(key, handler);
|
|
233
248
|
}
|
|
234
249
|
__name(addInterceptor, "addInterceptor");
|
|
235
|
-
function
|
|
236
|
-
Pcontext.
|
|
250
|
+
function getInstance(tag) {
|
|
251
|
+
return Pcontext.instanceRecord[tag];
|
|
237
252
|
}
|
|
238
|
-
__name(
|
|
253
|
+
__name(getInstance, "getInstance");
|
|
239
254
|
function parseMeta(meta) {
|
|
240
255
|
const { data: { params, guards, interceptors, middlewares }, reflect } = meta;
|
|
241
256
|
return {
|
|
@@ -255,6 +270,66 @@ function parseMeta(meta) {
|
|
|
255
270
|
}
|
|
256
271
|
__name(parseMeta, "parseMeta");
|
|
257
272
|
|
|
273
|
+
// src/context/server.ts
|
|
274
|
+
var _ServerContext = class extends Pcontext {
|
|
275
|
+
static useMiddleware(middlewares) {
|
|
276
|
+
return middlewares.map((m) => {
|
|
277
|
+
if (!(m in _ServerContext.middlewareRecord))
|
|
278
|
+
throw new WrongMetaException(`can't find middleware named ${m}`);
|
|
279
|
+
return _ServerContext.middlewareRecord[m];
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
async usePipe(args, reflect) {
|
|
283
|
+
return _ServerContext.pipe.transform?.(args, reflect);
|
|
284
|
+
}
|
|
285
|
+
useFilter(arg) {
|
|
286
|
+
return _ServerContext.filter(arg, this.data);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
var ServerContext = _ServerContext;
|
|
290
|
+
__name(ServerContext, "ServerContext");
|
|
291
|
+
__publicField(ServerContext, "pipe", defaultPipe);
|
|
292
|
+
__publicField(ServerContext, "filter", serverFilter);
|
|
293
|
+
__publicField(ServerContext, "middlewareRecord", {});
|
|
294
|
+
function useServerPipe(pipe) {
|
|
295
|
+
ServerContext.pipe = pipe;
|
|
296
|
+
}
|
|
297
|
+
__name(useServerPipe, "useServerPipe");
|
|
298
|
+
function useServerFilter(filter) {
|
|
299
|
+
ServerContext.filter = filter;
|
|
300
|
+
}
|
|
301
|
+
__name(useServerFilter, "useServerFilter");
|
|
302
|
+
|
|
303
|
+
// src/context/micro.ts
|
|
304
|
+
var _RabbitMqContext = class extends Pcontext {
|
|
305
|
+
static useMiddleware(middlewares) {
|
|
306
|
+
return middlewares.map((m) => {
|
|
307
|
+
if (!(m in _RabbitMqContext.middlewareRecord))
|
|
308
|
+
throw new WrongMetaException(`can't find middleware named ${m}`);
|
|
309
|
+
return _RabbitMqContext.middlewareRecord[m];
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
async usePipe(args, reflect) {
|
|
313
|
+
return _RabbitMqContext.pipe.transform?.(args, reflect);
|
|
314
|
+
}
|
|
315
|
+
useFilter(arg) {
|
|
316
|
+
return _RabbitMqContext.filter(arg, this.data);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
var RabbitMqContext = _RabbitMqContext;
|
|
320
|
+
__name(RabbitMqContext, "RabbitMqContext");
|
|
321
|
+
__publicField(RabbitMqContext, "pipe", defaultPipe);
|
|
322
|
+
__publicField(RabbitMqContext, "filter", rabbitMqFilter);
|
|
323
|
+
__publicField(RabbitMqContext, "middlewareRecord", {});
|
|
324
|
+
function useMqPipe(pipe) {
|
|
325
|
+
RabbitMqContext.pipe = pipe;
|
|
326
|
+
}
|
|
327
|
+
__name(useMqPipe, "useMqPipe");
|
|
328
|
+
function useMqFilter(filter) {
|
|
329
|
+
RabbitMqContext.filter = filter;
|
|
330
|
+
}
|
|
331
|
+
__name(useMqFilter, "useMqFilter");
|
|
332
|
+
|
|
258
333
|
// src/compiler.ts
|
|
259
334
|
var Pcompiler = class {
|
|
260
335
|
content = "";
|
|
@@ -277,10 +352,22 @@ var Pcompiler = class {
|
|
|
277
352
|
this.classMap[className] = {};
|
|
278
353
|
this.classMap[className][methodName] = `
|
|
279
354
|
${methodName}(${genParams(params)}){
|
|
280
|
-
const ret={
|
|
281
|
-
${params.reduce((p, c, i) => `${p}ret.${c.type}.${c.key}=arg${i}
|
|
355
|
+
const ret={tag:"${className}-${methodName}",body:{},query:{},params:{},realParam:'',method:"${requestType}",url:"${url}"}
|
|
356
|
+
${params.filter((item) => item.key).reduce((p, c, i) => `${p}ret.${c.type}.${c.key}=arg${i}
|
|
282
357
|
${c.type === "params" ? `ret.realParam+='/'+arg${i}
|
|
283
358
|
` : ""}`, "")}
|
|
359
|
+
return ret
|
|
360
|
+
}
|
|
361
|
+
`;
|
|
362
|
+
}
|
|
363
|
+
addMqMethod(className, methodName, exchange = "", routeKey = "", queue = "", params = []) {
|
|
364
|
+
if (!this.classMap[className])
|
|
365
|
+
this.classMap[className] = {};
|
|
366
|
+
this.classMap[className][methodName] = `
|
|
367
|
+
${methodName}(${genParams(params)}){
|
|
368
|
+
const ret={tag:"${className}-${methodName}",exchange:"${exchange}",routeKey:"${routeKey}",queue:"${queue}",args:{}}
|
|
369
|
+
${params.reduce((p, c, i) => `${p}ret.args.${c.key}=arg${i}
|
|
370
|
+
`, "")}
|
|
284
371
|
return ret
|
|
285
372
|
}
|
|
286
373
|
`;
|
|
@@ -318,7 +405,7 @@ __name(NotFoundException, "NotFoundException");
|
|
|
318
405
|
var SERIES_SYMBOL = "__symbol_series__";
|
|
319
406
|
var REQ_SYMBOL = "__symbol_req__";
|
|
320
407
|
|
|
321
|
-
// src/express.ts
|
|
408
|
+
// src/server/express.ts
|
|
322
409
|
function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
323
410
|
const { globalGuards, globalInterceptors, route, middlewares: proMiddle } = {
|
|
324
411
|
route: "/__PHECDA_SERVER__",
|
|
@@ -332,7 +419,8 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
332
419
|
const { name, method, route: route2, header } = i.data;
|
|
333
420
|
const instance = moduleMap.get(name);
|
|
334
421
|
const tag = `${name}-${method}`;
|
|
335
|
-
|
|
422
|
+
Pcontext.metaRecord[tag] = i;
|
|
423
|
+
let { guards, reflect, interceptors, params, middlewares } = Pcontext.metaDataRecord[tag] ? Pcontext.metaDataRecord[tag] : Pcontext.metaDataRecord[tag] = parseMeta(i);
|
|
336
424
|
guards = [
|
|
337
425
|
...globalGuards,
|
|
338
426
|
...guards
|
|
@@ -343,12 +431,16 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
343
431
|
];
|
|
344
432
|
const handler = instance[method].bind(instance);
|
|
345
433
|
methodMap[tag] = handler;
|
|
434
|
+
Pcontext.instanceRecord[name] = instance;
|
|
346
435
|
if (route2) {
|
|
347
|
-
app[route2.type](route2.route, ...
|
|
436
|
+
app[route2.type](route2.route, ...ServerContext.useMiddleware(middlewares), async (req, res) => {
|
|
437
|
+
const contextData = {
|
|
438
|
+
request: req,
|
|
439
|
+
tag,
|
|
440
|
+
response: res
|
|
441
|
+
};
|
|
442
|
+
const context = new ServerContext(tag, contextData);
|
|
348
443
|
try {
|
|
349
|
-
const context = new Pcontext(`${name}-${method}`, req);
|
|
350
|
-
instance.ctx = context;
|
|
351
|
-
instance.request = req;
|
|
352
444
|
for (const name2 in header)
|
|
353
445
|
res.set(name2, header[name2]);
|
|
354
446
|
await context.useGuard(guards);
|
|
@@ -359,15 +451,16 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
359
451
|
validate
|
|
360
452
|
};
|
|
361
453
|
}), reflect);
|
|
454
|
+
instance.meta = contextData;
|
|
362
455
|
const ret = await context.usePost(await handler(...args));
|
|
363
456
|
if (isObject(ret))
|
|
364
457
|
res.json(ret);
|
|
365
458
|
else
|
|
366
459
|
res.send(String(ret));
|
|
367
460
|
} catch (e) {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
res.status(
|
|
461
|
+
i.handlers.forEach((handler2) => handler2.error?.(e));
|
|
462
|
+
const err = await context.useFilter(e);
|
|
463
|
+
res.status(err.status).json(err);
|
|
371
464
|
}
|
|
372
465
|
});
|
|
373
466
|
}
|
|
@@ -375,22 +468,24 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
375
468
|
app.post(route, (req, _res, next) => {
|
|
376
469
|
req[REQ_SYMBOL] = true;
|
|
377
470
|
next();
|
|
378
|
-
}, ...
|
|
379
|
-
const
|
|
471
|
+
}, ...ServerContext.useMiddleware(proMiddle), async (req, res) => {
|
|
472
|
+
const contextData = {
|
|
473
|
+
request: req,
|
|
474
|
+
response: res
|
|
475
|
+
};
|
|
476
|
+
const context = new ServerContext(route, contextData);
|
|
380
477
|
const ret = [];
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
instance.ctx = context;
|
|
389
|
-
instance.request = req;
|
|
478
|
+
const { body } = req;
|
|
479
|
+
for (const i in body) {
|
|
480
|
+
const { tag } = body[i];
|
|
481
|
+
const [name] = tag.split("-");
|
|
482
|
+
const { guards, reflect, interceptors, params } = Pcontext.metaDataRecord[tag];
|
|
483
|
+
const instance = moduleMap.get(name);
|
|
484
|
+
try {
|
|
390
485
|
if (!params)
|
|
391
486
|
throw new NotFoundException(`"${tag}" doesn't exist`);
|
|
392
|
-
await context.useGuard(guards);
|
|
393
|
-
await context.useInterceptor(interceptors);
|
|
487
|
+
await context.useGuard(guards, true);
|
|
488
|
+
await context.useInterceptor(interceptors, true);
|
|
394
489
|
const args = await context.usePipe(params.map(({ type, key, validate }) => {
|
|
395
490
|
const arg = resolveDep(body[i][type], key);
|
|
396
491
|
if (typeof arg === "string" && arg.startsWith(SERIES_SYMBOL)) {
|
|
@@ -405,13 +500,13 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
405
500
|
validate
|
|
406
501
|
};
|
|
407
502
|
}), reflect);
|
|
503
|
+
instance.meta = contextData;
|
|
408
504
|
ret.push(await context.usePost(await methodMap[tag](...args)));
|
|
505
|
+
} catch (e) {
|
|
506
|
+
const m = Pcontext.metaRecord[tag];
|
|
507
|
+
m.handlers.forEach((handler) => handler.error?.(e));
|
|
508
|
+
ret.push(await context.useFilter(e));
|
|
409
509
|
}
|
|
410
|
-
} catch (e) {
|
|
411
|
-
if (!(e instanceof HttpException))
|
|
412
|
-
e = new UndefinedException(e.message || e);
|
|
413
|
-
console.error(e.stack);
|
|
414
|
-
ret.push(e.data);
|
|
415
510
|
}
|
|
416
511
|
res.json(ret);
|
|
417
512
|
});
|
|
@@ -425,26 +520,29 @@ var import_phecda_core2 = require("phecda-core");
|
|
|
425
520
|
// src/meta.ts
|
|
426
521
|
var Pmeta = class {
|
|
427
522
|
data;
|
|
523
|
+
handlers;
|
|
428
524
|
reflect;
|
|
429
|
-
constructor(data, reflect) {
|
|
525
|
+
constructor(data, handlers, reflect) {
|
|
430
526
|
this.data = data;
|
|
527
|
+
this.handlers = handlers;
|
|
431
528
|
this.reflect = reflect;
|
|
432
529
|
}
|
|
433
530
|
};
|
|
434
531
|
__name(Pmeta, "Pmeta");
|
|
435
532
|
|
|
436
533
|
// src/core.ts
|
|
437
|
-
function Factory(Modules) {
|
|
534
|
+
async function Factory(Modules) {
|
|
438
535
|
const moduleMap = /* @__PURE__ */ new Map();
|
|
439
536
|
const meta = [];
|
|
440
|
-
|
|
537
|
+
for (const Module of Modules)
|
|
538
|
+
await buildNestModule(Module, moduleMap, meta);
|
|
441
539
|
return {
|
|
442
540
|
moduleMap,
|
|
443
541
|
meta
|
|
444
542
|
};
|
|
445
543
|
}
|
|
446
544
|
__name(Factory, "Factory");
|
|
447
|
-
function buildNestModule(Module, map, meta) {
|
|
545
|
+
async function buildNestModule(Module, map, meta) {
|
|
448
546
|
const paramtypes = getParamtypes(Module);
|
|
449
547
|
let instance;
|
|
450
548
|
const name = Module.name;
|
|
@@ -456,21 +554,24 @@ function buildNestModule(Module, map, meta) {
|
|
|
456
554
|
}
|
|
457
555
|
map.set(name, void 0);
|
|
458
556
|
if (paramtypes) {
|
|
459
|
-
|
|
557
|
+
for (const i in paramtypes)
|
|
558
|
+
paramtypes[i] = await buildNestModule(paramtypes[i], map, meta);
|
|
559
|
+
instance = new Module(...paramtypes);
|
|
460
560
|
} else {
|
|
461
561
|
instance = new Module();
|
|
462
562
|
}
|
|
463
563
|
meta.push(...getMetaFromInstance(instance, name));
|
|
564
|
+
await (0, import_phecda_core2.registerAsync)(instance);
|
|
464
565
|
map.set(name, instance);
|
|
465
566
|
return instance;
|
|
466
567
|
}
|
|
467
568
|
__name(buildNestModule, "buildNestModule");
|
|
468
569
|
function getMetaFromInstance(instance, name) {
|
|
469
|
-
const vars = (0, import_phecda_core2.
|
|
570
|
+
const vars = (0, import_phecda_core2.getExposeKey)(instance).filter((item) => item !== "__CLASS");
|
|
470
571
|
const baseState = (0, import_phecda_core2.getState)(instance, "__CLASS") || {};
|
|
471
572
|
initState(baseState);
|
|
472
573
|
return vars.map((i) => {
|
|
473
|
-
const state = (0, import_phecda_core2.getState)(instance, i);
|
|
574
|
+
const state = (0, import_phecda_core2.getState)(instance, i) || {};
|
|
474
575
|
if (baseState.route && state.route)
|
|
475
576
|
state.route.route = baseState.route.route + state.route.route;
|
|
476
577
|
state.name = name;
|
|
@@ -502,7 +603,7 @@ function getMetaFromInstance(instance, name) {
|
|
|
502
603
|
...state.interceptors
|
|
503
604
|
])
|
|
504
605
|
];
|
|
505
|
-
return new Pmeta(state, getParamtypes(instance, i) || []);
|
|
606
|
+
return new Pmeta(state, (0, import_phecda_core2.getHandler)(instance, i), getParamtypes(instance, i) || []);
|
|
506
607
|
});
|
|
507
608
|
}
|
|
508
609
|
__name(getMetaFromInstance, "getMetaFromInstance");
|
|
@@ -523,7 +624,7 @@ function initState(state) {
|
|
|
523
624
|
__name(initState, "initState");
|
|
524
625
|
|
|
525
626
|
// src/decorators/index.ts
|
|
526
|
-
var
|
|
627
|
+
var import_phecda_core6 = require("phecda-core");
|
|
527
628
|
|
|
528
629
|
// src/decorators/param.ts
|
|
529
630
|
var import_phecda_core3 = require("phecda-core");
|
|
@@ -641,14 +742,30 @@ function Interceptor(interceptorKey) {
|
|
|
641
742
|
}
|
|
642
743
|
__name(Interceptor, "Interceptor");
|
|
643
744
|
|
|
745
|
+
// src/decorators/micro.ts
|
|
746
|
+
var import_phecda_core5 = require("phecda-core");
|
|
747
|
+
function MQ(queue, routeKey, options) {
|
|
748
|
+
return (target, k) => {
|
|
749
|
+
(0, import_phecda_core5.setModalVar)(target, k);
|
|
750
|
+
(0, import_phecda_core5.mergeState)(target, k, {
|
|
751
|
+
mq: {
|
|
752
|
+
queue,
|
|
753
|
+
routeKey,
|
|
754
|
+
options
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
__name(MQ, "MQ");
|
|
760
|
+
|
|
644
761
|
// src/decorators/index.ts
|
|
645
|
-
function Inject() {
|
|
762
|
+
function Inject(_target) {
|
|
646
763
|
}
|
|
647
764
|
__name(Inject, "Inject");
|
|
648
765
|
function Header(name, value) {
|
|
649
766
|
return (target, k) => {
|
|
650
|
-
(0,
|
|
651
|
-
(0,
|
|
767
|
+
(0, import_phecda_core6.setModalVar)(target, k);
|
|
768
|
+
(0, import_phecda_core6.mergeState)(target, k, {
|
|
652
769
|
header: {
|
|
653
770
|
name,
|
|
654
771
|
value
|
|
@@ -689,6 +806,83 @@ function Server(localPath) {
|
|
|
689
806
|
};
|
|
690
807
|
}
|
|
691
808
|
__name(Server, "Server");
|
|
809
|
+
|
|
810
|
+
// src/index.ts
|
|
811
|
+
__reExport(src_exports, require("phecda-core"), module.exports);
|
|
812
|
+
|
|
813
|
+
// src/config.ts
|
|
814
|
+
var Pconfig = {
|
|
815
|
+
rabbitmq: {
|
|
816
|
+
guard: false,
|
|
817
|
+
interceptor: false
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
// src/micro-services/rabbitmq.ts
|
|
822
|
+
async function bindMQ(ch, { meta, moduleMap }) {
|
|
823
|
+
for (const item of meta) {
|
|
824
|
+
const { route, name, method, mq: { routeKey, queue: queueName, options } = {} } = item.data;
|
|
825
|
+
const tag = `${name}-${method}`;
|
|
826
|
+
Pcontext.metaRecord[tag] = item;
|
|
827
|
+
const { guards, reflect, interceptors, params } = Pcontext.metaDataRecord[tag] ? Pcontext.metaDataRecord[tag] : Pcontext.metaDataRecord[tag] = parseMeta(item);
|
|
828
|
+
const instance = moduleMap.get(name);
|
|
829
|
+
const handler = instance[method].bind(instance);
|
|
830
|
+
Pcontext.instanceRecord[name] = instance;
|
|
831
|
+
if (route) {
|
|
832
|
+
const { queue } = await ch.assertQueue(route.route);
|
|
833
|
+
if (queueName && routeKey)
|
|
834
|
+
await ch.bindQueue(queue, queueName, routeKey);
|
|
835
|
+
ch.consume(route.route, async (msg) => {
|
|
836
|
+
if (msg !== null) {
|
|
837
|
+
const content = msg.content.toString();
|
|
838
|
+
const data = params.length > 0 ? JSON.parse(content) : content;
|
|
839
|
+
const contextMeta = {
|
|
840
|
+
message: msg,
|
|
841
|
+
content,
|
|
842
|
+
channel: ch
|
|
843
|
+
};
|
|
844
|
+
const context = new RabbitMqContext(tag, contextMeta);
|
|
845
|
+
try {
|
|
846
|
+
if (Pconfig.rabbitmq.guard)
|
|
847
|
+
await context.useGuard(guards);
|
|
848
|
+
if (Pconfig.rabbitmq.interceptor)
|
|
849
|
+
await context.useInterceptor(interceptors);
|
|
850
|
+
const args = await context.usePipe(params.map(({ key, validate }) => {
|
|
851
|
+
return {
|
|
852
|
+
arg: resolveDep(data, key),
|
|
853
|
+
validate
|
|
854
|
+
};
|
|
855
|
+
}), reflect);
|
|
856
|
+
instance.meta = contextMeta;
|
|
857
|
+
await context.usePost(await handler(...args));
|
|
858
|
+
ch.ack(msg);
|
|
859
|
+
} catch (e) {
|
|
860
|
+
item.handlers.forEach((handler2) => handler2.error?.(e));
|
|
861
|
+
context.useFilter(e);
|
|
862
|
+
}
|
|
863
|
+
} else {
|
|
864
|
+
}
|
|
865
|
+
}, options);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
__name(bindMQ, "bindMQ");
|
|
870
|
+
async function createPub(ch, method, type) {
|
|
871
|
+
const { exchange, routeKey, queue } = method();
|
|
872
|
+
if (exchange)
|
|
873
|
+
await ch.assertExchange(exchange, type);
|
|
874
|
+
else
|
|
875
|
+
await ch.assertQueue(queue);
|
|
876
|
+
return async (arg) => {
|
|
877
|
+
const { args } = method(arg);
|
|
878
|
+
const msg = Buffer.from(JSON.stringify(args));
|
|
879
|
+
if (exchange)
|
|
880
|
+
await ch.publish(exchange, routeKey, msg);
|
|
881
|
+
else
|
|
882
|
+
await ch.sendToQueue(queue, msg);
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
__name(createPub, "createPub");
|
|
692
886
|
// Annotate the CommonJS export names for ESM import in node:
|
|
693
887
|
0 && (module.exports = {
|
|
694
888
|
BadRequestException,
|
|
@@ -704,6 +898,7 @@ __name(Server, "Server");
|
|
|
704
898
|
HttpException,
|
|
705
899
|
Inject,
|
|
706
900
|
Interceptor,
|
|
901
|
+
MQ,
|
|
707
902
|
Param,
|
|
708
903
|
Pcompiler,
|
|
709
904
|
Pcontext,
|
|
@@ -711,15 +906,23 @@ __name(Server, "Server");
|
|
|
711
906
|
Post,
|
|
712
907
|
Put,
|
|
713
908
|
Query,
|
|
909
|
+
RabbitMqContext,
|
|
714
910
|
Route,
|
|
715
911
|
Server,
|
|
912
|
+
ServerContext,
|
|
716
913
|
UndefinedException,
|
|
717
914
|
ValidateException,
|
|
718
915
|
addGuard,
|
|
719
916
|
addInterceptor,
|
|
720
917
|
bindApp,
|
|
918
|
+
bindMQ,
|
|
919
|
+
createPub,
|
|
721
920
|
defaultPipe,
|
|
921
|
+
getInstance,
|
|
722
922
|
parseMeta,
|
|
723
|
-
|
|
923
|
+
useMqFilter,
|
|
924
|
+
useMqPipe,
|
|
925
|
+
useServerFilter,
|
|
926
|
+
useServerPipe
|
|
724
927
|
});
|
|
725
928
|
//# sourceMappingURL=index.js.map
|