moost 0.6.18 → 0.6.21
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.cjs +800 -598
- package/dist/index.d.ts +416 -271
- package/dist/index.mjs +795 -599
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -214,12 +214,27 @@ const scopeVarsMap = /* @__PURE__ */ new Map();
|
|
|
214
214
|
});
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region packages/moost/src/composables/global-key.ts
|
|
219
|
+
var _g;
|
|
220
|
+
const registry = (_g = globalThis).__moost_keys ?? (_g.__moost_keys = /* @__PURE__ */ new Map());
|
|
221
|
+
/**
|
|
222
|
+
* Creates a typed event-context key that stays a single instance across duplicate
|
|
223
|
+
* moost loads (dual ESM/CJS, or duplicate installs). Use this for every
|
|
224
|
+
* moost-owned context key instead of `key()` directly.
|
|
225
|
+
*
|
|
226
|
+
* @param name - Debug label AND the registry lookup key; must be unique per slot.
|
|
227
|
+
*/ function globalKey(name) {
|
|
228
|
+
if (!registry.has(name)) registry.set(name, key$1(name));
|
|
229
|
+
return registry.get(name);
|
|
230
|
+
}
|
|
231
|
+
|
|
217
232
|
//#endregion
|
|
218
233
|
//#region packages/moost/src/composables/controller.composable.ts
|
|
219
|
-
const controllerInstanceKey =
|
|
220
|
-
const controllerMethodKey =
|
|
221
|
-
const controllerRouteKey =
|
|
222
|
-
const controllerPrefixKey =
|
|
234
|
+
const controllerInstanceKey = globalKey("controller.instance");
|
|
235
|
+
const controllerMethodKey = globalKey("controller.method");
|
|
236
|
+
const controllerRouteKey = globalKey("controller.route");
|
|
237
|
+
const controllerPrefixKey = globalKey("controller.prefix");
|
|
223
238
|
/**
|
|
224
239
|
* Sets the controller context for the current event scope.
|
|
225
240
|
* Called internally by adapters when dispatching events to handlers.
|
|
@@ -261,10 +276,8 @@ const controllerPrefixKey = key$1("controller.prefix");
|
|
|
261
276
|
|
|
262
277
|
//#endregion
|
|
263
278
|
//#region packages/moost/src/composables/interceptor.composable.ts
|
|
264
|
-
|
|
265
|
-
const
|
|
266
|
-
const overtakeKey = (_g = g).__moost_overtakeKey ?? (_g.__moost_overtakeKey = key$1("interceptor.overtake"));
|
|
267
|
-
const interceptResultKey = (_g1 = g).__moost_interceptResultKey ?? (_g1.__moost_interceptResultKey = key$1("interceptor.result"));
|
|
279
|
+
const overtakeKey = globalKey("interceptor.overtake");
|
|
280
|
+
const interceptResultKey = globalKey("interceptor.result");
|
|
268
281
|
/** Stores the overtake (reply) function in the current event context. */ function setOvertake(fn, ctx) {
|
|
269
282
|
(ctx || current$1()).set(overtakeKey, fn);
|
|
270
283
|
}
|
|
@@ -466,432 +479,68 @@ function getParentProps(constructor) {
|
|
|
466
479
|
}
|
|
467
480
|
|
|
468
481
|
//#endregion
|
|
469
|
-
//#region packages/moost/src/
|
|
482
|
+
//#region packages/moost/src/decorators/circular.decorator.ts
|
|
470
483
|
/**
|
|
471
|
-
*
|
|
484
|
+
* Marks a constructor parameter dependency as circular.
|
|
485
|
+
* The resolver function is called lazily to break the circular reference.
|
|
472
486
|
*
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
const result = runPipes(pipes, void 0, {
|
|
484
|
-
classMeta: context.classMeta,
|
|
485
|
-
methodMeta: context.methodMeta,
|
|
486
|
-
paramMeta,
|
|
487
|
-
type: context.type,
|
|
488
|
-
key: context.key,
|
|
489
|
-
index: i,
|
|
490
|
-
targetMeta: paramMeta,
|
|
491
|
-
instantiate: (t) => useControllerContext().instantiate(t)
|
|
492
|
-
}, "PARAM");
|
|
493
|
-
if (!hasAsync && isThenable(result)) hasAsync = true;
|
|
494
|
-
args[i] = result;
|
|
495
|
-
}
|
|
496
|
-
return hasAsync ? Promise.all(args) : args;
|
|
497
|
-
};
|
|
487
|
+
* @param resolver - Factory that returns the class constructor (e.g. `() => MyService`).
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```ts
|
|
491
|
+
* class ServiceA {
|
|
492
|
+
* constructor(@Circular(() => ServiceB) private b: ServiceB) {}
|
|
493
|
+
* }
|
|
494
|
+
* ```
|
|
495
|
+
*/ function Circular(resolver) {
|
|
496
|
+
return getMoostMate().decorate("circular", resolver);
|
|
498
497
|
}
|
|
499
498
|
|
|
500
499
|
//#endregion
|
|
501
|
-
//#region packages/moost/src/
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
else obj[key] = value;
|
|
510
|
-
return obj;
|
|
500
|
+
//#region packages/moost/src/decorators/common.decorator.ts
|
|
501
|
+
/**
|
|
502
|
+
* Apply Multiple Decorators
|
|
503
|
+
*
|
|
504
|
+
* @param decorators - array of decorators
|
|
505
|
+
* @returns
|
|
506
|
+
*/ function ApplyDecorators(...decorators) {
|
|
507
|
+
return getMoostMate().apply(...decorators);
|
|
511
508
|
}
|
|
512
509
|
/**
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
get count() {
|
|
523
|
-
return this.handlers.length;
|
|
524
|
-
}
|
|
525
|
-
get countAfter() {
|
|
526
|
-
return this.after?.length ?? 0;
|
|
527
|
-
}
|
|
528
|
-
get countOnError() {
|
|
529
|
-
return this.onError?.length ?? 0;
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* Register hooks from a TInterceptorDef.
|
|
533
|
-
* Returns a pending PromiseLike if `before` went async, or undefined.
|
|
534
|
-
*/ registerDef(def, entry, ci) {
|
|
535
|
-
if (def.after) (this.after ?? (this.after = [])).unshift({
|
|
536
|
-
name: entry.name,
|
|
537
|
-
fn: def.after
|
|
538
|
-
});
|
|
539
|
-
if (def.error) (this.onError ?? (this.onError = [])).unshift({
|
|
540
|
-
name: entry.name,
|
|
541
|
-
fn: def.error
|
|
542
|
-
});
|
|
543
|
-
if (def.before) {
|
|
544
|
-
const spanName = entry.spanName;
|
|
545
|
-
const result = ci ? ci.with(spanName, { "moost.interceptor.stage": "before" }, () => def.before?.(this.getReplyFn())) : def.before(this.getReplyFn());
|
|
546
|
-
if (isThenable(result)) return result;
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
before() {
|
|
550
|
-
const ci = getContextInjector$1();
|
|
551
|
-
for (let i = 0; i < this.handlers.length; i++) {
|
|
552
|
-
const entry = this.handlers[i];
|
|
553
|
-
const { handler } = entry;
|
|
554
|
-
if (typeof handler === "function") {
|
|
555
|
-
const factoryResult = handler();
|
|
556
|
-
if (isThenable(factoryResult)) return this._beforeAsyncFactory(ci, factoryResult, i);
|
|
557
|
-
const pending = this.registerDef(factoryResult, entry, ci);
|
|
558
|
-
if (pending) return this._beforeAsyncPending(ci, pending, i);
|
|
559
|
-
if (this.responseOverwritten) return this.response;
|
|
560
|
-
} else {
|
|
561
|
-
const pending = this.registerDef(handler, entry, ci);
|
|
562
|
-
if (pending) return this._beforeAsyncPending(ci, pending, i);
|
|
563
|
-
if (this.responseOverwritten) return this.response;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
async _beforeAsyncFactory(ci, factoryPromise, startIndex) {
|
|
568
|
-
const def = await factoryPromise;
|
|
569
|
-
const entry = this.handlers[startIndex];
|
|
570
|
-
const pending = this.registerDef(def, entry, ci);
|
|
571
|
-
if (pending) await pending;
|
|
572
|
-
if (this.responseOverwritten) return this.response;
|
|
573
|
-
return this._beforeFrom(ci, startIndex + 1);
|
|
574
|
-
}
|
|
575
|
-
async _beforeAsyncPending(ci, pending, startIndex) {
|
|
576
|
-
await pending;
|
|
577
|
-
if (this.responseOverwritten) return this.response;
|
|
578
|
-
return this._beforeFrom(ci, startIndex + 1);
|
|
579
|
-
}
|
|
580
|
-
async _beforeFrom(ci, startIndex) {
|
|
581
|
-
for (let i = startIndex; i < this.handlers.length; i++) {
|
|
582
|
-
const entry = this.handlers[i];
|
|
583
|
-
const { handler } = entry;
|
|
584
|
-
let def;
|
|
585
|
-
if (typeof handler === "function") def = await handler();
|
|
586
|
-
else def = handler;
|
|
587
|
-
const pending = this.registerDef(def, entry, ci);
|
|
588
|
-
if (pending) await pending;
|
|
589
|
-
if (this.responseOverwritten) return this.response;
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
fireAfter(response) {
|
|
593
|
-
this.response = response;
|
|
594
|
-
const isError = response instanceof Error;
|
|
595
|
-
const handlers = isError ? this.onError : this.after;
|
|
596
|
-
if (!handlers) return this.response;
|
|
597
|
-
const ci = getContextInjector$1();
|
|
598
|
-
const stage = isError ? "onError" : "after";
|
|
599
|
-
for (let i = 0; i < handlers.length; i++) {
|
|
600
|
-
const { name, fn } = handlers[i];
|
|
601
|
-
const result = ci ? ci.with(`Interceptor:${name}`, { "moost.interceptor.stage": stage }, () => fn(response, this.getReplyFn())) : fn(response, this.getReplyFn());
|
|
602
|
-
if (isThenable(result)) return this._fireAfterAsync({
|
|
603
|
-
ci,
|
|
604
|
-
handlers,
|
|
605
|
-
stage,
|
|
606
|
-
response
|
|
607
|
-
}, result, i);
|
|
608
|
-
}
|
|
609
|
-
return this.response;
|
|
610
|
-
}
|
|
611
|
-
async _fireAfterAsync(ctx, pending, startIndex) {
|
|
612
|
-
await pending;
|
|
613
|
-
for (let i = startIndex + 1; i < ctx.handlers.length; i++) {
|
|
614
|
-
const { name, fn } = ctx.handlers[i];
|
|
615
|
-
if (ctx.ci) await ctx.ci.with(`Interceptor:${name}`, { "moost.interceptor.stage": ctx.stage }, () => fn(ctx.response, this.getReplyFn()));
|
|
616
|
-
else await fn(ctx.response, this.getReplyFn());
|
|
617
|
-
}
|
|
618
|
-
return this.response;
|
|
619
|
-
}
|
|
620
|
-
constructor(handlers) {
|
|
621
|
-
_define_property$1(this, "handlers", void 0);
|
|
622
|
-
_define_property$1(this, "after", void 0);
|
|
623
|
-
_define_property$1(this, "onError", void 0);
|
|
624
|
-
_define_property$1(this, "response", void 0);
|
|
625
|
-
_define_property$1(this, "responseOverwritten", void 0);
|
|
626
|
-
_define_property$1(this, "_boundReplyFn", void 0);
|
|
627
|
-
this.handlers = handlers;
|
|
628
|
-
this.responseOverwritten = false;
|
|
629
|
-
}
|
|
630
|
-
};
|
|
631
|
-
|
|
632
|
-
//#endregion
|
|
633
|
-
//#region packages/moost/src/utils.ts
|
|
634
|
-
const mate = getMoostMate();
|
|
635
|
-
const noInterceptors = () => void 0;
|
|
636
|
-
function findInterceptorMethods(handler) {
|
|
637
|
-
const fakeInstance = Object.create(handler.prototype);
|
|
638
|
-
const methods = getInstanceOwnMethods(fakeInstance);
|
|
639
|
-
const result = {};
|
|
640
|
-
for (const method of methods) {
|
|
641
|
-
const hook = mate.read(fakeInstance, method)?.interceptorHook;
|
|
642
|
-
if (hook === "before" || hook === "after" || hook === "error") result[hook] = method;
|
|
643
|
-
}
|
|
644
|
-
return result;
|
|
510
|
+
* ## Label
|
|
511
|
+
* ### @Decorator
|
|
512
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
513
|
+
*
|
|
514
|
+
* Stores Label metadata
|
|
515
|
+
*/ function Label(value) {
|
|
516
|
+
return getMoostMate().decorate("label", value);
|
|
645
517
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
if (argsPipes.length === 0) return;
|
|
655
|
-
return resolveArguments(argsPipes, {
|
|
656
|
-
classMeta: opts.classMeta,
|
|
657
|
-
methodMeta,
|
|
658
|
-
type: opts.handler,
|
|
659
|
-
key: opts.methodName
|
|
660
|
-
});
|
|
518
|
+
/**
|
|
519
|
+
* ## Description
|
|
520
|
+
* ### @Decorator
|
|
521
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
522
|
+
*
|
|
523
|
+
* Stores Description metadata
|
|
524
|
+
*/ function Description(value) {
|
|
525
|
+
return getMoostMate().decorate("description", value);
|
|
661
526
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
527
|
+
/**
|
|
528
|
+
* ## Value
|
|
529
|
+
* ### @Decorator
|
|
530
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
531
|
+
*
|
|
532
|
+
* Stores Value metadata
|
|
533
|
+
*/ function Value(value) {
|
|
534
|
+
return getMoostMate().decorate("value", value);
|
|
667
535
|
}
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
if (opts.methods.before) {
|
|
677
|
-
const methodName = opts.methods.before;
|
|
678
|
-
const resolveArgs = opts.resolvers[methodName];
|
|
679
|
-
if (resolveArgs) def.before = (reply) => {
|
|
680
|
-
setOvertake(reply);
|
|
681
|
-
return callWithArgs(instance, methodName, resolveArgs);
|
|
682
|
-
};
|
|
683
|
-
else def.before = () => instance[methodName]();
|
|
684
|
-
}
|
|
685
|
-
if (opts.methods.after) {
|
|
686
|
-
const methodName = opts.methods.after;
|
|
687
|
-
const resolveArgs = opts.resolvers[methodName];
|
|
688
|
-
def.after = (response, reply) => {
|
|
689
|
-
setOvertake(reply);
|
|
690
|
-
setInterceptResult(response);
|
|
691
|
-
return callMethod(instance, methodName, resolveArgs);
|
|
692
|
-
};
|
|
693
|
-
}
|
|
694
|
-
if (opts.methods.error) {
|
|
695
|
-
const methodName = opts.methods.error;
|
|
696
|
-
const resolveArgs = opts.resolvers[methodName];
|
|
697
|
-
def.error = (error, reply) => {
|
|
698
|
-
setOvertake(reply);
|
|
699
|
-
setInterceptResult(error);
|
|
700
|
-
return callMethod(instance, methodName, resolveArgs);
|
|
701
|
-
};
|
|
702
|
-
}
|
|
703
|
-
return def;
|
|
704
|
-
}
|
|
705
|
-
function fromTarget(targetInstance) {
|
|
706
|
-
const result = infact.getForInstance(targetInstance, opts.handler, { customData: { pipes: opts.pipes } });
|
|
707
|
-
if (isThenable(result)) return result.then(buildDef);
|
|
708
|
-
return buildDef(result);
|
|
709
|
-
}
|
|
710
|
-
return () => {
|
|
711
|
-
const result = opts.getTargetInstance();
|
|
712
|
-
if (isThenable(result)) return result.then(fromTarget);
|
|
713
|
-
return fromTarget(result);
|
|
714
|
-
};
|
|
715
|
-
}
|
|
716
|
-
function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes) {
|
|
717
|
-
if (interceptors.length === 0) return noInterceptors;
|
|
718
|
-
const precomputedHandlers = interceptors.map(({ handler, name }) => {
|
|
719
|
-
const spanName = `Interceptor:${name}`;
|
|
720
|
-
if (typeof handler !== "function") return {
|
|
721
|
-
handler,
|
|
722
|
-
name,
|
|
723
|
-
spanName
|
|
724
|
-
};
|
|
725
|
-
const interceptorMeta = mate.read(handler);
|
|
726
|
-
if (!interceptorMeta?.interceptor) throw new Error(`Invalid interceptor "${name}": must be TInterceptorDef or @Interceptor class`);
|
|
727
|
-
const classMeta = interceptorMeta;
|
|
728
|
-
const methods = findInterceptorMethods(handler);
|
|
729
|
-
const mergedPipes = mergeSorted(pipes, classMeta.pipes);
|
|
730
|
-
const resolvers = {};
|
|
731
|
-
for (const hook of [
|
|
732
|
-
"before",
|
|
733
|
-
"after",
|
|
734
|
-
"error"
|
|
735
|
-
]) {
|
|
736
|
-
const methodName = methods[hook];
|
|
737
|
-
if (methodName) resolvers[methodName] = buildMethodResolver({
|
|
738
|
-
handler,
|
|
739
|
-
methodName,
|
|
740
|
-
pipes: mergedPipes,
|
|
741
|
-
classMeta
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
return {
|
|
745
|
-
handler: createClassInterceptorFactory({
|
|
746
|
-
handler,
|
|
747
|
-
methods,
|
|
748
|
-
resolvers,
|
|
749
|
-
getTargetInstance,
|
|
750
|
-
pipes: mergedPipes
|
|
751
|
-
}),
|
|
752
|
-
name,
|
|
753
|
-
spanName
|
|
754
|
-
};
|
|
755
|
-
});
|
|
756
|
-
return () => new InterceptorHandler(precomputedHandlers);
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
//#endregion
|
|
760
|
-
//#region packages/moost/src/binding/bind-controller.ts
|
|
761
|
-
async function bindControllerMethods(options) {
|
|
762
|
-
const opts = options || {};
|
|
763
|
-
const { getInstance } = opts;
|
|
764
|
-
const { classConstructor } = opts;
|
|
765
|
-
const { adapters } = opts;
|
|
766
|
-
opts.globalPrefix = opts.globalPrefix || "";
|
|
767
|
-
opts.provide = opts.provide || {};
|
|
768
|
-
const fakeInstance = Object.create(classConstructor.prototype);
|
|
769
|
-
const methods = getInstanceOwnMethods(fakeInstance);
|
|
770
|
-
const meta = getMoostMate().read(classConstructor) || {};
|
|
771
|
-
const ownPrefix = typeof opts.replaceOwnPrefix === "string" ? opts.replaceOwnPrefix : meta.controller?.prefix || "";
|
|
772
|
-
const prefix = `${opts.globalPrefix}/${ownPrefix}`;
|
|
773
|
-
const controllerOverview = {
|
|
774
|
-
meta,
|
|
775
|
-
computedPrefix: prefix,
|
|
776
|
-
type: classConstructor,
|
|
777
|
-
handlers: []
|
|
778
|
-
};
|
|
779
|
-
for (const method of methods) {
|
|
780
|
-
const methodMeta = getMoostMate().read(fakeInstance, method) || {};
|
|
781
|
-
if (!methodMeta.handlers?.length) continue;
|
|
782
|
-
const pipes = mergeSorted(opts.pipes, methodMeta.pipes);
|
|
783
|
-
const getIterceptorHandler = getIterceptorHandlerFactory(mergeSorted(opts.interceptors, meta.interceptors, methodMeta.interceptors), getInstance, pipes);
|
|
784
|
-
const argsPipes = [];
|
|
785
|
-
for (const p of methodMeta.params || []) argsPipes.push({
|
|
786
|
-
meta: p,
|
|
787
|
-
pipes: mergeSorted(pipes, p.pipes)
|
|
788
|
-
});
|
|
789
|
-
const resolveArgs = resolveArguments(argsPipes, {
|
|
790
|
-
classMeta: meta,
|
|
791
|
-
methodMeta,
|
|
792
|
-
type: classConstructor,
|
|
793
|
-
key: method
|
|
794
|
-
});
|
|
795
|
-
const wm = /* @__PURE__ */ new WeakMap();
|
|
796
|
-
controllerOverview.handlers.push(...methodMeta.handlers.map((h) => {
|
|
797
|
-
const data = {
|
|
798
|
-
meta: methodMeta,
|
|
799
|
-
path: h.path,
|
|
800
|
-
type: h.type,
|
|
801
|
-
method,
|
|
802
|
-
handler: h,
|
|
803
|
-
registeredAs: []
|
|
804
|
-
};
|
|
805
|
-
wm.set(h, data);
|
|
806
|
-
return data;
|
|
807
|
-
}));
|
|
808
|
-
for (const adapter of adapters) await adapter.bindHandler({
|
|
809
|
-
prefix,
|
|
810
|
-
fakeInstance,
|
|
811
|
-
getInstance,
|
|
812
|
-
method,
|
|
813
|
-
handlers: methodMeta.handlers,
|
|
814
|
-
getIterceptorHandler,
|
|
815
|
-
resolveArgs,
|
|
816
|
-
controllerName: classConstructor.name,
|
|
817
|
-
logHandler: (eventName) => {
|
|
818
|
-
options.moostInstance.logMappedHandler(eventName, classConstructor, method);
|
|
819
|
-
},
|
|
820
|
-
register(h, path, args) {
|
|
821
|
-
const data = wm.get(h);
|
|
822
|
-
if (data) data.registeredAs.push({
|
|
823
|
-
path,
|
|
824
|
-
args
|
|
825
|
-
});
|
|
826
|
-
}
|
|
827
|
-
});
|
|
828
|
-
}
|
|
829
|
-
return controllerOverview;
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
//#endregion
|
|
833
|
-
//#region packages/moost/src/decorators/circular.decorator.ts
|
|
834
|
-
/**
|
|
835
|
-
* Marks a constructor parameter dependency as circular.
|
|
836
|
-
* The resolver function is called lazily to break the circular reference.
|
|
837
|
-
*
|
|
838
|
-
* @param resolver - Factory that returns the class constructor (e.g. `() => MyService`).
|
|
839
|
-
*
|
|
840
|
-
* @example
|
|
841
|
-
* ```ts
|
|
842
|
-
* class ServiceA {
|
|
843
|
-
* constructor(@Circular(() => ServiceB) private b: ServiceB) {}
|
|
844
|
-
* }
|
|
845
|
-
* ```
|
|
846
|
-
*/ function Circular(resolver) {
|
|
847
|
-
return getMoostMate().decorate("circular", resolver);
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
//#endregion
|
|
851
|
-
//#region packages/moost/src/decorators/common.decorator.ts
|
|
852
|
-
/**
|
|
853
|
-
* Apply Multiple Decorators
|
|
854
|
-
*
|
|
855
|
-
* @param decorators - array of decorators
|
|
856
|
-
* @returns
|
|
857
|
-
*/ function ApplyDecorators(...decorators) {
|
|
858
|
-
return getMoostMate().apply(...decorators);
|
|
859
|
-
}
|
|
860
|
-
/**
|
|
861
|
-
* ## Label
|
|
862
|
-
* ### @Decorator
|
|
863
|
-
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
864
|
-
*
|
|
865
|
-
* Stores Label metadata
|
|
866
|
-
*/ function Label(value) {
|
|
867
|
-
return getMoostMate().decorate("label", value);
|
|
868
|
-
}
|
|
869
|
-
/**
|
|
870
|
-
* ## Description
|
|
871
|
-
* ### @Decorator
|
|
872
|
-
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
873
|
-
*
|
|
874
|
-
* Stores Description metadata
|
|
875
|
-
*/ function Description(value) {
|
|
876
|
-
return getMoostMate().decorate("description", value);
|
|
877
|
-
}
|
|
878
|
-
/**
|
|
879
|
-
* ## Value
|
|
880
|
-
* ### @Decorator
|
|
881
|
-
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
882
|
-
*
|
|
883
|
-
* Stores Value metadata
|
|
884
|
-
*/ function Value(value) {
|
|
885
|
-
return getMoostMate().decorate("value", value);
|
|
886
|
-
}
|
|
887
|
-
/**
|
|
888
|
-
* ## Id
|
|
889
|
-
* ### @Decorator
|
|
890
|
-
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
891
|
-
*
|
|
892
|
-
* Stores Id metadata
|
|
893
|
-
*/ function Id(value) {
|
|
894
|
-
return getMoostMate().decorate("id", value);
|
|
536
|
+
/**
|
|
537
|
+
* ## Id
|
|
538
|
+
* ### @Decorator
|
|
539
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
540
|
+
*
|
|
541
|
+
* Stores Id metadata
|
|
542
|
+
*/ function Id(value) {
|
|
543
|
+
return getMoostMate().decorate("id", value);
|
|
895
544
|
}
|
|
896
545
|
/**
|
|
897
546
|
* ## Optional
|
|
@@ -957,69 +606,24 @@ function ImportController(prefix, controller, provide) {
|
|
|
957
606
|
}
|
|
958
607
|
|
|
959
608
|
//#endregion
|
|
960
|
-
//#region packages/moost/src/decorators/
|
|
609
|
+
//#region packages/moost/src/decorators/resolve.decorator.ts
|
|
961
610
|
/**
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
* @
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
return TInterceptorPriority;
|
|
979
|
-
}({});
|
|
980
|
-
/**
|
|
981
|
-
* ## Intercept
|
|
982
|
-
* ### @Decorator
|
|
983
|
-
* Attach an interceptor to a class or method.
|
|
984
|
-
* @param handler — @Interceptor class constructor or TInterceptorDef object
|
|
985
|
-
* @param priority — interceptor priority (overrides handler's own priority)
|
|
986
|
-
* @param name — interceptor name for tracing
|
|
987
|
-
*/ function Intercept(handler, priority, name) {
|
|
988
|
-
const mate = getMoostMate();
|
|
989
|
-
if (typeof handler === "function") {
|
|
990
|
-
const interceptorMeta = mate.read(handler);
|
|
991
|
-
return mate.decorate("interceptors", {
|
|
992
|
-
handler,
|
|
993
|
-
priority: priority ?? interceptorMeta?.interceptor?.priority ?? 4,
|
|
994
|
-
name: name || handler.name || "<anonymous>"
|
|
995
|
-
}, true);
|
|
996
|
-
}
|
|
997
|
-
return mate.decorate("interceptors", {
|
|
998
|
-
handler,
|
|
999
|
-
priority: priority ?? handler.priority ?? 4,
|
|
1000
|
-
name: name || handler._name || "<anonymous>"
|
|
1001
|
-
}, true);
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
//#endregion
|
|
1005
|
-
//#region packages/moost/src/decorators/resolve.decorator.ts
|
|
1006
|
-
/**
|
|
1007
|
-
* Hook to the Response Status
|
|
1008
|
-
* @decorator
|
|
1009
|
-
* @param resolver - resolver function
|
|
1010
|
-
* @param label - field label
|
|
1011
|
-
* @paramType unknown
|
|
1012
|
-
*/ function Resolve(resolver, label) {
|
|
1013
|
-
return (target, key, index) => {
|
|
1014
|
-
const i = typeof index === "number" ? index : void 0;
|
|
1015
|
-
getMoostMate().decorate("resolver", (metas, level) => {
|
|
1016
|
-
let newLabel = label;
|
|
1017
|
-
if (!newLabel && level === "PROP" && typeof metas.key === "string") newLabel = metas.key;
|
|
1018
|
-
fillLabel(target, key || "", i, newLabel);
|
|
1019
|
-
return resolver(metas, level);
|
|
1020
|
-
})(target, key, i);
|
|
1021
|
-
};
|
|
1022
|
-
}
|
|
611
|
+
* Hook to the Response Status
|
|
612
|
+
* @decorator
|
|
613
|
+
* @param resolver - resolver function
|
|
614
|
+
* @param label - field label
|
|
615
|
+
* @paramType unknown
|
|
616
|
+
*/ function Resolve(resolver, label) {
|
|
617
|
+
return (target, key, index) => {
|
|
618
|
+
const i = typeof index === "number" ? index : void 0;
|
|
619
|
+
getMoostMate().decorate("resolver", (metas, level) => {
|
|
620
|
+
let newLabel = label;
|
|
621
|
+
if (!newLabel && level === "PROP" && typeof metas.key === "string") newLabel = metas.key;
|
|
622
|
+
fillLabel(target, key || "", i, newLabel);
|
|
623
|
+
return resolver(metas, level);
|
|
624
|
+
})(target, key, i);
|
|
625
|
+
};
|
|
626
|
+
}
|
|
1023
627
|
/**
|
|
1024
628
|
* Get Param Value from url parh
|
|
1025
629
|
* @decorator
|
|
@@ -1062,6 +666,122 @@ function fillLabel(target, key, index, name) {
|
|
|
1062
666
|
}
|
|
1063
667
|
}
|
|
1064
668
|
|
|
669
|
+
//#endregion
|
|
670
|
+
//#region packages/moost/src/decorators/handler-paths.decorator.ts
|
|
671
|
+
/**
|
|
672
|
+
* ## HandlerPaths
|
|
673
|
+
* ### @Decorator
|
|
674
|
+
* Injects the actual mounted path(s) (`string[]`) of a handler method on the
|
|
675
|
+
* current controller, read from the post-bind overview. Built for `@MoostInit`
|
|
676
|
+
* method parameters:
|
|
677
|
+
*
|
|
678
|
+
* ```ts
|
|
679
|
+
* @MoostInit()
|
|
680
|
+
* init(@HandlerPaths('refresh') paths: string[]) {
|
|
681
|
+
* const path = paths[0] // e.g. '/api/auth/refresh'
|
|
682
|
+
* }
|
|
683
|
+
* ```
|
|
684
|
+
*
|
|
685
|
+
* `method` defaults to the current context method (useful inside event handlers);
|
|
686
|
+
* pass it explicitly in `@MoostInit`, where the current method is the init method,
|
|
687
|
+
* not the handler. Returns all distinct paths — see {@link useHandlerPaths}.
|
|
688
|
+
*/ function HandlerPaths(method, opts) {
|
|
689
|
+
return Resolve(() => useHandlerPaths(method, opts));
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
//#endregion
|
|
693
|
+
//#region packages/moost/src/decorators/inherit.decorator.ts
|
|
694
|
+
/**
|
|
695
|
+
* ## Inherit
|
|
696
|
+
* ### @Decorator
|
|
697
|
+
* Inherit metadata from super class
|
|
698
|
+
* @returns
|
|
699
|
+
*/ const Inherit = () => getMoostMate().decorate("inherit", true);
|
|
700
|
+
|
|
701
|
+
//#endregion
|
|
702
|
+
//#region packages/moost/src/decorators/init.decorator.ts
|
|
703
|
+
/**
|
|
704
|
+
* ## MoostInit
|
|
705
|
+
* ### @Decorator
|
|
706
|
+
* Marks a controller method to run **once**, after Moost has bound every
|
|
707
|
+
* controller (so the full `getControllersOverview()` is available) and
|
|
708
|
+
* **before** adapters begin serving.
|
|
709
|
+
*
|
|
710
|
+
* The method runs on the controller's SINGLETON instance inside a synthetic
|
|
711
|
+
* init context. Argument injection goes through the RESOLVE pipe only —
|
|
712
|
+
* constructor injection, `@InjectMoost`, `@Inject`, and other `@Resolve`-based
|
|
713
|
+
* params work; transform/validate pipes and interceptors are **not** applied
|
|
714
|
+
* (init is application setup, not an event). Request-scoped composables
|
|
715
|
+
* (`useRequest`, `useHeaders`, `useRouteParams`, …) are unavailable. Async
|
|
716
|
+
* methods are awaited; a throwing hook rejects `Moost.init()` (fail-fast).
|
|
717
|
+
*
|
|
718
|
+
* Applying `@MoostInit` to a `FOR_EVENT` controller is a configuration error and
|
|
719
|
+
* throws at bind time (there is no singleton instance / event at init).
|
|
720
|
+
*
|
|
721
|
+
* @param opts.priority lower runs first across all `@MoostInit` methods (default 0)
|
|
722
|
+
*
|
|
723
|
+
* @example
|
|
724
|
+
* ```ts
|
|
725
|
+
* │ @Controller('auth')
|
|
726
|
+
* │ export class AuthController {
|
|
727
|
+
* │ constructor(private readonly holder: RefreshPathHolder) {}
|
|
728
|
+
* │
|
|
729
|
+
* │ @MoostInit()
|
|
730
|
+
* │ initRefreshCookiePath(@InjectMoost() moost: Moost) {
|
|
731
|
+
* │ // overview is COMPLETE here — this controller's route is mounted
|
|
732
|
+
* │ this.holder.value = resolveRefreshCookiePath(moost)
|
|
733
|
+
* │ }
|
|
734
|
+
* │ }
|
|
735
|
+
* ```
|
|
736
|
+
*/ function MoostInit(opts) {
|
|
737
|
+
return getMoostMate().decorate("moostInit", { priority: opts?.priority ?? 0 }, false);
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* ## InjectMoost
|
|
741
|
+
* ### @Decorator
|
|
742
|
+
* Injects the running {@link Moost} application instance into a method (or
|
|
743
|
+
* constructor) parameter. Intended for `@MoostInit` methods that need the wired
|
|
744
|
+
* app (e.g. `getControllersOverview()`), but works in any DI-resolved position.
|
|
745
|
+
*/ function InjectMoost() {
|
|
746
|
+
return Resolve(() => resolveMoost());
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
//#endregion
|
|
750
|
+
//#region packages/moost/src/decorators/intercept.decorator.ts
|
|
751
|
+
var TInterceptorPriority = /* @__PURE__ */ function(TInterceptorPriority) {
|
|
752
|
+
TInterceptorPriority[TInterceptorPriority["BEFORE_ALL"] = 0] = "BEFORE_ALL";
|
|
753
|
+
TInterceptorPriority[TInterceptorPriority["BEFORE_GUARD"] = 1] = "BEFORE_GUARD";
|
|
754
|
+
TInterceptorPriority[TInterceptorPriority["GUARD"] = 2] = "GUARD";
|
|
755
|
+
TInterceptorPriority[TInterceptorPriority["AFTER_GUARD"] = 3] = "AFTER_GUARD";
|
|
756
|
+
TInterceptorPriority[TInterceptorPriority["INTERCEPTOR"] = 4] = "INTERCEPTOR";
|
|
757
|
+
TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
|
|
758
|
+
TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
|
|
759
|
+
return TInterceptorPriority;
|
|
760
|
+
}({});
|
|
761
|
+
/**
|
|
762
|
+
* ## Intercept
|
|
763
|
+
* ### @Decorator
|
|
764
|
+
* Attach an interceptor to a class or method.
|
|
765
|
+
* @param handler — @Interceptor class constructor or TInterceptorDef object
|
|
766
|
+
* @param priority — interceptor priority (overrides handler's own priority)
|
|
767
|
+
* @param name — interceptor name for tracing
|
|
768
|
+
*/ function Intercept(handler, priority, name) {
|
|
769
|
+
const mate = getMoostMate();
|
|
770
|
+
if (typeof handler === "function") {
|
|
771
|
+
const interceptorMeta = mate.read(handler);
|
|
772
|
+
return mate.decorate("interceptors", {
|
|
773
|
+
handler,
|
|
774
|
+
priority: priority ?? interceptorMeta?.interceptor?.priority ?? 4,
|
|
775
|
+
name: name || handler.name || "<anonymous>"
|
|
776
|
+
}, true);
|
|
777
|
+
}
|
|
778
|
+
return mate.decorate("interceptors", {
|
|
779
|
+
handler,
|
|
780
|
+
priority: priority ?? handler.priority ?? 4,
|
|
781
|
+
name: name || handler._name || "<anonymous>"
|
|
782
|
+
}, true);
|
|
783
|
+
}
|
|
784
|
+
|
|
1065
785
|
//#endregion
|
|
1066
786
|
//#region packages/moost/src/decorators/interceptor.decorator.ts
|
|
1067
787
|
/**
|
|
@@ -1144,9 +864,7 @@ function fillLabel(target, key, index, name) {
|
|
|
1144
864
|
* @returns
|
|
1145
865
|
*/ function InjectMoostLogger(topic) {
|
|
1146
866
|
return Resolve(async (metas) => {
|
|
1147
|
-
const
|
|
1148
|
-
const controller = getController();
|
|
1149
|
-
const moostApp = controller instanceof Moost ? controller : await instantiate(Moost);
|
|
867
|
+
const moostApp = await resolveMoost();
|
|
1150
868
|
const meta = metas.classMeta;
|
|
1151
869
|
return moostApp.getLogger(meta?.loggerTopic || topic || meta?.id);
|
|
1152
870
|
});
|
|
@@ -1236,119 +954,502 @@ function fillLabel(target, key, index, name) {
|
|
|
1236
954
|
*/ function InjectFromScope(name) {
|
|
1237
955
|
return getMoostMate().decorate("fromScope", name);
|
|
1238
956
|
}
|
|
1239
|
-
/**
|
|
1240
|
-
* Inject vars from scope for instances
|
|
1241
|
-
* instantiated with `@InjectFromScope` decorator
|
|
1242
|
-
*/ function InjectScopeVars(name) {
|
|
1243
|
-
return Resolve(({ scopeId }) => {
|
|
1244
|
-
if (scopeId) return name ? getInfactScopeVars(scopeId)?.[name] : getInfactScopeVars(scopeId);
|
|
957
|
+
/**
|
|
958
|
+
* Inject vars from scope for instances
|
|
959
|
+
* instantiated with `@InjectFromScope` decorator
|
|
960
|
+
*/ function InjectScopeVars(name) {
|
|
961
|
+
return Resolve(({ scopeId }) => {
|
|
962
|
+
if (scopeId) return name ? getInfactScopeVars(scopeId)?.[name] : getInfactScopeVars(scopeId);
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
//#endregion
|
|
967
|
+
//#region packages/moost/src/define.ts
|
|
968
|
+
/**
|
|
969
|
+
* Define a before-phase interceptor.
|
|
970
|
+
*
|
|
971
|
+
* Runs before argument resolution and handler execution.
|
|
972
|
+
* Call `reply(value)` to short-circuit the handler and respond early.
|
|
973
|
+
*
|
|
974
|
+
* @example
|
|
975
|
+
* ```ts
|
|
976
|
+
* const authGuard = defineBeforeInterceptor((reply) => {
|
|
977
|
+
* if (!isAuthenticated()) reply(new HttpError(401))
|
|
978
|
+
* }, TInterceptorPriority.GUARD)
|
|
979
|
+
* ```
|
|
980
|
+
*/ function defineBeforeInterceptor(fn, priority = TInterceptorPriority.INTERCEPTOR) {
|
|
981
|
+
return {
|
|
982
|
+
before: fn,
|
|
983
|
+
priority
|
|
984
|
+
};
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* Define an after-phase interceptor.
|
|
988
|
+
*
|
|
989
|
+
* Runs after successful handler execution.
|
|
990
|
+
* Call `reply(value)` to transform/replace the response.
|
|
991
|
+
*
|
|
992
|
+
* @example
|
|
993
|
+
* ```ts
|
|
994
|
+
* const setHeader = defineAfterInterceptor(() => {
|
|
995
|
+
* useResponse().setHeader('x-server', 'my-server')
|
|
996
|
+
* }, TInterceptorPriority.AFTER_ALL)
|
|
997
|
+
* ```
|
|
998
|
+
*/ function defineAfterInterceptor(fn, priority = TInterceptorPriority.AFTER_ALL) {
|
|
999
|
+
return {
|
|
1000
|
+
after: fn,
|
|
1001
|
+
priority
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Define an error-phase interceptor.
|
|
1006
|
+
*
|
|
1007
|
+
* Runs when the handler throws or returns an Error.
|
|
1008
|
+
* Call `reply(value)` to recover from the error with a replacement response.
|
|
1009
|
+
*
|
|
1010
|
+
* @example
|
|
1011
|
+
* ```ts
|
|
1012
|
+
* const errorFormatter = defineErrorInterceptor((error, reply) => {
|
|
1013
|
+
* reply({ message: error.message, status: 500 })
|
|
1014
|
+
* }, TInterceptorPriority.CATCH_ERROR)
|
|
1015
|
+
* ```
|
|
1016
|
+
*/ function defineErrorInterceptor(fn, priority = TInterceptorPriority.CATCH_ERROR) {
|
|
1017
|
+
return {
|
|
1018
|
+
error: fn,
|
|
1019
|
+
priority
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* Define a full interceptor with multiple lifecycle hooks.
|
|
1024
|
+
*
|
|
1025
|
+
* @example
|
|
1026
|
+
* ```ts
|
|
1027
|
+
* const myInterceptor = defineInterceptor({
|
|
1028
|
+
* before(reply) { ... },
|
|
1029
|
+
* after(response, reply) { ... },
|
|
1030
|
+
* error(error, reply) { ... },
|
|
1031
|
+
* }, TInterceptorPriority.INTERCEPTOR)
|
|
1032
|
+
* ```
|
|
1033
|
+
*/ function defineInterceptor(def, priority = TInterceptorPriority.INTERCEPTOR) {
|
|
1034
|
+
def.priority = priority;
|
|
1035
|
+
return def;
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* ### Define Pipe Function
|
|
1039
|
+
*
|
|
1040
|
+
* ```ts
|
|
1041
|
+
* // example of a transform pipe
|
|
1042
|
+
* const uppercaseTransformPipe = definePipeFn((value, metas, level) => {
|
|
1043
|
+
* return typeof value === 'string' ? value.toUpperCase() : value
|
|
1044
|
+
* },
|
|
1045
|
+
* TPipePriority.TRANSFORM,
|
|
1046
|
+
* )
|
|
1047
|
+
* ```
|
|
1048
|
+
*
|
|
1049
|
+
* @param fn pipe function
|
|
1050
|
+
* @param priority priority of the pipe
|
|
1051
|
+
* @returns
|
|
1052
|
+
*/ function definePipeFn(fn, priority = TPipePriority.TRANSFORM) {
|
|
1053
|
+
fn.priority = priority;
|
|
1054
|
+
return fn;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
//#endregion
|
|
1058
|
+
//#region packages/moost/src/pipes/resolve.pipe.ts
|
|
1059
|
+
const resolvePipe = definePipeFn((_value, metas, level) => {
|
|
1060
|
+
const resolver = metas.targetMeta?.resolver;
|
|
1061
|
+
if (resolver) return resolver(metas, level);
|
|
1062
|
+
}, TPipePriority.RESOLVE);
|
|
1063
|
+
|
|
1064
|
+
//#endregion
|
|
1065
|
+
//#region packages/moost/src/pipes/shared-pipes.ts
|
|
1066
|
+
const sharedPipes = [{
|
|
1067
|
+
handler: resolvePipe,
|
|
1068
|
+
priority: TPipePriority.RESOLVE
|
|
1069
|
+
}];
|
|
1070
|
+
|
|
1071
|
+
//#endregion
|
|
1072
|
+
//#region packages/moost/src/resolve-arguments.ts
|
|
1073
|
+
/**
|
|
1074
|
+
* Builds an argument-resolver function from pre-computed per-parameter pipe lists.
|
|
1075
|
+
*
|
|
1076
|
+
* Returns `undefined` when there are no parameters to resolve.
|
|
1077
|
+
* The returned function runs pipes for each parameter and returns
|
|
1078
|
+
* `unknown[]` synchronously when possible, or `Promise<unknown[]>` otherwise.
|
|
1079
|
+
*/ function resolveArguments(argsPipes, context) {
|
|
1080
|
+
if (argsPipes.length === 0) return;
|
|
1081
|
+
return () => {
|
|
1082
|
+
const args = [];
|
|
1083
|
+
let hasAsync = false;
|
|
1084
|
+
for (let i = 0; i < argsPipes.length; i++) {
|
|
1085
|
+
const { pipes, meta: paramMeta } = argsPipes[i];
|
|
1086
|
+
const result = runPipes(pipes, void 0, {
|
|
1087
|
+
classMeta: context.classMeta,
|
|
1088
|
+
methodMeta: context.methodMeta,
|
|
1089
|
+
paramMeta,
|
|
1090
|
+
type: context.type,
|
|
1091
|
+
key: context.key,
|
|
1092
|
+
index: i,
|
|
1093
|
+
targetMeta: paramMeta,
|
|
1094
|
+
instantiate: (t) => useControllerContext().instantiate(t)
|
|
1095
|
+
}, "PARAM");
|
|
1096
|
+
if (!hasAsync && isThenable(result)) hasAsync = true;
|
|
1097
|
+
args[i] = result;
|
|
1098
|
+
}
|
|
1099
|
+
return hasAsync ? Promise.all(args) : args;
|
|
1100
|
+
};
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
//#endregion
|
|
1104
|
+
//#region packages/moost/src/interceptor-handler.ts
|
|
1105
|
+
function _define_property$1(obj, key, value) {
|
|
1106
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
1107
|
+
value,
|
|
1108
|
+
enumerable: true,
|
|
1109
|
+
configurable: true,
|
|
1110
|
+
writable: true
|
|
1111
|
+
});
|
|
1112
|
+
else obj[key] = value;
|
|
1113
|
+
return obj;
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Manages the before/after/error interceptor lifecycle for a single event.
|
|
1117
|
+
* Optimised for the common sync path — only allocates promises when an interceptor goes async.
|
|
1118
|
+
*/ var InterceptorHandler = class {
|
|
1119
|
+
getReplyFn() {
|
|
1120
|
+
return this._boundReplyFn ?? (this._boundReplyFn = (reply) => {
|
|
1121
|
+
this.response = reply;
|
|
1122
|
+
this.responseOverwritten = true;
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
get count() {
|
|
1126
|
+
return this.handlers.length;
|
|
1127
|
+
}
|
|
1128
|
+
get countAfter() {
|
|
1129
|
+
return this.after?.length ?? 0;
|
|
1130
|
+
}
|
|
1131
|
+
get countOnError() {
|
|
1132
|
+
return this.onError?.length ?? 0;
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Register hooks from a TInterceptorDef.
|
|
1136
|
+
* Returns a pending PromiseLike if `before` went async, or undefined.
|
|
1137
|
+
*/ registerDef(def, entry, ci) {
|
|
1138
|
+
if (def.after) (this.after ?? (this.after = [])).unshift({
|
|
1139
|
+
name: entry.name,
|
|
1140
|
+
fn: def.after
|
|
1141
|
+
});
|
|
1142
|
+
if (def.error) (this.onError ?? (this.onError = [])).unshift({
|
|
1143
|
+
name: entry.name,
|
|
1144
|
+
fn: def.error
|
|
1145
|
+
});
|
|
1146
|
+
if (def.before) {
|
|
1147
|
+
const spanName = entry.spanName;
|
|
1148
|
+
const result = ci ? ci.with(spanName, { "moost.interceptor.stage": "before" }, () => def.before?.(this.getReplyFn())) : def.before(this.getReplyFn());
|
|
1149
|
+
if (isThenable(result)) return result;
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
before() {
|
|
1153
|
+
const ci = getContextInjector$1();
|
|
1154
|
+
for (let i = 0; i < this.handlers.length; i++) {
|
|
1155
|
+
const entry = this.handlers[i];
|
|
1156
|
+
const { handler } = entry;
|
|
1157
|
+
if (typeof handler === "function") {
|
|
1158
|
+
const factoryResult = handler();
|
|
1159
|
+
if (isThenable(factoryResult)) return this._beforeAsyncFactory(ci, factoryResult, i);
|
|
1160
|
+
const pending = this.registerDef(factoryResult, entry, ci);
|
|
1161
|
+
if (pending) return this._beforeAsyncPending(ci, pending, i);
|
|
1162
|
+
if (this.responseOverwritten) return this.response;
|
|
1163
|
+
} else {
|
|
1164
|
+
const pending = this.registerDef(handler, entry, ci);
|
|
1165
|
+
if (pending) return this._beforeAsyncPending(ci, pending, i);
|
|
1166
|
+
if (this.responseOverwritten) return this.response;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
async _beforeAsyncFactory(ci, factoryPromise, startIndex) {
|
|
1171
|
+
const def = await factoryPromise;
|
|
1172
|
+
const entry = this.handlers[startIndex];
|
|
1173
|
+
const pending = this.registerDef(def, entry, ci);
|
|
1174
|
+
if (pending) await pending;
|
|
1175
|
+
if (this.responseOverwritten) return this.response;
|
|
1176
|
+
return this._beforeFrom(ci, startIndex + 1);
|
|
1177
|
+
}
|
|
1178
|
+
async _beforeAsyncPending(ci, pending, startIndex) {
|
|
1179
|
+
await pending;
|
|
1180
|
+
if (this.responseOverwritten) return this.response;
|
|
1181
|
+
return this._beforeFrom(ci, startIndex + 1);
|
|
1182
|
+
}
|
|
1183
|
+
async _beforeFrom(ci, startIndex) {
|
|
1184
|
+
for (let i = startIndex; i < this.handlers.length; i++) {
|
|
1185
|
+
const entry = this.handlers[i];
|
|
1186
|
+
const { handler } = entry;
|
|
1187
|
+
let def;
|
|
1188
|
+
if (typeof handler === "function") def = await handler();
|
|
1189
|
+
else def = handler;
|
|
1190
|
+
const pending = this.registerDef(def, entry, ci);
|
|
1191
|
+
if (pending) await pending;
|
|
1192
|
+
if (this.responseOverwritten) return this.response;
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
fireAfter(response) {
|
|
1196
|
+
this.response = response;
|
|
1197
|
+
const isError = response instanceof Error;
|
|
1198
|
+
const handlers = isError ? this.onError : this.after;
|
|
1199
|
+
if (!handlers) return this.response;
|
|
1200
|
+
const ci = getContextInjector$1();
|
|
1201
|
+
const stage = isError ? "onError" : "after";
|
|
1202
|
+
for (let i = 0; i < handlers.length; i++) {
|
|
1203
|
+
const { name, fn } = handlers[i];
|
|
1204
|
+
const result = ci ? ci.with(`Interceptor:${name}`, { "moost.interceptor.stage": stage }, () => fn(response, this.getReplyFn())) : fn(response, this.getReplyFn());
|
|
1205
|
+
if (isThenable(result)) return this._fireAfterAsync({
|
|
1206
|
+
ci,
|
|
1207
|
+
handlers,
|
|
1208
|
+
stage,
|
|
1209
|
+
response
|
|
1210
|
+
}, result, i);
|
|
1211
|
+
}
|
|
1212
|
+
return this.response;
|
|
1213
|
+
}
|
|
1214
|
+
async _fireAfterAsync(ctx, pending, startIndex) {
|
|
1215
|
+
await pending;
|
|
1216
|
+
for (let i = startIndex + 1; i < ctx.handlers.length; i++) {
|
|
1217
|
+
const { name, fn } = ctx.handlers[i];
|
|
1218
|
+
if (ctx.ci) await ctx.ci.with(`Interceptor:${name}`, { "moost.interceptor.stage": ctx.stage }, () => fn(ctx.response, this.getReplyFn()));
|
|
1219
|
+
else await fn(ctx.response, this.getReplyFn());
|
|
1220
|
+
}
|
|
1221
|
+
return this.response;
|
|
1222
|
+
}
|
|
1223
|
+
constructor(handlers) {
|
|
1224
|
+
_define_property$1(this, "handlers", void 0);
|
|
1225
|
+
_define_property$1(this, "after", void 0);
|
|
1226
|
+
_define_property$1(this, "onError", void 0);
|
|
1227
|
+
_define_property$1(this, "response", void 0);
|
|
1228
|
+
_define_property$1(this, "responseOverwritten", void 0);
|
|
1229
|
+
_define_property$1(this, "_boundReplyFn", void 0);
|
|
1230
|
+
this.handlers = handlers;
|
|
1231
|
+
this.responseOverwritten = false;
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
|
|
1235
|
+
//#endregion
|
|
1236
|
+
//#region packages/moost/src/utils.ts
|
|
1237
|
+
const mate = getMoostMate();
|
|
1238
|
+
const noInterceptors = () => void 0;
|
|
1239
|
+
function findInterceptorMethods(handler) {
|
|
1240
|
+
const fakeInstance = Object.create(handler.prototype);
|
|
1241
|
+
const methods = getInstanceOwnMethods(fakeInstance);
|
|
1242
|
+
const result = {};
|
|
1243
|
+
for (const method of methods) {
|
|
1244
|
+
const hook = mate.read(fakeInstance, method)?.interceptorHook;
|
|
1245
|
+
if (hook === "before" || hook === "after" || hook === "error") result[hook] = method;
|
|
1246
|
+
}
|
|
1247
|
+
return result;
|
|
1248
|
+
}
|
|
1249
|
+
function buildMethodResolver(opts) {
|
|
1250
|
+
const fakeInstance = Object.create(opts.handler.prototype);
|
|
1251
|
+
const methodMeta = mate.read(fakeInstance, opts.methodName) || {};
|
|
1252
|
+
const argsPipes = [];
|
|
1253
|
+
for (const p of methodMeta.params || []) argsPipes.push({
|
|
1254
|
+
meta: p,
|
|
1255
|
+
pipes: mergeSorted(opts.pipes, p.pipes)
|
|
1256
|
+
});
|
|
1257
|
+
if (argsPipes.length === 0) return;
|
|
1258
|
+
return resolveArguments(argsPipes, {
|
|
1259
|
+
classMeta: opts.classMeta,
|
|
1260
|
+
methodMeta,
|
|
1261
|
+
type: opts.handler,
|
|
1262
|
+
key: opts.methodName
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
function callWithArgs(instance, methodName, resolveArgs) {
|
|
1266
|
+
const ci = getContextInjector$1();
|
|
1267
|
+
const args = ci ? ci.with("Arguments:resolve", resolveArgs) : resolveArgs();
|
|
1268
|
+
if (isThenable(args)) return args.then((a) => instance[methodName](...a));
|
|
1269
|
+
return instance[methodName](...args);
|
|
1270
|
+
}
|
|
1271
|
+
function callMethod(instance, methodName, resolveArgs) {
|
|
1272
|
+
if (resolveArgs) return callWithArgs(instance, methodName, resolveArgs);
|
|
1273
|
+
return instance[methodName]();
|
|
1274
|
+
}
|
|
1275
|
+
function createClassInterceptorFactory(opts) {
|
|
1276
|
+
const infact = getMoostInfact();
|
|
1277
|
+
function buildDef(instance) {
|
|
1278
|
+
const def = {};
|
|
1279
|
+
if (opts.methods.before) {
|
|
1280
|
+
const methodName = opts.methods.before;
|
|
1281
|
+
const resolveArgs = opts.resolvers[methodName];
|
|
1282
|
+
if (resolveArgs) def.before = (reply) => {
|
|
1283
|
+
setOvertake(reply);
|
|
1284
|
+
return callWithArgs(instance, methodName, resolveArgs);
|
|
1285
|
+
};
|
|
1286
|
+
else def.before = () => instance[methodName]();
|
|
1287
|
+
}
|
|
1288
|
+
if (opts.methods.after) {
|
|
1289
|
+
const methodName = opts.methods.after;
|
|
1290
|
+
const resolveArgs = opts.resolvers[methodName];
|
|
1291
|
+
def.after = (response, reply) => {
|
|
1292
|
+
setOvertake(reply);
|
|
1293
|
+
setInterceptResult(response);
|
|
1294
|
+
return callMethod(instance, methodName, resolveArgs);
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
if (opts.methods.error) {
|
|
1298
|
+
const methodName = opts.methods.error;
|
|
1299
|
+
const resolveArgs = opts.resolvers[methodName];
|
|
1300
|
+
def.error = (error, reply) => {
|
|
1301
|
+
setOvertake(reply);
|
|
1302
|
+
setInterceptResult(error);
|
|
1303
|
+
return callMethod(instance, methodName, resolveArgs);
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
return def;
|
|
1307
|
+
}
|
|
1308
|
+
function fromTarget(targetInstance) {
|
|
1309
|
+
const result = infact.getForInstance(targetInstance, opts.handler, { customData: { pipes: opts.pipes } });
|
|
1310
|
+
if (isThenable(result)) return result.then(buildDef);
|
|
1311
|
+
return buildDef(result);
|
|
1312
|
+
}
|
|
1313
|
+
return () => {
|
|
1314
|
+
const result = opts.getTargetInstance();
|
|
1315
|
+
if (isThenable(result)) return result.then(fromTarget);
|
|
1316
|
+
return fromTarget(result);
|
|
1317
|
+
};
|
|
1318
|
+
}
|
|
1319
|
+
function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes) {
|
|
1320
|
+
if (interceptors.length === 0) return noInterceptors;
|
|
1321
|
+
const precomputedHandlers = interceptors.map(({ handler, name }) => {
|
|
1322
|
+
const spanName = `Interceptor:${name}`;
|
|
1323
|
+
if (typeof handler !== "function") return {
|
|
1324
|
+
handler,
|
|
1325
|
+
name,
|
|
1326
|
+
spanName
|
|
1327
|
+
};
|
|
1328
|
+
const interceptorMeta = mate.read(handler);
|
|
1329
|
+
if (!interceptorMeta?.interceptor) throw new Error(`Invalid interceptor "${name}": must be TInterceptorDef or @Interceptor class`);
|
|
1330
|
+
const classMeta = interceptorMeta;
|
|
1331
|
+
const methods = findInterceptorMethods(handler);
|
|
1332
|
+
const mergedPipes = mergeSorted(pipes, classMeta.pipes);
|
|
1333
|
+
const resolvers = {};
|
|
1334
|
+
for (const hook of [
|
|
1335
|
+
"before",
|
|
1336
|
+
"after",
|
|
1337
|
+
"error"
|
|
1338
|
+
]) {
|
|
1339
|
+
const methodName = methods[hook];
|
|
1340
|
+
if (methodName) resolvers[methodName] = buildMethodResolver({
|
|
1341
|
+
handler,
|
|
1342
|
+
methodName,
|
|
1343
|
+
pipes: mergedPipes,
|
|
1344
|
+
classMeta
|
|
1345
|
+
});
|
|
1346
|
+
}
|
|
1347
|
+
return {
|
|
1348
|
+
handler: createClassInterceptorFactory({
|
|
1349
|
+
handler,
|
|
1350
|
+
methods,
|
|
1351
|
+
resolvers,
|
|
1352
|
+
getTargetInstance,
|
|
1353
|
+
pipes: mergedPipes
|
|
1354
|
+
}),
|
|
1355
|
+
name,
|
|
1356
|
+
spanName
|
|
1357
|
+
};
|
|
1245
1358
|
});
|
|
1359
|
+
return () => new InterceptorHandler(precomputedHandlers);
|
|
1246
1360
|
}
|
|
1247
1361
|
|
|
1248
1362
|
//#endregion
|
|
1249
|
-
//#region packages/moost/src/
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
}
|
|
1268
|
-
/**
|
|
1269
|
-
* Define an after-phase interceptor.
|
|
1270
|
-
*
|
|
1271
|
-
* Runs after successful handler execution.
|
|
1272
|
-
* Call `reply(value)` to transform/replace the response.
|
|
1273
|
-
*
|
|
1274
|
-
* @example
|
|
1275
|
-
* ```ts
|
|
1276
|
-
* const setHeader = defineAfterInterceptor(() => {
|
|
1277
|
-
* useResponse().setHeader('x-server', 'my-server')
|
|
1278
|
-
* }, TInterceptorPriority.AFTER_ALL)
|
|
1279
|
-
* ```
|
|
1280
|
-
*/ function defineAfterInterceptor(fn, priority = TInterceptorPriority.AFTER_ALL) {
|
|
1281
|
-
return {
|
|
1282
|
-
after: fn,
|
|
1283
|
-
priority
|
|
1284
|
-
};
|
|
1285
|
-
}
|
|
1286
|
-
/**
|
|
1287
|
-
* Define an error-phase interceptor.
|
|
1288
|
-
*
|
|
1289
|
-
* Runs when the handler throws or returns an Error.
|
|
1290
|
-
* Call `reply(value)` to recover from the error with a replacement response.
|
|
1291
|
-
*
|
|
1292
|
-
* @example
|
|
1293
|
-
* ```ts
|
|
1294
|
-
* const errorFormatter = defineErrorInterceptor((error, reply) => {
|
|
1295
|
-
* reply({ message: error.message, status: 500 })
|
|
1296
|
-
* }, TInterceptorPriority.CATCH_ERROR)
|
|
1297
|
-
* ```
|
|
1298
|
-
*/ function defineErrorInterceptor(fn, priority = TInterceptorPriority.CATCH_ERROR) {
|
|
1299
|
-
return {
|
|
1300
|
-
error: fn,
|
|
1301
|
-
priority
|
|
1363
|
+
//#region packages/moost/src/binding/bind-controller.ts
|
|
1364
|
+
async function bindControllerMethods(options) {
|
|
1365
|
+
const opts = options || {};
|
|
1366
|
+
const { getInstance } = opts;
|
|
1367
|
+
const { classConstructor } = opts;
|
|
1368
|
+
const { adapters } = opts;
|
|
1369
|
+
opts.globalPrefix = opts.globalPrefix || "";
|
|
1370
|
+
opts.provide = opts.provide || {};
|
|
1371
|
+
const fakeInstance = Object.create(classConstructor.prototype);
|
|
1372
|
+
const methods = getInstanceOwnMethods(fakeInstance);
|
|
1373
|
+
const meta = getMoostMate().read(classConstructor) || {};
|
|
1374
|
+
const ownPrefix = typeof opts.replaceOwnPrefix === "string" ? opts.replaceOwnPrefix : meta.controller?.prefix || "";
|
|
1375
|
+
const prefix = `${opts.globalPrefix}/${ownPrefix}`;
|
|
1376
|
+
const controllerOverview = {
|
|
1377
|
+
meta,
|
|
1378
|
+
computedPrefix: prefix,
|
|
1379
|
+
type: classConstructor,
|
|
1380
|
+
handlers: []
|
|
1302
1381
|
};
|
|
1382
|
+
for (const method of methods) {
|
|
1383
|
+
const methodMeta = getMoostMate().read(fakeInstance, method) || {};
|
|
1384
|
+
if (methodMeta.moostInit) {
|
|
1385
|
+
if (meta.injectable === "FOR_EVENT") throw new Error(`@MoostInit is not allowed on a FOR_EVENT controller (${classConstructor.name}.${String(method)}). Init hooks run once at boot on the SINGLETON instance; FOR_EVENT controllers have no init-time instance.`);
|
|
1386
|
+
const initArgsPipes = (methodMeta.params || []).map((p) => ({
|
|
1387
|
+
meta: p,
|
|
1388
|
+
pipes: sharedPipes
|
|
1389
|
+
}));
|
|
1390
|
+
options.registerInitHook?.({
|
|
1391
|
+
priority: methodMeta.moostInit.priority,
|
|
1392
|
+
method,
|
|
1393
|
+
computedPrefix: prefix,
|
|
1394
|
+
getInstance,
|
|
1395
|
+
resolveArgs: resolveArguments(initArgsPipes, {
|
|
1396
|
+
classMeta: meta,
|
|
1397
|
+
methodMeta,
|
|
1398
|
+
type: classConstructor,
|
|
1399
|
+
key: method
|
|
1400
|
+
})
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
1403
|
+
if (!methodMeta.handlers?.length) continue;
|
|
1404
|
+
const pipes = mergeSorted(opts.pipes, methodMeta.pipes);
|
|
1405
|
+
const getIterceptorHandler = getIterceptorHandlerFactory(mergeSorted(opts.interceptors, meta.interceptors, methodMeta.interceptors), getInstance, pipes);
|
|
1406
|
+
const argsPipes = [];
|
|
1407
|
+
for (const p of methodMeta.params || []) argsPipes.push({
|
|
1408
|
+
meta: p,
|
|
1409
|
+
pipes: mergeSorted(pipes, p.pipes)
|
|
1410
|
+
});
|
|
1411
|
+
const resolveArgs = resolveArguments(argsPipes, {
|
|
1412
|
+
classMeta: meta,
|
|
1413
|
+
methodMeta,
|
|
1414
|
+
type: classConstructor,
|
|
1415
|
+
key: method
|
|
1416
|
+
});
|
|
1417
|
+
const wm = /* @__PURE__ */ new WeakMap();
|
|
1418
|
+
controllerOverview.handlers.push(...methodMeta.handlers.map((h) => {
|
|
1419
|
+
const data = {
|
|
1420
|
+
meta: methodMeta,
|
|
1421
|
+
path: h.path,
|
|
1422
|
+
type: h.type,
|
|
1423
|
+
method,
|
|
1424
|
+
handler: h,
|
|
1425
|
+
registeredAs: []
|
|
1426
|
+
};
|
|
1427
|
+
wm.set(h, data);
|
|
1428
|
+
return data;
|
|
1429
|
+
}));
|
|
1430
|
+
for (const adapter of adapters) await adapter.bindHandler({
|
|
1431
|
+
prefix,
|
|
1432
|
+
fakeInstance,
|
|
1433
|
+
getInstance,
|
|
1434
|
+
method,
|
|
1435
|
+
handlers: methodMeta.handlers,
|
|
1436
|
+
getIterceptorHandler,
|
|
1437
|
+
resolveArgs,
|
|
1438
|
+
controllerName: classConstructor.name,
|
|
1439
|
+
logHandler: (eventName) => {
|
|
1440
|
+
options.moostInstance.logMappedHandler(eventName, classConstructor, method);
|
|
1441
|
+
},
|
|
1442
|
+
register(h, path, args) {
|
|
1443
|
+
const data = wm.get(h);
|
|
1444
|
+
if (data) data.registeredAs.push({
|
|
1445
|
+
path,
|
|
1446
|
+
args
|
|
1447
|
+
});
|
|
1448
|
+
}
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
return controllerOverview;
|
|
1303
1452
|
}
|
|
1304
|
-
/**
|
|
1305
|
-
* Define a full interceptor with multiple lifecycle hooks.
|
|
1306
|
-
*
|
|
1307
|
-
* @example
|
|
1308
|
-
* ```ts
|
|
1309
|
-
* const myInterceptor = defineInterceptor({
|
|
1310
|
-
* before(reply) { ... },
|
|
1311
|
-
* after(response, reply) { ... },
|
|
1312
|
-
* error(error, reply) { ... },
|
|
1313
|
-
* }, TInterceptorPriority.INTERCEPTOR)
|
|
1314
|
-
* ```
|
|
1315
|
-
*/ function defineInterceptor(def, priority = TInterceptorPriority.INTERCEPTOR) {
|
|
1316
|
-
def.priority = priority;
|
|
1317
|
-
return def;
|
|
1318
|
-
}
|
|
1319
|
-
/**
|
|
1320
|
-
* ### Define Pipe Function
|
|
1321
|
-
*
|
|
1322
|
-
* ```ts
|
|
1323
|
-
* // example of a transform pipe
|
|
1324
|
-
* const uppercaseTransformPipe = definePipeFn((value, metas, level) => {
|
|
1325
|
-
* return typeof value === 'string' ? value.toUpperCase() : value
|
|
1326
|
-
* },
|
|
1327
|
-
* TPipePriority.TRANSFORM,
|
|
1328
|
-
* )
|
|
1329
|
-
* ```
|
|
1330
|
-
*
|
|
1331
|
-
* @param fn pipe function
|
|
1332
|
-
* @param priority priority of the pipe
|
|
1333
|
-
* @returns
|
|
1334
|
-
*/ function definePipeFn(fn, priority = TPipePriority.TRANSFORM) {
|
|
1335
|
-
fn.priority = priority;
|
|
1336
|
-
return fn;
|
|
1337
|
-
}
|
|
1338
|
-
|
|
1339
|
-
//#endregion
|
|
1340
|
-
//#region packages/moost/src/pipes/resolve.pipe.ts
|
|
1341
|
-
const resolvePipe = definePipeFn((_value, metas, level) => {
|
|
1342
|
-
const resolver = metas.targetMeta?.resolver;
|
|
1343
|
-
if (resolver) return resolver(metas, level);
|
|
1344
|
-
}, TPipePriority.RESOLVE);
|
|
1345
|
-
|
|
1346
|
-
//#endregion
|
|
1347
|
-
//#region packages/moost/src/pipes/shared-pipes.ts
|
|
1348
|
-
const sharedPipes = [{
|
|
1349
|
-
handler: resolvePipe,
|
|
1350
|
-
priority: TPipePriority.RESOLVE
|
|
1351
|
-
}];
|
|
1352
1453
|
|
|
1353
1454
|
//#endregion
|
|
1354
1455
|
//#region packages/moost/src/moost.ts
|
|
@@ -1440,6 +1541,29 @@ function _define_property(obj, key, value) {
|
|
|
1440
1541
|
return this.controllersOverview;
|
|
1441
1542
|
}
|
|
1442
1543
|
/**
|
|
1544
|
+
* @internal Memoized index of the controllers overview (controller class →
|
|
1545
|
+
* method name → handler records), built once and reused for fast handler
|
|
1546
|
+
* lookups (see `getHandlerPaths`). Rebuilt whenever controllers are (re)bound.
|
|
1547
|
+
*/ getHandlerOverviewIndex() {
|
|
1548
|
+
if (!this.handlerOverviewIndex) {
|
|
1549
|
+
const index = /* @__PURE__ */ new Map();
|
|
1550
|
+
for (const c of this.controllersOverview) {
|
|
1551
|
+
let byMethod = index.get(c.type);
|
|
1552
|
+
if (!byMethod) {
|
|
1553
|
+
byMethod = /* @__PURE__ */ new Map();
|
|
1554
|
+
index.set(c.type, byMethod);
|
|
1555
|
+
}
|
|
1556
|
+
for (const h of c.handlers) {
|
|
1557
|
+
const list = byMethod.get(h.method);
|
|
1558
|
+
if (list) list.push(h);
|
|
1559
|
+
else byMethod.set(h.method, [h]);
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
this.handlerOverviewIndex = index;
|
|
1563
|
+
}
|
|
1564
|
+
return this.handlerOverviewIndex;
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1443
1567
|
* ### init
|
|
1444
1568
|
* Ititializes adapter. Must be called after adapters are attached.
|
|
1445
1569
|
*/ async init() {
|
|
@@ -1451,8 +1575,26 @@ function _define_property(obj, key, value) {
|
|
|
1451
1575
|
}
|
|
1452
1576
|
this.unregisteredControllers.unshift(this);
|
|
1453
1577
|
await this.bindControllers();
|
|
1578
|
+
await this.runInitHooks();
|
|
1454
1579
|
for (const a of this.adapters) await (a.onInit && a.onInit(this));
|
|
1455
1580
|
}
|
|
1581
|
+
/**
|
|
1582
|
+
* Runs every `@MoostInit`-decorated controller method exactly once, after all
|
|
1583
|
+
* controllers are bound (complete `getControllersOverview()`) and before the
|
|
1584
|
+
* `adapter.onInit` loop. Hooks run in ascending `priority`, then registration
|
|
1585
|
+
* order. Each runs on its controller's SINGLETON instance inside a synthetic
|
|
1586
|
+
* init context (no interceptors; params resolve via the RESOLVE pipe only).
|
|
1587
|
+
* A throwing hook rejects `init()` (fail-fast).
|
|
1588
|
+
*/ async runInitHooks() {
|
|
1589
|
+
if (this.initHooks.length === 0) return;
|
|
1590
|
+
const hooks = this.initHooks.toSorted((a, b) => a.priority - b.priority);
|
|
1591
|
+
for (const hook of hooks) await createEventContext$1({ logger: this.logger }, async () => {
|
|
1592
|
+
const instance = await hook.getInstance();
|
|
1593
|
+
setControllerContext(instance, hook.method, "", { prefix: hook.computedPrefix });
|
|
1594
|
+
const args = hook.resolveArgs ? await hook.resolveArgs() : [];
|
|
1595
|
+
await instance[hook.method](...args);
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1456
1598
|
async bindControllers() {
|
|
1457
1599
|
const thisMeta = getMoostMate().read(this);
|
|
1458
1600
|
const provide = {
|
|
@@ -1507,8 +1649,10 @@ function _define_property(obj, key, value) {
|
|
|
1507
1649
|
provide: classMeta?.provide,
|
|
1508
1650
|
replace: classMeta?.replace,
|
|
1509
1651
|
logger: this.logger,
|
|
1510
|
-
moostInstance: this
|
|
1652
|
+
moostInstance: this,
|
|
1653
|
+
registerInitHook: (hook) => this.initHooks.push(hook)
|
|
1511
1654
|
}));
|
|
1655
|
+
this.handlerOverviewIndex = void 0;
|
|
1512
1656
|
if (classMeta?.importController) {
|
|
1513
1657
|
const prefix = typeof replaceOwnPrefix === "string" ? replaceOwnPrefix : classMeta.controller?.prefix;
|
|
1514
1658
|
const mergedProvide = {
|
|
@@ -1608,7 +1752,7 @@ function _define_property(obj, key, value) {
|
|
|
1608
1752
|
this.logger.info(`${prefix || ""}${c}${eventName} ${"\x1B[0m\x1B[2m\x1B[32m" + c}→ ${classConstructor.name}.${"\x1B[36m" + c}${method}[32m()${coff}`);
|
|
1609
1753
|
}
|
|
1610
1754
|
constructor(options) {
|
|
1611
|
-
super(), _define_property(this, "options", void 0), _define_property(this, "logger", void 0), _define_property(this, "pipes", void 0), _define_property(this, "interceptors", void 0), _define_property(this, "adapters", void 0), _define_property(this, "controllersOverview", void 0), _define_property(this, "provide", void 0), _define_property(this, "replace", void 0), _define_property(this, "unregisteredControllers", void 0), _define_property(this, "globalInterceptorHandler", void 0), this.options = options, this.pipes = Array.from(sharedPipes), this.interceptors = [], this.adapters = [], this.controllersOverview = [], this.provide = createProvideRegistry$1([Infact, getMoostInfact], [Mate, getMoostMate]), this.replace = {}, this.unregisteredControllers = [];
|
|
1755
|
+
super(), _define_property(this, "options", void 0), _define_property(this, "logger", void 0), _define_property(this, "pipes", void 0), _define_property(this, "interceptors", void 0), _define_property(this, "adapters", void 0), _define_property(this, "controllersOverview", void 0), _define_property(this, "handlerOverviewIndex", void 0), _define_property(this, "initHooks", void 0), _define_property(this, "provide", void 0), _define_property(this, "replace", void 0), _define_property(this, "unregisteredControllers", void 0), _define_property(this, "globalInterceptorHandler", void 0), this.options = options, this.pipes = Array.from(sharedPipes), this.interceptors = [], this.adapters = [], this.controllersOverview = [], this.initHooks = [], this.provide = createProvideRegistry$1([Infact, getMoostInfact], [Mate, getMoostMate]), this.replace = {}, this.unregisteredControllers = [];
|
|
1612
1756
|
this.logger = options?.logger || getDefaultLogger(`[2m[35mmoost`);
|
|
1613
1757
|
setDefaultLogger(this.logger);
|
|
1614
1758
|
const mate = getMoostMate();
|
|
@@ -1617,4 +1761,56 @@ function _define_property(obj, key, value) {
|
|
|
1617
1761
|
};
|
|
1618
1762
|
|
|
1619
1763
|
//#endregion
|
|
1620
|
-
|
|
1764
|
+
//#region packages/moost/src/decorators/resolve-moost.ts
|
|
1765
|
+
/**
|
|
1766
|
+
* Resolves the running {@link Moost} application instance from the current
|
|
1767
|
+
* controller context: returns the controller directly when it already IS the
|
|
1768
|
+
* Moost app (Moost registers itself as a controller), otherwise resolves it
|
|
1769
|
+
* through DI. Shared by `@InjectMoost` and `@InjectMoostLogger`.
|
|
1770
|
+
*/ async function resolveMoost() {
|
|
1771
|
+
const { instantiate, getController } = useControllerContext();
|
|
1772
|
+
const controller = getController();
|
|
1773
|
+
return controller instanceof Moost ? controller : await instantiate(Moost);
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
//#endregion
|
|
1777
|
+
//#region packages/moost/src/handler-paths.ts
|
|
1778
|
+
/**
|
|
1779
|
+
* Returns every actual mounted path under which `controller.method` is registered,
|
|
1780
|
+
* read from the post-bind controllers overview. Accounts for multi-prefix mounts
|
|
1781
|
+
* (`@ImportController` at several places), multiple verbs on one method, and
|
|
1782
|
+
* multiple `registeredAs` entries. Returns **distinct** paths; empty array if none
|
|
1783
|
+
* match.
|
|
1784
|
+
*
|
|
1785
|
+
* Only meaningful after `Moost.init()` has bound controllers — typically called
|
|
1786
|
+
* from a `@MoostInit` method (see [MoostInit](./decorators/init.decorator.ts)).
|
|
1787
|
+
*
|
|
1788
|
+
* @param controller class constructor or instance whose method to look up
|
|
1789
|
+
* @param method controller method name (e.g. the one carrying `@Get('refresh')`)
|
|
1790
|
+
*/ function getHandlerPaths(moost, controller, method, opts) {
|
|
1791
|
+
const ctor = isConstructor$1(controller) ? controller : getConstructor$1(controller);
|
|
1792
|
+
const handlers = moost.getHandlerOverviewIndex().get(ctor)?.get(method);
|
|
1793
|
+
if (!handlers) return [];
|
|
1794
|
+
const { type, predicate } = opts ?? {};
|
|
1795
|
+
const paths = /* @__PURE__ */ new Set();
|
|
1796
|
+
for (const h of handlers) {
|
|
1797
|
+
if (type && h.type !== type) continue;
|
|
1798
|
+
if (predicate && !predicate(h)) continue;
|
|
1799
|
+
for (const r of h.registeredAs) paths.add(r.path);
|
|
1800
|
+
}
|
|
1801
|
+
return [...paths];
|
|
1802
|
+
}
|
|
1803
|
+
/**
|
|
1804
|
+
* Composable form of {@link getHandlerPaths}. Resolves the running Moost app and
|
|
1805
|
+
* defaults the controller to the current one from context — designed for use
|
|
1806
|
+
* inside a `@MoostInit` method or an event handler. `method` defaults to the
|
|
1807
|
+
* current context method; pass it explicitly from `@MoostInit` to target a
|
|
1808
|
+
* handler method (the init method itself is not a handler).
|
|
1809
|
+
*/ async function useHandlerPaths(method, opts) {
|
|
1810
|
+
const moost = await resolveMoost();
|
|
1811
|
+
const { getController, getMethod } = useControllerContext();
|
|
1812
|
+
return getHandlerPaths(moost, getController(), method ?? getMethod() ?? "", opts);
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
//#endregion
|
|
1816
|
+
export { After, ApplyDecorators, Before, Circular, Const, ConstFactory, ContextInjector, Controller, Description, HandlerPaths, Id, ImportController, Inherit, Inject, InjectEventLogger, InjectFromScope, InjectMoost, InjectMoostLogger, InjectScopeVars, Injectable, Intercept, Interceptor, InterceptorHandler, Label, LoggerTopic, Moost, MoostInit, OnError, Optional, Overtake, Param, Params, Pipe, ProstoLogger, Provide, Replace, Required, Resolve, Response, TInterceptorPriority, TPipePriority, Value, cached, clearGlobalWooks, createEventContext, createLogger, createProvideRegistry, createReplaceRegistry, current, defineAfterInterceptor, defineBeforeInterceptor, defineErrorInterceptor, defineInfactScope, defineInterceptor, defineMoostEventHandler, definePipeFn, eventTypeKey, getConstructor, getContextInjector, getGlobalWooks, getHandlerPaths, getInfactScopeVars, getInstanceOwnMethods, getInstanceOwnProps, getMoostInfact, getMoostMate, getNewMoostInfact, globalKey, isConstructor, isThenable, key, loggerConsoleTransport, mergeSorted, registerEventScope, replaceContextInjector, resetContextInjector, resolvePipe, run, setControllerContext, setInfactLoggingOptions, setInterceptResult, setOvertake, useControllerContext, useHandlerPaths, useInterceptResult, useLogger, useOvertake, useScopeId };
|