phecda-server 6.1.0 → 7.0.0-alpha.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.
Files changed (80) hide show
  1. package/bin/cli.mjs +160 -157
  2. package/dist/{chunk-3H3T54IC.mjs → chunk-5622RBNB.mjs} +3 -4
  3. package/dist/{chunk-NL6QBQAR.js → chunk-BGXSMOLX.js} +20 -21
  4. package/dist/{chunk-J7B24YSE.mjs → chunk-HMVLXNV3.mjs} +5 -14
  5. package/dist/{chunk-TVT62GIX.js → chunk-J5CFUN4V.js} +8 -1
  6. package/dist/{chunk-JDV2XJYE.js → chunk-MBCHNDAY.js} +27 -36
  7. package/dist/{chunk-2CFYUXPM.mjs → chunk-P75VKZJY.mjs} +101 -137
  8. package/dist/chunk-SW5IKE5H.js +503 -0
  9. package/dist/{chunk-G7TD2VB7.mjs → chunk-WHJ5FALK.mjs} +7 -0
  10. package/dist/{core-P1CsH6jz.d.mts → core-BIcUwV18.d.mts} +1 -1
  11. package/dist/{core-BlN0Spy9.d.ts → core-CYwEPfN4.d.ts} +1 -1
  12. package/dist/helper.d.mts +4 -6
  13. package/dist/helper.d.ts +4 -6
  14. package/dist/helper.js +3 -3
  15. package/dist/helper.mjs +2 -2
  16. package/dist/{server → http}/elysia/index.d.mts +5 -5
  17. package/dist/{server → http}/elysia/index.d.ts +5 -5
  18. package/dist/{server → http}/elysia/index.js +80 -62
  19. package/dist/{server → http}/elysia/index.mjs +50 -32
  20. package/dist/{server → http}/express/index.d.mts +5 -5
  21. package/dist/{server → http}/express/index.d.ts +5 -5
  22. package/dist/{server → http}/express/index.js +71 -55
  23. package/dist/{server → http}/express/index.mjs +42 -26
  24. package/dist/{server → http}/fastify/index.d.mts +5 -5
  25. package/dist/{server → http}/fastify/index.d.ts +5 -5
  26. package/dist/{server → http}/fastify/index.js +72 -72
  27. package/dist/{server → http}/fastify/index.mjs +47 -47
  28. package/dist/{server → http}/h3/index.d.mts +5 -5
  29. package/dist/{server → http}/h3/index.d.ts +5 -5
  30. package/dist/http/h3/index.js +160 -0
  31. package/dist/{server → http}/h3/index.mjs +68 -62
  32. package/dist/{server → http}/hono/index.d.mts +5 -5
  33. package/dist/{server → http}/hono/index.d.ts +5 -5
  34. package/dist/{server → http}/hono/index.js +69 -54
  35. package/dist/{server → http}/hono/index.mjs +41 -26
  36. package/dist/{server → http}/hyper-express/index.d.mts +5 -5
  37. package/dist/{server → http}/hyper-express/index.d.ts +5 -5
  38. package/dist/{server → http}/hyper-express/index.js +69 -56
  39. package/dist/{server → http}/hyper-express/index.mjs +41 -28
  40. package/dist/{server → http}/koa/index.d.mts +5 -5
  41. package/dist/{server → http}/koa/index.d.ts +5 -5
  42. package/dist/{server → http}/koa/index.js +71 -55
  43. package/dist/{server → http}/koa/index.mjs +42 -26
  44. package/dist/index.d.mts +51 -40
  45. package/dist/index.d.ts +51 -40
  46. package/dist/index.js +59 -81
  47. package/dist/index.mjs +39 -61
  48. package/dist/{meta-KtfiBIde.d.ts → meta-BXKLFTgG.d.mts} +2 -3
  49. package/dist/{meta-KtfiBIde.d.mts → meta-BXKLFTgG.d.ts} +2 -3
  50. package/dist/rpc/bullmq/index.d.mts +6 -6
  51. package/dist/rpc/bullmq/index.d.ts +6 -6
  52. package/dist/rpc/bullmq/index.js +24 -24
  53. package/dist/rpc/bullmq/index.mjs +14 -14
  54. package/dist/rpc/kafka/index.d.mts +3 -3
  55. package/dist/rpc/kafka/index.d.ts +3 -3
  56. package/dist/rpc/kafka/index.js +25 -21
  57. package/dist/rpc/kafka/index.mjs +15 -11
  58. package/dist/rpc/nats/index.d.mts +3 -3
  59. package/dist/rpc/nats/index.d.ts +3 -3
  60. package/dist/rpc/nats/index.js +22 -21
  61. package/dist/rpc/nats/index.mjs +13 -12
  62. package/dist/rpc/rabbitmq/index.d.mts +3 -3
  63. package/dist/rpc/rabbitmq/index.d.ts +3 -3
  64. package/dist/rpc/rabbitmq/index.js +23 -22
  65. package/dist/rpc/rabbitmq/index.mjs +12 -11
  66. package/dist/rpc/redis/index.d.mts +3 -3
  67. package/dist/rpc/redis/index.d.ts +3 -3
  68. package/dist/rpc/redis/index.js +24 -20
  69. package/dist/rpc/redis/index.mjs +15 -11
  70. package/dist/test.d.mts +3 -3
  71. package/dist/test.d.ts +3 -3
  72. package/dist/test.js +6 -6
  73. package/dist/test.mjs +2 -2
  74. package/dist/{types-JVT0mgLD.d.ts → types-BtbL49Zs.d.mts} +2 -1
  75. package/dist/{types-DIw7B6Up.d.ts → types-VFzEM7LL.d.ts} +5 -5
  76. package/dist/{types-BvD3B5Ny.d.mts → types-h40T3cRG.d.mts} +5 -5
  77. package/dist/{types-t9D1fU6G.d.mts → types-m3IEDKjP.d.ts} +2 -1
  78. package/package.json +23 -23
  79. package/dist/chunk-DVTBLV6I.js +0 -539
  80. package/dist/server/h3/index.js +0 -154
