phecda-server 1.3.0 → 1.4.1

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/index.js CHANGED
@@ -34,23 +34,30 @@ var __publicField = (obj, key, value) => {
34
34
  var src_exports = {};
35
35
  __export(src_exports, {
36
36
  $S: () => $S,
37
+ BadGatewayException: () => BadGatewayException,
37
38
  BadRequestException: () => BadRequestException,
39
+ Base: () => Base,
38
40
  BaseParam: () => BaseParam,
39
41
  Body: () => Body,
42
+ ConflictException: () => ConflictException,
40
43
  Controller: () => Controller,
41
44
  Delete: () => Delete,
42
45
  Factory: () => Factory,
43
46
  ForbiddenException: () => ForbiddenException,
47
+ FrameworkException: () => FrameworkException,
44
48
  Get: () => Get,
45
49
  Guard: () => Guard,
46
50
  Header: () => Header,
47
51
  HttpException: () => HttpException,
48
52
  Inject: () => Inject,
49
53
  Interceptor: () => Interceptor,
54
+ InvalidInputException: () => InvalidInputException,
50
55
  MQ: () => MQ,
56
+ Meta: () => Meta,
51
57
  Middle: () => Middle,
52
58
  NotFoundException: () => NotFoundException,
53
59
  Param: () => Param,
60
+ PayloadLargeException: () => PayloadLargeException,
54
61
  Pcompiler: () => Pcompiler,
55
62
  Pcontext: () => Pcontext,
56
63
  Pmeta: () => Pmeta,
@@ -60,9 +67,12 @@ __export(src_exports, {
60
67
  RabbitMqContext: () => RabbitMqContext,
61
68
  Route: () => Route,
62
69
  ServerContext: () => ServerContext,
70
+ ServiceUnavailableException: () => ServiceUnavailableException,
71
+ TimeoutException: () => TimeoutException,
72
+ UnauthorizedException: () => UnauthorizedException,
63
73
  UndefinedException: () => UndefinedException,
74
+ UnsupportedMediaTypeException: () => UnsupportedMediaTypeException,
64
75
  ValidateException: () => ValidateException,
65
- WrongMetaException: () => WrongMetaException,
66
76
  addGuard: () => addGuard,
67
77
  addInterceptor: () => addInterceptor,
68
78
  addMiddleware: () => addMiddleware,
@@ -168,14 +178,6 @@ var BadRequestException = class extends HttpException {
168
178
  };
169
179
  __name(BadRequestException, "BadRequestException");
170
180
 
171
- // src/exception/wrong-meta.ts
172
- var WrongMetaException = class extends HttpException {
173
- constructor(message) {
174
- super(message, 500, "Meta is not correct");
175
- }
176
- };
177
- __name(WrongMetaException, "WrongMetaException");
178
-
179
181
  // src/exception/not-found.ts
180
182
  var NotFoundException = class extends HttpException {
181
183
  constructor(message) {
@@ -184,6 +186,78 @@ var NotFoundException = class extends HttpException {
184
186
  };
185
187
  __name(NotFoundException, "NotFoundException");
186
188
 
189
+ // src/exception/conflict.ts
190
+ var ConflictException = class extends HttpException {
191
+ constructor(message) {
192
+ super(message, 409, "Conflict");
193
+ }
194
+ };
195
+ __name(ConflictException, "ConflictException");
196
+
197
+ // src/exception/bad-gateway.ts
198
+ var BadGatewayException = class extends HttpException {
199
+ constructor(message) {
200
+ super(message, 400, "Bad Gatrway");
201
+ }
202
+ };
203
+ __name(BadGatewayException, "BadGatewayException");
204
+
205
+ // src/exception/invalid-input.ts
206
+ var InvalidInputException = class extends HttpException {
207
+ constructor(message) {
208
+ super(message, 502, "Invalid Input");
209
+ }
210
+ };
211
+ __name(InvalidInputException, "InvalidInputException");
212
+
213
+ // src/exception/media-type.ts
214
+ var UnsupportedMediaTypeException = class extends HttpException {
215
+ constructor(message) {
216
+ super(message, 415, "Unsupported Media Type");
217
+ }
218
+ };
219
+ __name(UnsupportedMediaTypeException, "UnsupportedMediaTypeException");
220
+
221
+ // src/exception/payload-large.ts
222
+ var PayloadLargeException = class extends HttpException {
223
+ constructor(message) {
224
+ super(message, 413, "Payload Too Large");
225
+ }
226
+ };
227
+ __name(PayloadLargeException, "PayloadLargeException");
228
+
229
+ // src/exception/timeout.ts
230
+ var TimeoutException = class extends HttpException {
231
+ constructor(message) {
232
+ super(message, 408, "Request Timeout");
233
+ }
234
+ };
235
+ __name(TimeoutException, "TimeoutException");
236
+
237
+ // src/exception/unauthorized.ts
238
+ var UnauthorizedException = class extends HttpException {
239
+ constructor(message) {
240
+ super(message, 401, "Unauthorized");
241
+ }
242
+ };
243
+ __name(UnauthorizedException, "UnauthorizedException");
244
+
245
+ // src/exception/unavailable-service.ts
246
+ var ServiceUnavailableException = class extends HttpException {
247
+ constructor(message) {
248
+ super(message, 503, "Service Unavailable");
249
+ }
250
+ };
251
+ __name(ServiceUnavailableException, "ServiceUnavailableException");
252
+
253
+ // src/exception/framework.ts
254
+ var FrameworkException = class extends HttpException {
255
+ constructor(message) {
256
+ super(`[phecda-server] ${message}`, 500, "Framework Error");
257
+ }
258
+ };
259
+ __name(FrameworkException, "FrameworkException");
260
+
187
261
  // src/filter.ts
188
262
  var serverFilter = /* @__PURE__ */ __name((e) => {
189
263
  if (!(e instanceof HttpException))
@@ -232,7 +306,7 @@ var _Pcontext = class {
232
306
  for (const guard of guards) {
233
307
  if (this.history.record(guard, "guard")) {
234
308
  if (!(guard in _Pcontext.guardsRecord))
235
- throw new WrongMetaException(`can't find guard named ${guard}`);
309
+ throw new FrameworkException(`can't find guard named ${guard}`);
236
310
  if (!await _Pcontext.guardsRecord[guard](this.data, isMerge))
237
311
  throw new ForbiddenException(`Guard exception--${guard}`);
238
312
  }
@@ -243,7 +317,7 @@ var _Pcontext = class {
243
317
  for (const interceptor of interceptors) {
244
318
  if (this.history.record(interceptor, "interceptor")) {
245
319
  if (!(interceptor in _Pcontext.interceptorsRecord))
246
- throw new WrongMetaException(`can't find guard named ${interceptor}`);
320
+ throw new FrameworkException(`can't find guard named ${interceptor}`);
247
321
  const post = await _Pcontext.interceptorsRecord[interceptor](this.data, isMerge);
248
322
  if (post)
249
323
  ret.push(post);
@@ -302,13 +376,16 @@ var _ServerContext = class extends Pcontext {
302
376
  static useMiddleware(middlewares) {
303
377
  return middlewares.map((m) => {
304
378
  if (!(m in _ServerContext.middlewareRecord))
305
- throw new WrongMetaException(`can't find middleware named ${m}`);
379
+ throw new FrameworkException(`can't find middleware named ${m}`);
306
380
  return _ServerContext.middlewareRecord[m];
307
381
  });
308
382
  }
309
383
  async usePipe(args, reflect) {
310
384
  return _ServerContext.pipe.transform?.(args, reflect);
311
385
  }
386
+ static useFilter(arg, data) {
387
+ return _ServerContext.filter(arg, data);
388
+ }
312
389
  useFilter(arg) {
313
390
  return _ServerContext.filter(arg, this.data);
314
391
  }
@@ -336,13 +413,16 @@ var _RabbitMqContext = class extends Pcontext {
336
413
  static useMiddleware(middlewares) {
337
414
  return middlewares.map((m) => {
338
415
  if (!(m in _RabbitMqContext.middlewareRecord))
339
- throw new WrongMetaException(`can't find middleware named ${m}`);
416
+ throw new FrameworkException(`can't find middleware named ${m}`);
340
417
  return _RabbitMqContext.middlewareRecord[m];
341
418
  });
342
419
  }
343
420
  async usePipe(args, reflect) {
344
421
  return _RabbitMqContext.pipe.transform?.(args, reflect);
345
422
  }
423
+ static useFilter(arg, data) {
424
+ return _RabbitMqContext.filter(arg, data);
425
+ }
346
426
  useFilter(arg) {
347
427
  return _RabbitMqContext.filter(arg, this.data);
348
428
  }
@@ -361,10 +441,16 @@ function useMqFilter(filter) {
361
441
  }
362
442
  __name(useMqFilter, "useMqFilter");
363
443
 
444
+ // src/types.ts
445
+ var Base = class {
446
+ context;
447
+ };
448
+ __name(Base, "Base");
449
+
364
450
  // src/compiler.ts
365
451
  var Pcompiler = class {
366
- content = "";
367
452
  classMap = {};
453
+ name;
368
454
  constructor() {
369
455
  }
370
456
  getContent() {
@@ -377,9 +463,17 @@ var Pcompiler = class {
377
463
  }
378
464
  return content;
379
465
  }
466
+ createRequest() {
467
+ let content = "import {useC} from 'phecda-server'\n";
468
+ for (const name in this.classMap)
469
+ content += `export const {${Object.keys(this.classMap[name]).join(",")}}=useC(${name})
470
+ `;
471
+ return content;
472
+ }
380
473
  addMethod(args) {
381
474
  const { route: { route = "/", type = "get" } = {}, name, method, params, tag } = args;
382
475
  const url = route.replace(/\/\:([^\/]*)/g, "");
476
+ this.name = name;
383
477
  if (!this.classMap[name])
384
478
  this.classMap[name] = {};
385
479
  this.classMap[name][method] = `
@@ -414,7 +508,7 @@ __name(resolveDep, "resolveDep");
414
508
 
415
509
  // src/common.ts
416
510
  var SERIES_SYMBOL = "__symbol_series__";
417
- var REQ_SYMBOL = "__symbol_req__";
511
+ var MERGE_SYMBOL = "__symbol_req__";
418
512
 
419
513
  // src/server/express.ts
420
514
  function bindApp(app, { meta, moduleMap }, options = {}) {
@@ -426,10 +520,12 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
426
520
  ...options
427
521
  };
428
522
  const methodMap = {};
523
+ const contextMeta = {};
429
524
  for (const i of meta) {
430
525
  const { name, method, route: route2, header, tag } = i.data;
431
526
  const instance = moduleMap.get(tag);
432
527
  const methodTag = `${tag}-${method}`;
528
+ contextMeta[methodTag] = i;
433
529
  Pcontext.metaRecord[methodTag] = i;
434
530
  let { guards, reflect, interceptors, params, middlewares } = Pcontext.metaDataRecord[methodTag] ? Pcontext.metaDataRecord[methodTag] : Pcontext.metaDataRecord[methodTag] = parseMeta(i);
435
531
  guards = [
@@ -447,7 +543,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
447
543
  app[route2.type](route2.route, ...ServerContext.useMiddleware(middlewares), async (req, res) => {
448
544
  const contextData = {
449
545
  request: req,
450
- methodTag,
546
+ meta: i,
451
547
  response: res
452
548
  };
453
549
  const context = new ServerContext(methodTag, contextData);
@@ -462,7 +558,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
462
558
  validate
463
559
  };
464
560
  }), reflect);
465
- instance.meta = contextData;
561
+ instance.context = contextData;
466
562
  const ret = await context.usePost(await handler(...args));
467
563
  if (isObject(ret))
468
564
  res.json(ret);
@@ -477,16 +573,20 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
477
573
  }
478
574
  }
479
575
  app.post(route, (req, _res, next) => {
480
- req[REQ_SYMBOL] = true;
576
+ req[MERGE_SYMBOL] = true;
481
577
  next();
482
578
  }, ...ServerContext.useMiddleware(proMiddle), async (req, res) => {
579
+ const { body: { category, data } } = req;
483
580
  const contextData = {
484
581
  request: req,
485
- response: res
582
+ response: res,
583
+ meta: contextMeta
486
584
  };
585
+ if (!Array.isArray(data))
586
+ return res.json(await ServerContext.useFilter(new BadRequestException("data format should be an array"), contextData));
587
+ contextData.tags = data.map((item) => item.tag);
487
588
  const context = new ServerContext(route, contextData);
488
589
  const ret = [];
489
- const { body: { category, data } } = req;
490
590
  if (category === "series") {
491
591
  for (const item of data) {
492
592
  const { tag } = item;
@@ -495,7 +595,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
495
595
  const instance = moduleMap.get(name);
496
596
  try {
497
597
  if (!params)
498
- throw new NotFoundException(`"${tag}" doesn't exist`);
598
+ throw new BadRequestException(`"${tag}" doesn't exist`);
499
599
  await context.useGuard(guards, true);
500
600
  await context.useInterceptor(interceptors, true);
501
601
  const args = await context.usePipe(params.map(({ type, key, validate }) => {
@@ -512,7 +612,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
512
612
  validate
513
613
  };
514
614
  }), reflect);
515
- instance.meta = contextData;
615
+ instance.context = contextData;
516
616
  ret.push(await context.usePost(await methodMap[tag](...args)));
517
617
  } catch (e) {
518
618
  const m = Pcontext.metaRecord[tag];
@@ -531,7 +631,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
531
631
  const instance = moduleMap.get(name);
532
632
  try {
533
633
  if (!params)
534
- throw new NotFoundException(`"${tag}" doesn't exist`);
634
+ throw new BadRequestException(`"${tag}" doesn't exist`);
535
635
  await context.useGuard(guards, true);
536
636
  await context.useInterceptor(interceptors, true);
537
637
  const args = await context.usePipe(params.map(({ type, key, validate }) => {
@@ -541,7 +641,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
541
641
  validate
542
642
  };
543
643
  }), reflect);
544
- instance.meta = contextData;
644
+ instance.context = contextData;
545
645
  resolve(await context.usePost(await methodMap[tag](...args)));
546
646
  } catch (e) {
547
647
  const m = Pcontext.metaRecord[tag];
@@ -553,7 +653,7 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
553
653
  res.json(ret2);
554
654
  });
555
655
  }
556
- res.json(await context.useFilter(new NotFoundException("category should be 'parallel' or 'series'")));
656
+ res.json(await context.useFilter(new BadRequestException("category should be 'parallel' or 'series'")));
557
657
  });
558
658
  }
559
659
  __name(bindApp, "bindApp");
@@ -852,6 +952,17 @@ function Header(name, value) {
852
952
  };
853
953
  }
854
954
  __name(Header, "Header");
955
+ function Meta(key, value) {
956
+ return (target, k) => {
957
+ (0, import_phecda_core6.setModalVar)(target, k);
958
+ (0, import_phecda_core6.mergeState)(target, k, {
959
+ meta: {
960
+ [key]: value
961
+ }
962
+ });
963
+ };
964
+ }
965
+ __name(Meta, "Meta");
855
966
 
856
967
  // src/index.ts
857
968
  __reExport(src_exports, require("phecda-core"), module.exports);
@@ -1010,23 +1121,30 @@ __name(createMqReq, "createMqReq");
1010
1121
  // Annotate the CommonJS export names for ESM import in node:
1011
1122
  0 && (module.exports = {
1012
1123
  $S,
1124
+ BadGatewayException,
1013
1125
  BadRequestException,
1126
+ Base,
1014
1127
  BaseParam,
1015
1128
  Body,
1129
+ ConflictException,
1016
1130
  Controller,
1017
1131
  Delete,
1018
1132
  Factory,
1019
1133
  ForbiddenException,
1134
+ FrameworkException,
1020
1135
  Get,
1021
1136
  Guard,
1022
1137
  Header,
1023
1138
  HttpException,
1024
1139
  Inject,
1025
1140
  Interceptor,
1141
+ InvalidInputException,
1026
1142
  MQ,
1143
+ Meta,
1027
1144
  Middle,
1028
1145
  NotFoundException,
1029
1146
  Param,
1147
+ PayloadLargeException,
1030
1148
  Pcompiler,
1031
1149
  Pcontext,
1032
1150
  Pmeta,
@@ -1036,9 +1154,12 @@ __name(createMqReq, "createMqReq");
1036
1154
  RabbitMqContext,
1037
1155
  Route,
1038
1156
  ServerContext,
1157
+ ServiceUnavailableException,
1158
+ TimeoutException,
1159
+ UnauthorizedException,
1039
1160
  UndefinedException,
1161
+ UnsupportedMediaTypeException,
1040
1162
  ValidateException,
1041
- WrongMetaException,
1042
1163
  addGuard,
1043
1164
  addInterceptor,
1044
1165
  addMiddleware,