phecda-server 5.0.2 → 5.1.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/README.md +4 -102
- package/bin/cli.mjs +17 -14
- package/dist/{chunk-RCTOVYZY.mjs → chunk-2HKQPZDT.mjs} +1 -1
- package/dist/{chunk-BXLBWRHS.js → chunk-3BV2GRS7.js} +38 -38
- package/dist/{chunk-AB4OX3WV.mjs → chunk-665MB62T.mjs} +127 -99
- package/dist/{chunk-YERBWZCS.js → chunk-FSBD5R22.js} +180 -152
- package/dist/{chunk-F5YWXY5W.js → chunk-HMPTPTFL.js} +17 -17
- package/dist/{chunk-W5EOVGQD.mjs → chunk-UU6RHGRF.mjs} +2 -2
- package/dist/{chunk-ITTD2GBR.mjs → chunk-VLV3AO3H.mjs} +7 -19
- package/dist/{chunk-YR5Q5F2K.js → chunk-ZP7HNASU.js} +7 -19
- package/dist/{core-fd134ffa.d.ts → core-39f27fe8.d.ts} +4 -3
- package/dist/{helper-73e8d2f0.d.ts → helper-6133f78f.d.ts} +1 -1
- package/dist/{helper-f29f082f.d.ts → helper-fcbf6aa8.d.ts} +4 -3
- package/dist/index.d.ts +27 -32
- package/dist/index.js +44 -40
- package/dist/index.mjs +14 -10
- package/dist/rpc/bullmq/index.d.ts +10 -6
- package/dist/rpc/bullmq/index.js +32 -53
- package/dist/rpc/bullmq/index.mjs +26 -47
- package/dist/rpc/kafka/index.d.ts +10 -4
- package/dist/rpc/kafka/index.js +20 -37
- package/dist/rpc/kafka/index.mjs +15 -32
- package/dist/rpc/nats/index.d.ts +2 -2
- package/dist/rpc/nats/index.js +20 -34
- package/dist/rpc/nats/index.mjs +13 -27
- package/dist/rpc/rabbitmq/index.d.ts +2 -2
- package/dist/rpc/rabbitmq/index.js +23 -37
- package/dist/rpc/rabbitmq/index.mjs +14 -28
- package/dist/rpc/redis/index.d.ts +10 -4
- package/dist/rpc/redis/index.js +23 -37
- package/dist/rpc/redis/index.mjs +16 -30
- package/dist/server/elysia/index.d.ts +4 -3
- package/dist/server/elysia/index.js +77 -94
- package/dist/server/elysia/index.mjs +74 -91
- package/dist/server/express/index.d.ts +5 -4
- package/dist/server/express/index.js +79 -93
- package/dist/server/express/index.mjs +77 -91
- package/dist/server/fastify/index.d.ts +8 -5
- package/dist/server/fastify/index.js +83 -102
- package/dist/server/fastify/index.mjs +79 -98
- package/dist/server/h3/index.d.ts +5 -4
- package/dist/server/h3/index.js +81 -97
- package/dist/server/h3/index.mjs +80 -96
- package/dist/server/hono/index.d.ts +5 -4
- package/dist/server/hono/index.js +73 -90
- package/dist/server/hono/index.mjs +72 -89
- package/dist/server/hyper-express/index.d.ts +6 -5
- package/dist/server/hyper-express/index.js +78 -96
- package/dist/server/hyper-express/index.mjs +77 -95
- package/dist/server/koa/index.d.ts +5 -4
- package/dist/server/koa/index.js +79 -92
- package/dist/server/koa/index.mjs +76 -89
- package/dist/test.d.ts +1 -1
- package/dist/test.js +6 -6
- package/dist/test.mjs +2 -2
- package/package.json +9 -8
- package/register/loader.mjs +19 -5
|
@@ -1,98 +1,96 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BadRequestException,
|
|
3
3
|
Context,
|
|
4
|
-
HMR,
|
|
5
4
|
argToReq,
|
|
5
|
+
createControllerMetaMap,
|
|
6
6
|
detectAopDep
|
|
7
|
-
} from "../../chunk-
|
|
7
|
+
} from "../../chunk-665MB62T.mjs";
|
|
8
8
|
import {
|
|
9
9
|
__name
|
|
10
|
-
} from "../../chunk-
|
|
10
|
+
} from "../../chunk-VLV3AO3H.mjs";
|
|
11
11
|
|
|
12
12
|
// src/server/hono/bind.ts
|
|
13
13
|
import Debug from "debug";
|
|
14
14
|
var debug = Debug("phecda-server/hono");
|
|
15
15
|
function bind(router, data, opts = {}) {
|
|
16
|
-
const { globalGuards, globalInterceptors,
|
|
17
|
-
route: "/__PHECDA_SERVER__",
|
|
18
|
-
plugins: [],
|
|
19
|
-
...opts
|
|
20
|
-
};
|
|
16
|
+
const { globalGuards, globalInterceptors, parallelRoute = "/__PHECDA_SERVER__", globalPlugins = [], parallelPlugins = [], globalFilter, globalPipe } = opts;
|
|
21
17
|
const { moduleMap, meta } = data;
|
|
22
|
-
const metaMap =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
for (const item of meta) {
|
|
26
|
-
const { tag, func, controller, http } = item.data;
|
|
27
|
-
if (controller !== "http" || !http?.type)
|
|
28
|
-
continue;
|
|
18
|
+
const metaMap = createControllerMetaMap(meta, (meta2) => {
|
|
19
|
+
const { controller, http, func, tag } = meta2.data;
|
|
20
|
+
if (controller === "http" && http?.type) {
|
|
29
21
|
debug(`register method "${func}" in module "${tag}"`);
|
|
30
|
-
|
|
31
|
-
metaMap.get(tag)[func] = item;
|
|
32
|
-
else
|
|
33
|
-
metaMap.set(tag, {
|
|
34
|
-
[func]: item
|
|
35
|
-
});
|
|
22
|
+
return true;
|
|
36
23
|
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
24
|
+
});
|
|
25
|
+
detectAopDep(meta, {
|
|
26
|
+
plugins: [
|
|
27
|
+
...globalPlugins,
|
|
28
|
+
...parallelPlugins
|
|
29
|
+
],
|
|
30
|
+
guards: globalGuards,
|
|
31
|
+
interceptors: globalInterceptors
|
|
32
|
+
});
|
|
33
|
+
registerRoute();
|
|
34
|
+
async function registerRoute() {
|
|
35
|
+
Context.usePlugin(globalPlugins, "hono").forEach((p) => router.use(p));
|
|
36
|
+
if (parallelRoute) {
|
|
37
|
+
router.post(parallelRoute, ...Context.usePlugin(parallelPlugins, "hono"), async (c) => {
|
|
38
|
+
const body = await c.req.json();
|
|
39
|
+
async function errorHandler(e) {
|
|
40
|
+
const error = await Context.filterRecord.default(e);
|
|
41
|
+
c.status(error.status);
|
|
42
|
+
return c.json(error);
|
|
43
|
+
}
|
|
44
|
+
__name(errorHandler, "errorHandler");
|
|
45
|
+
if (!Array.isArray(body))
|
|
46
|
+
return errorHandler(new BadRequestException("data format should be an array"));
|
|
47
|
+
try {
|
|
48
|
+
return Promise.all(body.map((item, i) => {
|
|
49
|
+
return new Promise(async (resolve) => {
|
|
50
|
+
const { tag, func } = item;
|
|
51
|
+
debug(`(parallel)invoke method "${func}" in module "${tag}"`);
|
|
52
|
+
if (!metaMap.has(tag))
|
|
53
|
+
return resolve(await Context.filterRecord.default(new BadRequestException(`module "${tag}" doesn't exist`)));
|
|
54
|
+
const meta2 = metaMap.get(tag)[func];
|
|
55
|
+
if (!meta2)
|
|
56
|
+
return resolve(await Context.filterRecord.default(new BadRequestException(`"${func}" in "${tag}" doesn't exist`)));
|
|
57
|
+
const { data: { params } } = meta2;
|
|
58
|
+
const contextData = {
|
|
59
|
+
type: "hono",
|
|
60
|
+
parallel: true,
|
|
61
|
+
context: c,
|
|
62
|
+
index: i,
|
|
63
|
+
meta: meta2,
|
|
64
|
+
moduleMap,
|
|
65
|
+
tag,
|
|
66
|
+
func,
|
|
67
|
+
app: router,
|
|
68
|
+
...argToReq(params, item.args, c.req.header())
|
|
69
|
+
};
|
|
70
|
+
const context = new Context(contextData);
|
|
71
|
+
context.run({
|
|
72
|
+
globalGuards,
|
|
73
|
+
globalInterceptors,
|
|
74
|
+
globalFilter,
|
|
75
|
+
globalPipe
|
|
76
|
+
}, resolve, resolve);
|
|
77
|
+
});
|
|
78
|
+
})).then((ret) => {
|
|
79
|
+
return c.json(ret);
|
|
80
80
|
});
|
|
81
|
-
}
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
});
|
|
81
|
+
} catch (e) {
|
|
82
|
+
return errorHandler(e);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
88
86
|
for (const [tag, record] of metaMap) {
|
|
89
87
|
for (const func in record) {
|
|
90
88
|
const meta2 = metaMap.get(tag)[func];
|
|
91
|
-
const { data: { params, plugins
|
|
89
|
+
const { data: { params, plugins, http } } = meta2;
|
|
92
90
|
if (!http?.type)
|
|
93
91
|
continue;
|
|
94
92
|
const needBody = params.some((item) => item.type === "body");
|
|
95
|
-
router[http.type](http.prefix + http.route, ...Context.usePlugin(
|
|
93
|
+
router[http.type](http.prefix + http.route, ...Context.usePlugin(plugins, "hono"), async (c) => {
|
|
96
94
|
debug(`invoke method "${func}" in module "${tag}"`);
|
|
97
95
|
const contextData = {
|
|
98
96
|
type: "hono",
|
|
@@ -132,22 +130,7 @@ function bind(router, data, opts = {}) {
|
|
|
132
130
|
}
|
|
133
131
|
}
|
|
134
132
|
}
|
|
135
|
-
__name(
|
|
136
|
-
detectAopDep(meta, {
|
|
137
|
-
plugins,
|
|
138
|
-
guards: globalGuards,
|
|
139
|
-
interceptors: globalInterceptors
|
|
140
|
-
});
|
|
141
|
-
handleMeta();
|
|
142
|
-
createRoute();
|
|
143
|
-
HMR(async () => {
|
|
144
|
-
detectAopDep(meta, {
|
|
145
|
-
plugins,
|
|
146
|
-
guards: globalGuards,
|
|
147
|
-
interceptors: globalInterceptors
|
|
148
|
-
});
|
|
149
|
-
handleMeta();
|
|
150
|
-
});
|
|
133
|
+
__name(registerRoute, "registerRoute");
|
|
151
134
|
}
|
|
152
135
|
__name(bind, "bind");
|
|
153
136
|
export {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Request, Response, Router } from 'hyper-express';
|
|
2
|
-
import { a as HttpContext, H as HttpOptions } from '../../helper-
|
|
3
|
-
import { F as Factory } from '../../core-
|
|
1
|
+
import { Request, Response, Router, MiddlewareHandler } from 'hyper-express';
|
|
2
|
+
import { a as HttpContext, H as HttpOptions } from '../../helper-fcbf6aa8.js';
|
|
3
|
+
import { F as Factory } from '../../core-39f27fe8.js';
|
|
4
4
|
import 'node:http';
|
|
5
5
|
import 'phecda-core';
|
|
6
6
|
|
|
@@ -11,6 +11,7 @@ interface HyperExpressCtx extends HttpContext {
|
|
|
11
11
|
next: Function;
|
|
12
12
|
app: Router;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
type Plugin = MiddlewareHandler;
|
|
15
|
+
declare function bind(router: Router, data: Awaited<ReturnType<typeof Factory>>, opts?: HttpOptions): void;
|
|
15
16
|
|
|
16
|
-
export { HyperExpressCtx, bind };
|
|
17
|
+
export { HyperExpressCtx, Plugin, bind };
|
|
@@ -4,99 +4,96 @@
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunkFSBD5R22js = require('../../chunk-FSBD5R22.js');
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _chunkZP7HNASUjs = require('../../chunk-ZP7HNASU.js');
|
|
11
11
|
|
|
12
12
|
// src/server/hyper-express/bind.ts
|
|
13
13
|
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
14
14
|
var debug = _debug2.default.call(void 0, "phecda-server/hyper-express");
|
|
15
|
-
function bind(router,
|
|
16
|
-
const { globalGuards, globalInterceptors,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const metaMap = /* @__PURE__ */ new Map();
|
|
22
|
-
function handleMeta() {
|
|
23
|
-
metaMap.clear();
|
|
24
|
-
for (const item of meta) {
|
|
25
|
-
const { tag, func, controller, http } = item.data;
|
|
26
|
-
if (controller !== "http" || !_optionalChain([http, 'optionalAccess', _ => _.type]))
|
|
27
|
-
continue;
|
|
15
|
+
function bind(router, data, opts = {}) {
|
|
16
|
+
const { globalGuards, globalInterceptors, parallelRoute = "/__PHECDA_SERVER__", globalPlugins = [], parallelPlugins = [], globalFilter, globalPipe } = opts;
|
|
17
|
+
const { moduleMap, meta } = data;
|
|
18
|
+
const metaMap = _chunkFSBD5R22js.createControllerMetaMap.call(void 0, meta, (meta2) => {
|
|
19
|
+
const { controller, http, func, tag } = meta2.data;
|
|
20
|
+
if (controller === "http" && _optionalChain([http, 'optionalAccess', _ => _.type])) {
|
|
28
21
|
debug(`register method "${func}" in module "${tag}"`);
|
|
29
|
-
|
|
30
|
-
metaMap.get(tag)[func] = item;
|
|
31
|
-
else
|
|
32
|
-
metaMap.set(tag, {
|
|
33
|
-
[func]: item
|
|
34
|
-
});
|
|
22
|
+
return true;
|
|
35
23
|
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
24
|
+
});
|
|
25
|
+
_chunkFSBD5R22js.detectAopDep.call(void 0, meta, {
|
|
26
|
+
plugins: [
|
|
27
|
+
...globalPlugins,
|
|
28
|
+
...parallelPlugins
|
|
29
|
+
],
|
|
30
|
+
guards: globalGuards,
|
|
31
|
+
interceptors: globalInterceptors
|
|
32
|
+
});
|
|
33
|
+
registerRoute();
|
|
34
|
+
async function registerRoute() {
|
|
35
|
+
_chunkFSBD5R22js.Context.usePlugin(globalPlugins, "hyper-express").forEach((p) => router.use(p));
|
|
36
|
+
if (parallelRoute) {
|
|
37
|
+
router.post(parallelRoute, {
|
|
38
|
+
middlewares: _chunkFSBD5R22js.Context.usePlugin(parallelPlugins, "hyper-express")
|
|
39
|
+
}, async (req, res, next) => {
|
|
40
|
+
const body = await req.json();
|
|
41
|
+
async function errorHandler(e) {
|
|
42
|
+
const error = await _chunkFSBD5R22js.Context.filterRecord.default(e);
|
|
43
|
+
return res.status(error.status).json(error);
|
|
44
|
+
}
|
|
45
|
+
_chunkZP7HNASUjs.__name.call(void 0, errorHandler, "errorHandler");
|
|
46
|
+
if (!Array.isArray(body))
|
|
47
|
+
return errorHandler(new (0, _chunkFSBD5R22js.BadRequestException)("data format should be an array"));
|
|
48
|
+
try {
|
|
49
|
+
return Promise.all(body.map((item, i) => {
|
|
50
|
+
return new Promise(async (resolve) => {
|
|
51
|
+
const { tag, func } = item;
|
|
52
|
+
debug(`(parallel)invoke method "${func}" in module "${tag}"`);
|
|
53
|
+
if (!metaMap.has(tag))
|
|
54
|
+
return resolve(await _chunkFSBD5R22js.Context.filterRecord.default(new (0, _chunkFSBD5R22js.BadRequestException)(`module "${tag}" doesn't exist`)));
|
|
55
|
+
const meta2 = metaMap.get(tag)[func];
|
|
56
|
+
if (!meta2)
|
|
57
|
+
return resolve(await _chunkFSBD5R22js.Context.filterRecord.default(new (0, _chunkFSBD5R22js.BadRequestException)(`"${func}" in "${tag}" doesn't exist`)));
|
|
58
|
+
const { data: { params } } = meta2;
|
|
59
|
+
const contextData = {
|
|
60
|
+
type: "hyper-express",
|
|
61
|
+
parallel: true,
|
|
62
|
+
request: req,
|
|
63
|
+
index: i,
|
|
64
|
+
meta: meta2,
|
|
65
|
+
response: res,
|
|
66
|
+
moduleMap,
|
|
67
|
+
tag,
|
|
68
|
+
func,
|
|
69
|
+
next,
|
|
70
|
+
app: router,
|
|
71
|
+
..._chunkFSBD5R22js.argToReq.call(void 0, params, item.args, req.headers)
|
|
72
|
+
};
|
|
73
|
+
const context = new (0, _chunkFSBD5R22js.Context)(contextData);
|
|
74
|
+
context.run({
|
|
75
|
+
globalGuards,
|
|
76
|
+
globalInterceptors,
|
|
77
|
+
globalFilter,
|
|
78
|
+
globalPipe
|
|
79
|
+
}, resolve, resolve);
|
|
80
|
+
});
|
|
81
|
+
})).then((ret) => {
|
|
82
|
+
res.json(ret);
|
|
84
83
|
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
});
|
|
84
|
+
} catch (e) {
|
|
85
|
+
return errorHandler(e);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
92
89
|
for (const [tag, record] of metaMap) {
|
|
93
90
|
for (const func in record) {
|
|
94
91
|
const meta2 = metaMap.get(tag)[func];
|
|
95
|
-
const { data: { params, plugins
|
|
92
|
+
const { data: { params, plugins, http } } = meta2;
|
|
96
93
|
if (!_optionalChain([http, 'optionalAccess', _2 => _2.type]))
|
|
97
94
|
continue;
|
|
98
95
|
const needBody = params.some((item) => item.type === "body");
|
|
99
|
-
router[http.type](http.prefix + http.route, ...
|
|
96
|
+
router[http.type](http.prefix + http.route, ..._chunkFSBD5R22js.Context.usePlugin(plugins, "hyper-express"), async (req, res, next) => {
|
|
100
97
|
debug(`invoke method "${func}" in module "${tag}"`);
|
|
101
98
|
const contextData = {
|
|
102
99
|
type: "hyper-express",
|
|
@@ -113,7 +110,7 @@ function bind(router, { moduleMap, meta }, opts = {}) {
|
|
|
113
110
|
headers: req.headers,
|
|
114
111
|
next
|
|
115
112
|
};
|
|
116
|
-
const context = new (0,
|
|
113
|
+
const context = new (0, _chunkFSBD5R22js.Context)(contextData);
|
|
117
114
|
if (http.headers) {
|
|
118
115
|
for (const name in http.headers)
|
|
119
116
|
res.set(name, http.headers[name]);
|
|
@@ -139,24 +136,9 @@ function bind(router, { moduleMap, meta }, opts = {}) {
|
|
|
139
136
|
}
|
|
140
137
|
}
|
|
141
138
|
}
|
|
142
|
-
|
|
143
|
-
_chunkYERBWZCSjs.detectAopDep.call(void 0, meta, {
|
|
144
|
-
plugins,
|
|
145
|
-
guards: globalGuards,
|
|
146
|
-
interceptors: globalInterceptors
|
|
147
|
-
});
|
|
148
|
-
handleMeta();
|
|
149
|
-
createRoute();
|
|
150
|
-
_chunkYERBWZCSjs.HMR.call(void 0, () => {
|
|
151
|
-
_chunkYERBWZCSjs.detectAopDep.call(void 0, meta, {
|
|
152
|
-
plugins,
|
|
153
|
-
guards: globalGuards,
|
|
154
|
-
interceptors: globalInterceptors
|
|
155
|
-
});
|
|
156
|
-
handleMeta();
|
|
157
|
-
});
|
|
139
|
+
_chunkZP7HNASUjs.__name.call(void 0, registerRoute, "registerRoute");
|
|
158
140
|
}
|
|
159
|
-
|
|
141
|
+
_chunkZP7HNASUjs.__name.call(void 0, bind, "bind");
|
|
160
142
|
|
|
161
143
|
|
|
162
144
|
exports.bind = bind;
|
|
@@ -1,102 +1,99 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BadRequestException,
|
|
3
3
|
Context,
|
|
4
|
-
HMR,
|
|
5
4
|
argToReq,
|
|
5
|
+
createControllerMetaMap,
|
|
6
6
|
detectAopDep
|
|
7
|
-
} from "../../chunk-
|
|
7
|
+
} from "../../chunk-665MB62T.mjs";
|
|
8
8
|
import {
|
|
9
9
|
__name
|
|
10
|
-
} from "../../chunk-
|
|
10
|
+
} from "../../chunk-VLV3AO3H.mjs";
|
|
11
11
|
|
|
12
12
|
// src/server/hyper-express/bind.ts
|
|
13
13
|
import Debug from "debug";
|
|
14
14
|
var debug = Debug("phecda-server/hyper-express");
|
|
15
|
-
function bind(router,
|
|
16
|
-
const { globalGuards, globalInterceptors,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const metaMap = /* @__PURE__ */ new Map();
|
|
22
|
-
function handleMeta() {
|
|
23
|
-
metaMap.clear();
|
|
24
|
-
for (const item of meta) {
|
|
25
|
-
const { tag, func, controller, http } = item.data;
|
|
26
|
-
if (controller !== "http" || !http?.type)
|
|
27
|
-
continue;
|
|
15
|
+
function bind(router, data, opts = {}) {
|
|
16
|
+
const { globalGuards, globalInterceptors, parallelRoute = "/__PHECDA_SERVER__", globalPlugins = [], parallelPlugins = [], globalFilter, globalPipe } = opts;
|
|
17
|
+
const { moduleMap, meta } = data;
|
|
18
|
+
const metaMap = createControllerMetaMap(meta, (meta2) => {
|
|
19
|
+
const { controller, http, func, tag } = meta2.data;
|
|
20
|
+
if (controller === "http" && http?.type) {
|
|
28
21
|
debug(`register method "${func}" in module "${tag}"`);
|
|
29
|
-
|
|
30
|
-
metaMap.get(tag)[func] = item;
|
|
31
|
-
else
|
|
32
|
-
metaMap.set(tag, {
|
|
33
|
-
[func]: item
|
|
34
|
-
});
|
|
22
|
+
return true;
|
|
35
23
|
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
24
|
+
});
|
|
25
|
+
detectAopDep(meta, {
|
|
26
|
+
plugins: [
|
|
27
|
+
...globalPlugins,
|
|
28
|
+
...parallelPlugins
|
|
29
|
+
],
|
|
30
|
+
guards: globalGuards,
|
|
31
|
+
interceptors: globalInterceptors
|
|
32
|
+
});
|
|
33
|
+
registerRoute();
|
|
34
|
+
async function registerRoute() {
|
|
35
|
+
Context.usePlugin(globalPlugins, "hyper-express").forEach((p) => router.use(p));
|
|
36
|
+
if (parallelRoute) {
|
|
37
|
+
router.post(parallelRoute, {
|
|
38
|
+
middlewares: Context.usePlugin(parallelPlugins, "hyper-express")
|
|
39
|
+
}, async (req, res, next) => {
|
|
40
|
+
const body = await req.json();
|
|
41
|
+
async function errorHandler(e) {
|
|
42
|
+
const error = await Context.filterRecord.default(e);
|
|
43
|
+
return res.status(error.status).json(error);
|
|
44
|
+
}
|
|
45
|
+
__name(errorHandler, "errorHandler");
|
|
46
|
+
if (!Array.isArray(body))
|
|
47
|
+
return errorHandler(new BadRequestException("data format should be an array"));
|
|
48
|
+
try {
|
|
49
|
+
return Promise.all(body.map((item, i) => {
|
|
50
|
+
return new Promise(async (resolve) => {
|
|
51
|
+
const { tag, func } = item;
|
|
52
|
+
debug(`(parallel)invoke method "${func}" in module "${tag}"`);
|
|
53
|
+
if (!metaMap.has(tag))
|
|
54
|
+
return resolve(await Context.filterRecord.default(new BadRequestException(`module "${tag}" doesn't exist`)));
|
|
55
|
+
const meta2 = metaMap.get(tag)[func];
|
|
56
|
+
if (!meta2)
|
|
57
|
+
return resolve(await Context.filterRecord.default(new BadRequestException(`"${func}" in "${tag}" doesn't exist`)));
|
|
58
|
+
const { data: { params } } = meta2;
|
|
59
|
+
const contextData = {
|
|
60
|
+
type: "hyper-express",
|
|
61
|
+
parallel: true,
|
|
62
|
+
request: req,
|
|
63
|
+
index: i,
|
|
64
|
+
meta: meta2,
|
|
65
|
+
response: res,
|
|
66
|
+
moduleMap,
|
|
67
|
+
tag,
|
|
68
|
+
func,
|
|
69
|
+
next,
|
|
70
|
+
app: router,
|
|
71
|
+
...argToReq(params, item.args, req.headers)
|
|
72
|
+
};
|
|
73
|
+
const context = new Context(contextData);
|
|
74
|
+
context.run({
|
|
75
|
+
globalGuards,
|
|
76
|
+
globalInterceptors,
|
|
77
|
+
globalFilter,
|
|
78
|
+
globalPipe
|
|
79
|
+
}, resolve, resolve);
|
|
80
|
+
});
|
|
81
|
+
})).then((ret) => {
|
|
82
|
+
res.json(ret);
|
|
84
83
|
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
});
|
|
84
|
+
} catch (e) {
|
|
85
|
+
return errorHandler(e);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
92
89
|
for (const [tag, record] of metaMap) {
|
|
93
90
|
for (const func in record) {
|
|
94
91
|
const meta2 = metaMap.get(tag)[func];
|
|
95
|
-
const { data: { params, plugins
|
|
92
|
+
const { data: { params, plugins, http } } = meta2;
|
|
96
93
|
if (!http?.type)
|
|
97
94
|
continue;
|
|
98
95
|
const needBody = params.some((item) => item.type === "body");
|
|
99
|
-
router[http.type](http.prefix + http.route, ...Context.usePlugin(
|
|
96
|
+
router[http.type](http.prefix + http.route, ...Context.usePlugin(plugins, "hyper-express"), async (req, res, next) => {
|
|
100
97
|
debug(`invoke method "${func}" in module "${tag}"`);
|
|
101
98
|
const contextData = {
|
|
102
99
|
type: "hyper-express",
|
|
@@ -139,22 +136,7 @@ function bind(router, { moduleMap, meta }, opts = {}) {
|
|
|
139
136
|
}
|
|
140
137
|
}
|
|
141
138
|
}
|
|
142
|
-
__name(
|
|
143
|
-
detectAopDep(meta, {
|
|
144
|
-
plugins,
|
|
145
|
-
guards: globalGuards,
|
|
146
|
-
interceptors: globalInterceptors
|
|
147
|
-
});
|
|
148
|
-
handleMeta();
|
|
149
|
-
createRoute();
|
|
150
|
-
HMR(() => {
|
|
151
|
-
detectAopDep(meta, {
|
|
152
|
-
plugins,
|
|
153
|
-
guards: globalGuards,
|
|
154
|
-
interceptors: globalInterceptors
|
|
155
|
-
});
|
|
156
|
-
handleMeta();
|
|
157
|
-
});
|
|
139
|
+
__name(registerRoute, "registerRoute");
|
|
158
140
|
}
|
|
159
141
|
__name(bind, "bind");
|
|
160
142
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Router, { RouterParamContext } from '@koa/router';
|
|
2
2
|
import { DefaultContext, DefaultState } from 'koa';
|
|
3
|
-
import { a as HttpContext, H as HttpOptions } from '../../helper-
|
|
4
|
-
import { F as Factory } from '../../core-
|
|
3
|
+
import { a as HttpContext, H as HttpOptions } from '../../helper-fcbf6aa8.js';
|
|
4
|
+
import { F as Factory } from '../../core-39f27fe8.js';
|
|
5
5
|
import 'node:http';
|
|
6
6
|
import 'phecda-core';
|
|
7
7
|
|
|
@@ -11,6 +11,7 @@ interface KoaCtx extends HttpContext {
|
|
|
11
11
|
next: Function;
|
|
12
12
|
app: Router;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
type Plugin = Router.Middleware;
|
|
15
|
+
declare function bind(router: Router, data: Awaited<ReturnType<typeof Factory>>, opts?: HttpOptions): void;
|
|
15
16
|
|
|
16
|
-
export { KoaCtx, bind };
|
|
17
|
+
export { KoaCtx, Plugin, bind };
|