@@ -5,55 +5,62 @@
5
5
 
6
6
 
7
7
 
8
- var _chunkDVTBLV6Ijs = require('../../chunk-DVTBLV6I.js');
8
+ var _chunkSW5IKE5Hjs = require('../../chunk-SW5IKE5H.js');
9
9
 
10
10
 
11
- var _chunkTVT62GIXjs = require('../../chunk-TVT62GIX.js');
11
+ var _chunkJ5CFUN4Vjs = require('../../chunk-J5CFUN4V.js');
12
12
 
13
- // src/server/koa/bind.ts
13
+ // src/http/koa/bind.ts
14
+ var _router = require('@koa/router'); var _router2 = _interopRequireDefault(_router);
14
15
  var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
15
16
  var debug = _debug2.default.call(void 0, "phecda-server/koa");
16
17
  function bind(router, data, opts = {}) {
17
- const { globalGuards, globalInterceptors, parallelRoute = "/__PHECDA_SERVER__", globalPlugins = [], parallelPlugins = [], globalFilter, globalPipe } = opts;
18
+ const { globalGuards, parallelRoute = "/__PHECDA_SERVER__", globalAddons = [], parallelAddons = [], globalFilter, globalPipe, dynamic = false } = opts;
18
19
  const { moduleMap, meta } = data;
19
20
  const originStack = router.stack.slice(0, router.stack.length);
20
- const metaMap = _chunkDVTBLV6Ijs.createControllerMetaMap.call(void 0, meta, (meta2) => {
21
+ const metaMap = _chunkSW5IKE5Hjs.createControllerMetaMap.call(void 0, meta, (meta2) => {
21
22
  const { controller, http, func, tag } = meta2.data;
22
23
  if (controller === "http" && _optionalChain([http, 'optionalAccess', _ => _.type])) {
23
24
  debug(`register method "${func}" in module "${tag}"`);
24
25
  return true;
25
26
  }
26
27
  });
27
- _chunkDVTBLV6Ijs.detectAopDep.call(void 0, meta, {
28
- plugins: [
29
- ...globalPlugins,
30
- ...parallelPlugins
28
+ _chunkSW5IKE5Hjs.detectAopDep.call(void 0, meta, {
29
+ addons: [
30
+ ...globalAddons,
31
+ ...parallelAddons
31
32
  ],
32
- guards: globalGuards,
33
- interceptors: globalInterceptors
33
+ guards: globalGuards
34
34
  });
35
35
  registerRoute();
36
- async function registerRoute() {
37
- _chunkDVTBLV6Ijs.Context.usePlugin(globalPlugins, "koa").forEach((p) => router.use(p));
36
+ function registerRoute() {
37
+ _chunkSW5IKE5Hjs.Context.applyAddons(globalAddons, router, "koa");
38
38
  if (parallelRoute) {
39
- router.post(parallelRoute, ..._chunkDVTBLV6Ijs.Context.usePlugin(parallelPlugins, "koa"), async (ctx, next) => {
39
+ const subRouter = new (0, _router2.default)();
40
+ _chunkSW5IKE5Hjs.Context.applyAddons(parallelAddons, subRouter, "koa");
41
+ subRouter.post(parallelRoute, async (ctx, next) => {
40
42
  const { body } = ctx.request;
41
43
  async function errorHandler(e) {
42
- const error = await _chunkDVTBLV6Ijs.Context.filterRecord.default(e);
44
+ const error = await _chunkSW5IKE5Hjs.Context.filterRecord.default(e);
43
45
  ctx.status = error.status;
44
46
  ctx.body = error;
45
47
  }
46
- _chunkTVT62GIXjs.__name.call(void 0, errorHandler, "errorHandler");
47
- if (!Array.isArray(body)) return errorHandler(new (0, _chunkDVTBLV6Ijs.BadRequestException)("data format should be an array"));
48
+ _chunkJ5CFUN4Vjs.__name.call(void 0, errorHandler, "errorHandler");
49
+ if (!Array.isArray(body)) return errorHandler(new (0, _chunkSW5IKE5Hjs.BadRequestException)("data format should be an array"));
48
50
  try {
49
51
  return Promise.all(body.map((item, i) => {
50
52
  return new Promise(async (resolve) => {
51
53
  const { tag, func } = item;
52
54
  debug(`(parallel)invoke method "${func}" in module "${tag}"`);
53
- if (!metaMap.has(tag)) return resolve(await _chunkDVTBLV6Ijs.Context.filterRecord.default(new (0, _chunkDVTBLV6Ijs.BadRequestException)(`module "${tag}" doesn't exist`)));
55
+ if (!metaMap.has(tag)) return resolve(await _chunkSW5IKE5Hjs.Context.filterRecord.default(new (0, _chunkSW5IKE5Hjs.BadRequestException)(`module "${tag}" doesn't exist`)));
54
56
  const meta2 = metaMap.get(tag)[func];
55
- if (!meta2) return resolve(await _chunkDVTBLV6Ijs.Context.filterRecord.default(new (0, _chunkDVTBLV6Ijs.BadRequestException)(`"${func}" in "${tag}" doesn't exist`)));
57
+ if (!meta2) return resolve(await _chunkSW5IKE5Hjs.Context.filterRecord.default(new (0, _chunkSW5IKE5Hjs.BadRequestException)(`"${func}" in "${tag}" doesn't exist`)));
56
58
  const { data: { params } } = meta2;
59
+ const aop = _chunkSW5IKE5Hjs.Context.getAop(meta2, {
60
+ globalGuards,
61
+ globalFilter,
62
+ globalPipe
63
+ });
57
64
  const contextData = {
58
65
  type: "koa",
59
66
  index: i,
@@ -63,27 +70,22 @@ function bind(router, data, opts = {}) {
63
70
  parallel: true,
64
71
  next,
65
72
  app: router,
66
- ..._chunkDVTBLV6Ijs.argToReq.call(void 0, params, item.args, ctx.headers),
73
+ ..._chunkSW5IKE5Hjs.argToReq.call(void 0, params, item.args, ctx.headers),
67
74
  tag,
68
75
  func,
69
- getCookie: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (key) => ctx.cookies.get(key), "getCookie"),
70
- setCookie: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (key, value, opts2) => ctx.cookies.set(key, value, opts2), "setCookie"),
71
- delCookie: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (key) => ctx.cookies.set(key, "", {
76
+ getCookie: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (key) => ctx.cookies.get(key), "getCookie"),
77
+ setCookie: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (key, value, opts2) => ctx.cookies.set(key, value, opts2), "setCookie"),
78
+ delCookie: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (key) => ctx.cookies.set(key, "", {
72
79
  expires: /* @__PURE__ */ new Date(0)
73
80
  }), "delCookie"),
74
- redirect: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (url) => ctx.redirect(url), "redirect"),
75
- setResHeaders: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (headers) => ctx.set(headers), "setResHeaders"),
76
- setResStatus: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (status) => ctx.status = status, "setResStatus"),
77
- getRequest: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, () => ctx.req, "getRequest"),
78
- getResponse: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, () => ctx.res, "getResponse")
81
+ redirect: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (url) => ctx.redirect(url), "redirect"),
82
+ setResHeaders: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (headers) => ctx.set(headers), "setResHeaders"),
83
+ setResStatus: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (status) => ctx.status = status, "setResStatus"),
84
+ getRequest: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, () => ctx.req, "getRequest"),
85
+ getResponse: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, () => ctx.res, "getResponse")
79
86
  };
80
- const context = new (0, _chunkDVTBLV6Ijs.Context)(contextData);
81
- context.run({
82
- globalGuards,
83
- globalInterceptors,
84
- globalFilter,
85
- globalPipe
86
- }, resolve, resolve);
87
+ const context = new (0, _chunkSW5IKE5Hjs.Context)(contextData);
88
+ context.run(aop, resolve, resolve);
87
89
  });
88
90
  })).then((ret) => {
89
91
  ctx.body = ret;
@@ -92,13 +94,24 @@ function bind(router, data, opts = {}) {
92
94
  return errorHandler(e);
93
95
  }
94
96
  });
97
+ router.use(subRouter.routes()).use(subRouter.allowedMethods());
95
98
  }
96
99
  for (const [tag, record] of metaMap) {
97
100
  for (const func in record) {
98
101
  const meta2 = metaMap.get(tag)[func];
99
- const { data: { plugins, http } } = meta2;
102
+ const { data: { addons, http } } = meta2;
100
103
  if (!_optionalChain([http, 'optionalAccess', _2 => _2.type])) continue;
101
- router[http.type](http.prefix + http.route, ..._chunkDVTBLV6Ijs.Context.usePlugin(plugins, "koa"), async (ctx, next) => {
104
+ let aop;
105
+ if (!dynamic) {
106
+ aop = _chunkSW5IKE5Hjs.Context.getAop(meta2, {
107
+ globalFilter,
108
+ globalGuards,
109
+ globalPipe
110
+ });
111
+ }
112
+ const subRouter = new (0, _router2.default)();
113
+ _chunkSW5IKE5Hjs.Context.applyAddons(addons, subRouter, "koa");
114
+ router[http.type](http.prefix + http.route, async (ctx, next) => {
102
115
  debug(`invoke method "${func}" in module "${tag}"`);
103
116
  const contextData = {
104
117
  type: "koa",
@@ -113,25 +126,27 @@ function bind(router, data, opts = {}) {
113
126
  body: ctx.request.body,
114
127
  headers: ctx.headers,
115
128
  next,
116
- getCookie: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (key) => ctx.cookies.get(key), "getCookie"),
117
- setCookie: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (key, value, opts2) => ctx.cookies.set(key, value, opts2), "setCookie"),
118
- delCookie: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (key) => ctx.cookies.set(key, "", {
129
+ getCookie: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (key) => ctx.cookies.get(key), "getCookie"),
130
+ setCookie: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (key, value, opts2) => ctx.cookies.set(key, value, opts2), "setCookie"),
131
+ delCookie: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (key) => ctx.cookies.set(key, "", {
119
132
  expires: /* @__PURE__ */ new Date(0)
120
133
  }), "delCookie"),
121
- redirect: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (url) => ctx.redirect(url), "redirect"),
122
- setResHeaders: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (headers) => ctx.set(headers), "setResHeaders"),
123
- setResStatus: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, (status) => ctx.status = status, "setResStatus"),
124
- getRequest: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, () => ctx.req, "getRequest"),
125
- getResponse: /* @__PURE__ */ _chunkTVT62GIXjs.__name.call(void 0, () => ctx.res, "getResponse")
134
+ redirect: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (url) => ctx.redirect(url), "redirect"),
135
+ setResHeaders: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (headers) => ctx.set(headers), "setResHeaders"),
136
+ setResStatus: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, (status) => ctx.status = status, "setResStatus"),
137
+ getRequest: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, () => ctx.req, "getRequest"),
138
+ getResponse: /* @__PURE__ */ _chunkJ5CFUN4Vjs.__name.call(void 0, () => ctx.res, "getResponse")
126
139
  };
