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.mjs
CHANGED
|
@@ -6,6 +6,9 @@ var __publicField = (obj, key, value) => {
|
|
|
6
6
|
return value;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
// src/pipe.ts
|
|
10
|
+
import { isPhecda, plainToClass } from "phecda-core";
|
|
11
|
+
|
|
9
12
|
// src/exception/base.ts
|
|
10
13
|
var HttpException = class extends Error {
|
|
11
14
|
message;
|
|
@@ -28,27 +31,36 @@ var HttpException = class extends Error {
|
|
|
28
31
|
};
|
|
29
32
|
__name(HttpException, "HttpException");
|
|
30
33
|
|
|
31
|
-
// src/exception/
|
|
32
|
-
var
|
|
34
|
+
// src/exception/validate.ts
|
|
35
|
+
var ValidateException = class extends HttpException {
|
|
33
36
|
constructor(message) {
|
|
34
|
-
super(message,
|
|
37
|
+
super(message, 400, "Validate exception");
|
|
35
38
|
}
|
|
36
39
|
};
|
|
37
|
-
__name(
|
|
40
|
+
__name(ValidateException, "ValidateException");
|
|
38
41
|
|
|
39
|
-
// src/
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
// src/pipe.ts
|
|
43
|
+
var defaultPipe = {
|
|
44
|
+
async transform(args, reflect) {
|
|
45
|
+
for (const i in args) {
|
|
46
|
+
const { validate, arg } = args[i];
|
|
47
|
+
if (validate === false)
|
|
48
|
+
continue;
|
|
49
|
+
if (validate && !(arg?.constructor === reflect[i])) {
|
|
50
|
+
throw new ValidateException(`${arg} is not ${reflect[i].name}`);
|
|
51
|
+
} else {
|
|
52
|
+
if (isPhecda(reflect[i])) {
|
|
53
|
+
const ret = await plainToClass(reflect[i], arg, {
|
|
54
|
+
transform: false
|
|
55
|
+
});
|
|
56
|
+
if (ret.err.length > 0)
|
|
57
|
+
throw new ValidateException(ret.err[0]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
47
60
|
}
|
|
48
|
-
return
|
|
61
|
+
return args.map((item) => item.arg);
|
|
49
62
|
}
|
|
50
63
|
};
|
|
51
|
-
__name(Phistroy, "Phistroy");
|
|
52
64
|
|
|
53
65
|
// src/exception/undefine.ts
|
|
54
66
|
var UndefinedException = class extends HttpException {
|
|
@@ -58,14 +70,6 @@ var UndefinedException = class extends HttpException {
|
|
|
58
70
|
};
|
|
59
71
|
__name(UndefinedException, "UndefinedException");
|
|
60
72
|
|
|
61
|
-
// src/exception/validate.ts
|
|
62
|
-
var ValidateException = class extends HttpException {
|
|
63
|
-
constructor(message) {
|
|
64
|
-
super(message, 400, "Validate exception");
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
__name(ValidateException, "ValidateException");
|
|
68
|
-
|
|
69
73
|
// src/exception/forbidden.ts
|
|
70
74
|
var ForbiddenException = class extends HttpException {
|
|
71
75
|
constructor(message) {
|
|
@@ -82,41 +86,50 @@ var BadRequestException = class extends HttpException {
|
|
|
82
86
|
};
|
|
83
87
|
__name(BadRequestException, "BadRequestException");
|
|
84
88
|
|
|
85
|
-
// src/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
89
|
+
// src/filter.ts
|
|
90
|
+
var serverFilter = /* @__PURE__ */ __name((e) => {
|
|
91
|
+
if (!(e instanceof HttpException))
|
|
92
|
+
e = new UndefinedException(e.message || e);
|
|
93
|
+
return e.data;
|
|
94
|
+
}, "serverFilter");
|
|
95
|
+
var rabbitMqFilter = /* @__PURE__ */ __name((e, data) => {
|
|
96
|
+
const { channel, message } = data;
|
|
97
|
+
channel.reject(message, true);
|
|
98
|
+
}, "rabbitMqFilter");
|
|
99
|
+
|
|
100
|
+
// src/exception/wrong-meta.ts
|
|
101
|
+
var WrongMetaException = class extends HttpException {
|
|
102
|
+
constructor(message) {
|
|
103
|
+
super(message, 500, "Meta is not correct");
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
__name(WrongMetaException, "WrongMetaException");
|
|
107
|
+
|
|
108
|
+
// src/history.ts
|
|
109
|
+
var Phistroy = class {
|
|
110
|
+
guard = [];
|
|
111
|
+
interceptor = [];
|
|
112
|
+
record(name, type) {
|
|
113
|
+
if (!this[type].includes(name)) {
|
|
114
|
+
this[type].push(name);
|
|
115
|
+
return true;
|
|
104
116
|
}
|
|
105
|
-
return
|
|
117
|
+
return false;
|
|
106
118
|
}
|
|
107
119
|
};
|
|
120
|
+
__name(Phistroy, "Phistroy");
|
|
108
121
|
|
|
109
|
-
// src/context.ts
|
|
122
|
+
// src/context/base.ts
|
|
110
123
|
var _Pcontext = class {
|
|
111
124
|
key;
|
|
112
|
-
|
|
125
|
+
data;
|
|
113
126
|
method;
|
|
114
127
|
params;
|
|
115
128
|
post;
|
|
116
129
|
history;
|
|
117
|
-
constructor(key,
|
|
130
|
+
constructor(key, data) {
|
|
118
131
|
this.key = key;
|
|
119
|
-
this.
|
|
132
|
+
this.data = data;
|
|
120
133
|
this.history = new Phistroy();
|
|
121
134
|
}
|
|
122
135
|
static registerGuard(key, handler) {
|
|
@@ -125,19 +138,12 @@ var _Pcontext = class {
|
|
|
125
138
|
static registerInterceptor(key, handler) {
|
|
126
139
|
_Pcontext.interceptorsRecord[key] = handler;
|
|
127
140
|
}
|
|
128
|
-
static useMiddleware(middlewares) {
|
|
129
|
-
return middlewares.map((m) => {
|
|
130
|
-
if (!(m in _Pcontext.middlewareRecord))
|
|
131
|
-
throw new WrongMetaException(`can't find middleware named ${m}`);
|
|
132
|
-
return _Pcontext.middlewareRecord[m];
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
141
|
async useGuard(guards, isMerge = false) {
|
|
136
142
|
for (const guard of guards) {
|
|
137
143
|
if (this.history.record(guard, "guard")) {
|
|
138
144
|
if (!(guard in _Pcontext.guardsRecord))
|
|
139
145
|
throw new WrongMetaException(`can't find guard named ${guard}`);
|
|
140
|
-
if (!await _Pcontext.guardsRecord[guard](this.
|
|
146
|
+
if (!await _Pcontext.guardsRecord[guard](this.data, isMerge))
|
|
141
147
|
throw new ForbiddenException(`Guard exception--${guard}`);
|
|
142
148
|
}
|
|
143
149
|
}
|
|
@@ -148,7 +154,7 @@ var _Pcontext = class {
|
|
|
148
154
|
if (this.history.record(interceptor, "interceptor")) {
|
|
149
155
|
if (!(interceptor in _Pcontext.interceptorsRecord))
|
|
150
156
|
throw new WrongMetaException(`can't find guard named ${interceptor}`);
|
|
151
|
-
const post = await _Pcontext.interceptorsRecord[interceptor](this.
|
|
157
|
+
const post = await _Pcontext.interceptorsRecord[interceptor](this.data, isMerge);
|
|
152
158
|
if (post)
|
|
153
159
|
ret.push(post);
|
|
154
160
|
}
|
|
@@ -156,20 +162,19 @@ var _Pcontext = class {
|
|
|
156
162
|
this.post = ret;
|
|
157
163
|
}
|
|
158
164
|
async usePost(data) {
|
|
165
|
+
if (!this.post)
|
|
166
|
+
return data;
|
|
159
167
|
for (const cb of this.post)
|
|
160
168
|
data = await cb(data) | data;
|
|
161
169
|
return data;
|
|
162
170
|
}
|
|
163
|
-
async usePipe(args, reflect) {
|
|
164
|
-
return _Pcontext.pipe.transform(args, reflect);
|
|
165
|
-
}
|
|
166
171
|
};
|
|
167
172
|
var Pcontext = _Pcontext;
|
|
168
173
|
__name(Pcontext, "Pcontext");
|
|
169
|
-
__publicField(Pcontext, "pipe", defaultPipe);
|
|
170
174
|
__publicField(Pcontext, "metaRecord", {});
|
|
175
|
+
__publicField(Pcontext, "metaDataRecord", {});
|
|
176
|
+
__publicField(Pcontext, "instanceRecord", {});
|
|
171
177
|
__publicField(Pcontext, "guardsRecord", {});
|
|
172
|
-
__publicField(Pcontext, "middlewareRecord", {});
|
|
173
178
|
__publicField(Pcontext, "interceptorsRecord", {});
|
|
174
179
|
function addGuard(key, handler) {
|
|
175
180
|
Pcontext.registerGuard(key, handler);
|
|
@@ -179,10 +184,10 @@ function addInterceptor(key, handler) {
|
|
|
179
184
|
Pcontext.registerInterceptor(key, handler);
|
|
180
185
|
}
|
|
181
186
|
__name(addInterceptor, "addInterceptor");
|
|
182
|
-
function
|
|
183
|
-
Pcontext.
|
|
187
|
+
function getInstance(tag) {
|
|
188
|
+
return Pcontext.instanceRecord[tag];
|
|
184
189
|
}
|
|
185
|
-
__name(
|
|
190
|
+
__name(getInstance, "getInstance");
|
|
186
191
|
function parseMeta(meta) {
|
|
187
192
|
const { data: { params, guards, interceptors, middlewares }, reflect } = meta;
|
|
188
193
|
return {
|
|
@@ -202,6 +207,66 @@ function parseMeta(meta) {
|
|
|
202
207
|
}
|
|
203
208
|
__name(parseMeta, "parseMeta");
|
|
204
209
|
|
|
210
|
+
// src/context/server.ts
|
|
211
|
+
var _ServerContext = class extends Pcontext {
|
|
212
|
+
static useMiddleware(middlewares) {
|
|
213
|
+
return middlewares.map((m) => {
|
|
214
|
+
if (!(m in _ServerContext.middlewareRecord))
|
|
215
|
+
throw new WrongMetaException(`can't find middleware named ${m}`);
|
|
216
|
+
return _ServerContext.middlewareRecord[m];
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
async usePipe(args, reflect) {
|
|
220
|
+
return _ServerContext.pipe.transform?.(args, reflect);
|
|
221
|
+
}
|
|
222
|
+
useFilter(arg) {
|
|
223
|
+
return _ServerContext.filter(arg, this.data);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
var ServerContext = _ServerContext;
|
|
227
|
+
__name(ServerContext, "ServerContext");
|
|
228
|
+
__publicField(ServerContext, "pipe", defaultPipe);
|
|
229
|
+
__publicField(ServerContext, "filter", serverFilter);
|
|
230
|
+
__publicField(ServerContext, "middlewareRecord", {});
|
|
231
|
+
function useServerPipe(pipe) {
|
|
232
|
+
ServerContext.pipe = pipe;
|
|
233
|
+
}
|
|
234
|
+
__name(useServerPipe, "useServerPipe");
|
|
235
|
+
function useServerFilter(filter) {
|
|
236
|
+
ServerContext.filter = filter;
|
|
237
|
+
}
|
|
238
|
+
__name(useServerFilter, "useServerFilter");
|
|
239
|
+
|
|
240
|
+
// src/context/micro.ts
|
|
241
|
+
var _RabbitMqContext = class extends Pcontext {
|
|
242
|
+
static useMiddleware(middlewares) {
|
|
243
|
+
return middlewares.map((m) => {
|
|
244
|
+
if (!(m in _RabbitMqContext.middlewareRecord))
|
|
245
|
+
throw new WrongMetaException(`can't find middleware named ${m}`);
|
|
246
|
+
return _RabbitMqContext.middlewareRecord[m];
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async usePipe(args, reflect) {
|
|
250
|
+
return _RabbitMqContext.pipe.transform?.(args, reflect);
|
|
251
|
+
}
|
|
252
|
+
useFilter(arg) {
|
|
253
|
+
return _RabbitMqContext.filter(arg, this.data);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
var RabbitMqContext = _RabbitMqContext;
|
|
257
|
+
__name(RabbitMqContext, "RabbitMqContext");
|
|
258
|
+
__publicField(RabbitMqContext, "pipe", defaultPipe);
|
|
259
|
+
__publicField(RabbitMqContext, "filter", rabbitMqFilter);
|
|
260
|
+
__publicField(RabbitMqContext, "middlewareRecord", {});
|
|
261
|
+
function useMqPipe(pipe) {
|
|
262
|
+
RabbitMqContext.pipe = pipe;
|
|
263
|
+
}
|
|
264
|
+
__name(useMqPipe, "useMqPipe");
|
|
265
|
+
function useMqFilter(filter) {
|
|
266
|
+
RabbitMqContext.filter = filter;
|
|
267
|
+
}
|
|
268
|
+
__name(useMqFilter, "useMqFilter");
|
|
269
|
+
|
|
205
270
|
// src/compiler.ts
|
|
206
271
|
var Pcompiler = class {
|
|
207
272
|
content = "";
|
|
@@ -224,10 +289,22 @@ var Pcompiler = class {
|
|
|
224
289
|
this.classMap[className] = {};
|
|
225
290
|
this.classMap[className][methodName] = `
|
|
226
291
|
${methodName}(${genParams(params)}){
|
|
227
|
-
const ret={
|
|
228
|
-
${params.reduce((p, c, i) => `${p}ret.${c.type}.${c.key}=arg${i}
|
|
292
|
+
const ret={tag:"${className}-${methodName}",body:{},query:{},params:{},realParam:'',method:"${requestType}",url:"${url}"}
|
|
293
|
+
${params.filter((item) => item.key).reduce((p, c, i) => `${p}ret.${c.type}.${c.key}=arg${i}
|
|
229
294
|
${c.type === "params" ? `ret.realParam+='/'+arg${i}
|
|
230
295
|
` : ""}`, "")}
|
|
296
|
+
return ret
|
|
297
|
+
}
|
|
298
|
+
`;
|
|
299
|
+
}
|
|
300
|
+
addMqMethod(className, methodName, exchange = "", routeKey = "", queue = "", params = []) {
|
|
301
|
+
if (!this.classMap[className])
|
|
302
|
+
this.classMap[className] = {};
|
|
303
|
+
this.classMap[className][methodName] = `
|
|
304
|
+
${methodName}(${genParams(params)}){
|
|
305
|
+
const ret={tag:"${className}-${methodName}",exchange:"${exchange}",routeKey:"${routeKey}",queue:"${queue}",args:{}}
|
|
306
|
+
${params.reduce((p, c, i) => `${p}ret.args.${c.key}=arg${i}
|
|
307
|
+
`, "")}
|
|
231
308
|
return ret
|
|
232
309
|
}
|
|
233
310
|
`;
|
|
@@ -265,7 +342,7 @@ __name(NotFoundException, "NotFoundException");
|
|
|
265
342
|
var SERIES_SYMBOL = "__symbol_series__";
|
|
266
343
|
var REQ_SYMBOL = "__symbol_req__";
|
|
267
344
|
|
|
268
|
-
// src/express.ts
|
|
345
|
+
// src/server/express.ts
|
|
269
346
|
function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
270
347
|
const { globalGuards, globalInterceptors, route, middlewares: proMiddle } = {
|
|
271
348
|
route: "/__PHECDA_SERVER__",
|
|
@@ -279,7 +356,8 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
279
356
|
const { name, method, route: route2, header } = i.data;
|
|
280
357
|
const instance = moduleMap.get(name);
|
|
281
358
|
const tag = `${name}-${method}`;
|
|
282
|
-
|
|
359
|
+
Pcontext.metaRecord[tag] = i;
|
|
360
|
+
let { guards, reflect, interceptors, params, middlewares } = Pcontext.metaDataRecord[tag] ? Pcontext.metaDataRecord[tag] : Pcontext.metaDataRecord[tag] = parseMeta(i);
|
|
283
361
|
guards = [
|
|
284
362
|
...globalGuards,
|
|
285
363
|
...guards
|
|
@@ -290,12 +368,16 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
290
368
|
];
|
|
291
369
|
const handler = instance[method].bind(instance);
|
|
292
370
|
methodMap[tag] = handler;
|
|
371
|
+
Pcontext.instanceRecord[name] = instance;
|
|
293
372
|
if (route2) {
|
|
294
|
-
app[route2.type](route2.route, ...
|
|
373
|
+
app[route2.type](route2.route, ...ServerContext.useMiddleware(middlewares), async (req, res) => {
|
|
374
|
+
const contextData = {
|
|
375
|
+
request: req,
|
|
376
|
+
tag,
|
|
377
|
+
response: res
|
|
378
|
+
};
|
|
379
|
+
const context = new ServerContext(tag, contextData);
|
|
295
380
|
try {
|
|
296
|
-
const context = new Pcontext(`${name}-${method}`, req);
|
|
297
|
-
instance.ctx = context;
|
|
298
|
-
instance.request = req;
|
|
299
381
|
for (const name2 in header)
|
|
300
382
|
res.set(name2, header[name2]);
|
|
301
383
|
await context.useGuard(guards);
|
|
@@ -306,15 +388,16 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
306
388
|
validate
|
|
307
389
|
};
|
|
308
390
|
}), reflect);
|
|
391
|
+
instance.meta = contextData;
|
|
309
392
|
const ret = await context.usePost(await handler(...args));
|
|
310
393
|
if (isObject(ret))
|
|
311
394
|
res.json(ret);
|
|
312
395
|
else
|
|
313
396
|
res.send(String(ret));
|
|
314
397
|
} catch (e) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
res.status(
|
|
398
|
+
i.handlers.forEach((handler2) => handler2.error?.(e));
|
|
399
|
+
const err = await context.useFilter(e);
|
|
400
|
+
res.status(err.status).json(err);
|
|
318
401
|
}
|
|
319
402
|
});
|
|
320
403
|
}
|
|
@@ -322,22 +405,24 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
322
405
|
app.post(route, (req, _res, next) => {
|
|
323
406
|
req[REQ_SYMBOL] = true;
|
|
324
407
|
next();
|
|
325
|
-
}, ...
|
|
326
|
-
const
|
|
408
|
+
}, ...ServerContext.useMiddleware(proMiddle), async (req, res) => {
|
|
409
|
+
const contextData = {
|
|
410
|
+
request: req,
|
|
411
|
+
response: res
|
|
412
|
+
};
|
|
413
|
+
const context = new ServerContext(route, contextData);
|
|
327
414
|
const ret = [];
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
instance.ctx = context;
|
|
336
|
-
instance.request = req;
|
|
415
|
+
const { body } = req;
|
|
416
|
+
for (const i in body) {
|
|
417
|
+
const { tag } = body[i];
|
|
418
|
+
const [name] = tag.split("-");
|
|
419
|
+
const { guards, reflect, interceptors, params } = Pcontext.metaDataRecord[tag];
|
|
420
|
+
const instance = moduleMap.get(name);
|
|
421
|
+
try {
|
|
337
422
|
if (!params)
|
|
338
423
|
throw new NotFoundException(`"${tag}" doesn't exist`);
|
|
339
|
-
await context.useGuard(guards);
|
|
340
|
-
await context.useInterceptor(interceptors);
|
|
424
|
+
await context.useGuard(guards, true);
|
|
425
|
+
await context.useInterceptor(interceptors, true);
|
|
341
426
|
const args = await context.usePipe(params.map(({ type, key, validate }) => {
|
|
342
427
|
const arg = resolveDep(body[i][type], key);
|
|
343
428
|
if (typeof arg === "string" && arg.startsWith(SERIES_SYMBOL)) {
|
|
@@ -352,13 +437,13 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
352
437
|
validate
|
|
353
438
|
};
|
|
354
439
|
}), reflect);
|
|
440
|
+
instance.meta = contextData;
|
|
355
441
|
ret.push(await context.usePost(await methodMap[tag](...args)));
|
|
442
|
+
} catch (e) {
|
|
443
|
+
const m = Pcontext.metaRecord[tag];
|
|
444
|
+
m.handlers.forEach((handler) => handler.error?.(e));
|
|
445
|
+
ret.push(await context.useFilter(e));
|
|
356
446
|
}
|
|
357
|
-
} catch (e) {
|
|
358
|
-
if (!(e instanceof HttpException))
|
|
359
|
-
e = new UndefinedException(e.message || e);
|
|
360
|
-
console.error(e.stack);
|
|
361
|
-
ret.push(e.data);
|
|
362
447
|
}
|
|
363
448
|
res.json(ret);
|
|
364
449
|
});
|
|
@@ -367,31 +452,34 @@ __name(bindApp, "bindApp");
|
|
|
367
452
|
|
|
368
453
|
// src/core.ts
|
|
369
454
|
import "reflect-metadata";
|
|
370
|
-
import {
|
|
455
|
+
import { getExposeKey, getHandler, getState, registerAsync } from "phecda-core";
|
|
371
456
|
|
|
372
457
|
// src/meta.ts
|
|
373
458
|
var Pmeta = class {
|
|
374
459
|
data;
|
|
460
|
+
handlers;
|
|
375
461
|
reflect;
|
|
376
|
-
constructor(data, reflect) {
|
|
462
|
+
constructor(data, handlers, reflect) {
|
|
377
463
|
this.data = data;
|
|
464
|
+
this.handlers = handlers;
|
|
378
465
|
this.reflect = reflect;
|
|
379
466
|
}
|
|
380
467
|
};
|
|
381
468
|
__name(Pmeta, "Pmeta");
|
|
382
469
|
|
|
383
470
|
// src/core.ts
|
|
384
|
-
function Factory(Modules) {
|
|
471
|
+
async function Factory(Modules) {
|
|
385
472
|
const moduleMap = /* @__PURE__ */ new Map();
|
|
386
473
|
const meta = [];
|
|
387
|
-
|
|
474
|
+
for (const Module of Modules)
|
|
475
|
+
await buildNestModule(Module, moduleMap, meta);
|
|
388
476
|
return {
|
|
389
477
|
moduleMap,
|
|
390
478
|
meta
|
|
391
479
|
};
|
|
392
480
|
}
|
|
393
481
|
__name(Factory, "Factory");
|
|
394
|
-
function buildNestModule(Module, map, meta) {
|
|
482
|
+
async function buildNestModule(Module, map, meta) {
|
|
395
483
|
const paramtypes = getParamtypes(Module);
|
|
396
484
|
let instance;
|
|
397
485
|
const name = Module.name;
|
|
@@ -403,21 +491,24 @@ function buildNestModule(Module, map, meta) {
|
|
|
403
491
|
}
|
|
404
492
|
map.set(name, void 0);
|
|
405
493
|
if (paramtypes) {
|
|
406
|
-
|
|
494
|
+
for (const i in paramtypes)
|
|
495
|
+
paramtypes[i] = await buildNestModule(paramtypes[i], map, meta);
|
|
496
|
+
instance = new Module(...paramtypes);
|
|
407
497
|
} else {
|
|
408
498
|
instance = new Module();
|
|
409
499
|
}
|
|
410
500
|
meta.push(...getMetaFromInstance(instance, name));
|
|
501
|
+
await registerAsync(instance);
|
|
411
502
|
map.set(name, instance);
|
|
412
503
|
return instance;
|
|
413
504
|
}
|
|
414
505
|
__name(buildNestModule, "buildNestModule");
|
|
415
506
|
function getMetaFromInstance(instance, name) {
|
|
416
|
-
const vars =
|
|
507
|
+
const vars = getExposeKey(instance).filter((item) => item !== "__CLASS");
|
|
417
508
|
const baseState = getState(instance, "__CLASS") || {};
|
|
418
509
|
initState(baseState);
|
|
419
510
|
return vars.map((i) => {
|
|
420
|
-
const state = getState(instance, i);
|
|
511
|
+
const state = getState(instance, i) || {};
|
|
421
512
|
if (baseState.route && state.route)
|
|
422
513
|
state.route.route = baseState.route.route + state.route.route;
|
|
423
514
|
state.name = name;
|
|
@@ -449,7 +540,7 @@ function getMetaFromInstance(instance, name) {
|
|
|
449
540
|
...state.interceptors
|
|
450
541
|
])
|
|
451
542
|
];
|
|
452
|
-
return new Pmeta(state, getParamtypes(instance, i) || []);
|
|
543
|
+
return new Pmeta(state, getHandler(instance, i), getParamtypes(instance, i) || []);
|
|
453
544
|
});
|
|
454
545
|
}
|
|
455
546
|
__name(getMetaFromInstance, "getMetaFromInstance");
|
|
@@ -470,7 +561,7 @@ function initState(state) {
|
|
|
470
561
|
__name(initState, "initState");
|
|
471
562
|
|
|
472
563
|
// src/decorators/index.ts
|
|
473
|
-
import { mergeState as
|
|
564
|
+
import { mergeState as mergeState4, setModalVar as setModalVar4 } from "phecda-core";
|
|
474
565
|
|
|
475
566
|
// src/decorators/param.ts
|
|
476
567
|
import { mergeState, setModalVar } from "phecda-core";
|
|
@@ -588,14 +679,30 @@ function Interceptor(interceptorKey) {
|
|
|
588
679
|
}
|
|
589
680
|
__name(Interceptor, "Interceptor");
|
|
590
681
|
|
|
682
|
+
// src/decorators/micro.ts
|
|
683
|
+
import { mergeState as mergeState3, setModalVar as setModalVar3 } from "phecda-core";
|
|
684
|
+
function MQ(queue, routeKey, options) {
|
|
685
|
+
return (target, k) => {
|
|
686
|
+
setModalVar3(target, k);
|
|
687
|
+
mergeState3(target, k, {
|
|
688
|
+
mq: {
|
|
689
|
+
queue,
|
|
690
|
+
routeKey,
|
|
691
|
+
options
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
__name(MQ, "MQ");
|
|
697
|
+
|
|
591
698
|
// src/decorators/index.ts
|
|
592
|
-
function Inject() {
|
|
699
|
+
function Inject(_target) {
|
|
593
700
|
}
|
|
594
701
|
__name(Inject, "Inject");
|
|
595
702
|
function Header(name, value) {
|
|
596
703
|
return (target, k) => {
|
|
597
|
-
|
|
598
|
-
|
|
704
|
+
setModalVar4(target, k);
|
|
705
|
+
mergeState4(target, k, {
|
|
599
706
|
header: {
|
|
600
707
|
name,
|
|
601
708
|
value
|
|
@@ -636,6 +743,83 @@ function Server(localPath) {
|
|
|
636
743
|
};
|
|
637
744
|
}
|
|
638
745
|
__name(Server, "Server");
|
|
746
|
+
|
|
747
|
+
// src/index.ts
|
|
748
|
+
export * from "phecda-core";
|
|
749
|
+
|
|
750
|
+
// src/config.ts
|
|
751
|
+
var Pconfig = {
|
|
752
|
+
rabbitmq: {
|
|
753
|
+
guard: false,
|
|
754
|
+
interceptor: false
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
// src/micro-services/rabbitmq.ts
|
|
759
|
+
async function bindMQ(ch, { meta, moduleMap }) {
|
|
760
|
+
for (const item of meta) {
|
|
761
|
+
const { route, name, method, mq: { routeKey, queue: queueName, options } = {} } = item.data;
|
|
762
|
+
const tag = `${name}-${method}`;
|
|
763
|
+
Pcontext.metaRecord[tag] = item;
|
|
764
|
+
const { guards, reflect, interceptors, params } = Pcontext.metaDataRecord[tag] ? Pcontext.metaDataRecord[tag] : Pcontext.metaDataRecord[tag] = parseMeta(item);
|
|
765
|
+
const instance = moduleMap.get(name);
|
|
766
|
+
const handler = instance[method].bind(instance);
|
|
767
|
+
Pcontext.instanceRecord[name] = instance;
|
|
768
|
+
if (route) {
|
|
769
|
+
const { queue } = await ch.assertQueue(route.route);
|
|
770
|
+
if (queueName && routeKey)
|
|
771
|
+
await ch.bindQueue(queue, queueName, routeKey);
|
|
772
|
+
ch.consume(route.route, async (msg) => {
|
|
773
|
+
if (msg !== null) {
|
|
774
|
+
const content = msg.content.toString();
|
|
775
|
+
const data = params.length > 0 ? JSON.parse(content) : content;
|
|
776
|
+
const contextMeta = {
|
|
777
|
+
message: msg,
|
|
778
|
+
content,
|
|
779
|
+
channel: ch
|
|
780
|
+
};
|
|
781
|
+
const context = new RabbitMqContext(tag, contextMeta);
|
|
782
|
+
try {
|
|
783
|
+
if (Pconfig.rabbitmq.guard)
|
|
784
|
+
await context.useGuard(guards);
|
|
785
|
+
if (Pconfig.rabbitmq.interceptor)
|
|
786
|
+
await context.useInterceptor(interceptors);
|
|
787
|
+
const args = await context.usePipe(params.map(({ key, validate }) => {
|
|
788
|
+
return {
|
|
789
|
+
arg: resolveDep(data, key),
|
|
790
|
+
validate
|
|
791
|
+
};
|
|
792
|
+
}), reflect);
|
|
793
|
+
instance.meta = contextMeta;
|
|
794
|
+
await context.usePost(await handler(...args));
|
|
795
|
+
ch.ack(msg);
|
|
796
|
+
} catch (e) {
|
|
797
|
+
item.handlers.forEach((handler2) => handler2.error?.(e));
|
|
798
|
+
context.useFilter(e);
|
|
799
|
+
}
|
|
800
|
+
} else {
|
|
801
|
+
}
|
|
802
|
+
}, options);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
__name(bindMQ, "bindMQ");
|
|
807
|
+
async function createPub(ch, method, type) {
|
|
808
|
+
const { exchange, routeKey, queue } = method();
|
|
809
|
+
if (exchange)
|
|
810
|
+
await ch.assertExchange(exchange, type);
|
|
811
|
+
else
|
|
812
|
+
await ch.assertQueue(queue);
|
|
813
|
+
return async (arg) => {
|
|
814
|
+
const { args } = method(arg);
|
|
815
|
+
const msg = Buffer.from(JSON.stringify(args));
|
|
816
|
+
if (exchange)
|
|
817
|
+
await ch.publish(exchange, routeKey, msg);
|
|
818
|
+
else
|
|
819
|
+
await ch.sendToQueue(queue, msg);
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
__name(createPub, "createPub");
|
|
639
823
|
export {
|
|
640
824
|
BadRequestException,
|
|
641
825
|
BaseParam,
|
|
@@ -650,6 +834,7 @@ export {
|
|
|
650
834
|
HttpException,
|
|
651
835
|
Inject,
|
|
652
836
|
Interceptor,
|
|
837
|
+
MQ,
|
|
653
838
|
Param,
|
|
654
839
|
Pcompiler,
|
|
655
840
|
Pcontext,
|
|
@@ -657,15 +842,23 @@ export {
|
|
|
657
842
|
Post,
|
|
658
843
|
Put,
|
|
659
844
|
Query,
|
|
845
|
+
RabbitMqContext,
|
|
660
846
|
Route,
|
|
661
847
|
Server,
|
|
848
|
+
ServerContext,
|
|
662
849
|
UndefinedException,
|
|
663
850
|
ValidateException,
|
|
664
851
|
addGuard,
|
|
665
852
|
addInterceptor,
|
|
666
853
|
bindApp,
|
|
854
|
+
bindMQ,
|
|
855
|
+
createPub,
|
|
667
856
|
defaultPipe,
|
|
857
|
+
getInstance,
|
|
668
858
|
parseMeta,
|
|
669
|
-
|
|
859
|
+
useMqFilter,
|
|
860
|
+
useMqPipe,
|
|
861
|
+
useServerFilter,
|
|
862
|
+
useServerPipe
|
|
670
863
|
};
|
|
671
864
|
//# sourceMappingURL=index.mjs.map
|