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