127
- const context = new (0, _chunkDVTBLV6Ijs.Context)(contextData);
140
+ const context = new (0, _chunkSW5IKE5Hjs.Context)(contextData);
128
141
  if (http.headers) ctx.set(http.headers);
129
- await context.run({
130
- globalGuards,
131
- globalInterceptors,
132
- globalFilter,
133
- globalPipe
134
- }, (returnData) => {
142
+ if (dynamic) {
143
+ aop = _chunkSW5IKE5Hjs.Context.getAop(meta2, {
144
+ globalFilter,
145
+ globalGuards,
146
+ globalPipe
147
+ });
148
+ }
149
+ await context.run(aop, (returnData) => {
135
150
  if (ctx.res.writableEnded) return;
136
151
  ctx.body = returnData;
137
152
  }, (err) => {
@@ -140,16 +155,17 @@ function bind(router, data, opts = {}) {
140
155
  ctx.body = err;
141
156
  });
142
157
  });
158
+ router.use(subRouter.routes()).use(subRouter.allowedMethods());
143
159
  }
144
160
  }
145
161
  }
146
- _chunkTVT62GIXjs.__name.call(void 0, registerRoute, "registerRoute");
147
- _chunkDVTBLV6Ijs.HMR.call(void 0, async () => {
162
+ _chunkJ5CFUN4Vjs.__name.call(void 0, registerRoute, "registerRoute");
163
+ _chunkSW5IKE5Hjs.HMR.call(void 0, async () => {
148
164
  router.stack = originStack;
149
165
  registerRoute();
150
166
  });
151
167
  }
152
- _chunkTVT62GIXjs.__name.call(void 0, bind, "bind");
168
+ _chunkJ5CFUN4Vjs.__name.call(void 0, bind, "bind");
153
169
 
154
170
 
155
171
  exports.bind = bind;
@@ -5,16 +5,17 @@ import {
5
5
  argToReq,
6
6
  createControllerMetaMap,
7
7
  detectAopDep
8
- } from "../../chunk-2CFYUXPM.mjs";
8
+ } from "../../chunk-P75VKZJY.mjs";
9
9
  import {
10
10
  __name
11
- } from "../../chunk-G7TD2VB7.mjs";
11
+ } from "../../chunk-WHJ5FALK.mjs";
12
12
 
