phecda-server 3.0.3 → 3.2.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.
- package/bin/cli.js +29 -8
- package/dist/index.d.ts +36 -20
- package/dist/index.js +223 -145
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +222 -144
- package/dist/index.mjs.map +1 -1
- package/package.json +15 -2
- package/register/compile.mjs +29 -0
- package/register/index.mjs +29 -0
- package/register/loader.mjs +145 -0
package/dist/index.js
CHANGED
|
@@ -43,6 +43,7 @@ __export(src_exports, {
|
|
|
43
43
|
Controller: () => Controller,
|
|
44
44
|
Define: () => Define,
|
|
45
45
|
Delete: () => Delete,
|
|
46
|
+
Dev: () => Dev,
|
|
46
47
|
Factory: () => Factory,
|
|
47
48
|
ForbiddenException: () => ForbiddenException,
|
|
48
49
|
FrameworkException: () => FrameworkException,
|
|
@@ -67,6 +68,7 @@ __export(src_exports, {
|
|
|
67
68
|
ServerContext: () => ServerContext,
|
|
68
69
|
ServiceUnavailableException: () => ServiceUnavailableException,
|
|
69
70
|
TimeoutException: () => TimeoutException,
|
|
71
|
+
UNMOUNT_SYMBOL: () => UNMOUNT_SYMBOL,
|
|
70
72
|
UnauthorizedException: () => UnauthorizedException,
|
|
71
73
|
UndefinedException: () => UndefinedException,
|
|
72
74
|
UnsupportedMediaTypeException: () => UnsupportedMediaTypeException,
|
|
@@ -75,10 +77,8 @@ __export(src_exports, {
|
|
|
75
77
|
addInterceptor: () => addInterceptor,
|
|
76
78
|
addMiddleware: () => addMiddleware,
|
|
77
79
|
bindApp: () => bindApp,
|
|
78
|
-
constructorMap: () => constructorMap,
|
|
79
80
|
defaultPipe: () => defaultPipe,
|
|
80
81
|
emitter: () => emitter,
|
|
81
|
-
getInstance: () => getInstance,
|
|
82
82
|
isMerge: () => isMerge,
|
|
83
83
|
parseMeta: () => parseMeta,
|
|
84
84
|
resolveDep: () => resolveDep,
|
|
@@ -122,18 +122,18 @@ __name(ValidateException, "ValidateException");
|
|
|
122
122
|
|
|
123
123
|
// src/pipe.ts
|
|
124
124
|
var defaultPipe = {
|
|
125
|
-
async transform(args
|
|
125
|
+
async transform(args) {
|
|
126
126
|
for (const i in args) {
|
|
127
|
-
const {
|
|
128
|
-
if (
|
|
127
|
+
const { option, arg, reflect } = args[i];
|
|
128
|
+
if (option === false)
|
|
129
129
|
continue;
|
|
130
|
-
if (!reflect
|
|
131
|
-
if (
|
|
132
|
-
args[i].arg =
|
|
130
|
+
if (!reflect) {
|
|
131
|
+
if (option && arg)
|
|
132
|
+
args[i].arg = option(arg);
|
|
133
133
|
continue;
|
|
134
134
|
}
|
|
135
|
-
if ((0, import_phecda_core.isPhecda)(reflect
|
|
136
|
-
const ret = await (0, import_phecda_core.plainToClass)(reflect
|
|
135
|
+
if ((0, import_phecda_core.isPhecda)(reflect)) {
|
|
136
|
+
const ret = await (0, import_phecda_core.plainToClass)(reflect, arg, {
|
|
137
137
|
transform: true
|
|
138
138
|
});
|
|
139
139
|
if (ret.err.length > 0)
|
|
@@ -143,9 +143,9 @@ var defaultPipe = {
|
|
|
143
143
|
if ([
|
|
144
144
|
Number,
|
|
145
145
|
Boolean
|
|
146
|
-
].includes(reflect
|
|
147
|
-
args[i].arg = reflect
|
|
148
|
-
if (reflect
|
|
146
|
+
].includes(reflect)) {
|
|
147
|
+
args[i].arg = reflect(arg);
|
|
148
|
+
if (reflect === Number && Object.is(args[i].arg, NaN))
|
|
149
149
|
throw new ValidateException(`parameter ${Number(i) + 1} should be a number`);
|
|
150
150
|
}
|
|
151
151
|
}
|
|
@@ -270,7 +270,7 @@ var serverFilter = /* @__PURE__ */ __name((e) => {
|
|
|
270
270
|
}, "serverFilter");
|
|
271
271
|
|
|
272
272
|
// src/history.ts
|
|
273
|
-
var
|
|
273
|
+
var Histroy = class {
|
|
274
274
|
guard = [];
|
|
275
275
|
interceptor = [];
|
|
276
276
|
record(name, type) {
|
|
@@ -281,7 +281,17 @@ var Phistroy = class {
|
|
|
281
281
|
return false;
|
|
282
282
|
}
|
|
283
283
|
};
|
|
284
|
-
__name(
|
|
284
|
+
__name(Histroy, "Histroy");
|
|
285
|
+
|
|
286
|
+
// src/utils.ts
|
|
287
|
+
var import_picocolors = __toESM(require("picocolors"));
|
|
288
|
+
var isUndefined = /* @__PURE__ */ __name((obj) => typeof obj === "undefined", "isUndefined");
|
|
289
|
+
var isNil = /* @__PURE__ */ __name((obj) => isUndefined(obj) || obj === null, "isNil");
|
|
290
|
+
var isObject = /* @__PURE__ */ __name((fn) => !isNil(fn) && typeof fn === "object", "isObject");
|
|
291
|
+
function warn(msg, color = "yellow") {
|
|
292
|
+
console.warn(`${import_picocolors.default.magenta("[phecda-server]")} ${import_picocolors.default[color](msg)}`);
|
|
293
|
+
}
|
|
294
|
+
__name(warn, "warn");
|
|
285
295
|
|
|
286
296
|
// src/context/base.ts
|
|
287
297
|
var _Context = class {
|
|
@@ -294,7 +304,7 @@ var _Context = class {
|
|
|
294
304
|
constructor(key, data) {
|
|
295
305
|
this.key = key;
|
|
296
306
|
this.data = data;
|
|
297
|
-
this.history = new
|
|
307
|
+
this.history = new Histroy();
|
|
298
308
|
}
|
|
299
309
|
static registerGuard(key, handler) {
|
|
300
310
|
_Context.guardsRecord[key] = handler;
|
|
@@ -337,7 +347,6 @@ var Context = _Context;
|
|
|
337
347
|
__name(Context, "Context");
|
|
338
348
|
__publicField(Context, "metaRecord", {});
|
|
339
349
|
__publicField(Context, "metaDataRecord", {});
|
|
340
|
-
__publicField(Context, "instanceRecord", {});
|
|
341
350
|
__publicField(Context, "guardsRecord", {});
|
|
342
351
|
__publicField(Context, "interceptorsRecord", {});
|
|
343
352
|
function addGuard(key, handler) {
|
|
@@ -348,26 +357,19 @@ function addInterceptor(key, handler) {
|
|
|
348
357
|
Context.registerInterceptor(key, handler);
|
|
349
358
|
}
|
|
350
359
|
__name(addInterceptor, "addInterceptor");
|
|
351
|
-
function getInstance(tag) {
|
|
352
|
-
return Context.instanceRecord[tag];
|
|
353
|
-
}
|
|
354
|
-
__name(getInstance, "getInstance");
|
|
355
360
|
function parseMeta(meta) {
|
|
356
361
|
const { data: { params, guards, interceptors, middlewares }, reflect, handlers } = meta;
|
|
362
|
+
params.forEach(({ index, key }, i) => {
|
|
363
|
+
if (index !== i)
|
|
364
|
+
warn(`the ${i + 1}th argument on the method '${key}' require decorator`);
|
|
365
|
+
});
|
|
357
366
|
return {
|
|
358
367
|
guards,
|
|
359
368
|
reflect,
|
|
360
369
|
interceptors,
|
|
361
370
|
middlewares,
|
|
362
371
|
handlers,
|
|
363
|
-
params
|
|
364
|
-
const { type, key, validate } = param;
|
|
365
|
-
return {
|
|
366
|
-
type,
|
|
367
|
-
key,
|
|
368
|
-
validate
|
|
369
|
-
};
|
|
370
|
-
})
|
|
372
|
+
params
|
|
371
373
|
};
|
|
372
374
|
}
|
|
373
375
|
__name(parseMeta, "parseMeta");
|
|
@@ -381,8 +383,8 @@ var _ServerContext = class extends Context {
|
|
|
381
383
|
return _ServerContext.middlewareRecord[m];
|
|
382
384
|
});
|
|
383
385
|
}
|
|
384
|
-
async usePipe(args,
|
|
385
|
-
return _ServerContext.pipe.transform?.(args,
|
|
386
|
+
async usePipe(args, tag) {
|
|
387
|
+
return _ServerContext.pipe.transform?.(args, tag, this.data);
|
|
386
388
|
}
|
|
387
389
|
static useFilter(arg, data) {
|
|
388
390
|
return _ServerContext.filter(arg, data);
|
|
@@ -415,16 +417,6 @@ var Base = class {
|
|
|
415
417
|
};
|
|
416
418
|
__name(Base, "Base");
|
|
417
419
|
|
|
418
|
-
// src/utils.ts
|
|
419
|
-
var import_picocolors = __toESM(require("picocolors"));
|
|
420
|
-
var isUndefined = /* @__PURE__ */ __name((obj) => typeof obj === "undefined", "isUndefined");
|
|
421
|
-
var isNil = /* @__PURE__ */ __name((obj) => isUndefined(obj) || obj === null, "isNil");
|
|
422
|
-
var isObject = /* @__PURE__ */ __name((fn) => !isNil(fn) && typeof fn === "object", "isObject");
|
|
423
|
-
function warn(msg) {
|
|
424
|
-
console.warn(`${import_picocolors.default.magenta("[phecda-server]")} ${import_picocolors.default.yellow(msg)}`);
|
|
425
|
-
}
|
|
426
|
-
__name(warn, "warn");
|
|
427
|
-
|
|
428
420
|
// src/helper.ts
|
|
429
421
|
function isMerge(data) {
|
|
430
422
|
return !!data.isMerge;
|
|
@@ -440,17 +432,17 @@ __name(resolveDep, "resolveDep");
|
|
|
440
432
|
// src/common.ts
|
|
441
433
|
var SERIES_SYMBOL = "__symbol_series__";
|
|
442
434
|
var MERGE_SYMBOL = "__symbol_req__";
|
|
435
|
+
var UNMOUNT_SYMBOL = "__symbol_unmount__";
|
|
443
436
|
|
|
444
437
|
// src/server/express.ts
|
|
445
|
-
function bindApp(app, {
|
|
446
|
-
const { globalGuards, globalInterceptors, route, middlewares: proMiddle } = {
|
|
438
|
+
function bindApp(app, { moduleMap, meta }, options = {}) {
|
|
439
|
+
const { dev = process.env.NODE_ENV !== "production", globalGuards, globalInterceptors, route, middlewares: proMiddle } = {
|
|
447
440
|
route: "/__PHECDA_SERVER__",
|
|
448
441
|
globalGuards: [],
|
|
449
442
|
globalInterceptors: [],
|
|
450
443
|
middlewares: [],
|
|
451
444
|
...options
|
|
452
445
|
};
|
|
453
|
-
const methodMap = {};
|
|
454
446
|
const contextMeta = {};
|
|
455
447
|
app.post(route, (req, _res, next) => {
|
|
456
448
|
req[MERGE_SYMBOL] = true;
|
|
@@ -492,28 +484,36 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
492
484
|
if (category === "series") {
|
|
493
485
|
for (const item of data) {
|
|
494
486
|
const { tag } = item;
|
|
495
|
-
const [name] = tag.split("-");
|
|
487
|
+
const [name, method] = tag.split("-");
|
|
496
488
|
const { reflect, params } = Context.metaDataRecord[tag];
|
|
497
489
|
const instance = moduleMap.get(name);
|
|
498
490
|
try {
|
|
499
491
|
if (!params)
|
|
500
492
|
throw new BadRequestException(`"${tag}" doesn't exist`);
|
|
501
|
-
const args = await context.usePipe(params.map(({ type, key,
|
|
493
|
+
const args = await context.usePipe(params.map(({ type, key, option, index }) => {
|
|
502
494
|
const arg = resolveDep(item[type], key);
|
|
503
495
|
if (typeof arg === "string" && arg.startsWith(SERIES_SYMBOL)) {
|
|
504
|
-
const [,
|
|
496
|
+
const [, argIndex, argKey] = arg.split("@");
|
|
505
497
|
return {
|
|
506
|
-
arg: resolveDep(ret[Number(
|
|
507
|
-
|
|
498
|
+
arg: resolveDep(ret[Number(argIndex)], argKey || key),
|
|
499
|
+
option,
|
|
500
|
+
index,
|
|
501
|
+
type,
|
|
502
|
+
key,
|
|
503
|
+
reflect: reflect[index]
|
|
508
504
|
};
|
|
509
505
|
}
|
|
510
506
|
return {
|
|
511
507
|
arg,
|
|
512
|
-
|
|
508
|
+
option,
|
|
509
|
+
index,
|
|
510
|
+
type,
|
|
511
|
+
key,
|
|
512
|
+
reflect: reflect[index]
|
|
513
513
|
};
|
|
514
|
-
}),
|
|
514
|
+
}), tag);
|
|
515
515
|
instance.context = contextData;
|
|
516
|
-
ret.push(await
|
|
516
|
+
ret.push(await moduleMap.get(name)[method](...args));
|
|
517
517
|
} catch (e) {
|
|
518
518
|
const m = Context.metaRecord[tag];
|
|
519
519
|
m.handlers.forEach((handler) => handler.error?.(e));
|
|
@@ -526,21 +526,25 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
526
526
|
return Promise.all(data.map((item) => {
|
|
527
527
|
return new Promise(async (resolve) => {
|
|
528
528
|
const { tag } = item;
|
|
529
|
-
const [name] = tag.split("-");
|
|
529
|
+
const [name, method] = tag.split("-");
|
|
530
530
|
const { reflect, params, handlers } = Context.metaDataRecord[tag];
|
|
531
531
|
const instance = moduleMap.get(name);
|
|
532
532
|
try {
|
|
533
533
|
if (!params)
|
|
534
534
|
throw new BadRequestException(`"${tag}" doesn't exist`);
|
|
535
|
-
const args = await context.usePipe(params.map(({ type, key,
|
|
535
|
+
const args = await context.usePipe(params.map(({ type, key, option, index }) => {
|
|
536
536
|
const arg = resolveDep(item[type], key);
|
|
537
537
|
return {
|
|
538
538
|
arg,
|
|
539
|
-
|
|
539
|
+
type,
|
|
540
|
+
key,
|
|
541
|
+
option,
|
|
542
|
+
index,
|
|
543
|
+
reflect: reflect[index]
|
|
540
544
|
};
|
|
541
|
-
}),
|
|
545
|
+
}), tag);
|
|
542
546
|
instance.context = contextData;
|
|
543
|
-
resolve(await
|
|
547
|
+
resolve(await moduleMap.get(name)[method](...args));
|
|
544
548
|
} catch (e) {
|
|
545
549
|
handlers.forEach((handler) => handler.error?.(e));
|
|
546
550
|
resolve(await context.useFilter(e));
|
|
@@ -555,58 +559,72 @@ function bindApp(app, { meta, moduleMap }, options = {}) {
|
|
|
555
559
|
res.status(err.status).json(err);
|
|
556
560
|
}
|
|
557
561
|
});
|
|
558
|
-
|
|
559
|
-
const
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
562
|
+
async function createRoute() {
|
|
563
|
+
for (const i of meta) {
|
|
564
|
+
const { method, route: route2, header, tag } = i.data;
|
|
565
|
+
const methodTag = `${tag}-${method}`;
|
|
566
|
+
contextMeta[methodTag] = i;
|
|
567
|
+
Context.metaRecord[methodTag] = i;
|
|
568
|
+
let { guards, reflect, interceptors, params, handlers, middlewares } = Context.metaDataRecord[methodTag] ? Context.metaDataRecord[methodTag] : Context.metaDataRecord[methodTag] = parseMeta(i);
|
|
569
|
+
guards = [
|
|
570
|
+
...globalGuards,
|
|
571
|
+
...guards
|
|
572
|
+
];
|
|
573
|
+
interceptors = [
|
|
574
|
+
...globalInterceptors,
|
|
575
|
+
...interceptors
|
|
576
|
+
];
|
|
577
|
+
if (route2) {
|
|
578
|
+
app[route2.type](route2.route, ...ServerContext.useMiddleware(middlewares), async (req, res) => {
|
|
579
|
+
const instance = moduleMap.get(tag);
|
|
580
|
+
const contextData = {
|
|
581
|
+
request: req,
|
|
582
|
+
meta: i,
|
|
583
|
+
response: res,
|
|
584
|
+
moduleMap
|
|
585
|
+
};
|
|
586
|
+
const context = new ServerContext(methodTag, contextData);
|
|
587
|
+
try {
|
|
588
|
+
for (const name in header)
|
|
589
|
+
res.set(name, header[name]);
|
|
590
|
+
await context.useGuard(guards);
|
|
591
|
+
await context.useInterceptor(interceptors);
|
|
592
|
+
const args = await context.usePipe(params.map(({ type, key, option, index }) => {
|
|
593
|
+
return {
|
|
594
|
+
arg: resolveDep(req[type], key),
|
|
595
|
+
option,
|
|
596
|
+
key,
|
|
597
|
+
type,
|
|
598
|
+
index,
|
|
599
|
+
reflect: reflect[index]
|
|
600
|
+
};
|
|
601
|
+
}), methodTag);
|
|
602
|
+
instance.context = contextData;
|
|
603
|
+
const ret = await context.usePost(await instance[method](...args));
|
|
604
|
+
if (isObject(ret))
|
|
605
|
+
res.json(ret);
|
|
606
|
+
else
|
|
607
|
+
res.send(String(ret));
|
|
608
|
+
} catch (e) {
|
|
609
|
+
handlers.forEach((handler) => handler.error?.(e));
|
|
610
|
+
const err = await context.useFilter(e);
|
|
611
|
+
res.status(err.status).json(err);
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
}
|
|
608
615
|
}
|
|
609
616
|
}
|
|
617
|
+
__name(createRoute, "createRoute");
|
|
618
|
+
createRoute();
|
|
619
|
+
if (dev) {
|
|
620
|
+
const rawMetaHmr = globalThis.__PHECDA_SERVER_META__;
|
|
621
|
+
globalThis.__PHECDA_SERVER_META__ = () => {
|
|
622
|
+
app.stack = app.stack.slice(0, 1);
|
|
623
|
+
Context.metaDataRecord = {};
|
|
624
|
+
createRoute();
|
|
625
|
+
rawMetaHmr?.();
|
|
626
|
+
};
|
|
627
|
+
}
|
|
610
628
|
}
|
|
611
629
|
__name(bindApp, "bindApp");
|
|
612
630
|
|
|
@@ -615,6 +633,7 @@ var import_reflect_metadata = require("reflect-metadata");
|
|
|
615
633
|
var import_fs = __toESM(require("fs"));
|
|
616
634
|
var import_node_events = __toESM(require("events"));
|
|
617
635
|
var import_phecda_core2 = require("phecda-core");
|
|
636
|
+
var import_debug = __toESM(require("debug"));
|
|
618
637
|
|
|
619
638
|
// src/meta.ts
|
|
620
639
|
var Meta = class {
|
|
@@ -630,11 +649,14 @@ var Meta = class {
|
|
|
630
649
|
__name(Meta, "Meta");
|
|
631
650
|
|
|
632
651
|
// src/core.ts
|
|
652
|
+
var debug = (0, import_debug.default)("phecda-server");
|
|
633
653
|
var emitter = new import_node_events.default();
|
|
634
|
-
|
|
635
|
-
async function Factory(Modules) {
|
|
654
|
+
async function Factory(Modules, opts = {}) {
|
|
636
655
|
const moduleMap = /* @__PURE__ */ new Map();
|
|
637
656
|
const meta = [];
|
|
657
|
+
const constructorMap = /* @__PURE__ */ new Map();
|
|
658
|
+
const moduleGraph = /* @__PURE__ */ new WeakMap();
|
|
659
|
+
const { dev = process.env.NODE_ENV === "development", file = "pmeta.js" } = opts;
|
|
638
660
|
(0, import_phecda_core2.injectProperty)("watcher", ({ eventName, instance, key, options }) => {
|
|
639
661
|
const fn = typeof instance[key] === "function" ? instance[key].bind(instance) : (v) => instance[key] = v;
|
|
640
662
|
if (options?.once)
|
|
@@ -642,45 +664,94 @@ async function Factory(Modules) {
|
|
|
642
664
|
else
|
|
643
665
|
emitter.on(eventName, fn);
|
|
644
666
|
});
|
|
667
|
+
async function update(Module) {
|
|
668
|
+
const tag = Module.prototype?.__TAG__ || Module.name;
|
|
669
|
+
if (!moduleMap.has(tag))
|
|
670
|
+
return;
|
|
671
|
+
debug(`update module "${tag}"`);
|
|
672
|
+
const instance = moduleMap.get(tag);
|
|
673
|
+
if (instance?.[UNMOUNT_SYMBOL]) {
|
|
674
|
+
for (const cb of instance[UNMOUNT_SYMBOL])
|
|
675
|
+
await cb();
|
|
676
|
+
}
|
|
677
|
+
moduleMap.delete(tag);
|
|
678
|
+
constructorMap.delete(tag);
|
|
679
|
+
for (let i = meta.length - 1; i >= 0; i--) {
|
|
680
|
+
if (meta[i].data.tag === tag)
|
|
681
|
+
meta.splice(i, 1);
|
|
682
|
+
}
|
|
683
|
+
const newModule = await buildNestModule(Module, moduleMap);
|
|
684
|
+
moduleGraph.get(instance)?.forEach((module2) => {
|
|
685
|
+
for (const key in module2) {
|
|
686
|
+
if (module2[key] === instance)
|
|
687
|
+
module2[key] = newModule;
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
moduleGraph.get(instance);
|
|
691
|
+
moduleMap.set(tag, newModule);
|
|
692
|
+
}
|
|
693
|
+
__name(update, "update");
|
|
694
|
+
async function buildNestModule(Module, map) {
|
|
695
|
+
const paramtypes = getParamtypes(Module);
|
|
696
|
+
let instance;
|
|
697
|
+
const tag = Module.prototype?.__TAG__ || Module.name;
|
|
698
|
+
if (map.has(tag)) {
|
|
699
|
+
instance = map.get(tag);
|
|
700
|
+
if (!instance)
|
|
701
|
+
throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`);
|
|
702
|
+
if (constructorMap.get(tag) !== Module)
|
|
703
|
+
warn(`Synonym module: Module taged "${tag}" has been loaded before, so phecda-server won't load Module "${Module.name}"`);
|
|
704
|
+
return instance;
|
|
705
|
+
}
|
|
706
|
+
map.set(tag, void 0);
|
|
707
|
+
if (paramtypes) {
|
|
708
|
+
const paramtypesInstances = [];
|
|
709
|
+
for (const i in paramtypes)
|
|
710
|
+
paramtypesInstances[i] = await buildNestModule(paramtypes[i], map);
|
|
711
|
+
instance = new Module(...paramtypesInstances);
|
|
712
|
+
for (const i of paramtypesInstances) {
|
|
713
|
+
if (!moduleGraph.has(i))
|
|
714
|
+
moduleGraph.set(i, []);
|
|
715
|
+
moduleGraph.get(i).push(instance);
|
|
716
|
+
}
|
|
717
|
+
} else {
|
|
718
|
+
instance = new Module();
|
|
719
|
+
}
|
|
720
|
+
meta.push(...getMetaFromInstance(instance, tag, Module.name));
|
|
721
|
+
await (0, import_phecda_core2.registerAsync)(instance);
|
|
722
|
+
map.set(tag, instance);
|
|
723
|
+
constructorMap.set(tag, Module);
|
|
724
|
+
return instance;
|
|
725
|
+
}
|
|
726
|
+
__name(buildNestModule, "buildNestModule");
|
|
645
727
|
for (const Module of Modules)
|
|
646
|
-
await buildNestModule(Module, moduleMap
|
|
647
|
-
|
|
728
|
+
await buildNestModule(Module, moduleMap);
|
|
729
|
+
function writeMeta() {
|
|
730
|
+
debug("write metadata");
|
|
731
|
+
import_fs.default.promises.writeFile(file, JSON.stringify(meta.map((item) => item.data)));
|
|
732
|
+
}
|
|
733
|
+
__name(writeMeta, "writeMeta");
|
|
734
|
+
writeMeta();
|
|
735
|
+
if (dev) {
|
|
736
|
+
globalThis.__PHECDA_SERVER_HMR__ = async (file2) => {
|
|
737
|
+
debug(`reload file ${file2}`);
|
|
738
|
+
const module2 = await import(file2);
|
|
739
|
+
for (const i in module2) {
|
|
740
|
+
if ((0, import_phecda_core2.isPhecda)(module2[i]))
|
|
741
|
+
await update(module2[i]);
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
globalThis.__PHECDA_SERVER_META__ = writeMeta;
|
|
745
|
+
}
|
|
648
746
|
return {
|
|
649
747
|
moduleMap,
|
|
650
748
|
meta,
|
|
651
|
-
|
|
749
|
+
constructorMap,
|
|
750
|
+
update
|
|
652
751
|
};
|
|
653
752
|
}
|
|
654
753
|
__name(Factory, "Factory");
|
|
655
|
-
|
|
656
|
-
const paramtypes = getParamtypes(Module);
|
|
657
|
-
let instance;
|
|
658
|
-
const tag = Module.prototype?.__TAG__ || Module.name;
|
|
659
|
-
if (map.has(tag)) {
|
|
660
|
-
instance = map.get(tag);
|
|
661
|
-
if (!instance)
|
|
662
|
-
throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${tag}--[module] ${Module}`);
|
|
663
|
-
if (constructorMap.get(tag) !== Module)
|
|
664
|
-
warn(`Synonym module: Module taged "${tag}" has been loaded before, so phecda-server won't load Module "${Module.name}"`);
|
|
665
|
-
return instance;
|
|
666
|
-
}
|
|
667
|
-
map.set(tag, void 0);
|
|
668
|
-
if (paramtypes) {
|
|
669
|
-
const paramtypesInstances = [];
|
|
670
|
-
for (const i in paramtypes)
|
|
671
|
-
paramtypesInstances[i] = await buildNestModule(paramtypes[i], map, meta);
|
|
672
|
-
instance = new Module(...paramtypesInstances);
|
|
673
|
-
} else {
|
|
674
|
-
instance = new Module();
|
|
675
|
-
}
|
|
676
|
-
meta.push(...getMetaFromInstance(instance, Module.name, tag));
|
|
677
|
-
await (0, import_phecda_core2.registerAsync)(instance);
|
|
678
|
-
map.set(tag, instance);
|
|
679
|
-
constructorMap.set(tag, Module);
|
|
680
|
-
return instance;
|
|
681
|
-
}
|
|
682
|
-
__name(buildNestModule, "buildNestModule");
|
|
683
|
-
function getMetaFromInstance(instance, name, tag) {
|
|
754
|
+
function getMetaFromInstance(instance, tag, name) {
|
|
684
755
|
const vars = (0, import_phecda_core2.getExposeKey)(instance).filter((item) => item !== "__CLASS");
|
|
685
756
|
const baseState = (0, import_phecda_core2.getState)(instance, "__CLASS") || {};
|
|
686
757
|
initState(baseState);
|
|
@@ -751,13 +822,20 @@ function initState(state) {
|
|
|
751
822
|
state.interceptors = [];
|
|
752
823
|
}
|
|
753
824
|
__name(initState, "initState");
|
|
825
|
+
var Dev = class {
|
|
826
|
+
[UNMOUNT_SYMBOL] = [];
|
|
827
|
+
onUnmount(cb) {
|
|
828
|
+
this[UNMOUNT_SYMBOL].push(cb);
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
__name(Dev, "Dev");
|
|
754
832
|
|
|
755
833
|
// src/decorators/index.ts
|
|
756
834
|
var import_phecda_core5 = require("phecda-core");
|
|
757
835
|
|
|
758
836
|
// src/decorators/param.ts
|
|
759
837
|
var import_phecda_core3 = require("phecda-core");
|
|
760
|
-
function BaseParam(type, key,
|
|
838
|
+
function BaseParam(type, key, option) {
|
|
761
839
|
return (target, k, index) => {
|
|
762
840
|
(0, import_phecda_core3.setModelVar)(target, k);
|
|
763
841
|
const state = target._namespace.__STATE_NAMESPACE__.get(k) || {};
|
|
@@ -767,7 +845,7 @@ function BaseParam(type, key, validate) {
|
|
|
767
845
|
type,
|
|
768
846
|
key,
|
|
769
847
|
index,
|
|
770
|
-
|
|
848
|
+
option
|
|
771
849
|
});
|
|
772
850
|
(0, import_phecda_core3.setState)(target, k, state);
|
|
773
851
|
};
|
|
@@ -912,6 +990,7 @@ __reExport(src_exports, require("phecda-core"), module.exports);
|
|
|
912
990
|
Controller,
|
|
913
991
|
Define,
|
|
914
992
|
Delete,
|
|
993
|
+
Dev,
|
|
915
994
|
Factory,
|
|
916
995
|
ForbiddenException,
|
|
917
996
|
FrameworkException,
|
|
@@ -936,6 +1015,7 @@ __reExport(src_exports, require("phecda-core"), module.exports);
|
|
|
936
1015
|
ServerContext,
|
|
937
1016
|
ServiceUnavailableException,
|
|
938
1017
|
TimeoutException,
|
|
1018
|
+
UNMOUNT_SYMBOL,
|
|
939
1019
|
UnauthorizedException,
|
|
940
1020
|
UndefinedException,
|
|
941
1021
|
UnsupportedMediaTypeException,
|
|
@@ -944,10 +1024,8 @@ __reExport(src_exports, require("phecda-core"), module.exports);
|
|
|
944
1024
|
addInterceptor,
|
|
945
1025
|
addMiddleware,
|
|
946
1026
|
bindApp,
|
|
947
|
-
constructorMap,
|
|
948
1027
|
defaultPipe,
|
|
949
1028
|
emitter,
|
|
950
|
-
getInstance,
|
|
951
1029
|
isMerge,
|
|
952
1030
|
parseMeta,
|
|
953
1031
|
resolveDep,
|