moost 0.2.9 → 0.2.11
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 +115 -58
- package/dist/index.d.ts +29 -13
- package/dist/index.mjs +115 -58
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -203,50 +203,23 @@ const METADATA_WORKSPACE = 'moost';
|
|
|
203
203
|
const moostMate = new mate.Mate(METADATA_WORKSPACE, {
|
|
204
204
|
readType: true,
|
|
205
205
|
readReturnType: true,
|
|
206
|
+
collectPropKeys: true,
|
|
206
207
|
});
|
|
207
208
|
function getMoostMate() {
|
|
208
209
|
return moostMate;
|
|
209
210
|
}
|
|
210
211
|
|
|
211
|
-
function runPipes(pipes,
|
|
212
|
+
function runPipes(pipes, initialValue, metas, level, restoreCtx) {
|
|
212
213
|
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
-
let v =
|
|
214
|
+
let v = initialValue;
|
|
214
215
|
for (const pipe of pipes) {
|
|
215
216
|
restoreCtx && restoreCtx();
|
|
216
|
-
v = yield pipe.handler(v,
|
|
217
|
+
v = yield pipe.handler(v, metas, level);
|
|
217
218
|
}
|
|
218
219
|
return v;
|
|
219
220
|
});
|
|
220
221
|
}
|
|
221
222
|
|
|
222
|
-
exports.TPipePriority = void 0;
|
|
223
|
-
(function (TPipePriority) {
|
|
224
|
-
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
225
|
-
TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
|
|
226
|
-
TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
|
|
227
|
-
TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
|
|
228
|
-
TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
|
|
229
|
-
TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
|
|
230
|
-
TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
|
|
231
|
-
TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
|
|
232
|
-
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
233
|
-
})(exports.TPipePriority || (exports.TPipePriority = {}));
|
|
234
|
-
|
|
235
|
-
const resolvePipe = (_value, meta) => {
|
|
236
|
-
if (meta === null || meta === void 0 ? void 0 : meta.resolver) {
|
|
237
|
-
return meta.resolver();
|
|
238
|
-
}
|
|
239
|
-
return undefined;
|
|
240
|
-
};
|
|
241
|
-
resolvePipe.priority = exports.TPipePriority.RESOLVE;
|
|
242
|
-
|
|
243
|
-
const sharedPipes = [
|
|
244
|
-
{
|
|
245
|
-
handler: resolvePipe,
|
|
246
|
-
priority: exports.TPipePriority.RESOLVE,
|
|
247
|
-
},
|
|
248
|
-
];
|
|
249
|
-
|
|
250
223
|
const sharedMoostInfact = getNewMoostInfact();
|
|
251
224
|
function getMoostInfact() {
|
|
252
225
|
return sharedMoostInfact;
|
|
@@ -260,13 +233,23 @@ function getNewMoostInfact() {
|
|
|
260
233
|
global: false,
|
|
261
234
|
constructorParams: (meta === null || meta === void 0 ? void 0 : meta.params) || [],
|
|
262
235
|
provide: meta === null || meta === void 0 ? void 0 : meta.provide,
|
|
236
|
+
properties: (meta === null || meta === void 0 ? void 0 : meta.properties) || [],
|
|
263
237
|
scopeId: (meta === null || meta === void 0 ? void 0 : meta.injectable) === 'FOR_EVENT' ? eventCore.useEventId().getId() : undefined,
|
|
264
238
|
};
|
|
265
239
|
return infactMeta;
|
|
266
240
|
},
|
|
267
|
-
resolveParam(paramMeta) {
|
|
268
|
-
if (paramMeta.
|
|
269
|
-
return runPipes(
|
|
241
|
+
resolveParam({ paramMeta, classMeta, customData }) {
|
|
242
|
+
if (paramMeta && customData && customData.pipes) {
|
|
243
|
+
return runPipes(customData.pipes, undefined, { paramMeta, classMeta: classMeta }, 'PARAM');
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
describeProp(classConstructor, key) {
|
|
247
|
+
const meta = getMoostMate().read(classConstructor, key);
|
|
248
|
+
return meta;
|
|
249
|
+
},
|
|
250
|
+
resolveProp({ instance, key, initialValue, propMeta, classMeta, customData }) {
|
|
251
|
+
if (propMeta && customData && customData.pipes) {
|
|
252
|
+
return runPipes(customData.pipes, initialValue, { instance, key, propMeta, classMeta: classMeta }, 'PROP');
|
|
270
253
|
}
|
|
271
254
|
},
|
|
272
255
|
storeProvideRegByInstance: true,
|
|
@@ -304,7 +287,10 @@ function getCallableFn(targetInstance, fn, restoreCtx) {
|
|
|
304
287
|
if (meta === null || meta === void 0 ? void 0 : meta.injectable) {
|
|
305
288
|
const infact = getMoostInfact();
|
|
306
289
|
infact.silent(meta.injectable === 'FOR_EVENT');
|
|
307
|
-
const instance = yield infact.getForInstance(targetInstance, fn,
|
|
290
|
+
const instance = yield infact.getForInstance(targetInstance, fn, {
|
|
291
|
+
syncContextFn: () => { restoreCtx && restoreCtx(); },
|
|
292
|
+
customData: { pipes: meta.pipes },
|
|
293
|
+
});
|
|
308
294
|
infact.silent(false);
|
|
309
295
|
return ((...args) => {
|
|
310
296
|
return instance.handler(...args);
|
|
@@ -394,7 +380,7 @@ function bindControllerMethods(options) {
|
|
|
394
380
|
continue;
|
|
395
381
|
// preparing interceptors
|
|
396
382
|
const interceptors = [...(opts.interceptors || []), ...(meta.interceptors || []), ...(methodMeta.interceptors || [])].sort((a, b) => a.priority - b.priority);
|
|
397
|
-
const getIterceptorHandler = () =>
|
|
383
|
+
const getIterceptorHandler = () => {
|
|
398
384
|
const interceptorHandlers = [];
|
|
399
385
|
for (const { handler } of interceptors) {
|
|
400
386
|
const interceptorMeta = mate.read(handler);
|
|
@@ -410,10 +396,10 @@ function bindControllerMethods(options) {
|
|
|
410
396
|
interceptorHandlers.push(handler);
|
|
411
397
|
}
|
|
412
398
|
}
|
|
413
|
-
return new InterceptorHandler(interceptorHandlers);
|
|
414
|
-
}
|
|
399
|
+
return Promise.resolve(new InterceptorHandler(interceptorHandlers));
|
|
400
|
+
};
|
|
415
401
|
// preparing pipes
|
|
416
|
-
const pipes = [...(opts.pipes || []), ...(
|
|
402
|
+
const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])];
|
|
417
403
|
const argsPipes = [];
|
|
418
404
|
for (const p of methodMeta.params || []) {
|
|
419
405
|
argsPipes.push({
|
|
@@ -425,8 +411,12 @@ function bindControllerMethods(options) {
|
|
|
425
411
|
const args = [];
|
|
426
412
|
const { restoreCtx } = eventCore.useEventContext();
|
|
427
413
|
for (let i = 0; i < argsPipes.length; i++) {
|
|
428
|
-
const { pipes, meta } = argsPipes[i];
|
|
429
|
-
args[i] = yield runPipes(pipes,
|
|
414
|
+
const { pipes, meta: paramMeta } = argsPipes[i];
|
|
415
|
+
args[i] = yield runPipes(pipes, undefined, {
|
|
416
|
+
classMeta: meta,
|
|
417
|
+
methodMeta,
|
|
418
|
+
paramMeta,
|
|
419
|
+
}, 'PARAM', restoreCtx);
|
|
430
420
|
}
|
|
431
421
|
return args;
|
|
432
422
|
});
|
|
@@ -447,7 +437,7 @@ function bindControllerMethods(options) {
|
|
|
447
437
|
handlers: methodMeta.handlers,
|
|
448
438
|
getIterceptorHandler,
|
|
449
439
|
resolveArgs,
|
|
450
|
-
logHandler: opts.silent ? (
|
|
440
|
+
logHandler: opts.silent ? () => { } : (eventName) => log(`• ${eventName} ${'[0m' + '[2m' + '[32m'}→ ${classConstructor.name}.${'[36m'}${method}${'[32m'}()`),
|
|
451
441
|
});
|
|
452
442
|
}
|
|
453
443
|
}
|
|
@@ -487,8 +477,9 @@ function Required() {
|
|
|
487
477
|
*/
|
|
488
478
|
function Resolve(resolver, label) {
|
|
489
479
|
return (target, key, index) => {
|
|
490
|
-
|
|
491
|
-
|
|
480
|
+
const i = typeof index === 'number' ? index : undefined;
|
|
481
|
+
fillLabel(target, key, i, label);
|
|
482
|
+
getMoostMate().decorate('resolver', resolver)(target, key, i);
|
|
492
483
|
};
|
|
493
484
|
}
|
|
494
485
|
/**
|
|
@@ -521,8 +512,15 @@ function Const(value, label) {
|
|
|
521
512
|
function fillLabel(target, key, index, name) {
|
|
522
513
|
if (name) {
|
|
523
514
|
const meta = getMoostMate().read(target, key);
|
|
524
|
-
if (
|
|
525
|
-
|
|
515
|
+
if (typeof index === 'number') {
|
|
516
|
+
if (!(meta === null || meta === void 0 ? void 0 : meta.params) || !(meta === null || meta === void 0 ? void 0 : meta.params[index]) || !(meta === null || meta === void 0 ? void 0 : meta.params[index].label)) {
|
|
517
|
+
Label(name)(target, key, index);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
if (!(meta === null || meta === void 0 ? void 0 : meta.label)) {
|
|
522
|
+
Label(name)(target, key);
|
|
523
|
+
}
|
|
526
524
|
}
|
|
527
525
|
}
|
|
528
526
|
}
|
|
@@ -627,6 +625,48 @@ function IsBoolean(...args) {
|
|
|
627
625
|
return Validate(valido$1.validoIsBoolean(...args));
|
|
628
626
|
}
|
|
629
627
|
|
|
628
|
+
exports.TPipePriority = void 0;
|
|
629
|
+
(function (TPipePriority) {
|
|
630
|
+
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
631
|
+
TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
|
|
632
|
+
TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
|
|
633
|
+
TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
|
|
634
|
+
TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
|
|
635
|
+
TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
|
|
636
|
+
TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
|
|
637
|
+
TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
|
|
638
|
+
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
639
|
+
})(exports.TPipePriority || (exports.TPipePriority = {}));
|
|
640
|
+
|
|
641
|
+
const resolvePipe = (_value, metas, level) => {
|
|
642
|
+
var _a, _b, _c, _d;
|
|
643
|
+
let resolver;
|
|
644
|
+
if (level === 'PARAM') {
|
|
645
|
+
resolver = (_a = metas.paramMeta) === null || _a === void 0 ? void 0 : _a.resolver;
|
|
646
|
+
}
|
|
647
|
+
else if (level === 'PROP') {
|
|
648
|
+
resolver = (_b = metas.propMeta) === null || _b === void 0 ? void 0 : _b.resolver;
|
|
649
|
+
}
|
|
650
|
+
else if (level === 'METHOD') {
|
|
651
|
+
resolver = (_c = metas.methodMeta) === null || _c === void 0 ? void 0 : _c.resolver;
|
|
652
|
+
}
|
|
653
|
+
else if (level === 'CLASS') {
|
|
654
|
+
resolver = (_d = metas.classMeta) === null || _d === void 0 ? void 0 : _d.resolver;
|
|
655
|
+
}
|
|
656
|
+
if (resolver) {
|
|
657
|
+
return resolver(metas, level);
|
|
658
|
+
}
|
|
659
|
+
return undefined;
|
|
660
|
+
};
|
|
661
|
+
resolvePipe.priority = exports.TPipePriority.RESOLVE;
|
|
662
|
+
|
|
663
|
+
const sharedPipes = [
|
|
664
|
+
{
|
|
665
|
+
handler: resolvePipe,
|
|
666
|
+
priority: exports.TPipePriority.RESOLVE,
|
|
667
|
+
},
|
|
668
|
+
];
|
|
669
|
+
|
|
630
670
|
const valido = new valido$1.Valido({
|
|
631
671
|
getDtoMeta(value, _type) {
|
|
632
672
|
let type = _type;
|
|
@@ -699,9 +739,11 @@ class Moost {
|
|
|
699
739
|
const classMeta = meta.read(controller);
|
|
700
740
|
const infact = getMoostInfact();
|
|
701
741
|
const isControllerConsructor = mate.isConstructor(controller);
|
|
742
|
+
const pipes = [...this.pipes, ...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.pipes) || [])].sort((a, b) => a.priority - b.priority);
|
|
702
743
|
let instance;
|
|
744
|
+
const infactOpts = { provide, customData: { pipes } };
|
|
703
745
|
if (isControllerConsructor && ((classMeta === null || classMeta === void 0 ? void 0 : classMeta.injectable) === 'SINGLETON' || (classMeta === null || classMeta === void 0 ? void 0 : classMeta.injectable) === true)) {
|
|
704
|
-
instance = (yield infact.get(controller,
|
|
746
|
+
instance = (yield infact.get(controller, infactOpts));
|
|
705
747
|
}
|
|
706
748
|
else if (!isControllerConsructor) {
|
|
707
749
|
instance = controller;
|
|
@@ -711,7 +753,8 @@ class Moost {
|
|
|
711
753
|
const getInstance = instance ? () => Promise.resolve(instance) : () => __awaiter(this, void 0, void 0, function* () {
|
|
712
754
|
// if (!instance) {
|
|
713
755
|
infact.silent();
|
|
714
|
-
const
|
|
756
|
+
const { restoreCtx } = eventCore.useEventContext();
|
|
757
|
+
const instance = yield infact.get(controller, Object.assign(Object.assign({}, infactOpts), { syncContextFn: restoreCtx }));
|
|
715
758
|
infact.silent(false);
|
|
716
759
|
// }
|
|
717
760
|
return instance;
|
|
@@ -725,7 +768,7 @@ class Moost {
|
|
|
725
768
|
globalPrefix,
|
|
726
769
|
replaceOwnPrefix,
|
|
727
770
|
interceptors: [...this.interceptors],
|
|
728
|
-
pipes
|
|
771
|
+
pipes,
|
|
729
772
|
provide: (classMeta === null || classMeta === void 0 ? void 0 : classMeta.provide) || {},
|
|
730
773
|
});
|
|
731
774
|
if (classMeta && classMeta.importController) {
|
|
@@ -809,7 +852,7 @@ function useMoostStore() {
|
|
|
809
852
|
}
|
|
810
853
|
|
|
811
854
|
const genericTypesCastPipe = (strict) => {
|
|
812
|
-
const handler = (value, meta) => {
|
|
855
|
+
const handler = (value, { paramMeta: meta }) => {
|
|
813
856
|
if (meta === null || meta === void 0 ? void 0 : meta.type) {
|
|
814
857
|
if ((value === undefined || value === null || (meta.type !== String && value === '')) && meta.optional) {
|
|
815
858
|
return undefined;
|
|
@@ -878,17 +921,31 @@ function firstString(errors) {
|
|
|
878
921
|
return '';
|
|
879
922
|
}
|
|
880
923
|
const validatePipe = (opts) => {
|
|
881
|
-
const pipe = (_value,
|
|
882
|
-
const { restoreCtx } =
|
|
924
|
+
const pipe = (_value, metas, level) => __awaiter(void 0, void 0, void 0, function* () {
|
|
925
|
+
const { restoreCtx } = eventCore.useEventContext();
|
|
883
926
|
const valido = getMoostValido();
|
|
927
|
+
let meta = {};
|
|
928
|
+
if (level === 'PARAM') {
|
|
929
|
+
meta = metas.paramMeta || {};
|
|
930
|
+
}
|
|
931
|
+
else if (level === 'PROP') {
|
|
932
|
+
meta = metas.propMeta || {};
|
|
933
|
+
}
|
|
934
|
+
else if (level === 'METHOD') {
|
|
935
|
+
meta = metas.methodMeta || {};
|
|
936
|
+
}
|
|
937
|
+
else if (level === 'CLASS') {
|
|
938
|
+
meta = metas.classMeta || {};
|
|
939
|
+
}
|
|
884
940
|
const result = yield valido.validateParam(_value, meta, undefined, undefined, undefined, undefined, 0, 0, (opts === null || opts === void 0 ? void 0 : opts.errorLimit) || DEFAULT_ERROR_LIMIT, restoreCtx);
|
|
885
941
|
if (result !== true) {
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
message
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
942
|
+
const message = typeof result === 'string' ? result : firstString(result);
|
|
943
|
+
if (opts === null || opts === void 0 ? void 0 : opts.errorCb) {
|
|
944
|
+
opts.errorCb(message, result);
|
|
945
|
+
}
|
|
946
|
+
else {
|
|
947
|
+
throw new Error('Validation error: ' + message);
|
|
948
|
+
}
|
|
892
949
|
}
|
|
893
950
|
return _value;
|
|
894
951
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Infact } from '@prostojs/infact';
|
|
2
2
|
import { Mate } from '@prostojs/mate';
|
|
3
|
-
import { TInfactClassMeta } from '@prostojs/infact';
|
|
4
|
-
import { TInfactConstructorParamMeta } from '@prostojs/infact';
|
|
5
3
|
import { TProstoMetadata } from '@prostojs/mate';
|
|
6
4
|
import { TProstoParamsMetadata } from '@prostojs/mate';
|
|
7
5
|
import { TProvideFn } from '@prostojs/infact';
|
|
@@ -21,7 +19,7 @@ export declare function Circular<T = unknown>(resolver: () => TClassConstructor<
|
|
|
21
19
|
* @param label - label of the field
|
|
22
20
|
* @paramType unknown
|
|
23
21
|
*/
|
|
24
|
-
export declare function Const(value: unknown, label?: string): ParameterDecorator;
|
|
22
|
+
export declare function Const(value: unknown, label?: string): ParameterDecorator & PropertyDecorator;
|
|
25
23
|
|
|
26
24
|
/**
|
|
27
25
|
* Set Class as a Controller
|
|
@@ -34,11 +32,11 @@ export declare function Dto(dtoOptions?: TValidoDtoOptions): ClassDecorator;
|
|
|
34
32
|
|
|
35
33
|
export declare const genericTypesCastPipe: (strict?: boolean) => TPipeFn;
|
|
36
34
|
|
|
37
|
-
export declare function getMoostInfact(): Infact<
|
|
35
|
+
export declare function getMoostInfact(): Infact<TMoostMetadata, TMoostMetadata, TMoostParamsMetadata, TCustom>;
|
|
38
36
|
|
|
39
37
|
export declare function getMoostMate(): Mate<TMoostMetadata>;
|
|
40
38
|
|
|
41
|
-
export declare function getNewMoostInfact(): Infact<
|
|
39
|
+
export declare function getNewMoostInfact(): Infact<TMoostMetadata, TMoostMetadata, TMoostParamsMetadata, TCustom>;
|
|
42
40
|
|
|
43
41
|
export declare function Id(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
44
42
|
|
|
@@ -135,14 +133,14 @@ export declare function Optional(): MethodDecorator & ClassDecorator & Parameter
|
|
|
135
133
|
* @param name - param name
|
|
136
134
|
* @paramType string
|
|
137
135
|
*/
|
|
138
|
-
export declare function Param(name: string): ParameterDecorator;
|
|
136
|
+
export declare function Param(name: string): ParameterDecorator & PropertyDecorator;
|
|
139
137
|
|
|
140
138
|
/**
|
|
141
139
|
* Get Parsed Params from url parh
|
|
142
140
|
* @decorator
|
|
143
141
|
* @paramType object
|
|
144
142
|
*/
|
|
145
|
-
export declare function Params(): ParameterDecorator;
|
|
143
|
+
export declare function Params(): ParameterDecorator & PropertyDecorator;
|
|
146
144
|
|
|
147
145
|
export declare function Provide(type: string | TClassConstructor, fn: TProvideFn): ClassDecorator;
|
|
148
146
|
|
|
@@ -156,7 +154,7 @@ export { Required_2 as Required }
|
|
|
156
154
|
* @param label - field label
|
|
157
155
|
* @paramType unknown
|
|
158
156
|
*/
|
|
159
|
-
export declare function Resolve(resolver: () => unknown, label?: string): ParameterDecorator;
|
|
157
|
+
export declare function Resolve<T extends TObject = TEmpty>(resolver: (metas: TPipeMetas<T>, level: TDecoratorLevel) => unknown, label?: string): ParameterDecorator & PropertyDecorator;
|
|
160
158
|
|
|
161
159
|
export declare const resolvePipe: TPipeFn;
|
|
162
160
|
|
|
@@ -187,11 +185,19 @@ declare interface TCommonMetaFields {
|
|
|
187
185
|
|
|
188
186
|
declare interface TCommonMoostMeta {
|
|
189
187
|
pipes?: TPipeData[];
|
|
188
|
+
resolver?: (metas: TPipeMetas, level: TDecoratorLevel) => unknown;
|
|
190
189
|
}
|
|
191
190
|
|
|
192
|
-
declare
|
|
191
|
+
declare interface TCustom {
|
|
192
|
+
pipes?: TPipeData[];
|
|
193
|
+
}
|
|
193
194
|
|
|
194
|
-
declare type
|
|
195
|
+
declare type TDecoratorLevel = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM';
|
|
196
|
+
|
|
197
|
+
declare interface TEmpty {
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
declare type TFunction = Function;
|
|
195
201
|
|
|
196
202
|
export declare type TInjectableScope = 'FOR_EVENT' | 'SINGLETON';
|
|
197
203
|
|
|
@@ -256,6 +262,7 @@ export declare interface TMoostMetadata extends TProstoAndCommonMetadata {
|
|
|
256
262
|
typeResolver?: TClassConstructor | (() => TClassConstructor | TObject | Promise<TClassConstructor | TObject>);
|
|
257
263
|
provide?: TProvideRegistry;
|
|
258
264
|
}[];
|
|
265
|
+
properties?: (string | symbol)[];
|
|
259
266
|
injectable?: true | TInjectableScope;
|
|
260
267
|
interceptors?: TInterceptorData[];
|
|
261
268
|
handlers?: TMoostHandler<TAny>[];
|
|
@@ -274,7 +281,6 @@ export declare interface TMoostParamsMetadata extends TProstoParamsAndCommonMeta
|
|
|
274
281
|
validators?: TValidoFn[];
|
|
275
282
|
validatorsOfItem?: TValidoFn[];
|
|
276
283
|
arrayType?: true | TValidateArrayOptions;
|
|
277
|
-
resolver?: TFunction;
|
|
278
284
|
circular?: () => TAny;
|
|
279
285
|
inject?: string | symbol;
|
|
280
286
|
}
|
|
@@ -286,11 +292,20 @@ export declare interface TPipeData {
|
|
|
286
292
|
priority: TPipePriority;
|
|
287
293
|
}
|
|
288
294
|
|
|
289
|
-
export declare type TPipeFn<T extends
|
|
290
|
-
(value: unknown,
|
|
295
|
+
export declare type TPipeFn<T extends TObject = TEmpty> = {
|
|
296
|
+
(value: unknown, metas: TPipeMetas<T>, level: TDecoratorLevel): unknown | Promise<unknown>;
|
|
291
297
|
priority?: TPipePriority;
|
|
292
298
|
};
|
|
293
299
|
|
|
300
|
+
export declare interface TPipeMetas<T extends TObject = TEmpty> {
|
|
301
|
+
classMeta?: TMoostMetadata & T;
|
|
302
|
+
methodMeta?: TMoostMetadata & T;
|
|
303
|
+
propMeta?: TMoostMetadata & T;
|
|
304
|
+
paramMeta?: TMoostParamsMetadata & T;
|
|
305
|
+
instance?: TObject;
|
|
306
|
+
key?: string | symbol;
|
|
307
|
+
}
|
|
308
|
+
|
|
294
309
|
export declare enum TPipePriority {
|
|
295
310
|
BEFORE_RESOLVE = 0,
|
|
296
311
|
RESOLVE = 1,
|
|
@@ -316,6 +331,7 @@ export declare interface TValidateArrayOptions<T = unknown> {
|
|
|
316
331
|
|
|
317
332
|
declare interface TValidatePipeOptions {
|
|
318
333
|
errorLimit?: number;
|
|
334
|
+
errorCb?: (message: string, details: string | TObject) => unknown;
|
|
319
335
|
}
|
|
320
336
|
|
|
321
337
|
export declare function useControllerMeta<T extends TMoostMetadata = TMoostMetadata>(): {
|
package/dist/index.mjs
CHANGED
|
@@ -201,50 +201,23 @@ const METADATA_WORKSPACE = 'moost';
|
|
|
201
201
|
const moostMate = new Mate(METADATA_WORKSPACE, {
|
|
202
202
|
readType: true,
|
|
203
203
|
readReturnType: true,
|
|
204
|
+
collectPropKeys: true,
|
|
204
205
|
});
|
|
205
206
|
function getMoostMate() {
|
|
206
207
|
return moostMate;
|
|
207
208
|
}
|
|
208
209
|
|
|
209
|
-
function runPipes(pipes,
|
|
210
|
+
function runPipes(pipes, initialValue, metas, level, restoreCtx) {
|
|
210
211
|
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
-
let v =
|
|
212
|
+
let v = initialValue;
|
|
212
213
|
for (const pipe of pipes) {
|
|
213
214
|
restoreCtx && restoreCtx();
|
|
214
|
-
v = yield pipe.handler(v,
|
|
215
|
+
v = yield pipe.handler(v, metas, level);
|
|
215
216
|
}
|
|
216
217
|
return v;
|
|
217
218
|
});
|
|
218
219
|
}
|
|
219
220
|
|
|
220
|
-
var TPipePriority;
|
|
221
|
-
(function (TPipePriority) {
|
|
222
|
-
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
223
|
-
TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
|
|
224
|
-
TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
|
|
225
|
-
TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
|
|
226
|
-
TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
|
|
227
|
-
TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
|
|
228
|
-
TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
|
|
229
|
-
TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
|
|
230
|
-
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
231
|
-
})(TPipePriority || (TPipePriority = {}));
|
|
232
|
-
|
|
233
|
-
const resolvePipe = (_value, meta) => {
|
|
234
|
-
if (meta === null || meta === void 0 ? void 0 : meta.resolver) {
|
|
235
|
-
return meta.resolver();
|
|
236
|
-
}
|
|
237
|
-
return undefined;
|
|
238
|
-
};
|
|
239
|
-
resolvePipe.priority = TPipePriority.RESOLVE;
|
|
240
|
-
|
|
241
|
-
const sharedPipes = [
|
|
242
|
-
{
|
|
243
|
-
handler: resolvePipe,
|
|
244
|
-
priority: TPipePriority.RESOLVE,
|
|
245
|
-
},
|
|
246
|
-
];
|
|
247
|
-
|
|
248
221
|
const sharedMoostInfact = getNewMoostInfact();
|
|
249
222
|
function getMoostInfact() {
|
|
250
223
|
return sharedMoostInfact;
|
|
@@ -258,13 +231,23 @@ function getNewMoostInfact() {
|
|
|
258
231
|
global: false,
|
|
259
232
|
constructorParams: (meta === null || meta === void 0 ? void 0 : meta.params) || [],
|
|
260
233
|
provide: meta === null || meta === void 0 ? void 0 : meta.provide,
|
|
234
|
+
properties: (meta === null || meta === void 0 ? void 0 : meta.properties) || [],
|
|
261
235
|
scopeId: (meta === null || meta === void 0 ? void 0 : meta.injectable) === 'FOR_EVENT' ? useEventId().getId() : undefined,
|
|
262
236
|
};
|
|
263
237
|
return infactMeta;
|
|
264
238
|
},
|
|
265
|
-
resolveParam(paramMeta) {
|
|
266
|
-
if (paramMeta.
|
|
267
|
-
return runPipes(
|
|
239
|
+
resolveParam({ paramMeta, classMeta, customData }) {
|
|
240
|
+
if (paramMeta && customData && customData.pipes) {
|
|
241
|
+
return runPipes(customData.pipes, undefined, { paramMeta, classMeta: classMeta }, 'PARAM');
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
describeProp(classConstructor, key) {
|
|
245
|
+
const meta = getMoostMate().read(classConstructor, key);
|
|
246
|
+
return meta;
|
|
247
|
+
},
|
|
248
|
+
resolveProp({ instance, key, initialValue, propMeta, classMeta, customData }) {
|
|
249
|
+
if (propMeta && customData && customData.pipes) {
|
|
250
|
+
return runPipes(customData.pipes, initialValue, { instance, key, propMeta, classMeta: classMeta }, 'PROP');
|
|
268
251
|
}
|
|
269
252
|
},
|
|
270
253
|
storeProvideRegByInstance: true,
|
|
@@ -302,7 +285,10 @@ function getCallableFn(targetInstance, fn, restoreCtx) {
|
|
|
302
285
|
if (meta === null || meta === void 0 ? void 0 : meta.injectable) {
|
|
303
286
|
const infact = getMoostInfact();
|
|
304
287
|
infact.silent(meta.injectable === 'FOR_EVENT');
|
|
305
|
-
const instance = yield infact.getForInstance(targetInstance, fn,
|
|
288
|
+
const instance = yield infact.getForInstance(targetInstance, fn, {
|
|
289
|
+
syncContextFn: () => { restoreCtx && restoreCtx(); },
|
|
290
|
+
customData: { pipes: meta.pipes },
|
|
291
|
+
});
|
|
306
292
|
infact.silent(false);
|
|
307
293
|
return ((...args) => {
|
|
308
294
|
return instance.handler(...args);
|
|
@@ -392,7 +378,7 @@ function bindControllerMethods(options) {
|
|
|
392
378
|
continue;
|
|
393
379
|
// preparing interceptors
|
|
394
380
|
const interceptors = [...(opts.interceptors || []), ...(meta.interceptors || []), ...(methodMeta.interceptors || [])].sort((a, b) => a.priority - b.priority);
|
|
395
|
-
const getIterceptorHandler = () =>
|
|
381
|
+
const getIterceptorHandler = () => {
|
|
396
382
|
const interceptorHandlers = [];
|
|
397
383
|
for (const { handler } of interceptors) {
|
|
398
384
|
const interceptorMeta = mate.read(handler);
|
|
@@ -408,10 +394,10 @@ function bindControllerMethods(options) {
|
|
|
408
394
|
interceptorHandlers.push(handler);
|
|
409
395
|
}
|
|
410
396
|
}
|
|
411
|
-
return new InterceptorHandler(interceptorHandlers);
|
|
412
|
-
}
|
|
397
|
+
return Promise.resolve(new InterceptorHandler(interceptorHandlers));
|
|
398
|
+
};
|
|
413
399
|
// preparing pipes
|
|
414
|
-
const pipes = [...(opts.pipes || []), ...(
|
|
400
|
+
const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])];
|
|
415
401
|
const argsPipes = [];
|
|
416
402
|
for (const p of methodMeta.params || []) {
|
|
417
403
|
argsPipes.push({
|
|
@@ -423,8 +409,12 @@ function bindControllerMethods(options) {
|
|
|
423
409
|
const args = [];
|
|
424
410
|
const { restoreCtx } = useEventContext();
|
|
425
411
|
for (let i = 0; i < argsPipes.length; i++) {
|
|
426
|
-
const { pipes, meta } = argsPipes[i];
|
|
427
|
-
args[i] = yield runPipes(pipes,
|
|
412
|
+
const { pipes, meta: paramMeta } = argsPipes[i];
|
|
413
|
+
args[i] = yield runPipes(pipes, undefined, {
|
|
414
|
+
classMeta: meta,
|
|
415
|
+
methodMeta,
|
|
416
|
+
paramMeta,
|
|
417
|
+
}, 'PARAM', restoreCtx);
|
|
428
418
|
}
|
|
429
419
|
return args;
|
|
430
420
|
});
|
|
@@ -445,7 +435,7 @@ function bindControllerMethods(options) {
|
|
|
445
435
|
handlers: methodMeta.handlers,
|
|
446
436
|
getIterceptorHandler,
|
|
447
437
|
resolveArgs,
|
|
448
|
-
logHandler: opts.silent ? (
|
|
438
|
+
logHandler: opts.silent ? () => { } : (eventName) => log(`• ${eventName} ${'[0m' + '[2m' + '[32m'}→ ${classConstructor.name}.${'[36m'}${method}${'[32m'}()`),
|
|
449
439
|
});
|
|
450
440
|
}
|
|
451
441
|
}
|
|
@@ -485,8 +475,9 @@ function Required() {
|
|
|
485
475
|
*/
|
|
486
476
|
function Resolve(resolver, label) {
|
|
487
477
|
return (target, key, index) => {
|
|
488
|
-
|
|
489
|
-
|
|
478
|
+
const i = typeof index === 'number' ? index : undefined;
|
|
479
|
+
fillLabel(target, key, i, label);
|
|
480
|
+
getMoostMate().decorate('resolver', resolver)(target, key, i);
|
|
490
481
|
};
|
|
491
482
|
}
|
|
492
483
|
/**
|
|
@@ -519,8 +510,15 @@ function Const(value, label) {
|
|
|
519
510
|
function fillLabel(target, key, index, name) {
|
|
520
511
|
if (name) {
|
|
521
512
|
const meta = getMoostMate().read(target, key);
|
|
522
|
-
if (
|
|
523
|
-
|
|
513
|
+
if (typeof index === 'number') {
|
|
514
|
+
if (!(meta === null || meta === void 0 ? void 0 : meta.params) || !(meta === null || meta === void 0 ? void 0 : meta.params[index]) || !(meta === null || meta === void 0 ? void 0 : meta.params[index].label)) {
|
|
515
|
+
Label(name)(target, key, index);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
if (!(meta === null || meta === void 0 ? void 0 : meta.label)) {
|
|
520
|
+
Label(name)(target, key);
|
|
521
|
+
}
|
|
524
522
|
}
|
|
525
523
|
}
|
|
526
524
|
}
|
|
@@ -625,6 +623,48 @@ function IsBoolean(...args) {
|
|
|
625
623
|
return Validate(validoIsBoolean(...args));
|
|
626
624
|
}
|
|
627
625
|
|
|
626
|
+
var TPipePriority;
|
|
627
|
+
(function (TPipePriority) {
|
|
628
|
+
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
629
|
+
TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
|
|
630
|
+
TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
|
|
631
|
+
TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
|
|
632
|
+
TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
|
|
633
|
+
TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
|
|
634
|
+
TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
|
|
635
|
+
TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
|
|
636
|
+
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
637
|
+
})(TPipePriority || (TPipePriority = {}));
|
|
638
|
+
|
|
639
|
+
const resolvePipe = (_value, metas, level) => {
|
|
640
|
+
var _a, _b, _c, _d;
|
|
641
|
+
let resolver;
|
|
642
|
+
if (level === 'PARAM') {
|
|
643
|
+
resolver = (_a = metas.paramMeta) === null || _a === void 0 ? void 0 : _a.resolver;
|
|
644
|
+
}
|
|
645
|
+
else if (level === 'PROP') {
|
|
646
|
+
resolver = (_b = metas.propMeta) === null || _b === void 0 ? void 0 : _b.resolver;
|
|
647
|
+
}
|
|
648
|
+
else if (level === 'METHOD') {
|
|
649
|
+
resolver = (_c = metas.methodMeta) === null || _c === void 0 ? void 0 : _c.resolver;
|
|
650
|
+
}
|
|
651
|
+
else if (level === 'CLASS') {
|
|
652
|
+
resolver = (_d = metas.classMeta) === null || _d === void 0 ? void 0 : _d.resolver;
|
|
653
|
+
}
|
|
654
|
+
if (resolver) {
|
|
655
|
+
return resolver(metas, level);
|
|
656
|
+
}
|
|
657
|
+
return undefined;
|
|
658
|
+
};
|
|
659
|
+
resolvePipe.priority = TPipePriority.RESOLVE;
|
|
660
|
+
|
|
661
|
+
const sharedPipes = [
|
|
662
|
+
{
|
|
663
|
+
handler: resolvePipe,
|
|
664
|
+
priority: TPipePriority.RESOLVE,
|
|
665
|
+
},
|
|
666
|
+
];
|
|
667
|
+
|
|
628
668
|
const valido = new Valido({
|
|
629
669
|
getDtoMeta(value, _type) {
|
|
630
670
|
let type = _type;
|
|
@@ -697,9 +737,11 @@ class Moost {
|
|
|
697
737
|
const classMeta = meta.read(controller);
|
|
698
738
|
const infact = getMoostInfact();
|
|
699
739
|
const isControllerConsructor = isConstructor(controller);
|
|
740
|
+
const pipes = [...this.pipes, ...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.pipes) || [])].sort((a, b) => a.priority - b.priority);
|
|
700
741
|
let instance;
|
|
742
|
+
const infactOpts = { provide, customData: { pipes } };
|
|
701
743
|
if (isControllerConsructor && ((classMeta === null || classMeta === void 0 ? void 0 : classMeta.injectable) === 'SINGLETON' || (classMeta === null || classMeta === void 0 ? void 0 : classMeta.injectable) === true)) {
|
|
702
|
-
instance = (yield infact.get(controller,
|
|
744
|
+
instance = (yield infact.get(controller, infactOpts));
|
|
703
745
|
}
|
|
704
746
|
else if (!isControllerConsructor) {
|
|
705
747
|
instance = controller;
|
|
@@ -709,7 +751,8 @@ class Moost {
|
|
|
709
751
|
const getInstance = instance ? () => Promise.resolve(instance) : () => __awaiter(this, void 0, void 0, function* () {
|
|
710
752
|
// if (!instance) {
|
|
711
753
|
infact.silent();
|
|
712
|
-
const
|
|
754
|
+
const { restoreCtx } = useEventContext();
|
|
755
|
+
const instance = yield infact.get(controller, Object.assign(Object.assign({}, infactOpts), { syncContextFn: restoreCtx }));
|
|
713
756
|
infact.silent(false);
|
|
714
757
|
// }
|
|
715
758
|
return instance;
|
|
@@ -723,7 +766,7 @@ class Moost {
|
|
|
723
766
|
globalPrefix,
|
|
724
767
|
replaceOwnPrefix,
|
|
725
768
|
interceptors: [...this.interceptors],
|
|
726
|
-
pipes
|
|
769
|
+
pipes,
|
|
727
770
|
provide: (classMeta === null || classMeta === void 0 ? void 0 : classMeta.provide) || {},
|
|
728
771
|
});
|
|
729
772
|
if (classMeta && classMeta.importController) {
|
|
@@ -807,7 +850,7 @@ function useMoostStore() {
|
|
|
807
850
|
}
|
|
808
851
|
|
|
809
852
|
const genericTypesCastPipe = (strict) => {
|
|
810
|
-
const handler = (value, meta) => {
|
|
853
|
+
const handler = (value, { paramMeta: meta }) => {
|
|
811
854
|
if (meta === null || meta === void 0 ? void 0 : meta.type) {
|
|
812
855
|
if ((value === undefined || value === null || (meta.type !== String && value === '')) && meta.optional) {
|
|
813
856
|
return undefined;
|
|
@@ -876,17 +919,31 @@ function firstString(errors) {
|
|
|
876
919
|
return '';
|
|
877
920
|
}
|
|
878
921
|
const validatePipe = (opts) => {
|
|
879
|
-
const pipe = (_value,
|
|
880
|
-
const { restoreCtx } =
|
|
922
|
+
const pipe = (_value, metas, level) => __awaiter(void 0, void 0, void 0, function* () {
|
|
923
|
+
const { restoreCtx } = useEventContext();
|
|
881
924
|
const valido = getMoostValido();
|
|
925
|
+
let meta = {};
|
|
926
|
+
if (level === 'PARAM') {
|
|
927
|
+
meta = metas.paramMeta || {};
|
|
928
|
+
}
|
|
929
|
+
else if (level === 'PROP') {
|
|
930
|
+
meta = metas.propMeta || {};
|
|
931
|
+
}
|
|
932
|
+
else if (level === 'METHOD') {
|
|
933
|
+
meta = metas.methodMeta || {};
|
|
934
|
+
}
|
|
935
|
+
else if (level === 'CLASS') {
|
|
936
|
+
meta = metas.classMeta || {};
|
|
937
|
+
}
|
|
882
938
|
const result = yield valido.validateParam(_value, meta, undefined, undefined, undefined, undefined, 0, 0, (opts === null || opts === void 0 ? void 0 : opts.errorLimit) || DEFAULT_ERROR_LIMIT, restoreCtx);
|
|
883
939
|
if (result !== true) {
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
message
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
940
|
+
const message = typeof result === 'string' ? result : firstString(result);
|
|
941
|
+
if (opts === null || opts === void 0 ? void 0 : opts.errorCb) {
|
|
942
|
+
opts.errorCb(message, result);
|
|
943
|
+
}
|
|
944
|
+
else {
|
|
945
|
+
throw new Error('Validation error: ' + message);
|
|
946
|
+
}
|
|
890
947
|
}
|
|
891
948
|
return _value;
|
|
892
949
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moost",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"description": "moost",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"prostojs"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@prostojs/infact": "^0.
|
|
26
|
-
"@prostojs/mate": "^0.1.
|
|
25
|
+
"@prostojs/infact": "^0.1.3",
|
|
26
|
+
"@prostojs/mate": "^0.1.12",
|
|
27
27
|
"@prostojs/valido": "^0.0.2",
|
|
28
|
-
"@wooksjs/event-core": "^0.2.
|
|
29
|
-
"wooks": "^0.2.
|
|
28
|
+
"@wooksjs/event-core": "^0.2.6",
|
|
29
|
+
"wooks": "^0.2.6"
|
|
30
30
|
},
|
|
31
31
|
"author": "Artem Maltsev",
|
|
32
32
|
"license": "MIT",
|