13
- // src/server/koa/bind.ts
13
+ // src/http/koa/bind.ts
14
+ import Router from "@koa/router";
14
15
  import Debug from "debug";
15
16
  var debug = Debug("phecda-server/koa");
16
17
  function bind(router, data, opts = {}) {
17
- const { globalGuards, globalInterceptors, parallelRoute = "/__PHECDA_SERVER__", globalPlugins = [], parallelPlugins = [], globalFilter, globalPipe } = opts;
18
+ const { globalGuards, parallelRoute = "/__PHECDA_SERVER__", globalAddons = [], parallelAddons = [], globalFilter, globalPipe, dynamic = false } = opts;
18
19
  const { moduleMap, meta } = data;
19
20
  const originStack = router.stack.slice(0, router.stack.length);
20
21
  const metaMap = createControllerMetaMap(meta, (meta2) => {
@@ -25,18 +26,19 @@ function bind(router, data, opts = {}) {
25
26
  }
26
27
  });
27
28
  detectAopDep(meta, {
28
- plugins: [
29
- ...globalPlugins,
30
- ...parallelPlugins
29
+ addons: [
30
+ ...globalAddons,
31
+ ...parallelAddons
31
32
  ],
32
- guards: globalGuards,
33
- interceptors: globalInterceptors
33
+ guards: globalGuards
34
34
  });
35
35
  registerRoute();
36
- async function registerRoute() {
37
- Context.usePlugin(globalPlugins, "koa").forEach((p) => router.use(p));
36
+ function registerRoute() {
37
+ Context.applyAddons(globalAddons, router, "koa");
38
38
  if (parallelRoute) {
39
- router.post(parallelRoute, ...Context.usePlugin(parallelPlugins, "koa"), async (ctx, next) => {
39
+ const subRouter = new Router();
40
+ Context.applyAddons(parallelAddons, subRouter, "koa");
41
+ subRouter.post(parallelRoute, async (ctx, next) => {
40
42
  const { body } = ctx.request;
41
43
  async function errorHandler(e) {
42
44
  const error = await Context.filterRecord.default(e);
@@ -54,6 +56,11 @@ function bind(router, data, opts = {}) {
54
56
  const meta2 = metaMap.get(tag)[func];
55
57
  if (!meta2) return resolve(await Context.filterRecord.default(new BadRequestException(`"${func}" in "${tag}" doesn't exist`)));
56
58
  const { data: { params } } = meta2;
59
+ const aop = Context.getAop(meta2, {
60
+ globalGuards,
61
+ globalFilter,
62
+ globalPipe
63
+ });
57
64
  const contextData = {
58
65
  type: "koa",
59
66
  index: i,
@@ -78,12 +85,7 @@ function bind(router, data, opts = {}) {
78
85
  getResponse: /* @__PURE__ */ __name(() => ctx.res, "getResponse")
79
86
  };
80
87
  const context = new Context(contextData);
81
- context.run({
82
- globalGuards,
83
- globalInterceptors,
84
- globalFilter,
85
- globalPipe
86
- }, resolve, resolve);
88
+ context.run(aop, resolve, resolve);
87
89
  });
88
90
  })).then((ret) => {
89
91
  ctx.body = ret;
@@ -92,13 +94,24 @@ function bind(router, data, opts = {}) {
92
94
  return errorHandler(e);
93
95
  }
94
96
  });
97
+ router.use(subRouter.routes()).use(subRouter.allowedMethods());
95
98
  }
96
99
  for (const [tag, record] of metaMap) {
97
100
  for (const func in record) {
98
101
  const meta2 = metaMap.get(tag)[func];
99
- const { data: { plugins, http } } = meta2;
102
+ const { data: { addons, http } } = meta2;
100
103
  if (!http?.type) continue;
101
- router[http.type](http.prefix + http.route, ...Context.usePlugin(plugins, "koa"), async (ctx, next) => {
104
+ let aop;
105
+ if (!dynamic) {
106
+ aop = Context.getAop(meta2, {
107
+ globalFilter,
108
+ globalGuards,
109
+ globalPipe
110
+ });
111
+ }
112
+ const subRouter = new Router();
113
+ Context.applyAddons(addons, subRouter, "koa");
114
+ router[http.type](http.prefix + http.route, async (ctx, next) => {
102
115
  debug(`invoke method "${func}" in module "${tag}"`);
103
116
  const contextData = {
104
117
  type: "koa",
@@ -126,12 +139,14 @@ function bind(router, data, opts = {}) {
126
139
  };
127
140
  const context = new Context(contextData);
128
141
  if (http.headers) ctx.set(http.headers);
129
- await context.run({
130
- globalGuards,
131
- globalInterceptors,
132
- globalFilter,
133
- globalPipe
134
- }, (returnData) => {
142
+ if (dynamic) {
143
+ aop = Context.getAop(meta2, {
144
+ globalFilter,
145
+ globalGuards,
146
+ globalPipe
147
+ });
148
+ }
149
+ await context.run(aop, (returnData) => {
135
150
  if (ctx.res.writableEnded) return;
136
151
  ctx.body = returnData;
137
152
  }, (err) => {
@@ -140,6 +155,7 @@ function bind(router, data, opts = {}) {
140
155
  ctx.body = err;
141
156
  });
142
157
  });
158
+ router.use(subRouter.routes()).use(subRouter.allowedMethods());
143
159
  }
144
160
  }
145
161
  }
package/dist/index.d.mts CHANGED
@@ -1,13 +1,13 @@
1
- import { B as BaseContext, D as DefaultOptions, C as ControllerMetaData, b as MetaData, E as Emitter, c as BaseError } from './meta-KtfiBIde.mjs';
2
- export { g as BaseRequestType, f as BaseReturn, a as ControllerMeta, h as CustomResponse, j as ERROR_SYMBOL, i as ExtractResponse, I as IS_HMR, k as IS_ONLY_GENERATE, l as IS_STRICT, L as LOG_LEVEL, M as Meta, O as OmitFunction, m as PS_EXIT_CODE, P as PickFunc, S as ServiceMetaData, e as ToClientFn, d as ToClientInstance, T as ToClientMap } from './meta-KtfiBIde.mjs';
3
- import { G as Generator } from './core-P1CsH6jz.mjs';
4
- export { F as Factory, O as Options, S as ServerPhecda, d as defaultServerInject, e as emitter, p as phecdaNamespace, u as useS } from './core-P1CsH6jz.mjs';
1
+ import { B as BaseContext, a as ControllerMeta, D as DefaultOptions, C as ControllerMetaData, b as MetaData, E as Emitter, c as BaseError } from './meta-BXKLFTgG.mjs';
2
+ export { g as BaseRequestType, f as BaseReturn, h as CustomResponse, j as ERROR_SYMBOL, i as ExtractResponse, I as IS_HMR, k as IS_ONLY_GENERATE, l as IS_STRICT, L as LOG_LEVEL, M as Meta, O as OmitFunction, m as PS_EXIT_CODE, P as PickFunc, S as ServiceMetaData, e as ToClientFn, d as ToClientInstance, T as ToClientMap } from './meta-BXKLFTgG.mjs';
3
+ import { G as Generator } from './core-BIcUwV18.mjs';
4
+ export { F as Factory, O as Options, S as ServerPhecda, d as defaultServerInject, e as emitter, p as phecdaNamespace, u as useS } from './core-BIcUwV18.mjs';
5
5
  import { Base } from 'phecda-core';
6
6
  export * from 'phecda-core';
7
- import { H as HttpContext } from './types-BvD3B5Ny.mjs';
8
- export { C as CookieSerializeOptions, a as HttpOptions } from './types-BvD3B5Ny.mjs';
9
- import { R as RpcContext } from './types-t9D1fU6G.mjs';
10
- export { b as RpcClientOptions, a as RpcServerOptions } from './types-t9D1fU6G.mjs';
7
+ import { H as HttpContext } from './types-h40T3cRG.mjs';
8
+ export { C as CookieSerializeOptions, a as HttpOptions } from './types-h40T3cRG.mjs';
9
+ import { R as RpcContext } from './types-BtbL49Zs.mjs';
10
+ export { b as RpcClientOptions, a as RpcServerOptions } from './types-BtbL49Zs.mjs';
11
11
  export { Mixin } from 'ts-mixer';
12
12
  import 'node:http';
13
13
 
@@ -88,6 +88,11 @@ declare class WorkerException extends Exception {
88
88
  constructor(message: string);
89
89
  }
90
90
 
91
+ interface AOP {
92
+ guards: GuardType[];
93
+ pipe: PipeType[];
94
+ filter: FilterType;
95
+ }
91
96
  interface PipeArg {
92
97
  arg: any;
93
98
  pipe?: string;
@@ -97,8 +102,7 @@ interface PipeArg {
97
102
  reflect: any;
98
103
  define: Record<string, any>;
99
104
  }
100
- type GuardType<C extends BaseContext = any> = ((ctx: C) => Promise<boolean> | boolean);
101
- type InterceptorType<C extends BaseContext = any> = (ctx: C) => (any | ((ret: any) => any));
105
+ type GuardType<C extends BaseContext = any> = (ctx: C, next: () => Promise<any>) => any;
102
106
  type PipeType<C extends BaseContext = any> = (arg: PipeArg, ctx: C) => Promise<any>;
103
107
  type FilterType<C extends BaseContext = any, E extends Exception = any> = (err: E | Error, ctx?: C) => Error | any;
104
108
  declare class Context<Data extends BaseContext> {
@@ -107,24 +111,34 @@ declare class Context<Data extends BaseContext> {
107
111
  params: string[];
108
112
  static filterRecord: Record<PropertyKey, FilterType>;
109
113
  static pipeRecord: Record<PropertyKey, PipeType>;
110
- static guardRecord: Record<PropertyKey, GuardType>;
111
- static interceptorRecord: Record<PropertyKey, InterceptorType>;
112
- static pluginRecord: Record<PropertyKey, (framework: string) => any>;
113
- private postInterceptors;
114
+ static guardRecord: Record<PropertyKey, {
115
+ value: GuardType;
116
+ priority: number;
117
+ }>;
118
+ static addonRecord: Record<PropertyKey, {
119
+ value: (router: any, framework: string) => any;
120
+ priority: number;
121
+ }>;
114
122
  constructor(data: Data);
115
- run<ReturnData = any, ReturnErr = any>(opts: DefaultOptions, successCb: (data: any) => ReturnData, failCb: (err: any) => ReturnErr): Promise<ReturnData | ReturnErr>;
116
- private usePipe;
117
- private useFilter;
118
- private useGuard;
119
- private usePostInterceptor;
120
- private useInterceptor;
121
- static usePlugin<Plugin>(plugins: string[], framework: string): Plugin[];
122
- }
123
- declare function addPlugin<T>(key: PropertyKey, handler: (framework: string) => T): void;
124
- declare function addPipe<C extends BaseContext>(key: PropertyKey, handler: PipeType<C>): void;
125
- declare function addFilter<C extends BaseContext>(key: PropertyKey, handler: FilterType<C>): void;
126
- declare function addGuard<C extends BaseContext>(key: PropertyKey, handler: GuardType<C>): void;
127
- declare function addInterceptor<C extends BaseContext>(key: PropertyKey, handler: InterceptorType<C>): void;
123
+ static getAop(meta: ControllerMeta, opts: DefaultOptions): {
124
+ guards: GuardType<any>[];
125
+ pipe: PipeType<any>[];
126
+ filter: FilterType<any, any>;
127
+ };
128
+ run<ResponseData = any, ReturnErr = any>({ guards, filter, pipe, }: {
129
+ guards: GuardType[];
130
+ filter: FilterType;
131
+ pipe: PipeType[];
132
+ }, successCb: (data: any) => ResponseData, failCb: (err: any) => ReturnErr): Promise<ResponseData | ReturnErr>;
133
+ static getPipe(pipe: string[]): PipeType<any>[];
134
+ static getFilter(filter?: string): FilterType<any, any>;
135
+ static getGuards(guards: string[]): GuardType<any>[];
136
+ static applyAddons(addons: string[], router: any, framework: string): void;
137
+ }
138
+ declare function addPipe<C extends BaseContext>(key: PropertyKey, pipe: PipeType<C>): void;
139
+ declare function addFilter<C extends BaseContext>(key: PropertyKey, filter: FilterType<C>): void;
140
+ declare function addGuard<C extends BaseContext>(key: PropertyKey, guard: GuardType<C>, priority?: number): void;
141
+ declare function addAddon<T>(key: PropertyKey, addon: (router: any, framework: string) => T, priority?: number): void;
128
142
 
129
143
  declare class RPCGenerator extends Generator {
130
144
  name: string;
@@ -154,8 +168,7 @@ declare function Param(key: string): ParameterDecorator;
154
168
  declare function Arg(target: any, k: string, index: number): void;
155
169
 
156
170
  declare function Guard(...guards: string[]): (target: any, property?: PropertyKey) => void;
157
- declare function Plugin(...plugins: string[]): (target: any, property?: PropertyKey) => void;
158
- declare function Interceptor(...interceptors: string[]): (target: any, property?: PropertyKey) => void;
171
+ declare function Addon(...addons: string[]): (target: any, property?: PropertyKey) => void;
159
172
  declare function Filter(filter: string): (target: any, property?: PropertyKey) => void;
160
173
  declare function Pipe(pipe?: string): (target: any, property?: any, index?: any) => void;
161
174
 
@@ -196,12 +209,7 @@ declare abstract class PFilter<C extends BaseContext = any, E extends Exception
196
209
  declare abstract class PGuard<C extends BaseContext = any> extends ServerBase {
197
210
  abstract use(ctx: C): Promise<boolean> | boolean;
198
211
  readonly key: PropertyKey;
199
- constructor(tag?: string);
200
- }
201
-
202
- declare abstract class PInterceptor<C extends BaseContext = any> extends ServerBase {
203
- abstract use(ctx: C): Function | Promise<Function> | any;
204
- readonly key: PropertyKey;
212
+ priority: number;
205
213
  constructor(tag?: string);
206
214
  }
207
215
 
@@ -218,14 +226,14 @@ declare abstract class PPipe<C extends BaseContext = any> extends ServerBase {
218
226
  }, ctx: C): any;
219
227
  }
220
228
 
221
- declare abstract class PPlugin extends ServerBase {
229
+ declare abstract class PAddon extends ServerBase {
222
230
  readonly key: PropertyKey;
231
+ priority: number;
223
232
  constructor(tag?: string);
224
- abstract use<Plugin = any>(framework: string): Plugin | undefined;
233
+ abstract use<Addon = any>(framework: string): Addon | undefined;
225
234
  }
226
235
 
227
236
  interface PExtension<C extends BaseContext = any, E extends Exception = Exception> {
228
- intercept(ctx: C): Function | Promise<Function> | any;
229
237
  guard(ctx: C): Promise<boolean> | boolean;
230
238
  pipe(param: {
231
239
  arg: any;
@@ -236,10 +244,12 @@ interface PExtension<C extends BaseContext = any, E extends Exception = Exceptio
236
244
  reflect: any;
237
245
  }, ctx: C): any;
238
246
  filter(error: Error | E, ctx?: C): BaseError;
239
- plugin<Plugin = any>(framework: string): Plugin;
247
+ addon<Addon = any>(framework: string): Addon;
240
248
  }
241
249
  declare class PExtension extends ServerBase {
242
250
  readonly key: PropertyKey;
251
+ guardPriority: number;
252
+ addonPriority: number;
243
253
  constructor(tag?: string);
244
254
  }
245
255
 
@@ -247,5 +257,6 @@ type LogLevel = 'error' | 'info' | 'warn' | 'log' | 'debug';
247
257
  declare function setLogger(logger: Record<LogLevel, (msg: string) => void>): void;
248
258
  declare function getLogger(): Record<LogLevel, (msg: string) => void>;
249
259
  declare function log(msg: string, level?: LogLevel): void;
260
+ declare function runMiddleware(ctx: HttpContext, middleware: (req: any, res: any, next?: any) => any): Promise<unknown>;
250
261
 
251
- export { Arg, BadGatewayException, BadRequestException, BaseContext, BaseError, BaseParam, Body, ConflictException, Context, Controller, ControllerMetaData, Ctx, DefaultOptions, Define, Delete, Emitter, Exception, Filter, type FilterType, ForbiddenException, FrameworkException, Generator, Get, Guard, type GuardType, HTTPGenerator, Head, Header, HttpBase, HttpContext, Interceptor, type InterceptorType, InvalidInputException, MetaData, NotFoundException, PExtension, PFilter, PGuard, PInterceptor, PPipe, PPlugin, Param, Patch, PayloadLargeException, Pipe, type PipeArg, type PipeType, Plugin, Post, Put, Query, Queue, RPCGenerator, Route, Rpc, RpcBase, RpcContext, Search, ServerBase, ServiceUnavailableException, TimeoutException, TimerException, UnauthorizedException, UndefinedException, UnsupportedMediaTypeException, ValidateException, WorkerException, addFilter, addGuard, addInterceptor, addPipe, addPlugin, defaultPipe, getLogger, log, setLogger };
262
+ export { type AOP, Addon, Arg, BadGatewayException, BadRequestException, BaseContext, BaseError, BaseParam, Body, ConflictException, Context, Controller, ControllerMeta, ControllerMetaData, Ctx, DefaultOptions, Define, Delete, Emitter, Exception, Filter, type FilterType, ForbiddenException, FrameworkException, Generator, Get, Guard, type GuardType, HTTPGenerator, Head, Header, HttpBase, HttpContext, InvalidInputException, MetaData, NotFoundException, PAddon, PExtension, PFilter, PGuard, PPipe, Param, Patch, PayloadLargeException, Pipe, type PipeArg, type PipeType, Post, Put, Query, Queue, RPCGenerator, Route, Rpc, RpcBase, RpcContext, Search, ServerBase, ServiceUnavailableException, TimeoutException, TimerException, UnauthorizedException, UndefinedException, UnsupportedMediaTypeException, ValidateException, WorkerException, addAddon, addFilter, addGuard, addPipe, defaultPipe, getLogger, log, runMiddleware, setLogger };