moost 0.2.12 → 0.2.14

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 CHANGED
@@ -204,6 +204,15 @@ const moostMate = new mate.Mate(METADATA_WORKSPACE, {
204
204
  readType: true,
205
205
  readReturnType: true,
206
206
  collectPropKeys: true,
207
+ inherit(classMeta, targetMeta, level) {
208
+ if (level === 'CLASS') {
209
+ return !!(classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit);
210
+ }
211
+ if (level === 'PROP') {
212
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit) || !!((classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit) && !targetMeta);
213
+ }
214
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit);
215
+ },
207
216
  });
208
217
  function getMoostMate() {
209
218
  return moostMate;
@@ -260,9 +269,18 @@ function getInstanceOwnMethods(instance) {
260
269
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
261
270
  const proto = Object.getPrototypeOf(instance);
262
271
  return [
272
+ ...getParentProps(mate.getConstructor(instance)),
263
273
  ...Object.getOwnPropertyNames(proto),
264
274
  ...Object.getOwnPropertyNames(instance),
265
275
  ].filter(m => typeof instance[m] === 'function');
276
+ }
277
+ const fnProto = Object.getPrototypeOf(Function);
278
+ function getParentProps(constructor) {
279
+ const parent = Object.getPrototypeOf(constructor);
280
+ if (typeof parent === 'function' && parent !== fnProto && parent !== constructor && parent.prototype) {
281
+ return [...getParentProps(parent), ...Object.getOwnPropertyNames(parent.prototype)];
282
+ }
283
+ return [];
266
284
  }
267
285
 
268
286
  const banner = () => `[${"moost"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
@@ -280,18 +298,18 @@ function panic(error) {
280
298
  return new Error(error);
281
299
  }
282
300
 
283
- function getCallableFn(targetInstance, fn, restoreCtx) {
301
+ function getCallableFn(targetInstance, fn, restoreCtx, pipes, silent) {
284
302
  return __awaiter(this, void 0, void 0, function* () {
285
303
  const mate$1 = getMoostMate();
286
304
  const meta = mate$1.read(fn);
287
305
  if (meta === null || meta === void 0 ? void 0 : meta.injectable) {
288
306
  const infact = getMoostInfact();
289
- infact.silent(meta.injectable === 'FOR_EVENT');
307
+ infact.silent(silent || (meta.injectable === 'FOR_EVENT' ? 'logs' : false));
290
308
  const instance = yield infact.getForInstance(targetInstance, fn, {
291
309
  syncContextFn: () => { restoreCtx && restoreCtx(); },
292
- customData: { pipes: meta.pipes },
310
+ customData: { pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority) },
293
311
  });
294
- infact.silent(false);
312
+ infact.silent(!!silent);
295
313
  return ((...args) => {
296
314
  return instance.handler(...args);
297
315
  });
@@ -378,6 +396,7 @@ function bindControllerMethods(options) {
378
396
  const methodMeta = getMoostMate().read(fakeInstance, method) || {};
379
397
  if (!methodMeta.handlers || !methodMeta.handlers.length)
380
398
  continue;
399
+ const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])].sort((a, b) => a.priority - b.priority);
381
400
  // preparing interceptors
382
401
  const interceptors = [...(opts.interceptors || []), ...(meta.interceptors || []), ...(methodMeta.interceptors || [])].sort((a, b) => a.priority - b.priority);
383
402
  const getIterceptorHandler = () => {
@@ -389,7 +408,7 @@ function bindControllerMethods(options) {
389
408
  const { restoreCtx } = useHttpContext();
390
409
  const targetInstance = yield getInstance();
391
410
  restoreCtx();
392
- return (yield getCallableFn(targetInstance, handler, restoreCtx))(...args);
411
+ return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, options.silent))(...args);
393
412
  }));
394
413
  }
395
414
  else {
@@ -399,7 +418,6 @@ function bindControllerMethods(options) {
399
418
  return Promise.resolve(new InterceptorHandler(interceptorHandlers));
400
419
  };
401
420
  // preparing pipes
402
- const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])];
403
421
  const argsPipes = [];
404
422
  for (const p of methodMeta.params || []) {
405
423
  argsPipes.push({
@@ -457,7 +475,7 @@ function Required() {
457
475
  const mate = getMoostMate();
458
476
  return mate.apply(mate.decorate('required', true),
459
477
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
460
- mate.decorateClass((meta, key, index) => {
478
+ mate.decorateClass((meta, level, key, index) => {
461
479
  if (typeof index !== 'number' && meta && ['string', 'symbol'].includes(typeof key)) {
462
480
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
463
481
  meta.requiredProps = meta.requiredProps || [];
@@ -625,6 +643,8 @@ function IsBoolean(...args) {
625
643
  return Validate(valido$1.validoIsBoolean(...args));
626
644
  }
627
645
 
646
+ const Inherit = () => getMoostMate().decorate('inherit', true);
647
+
628
648
  exports.TPipePriority = void 0;
629
649
  (function (TPipePriority) {
630
650
  TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
@@ -694,7 +714,7 @@ class Moost {
694
714
  this.provide = infact.createProvideRegistry([infact.Infact, getMoostInfact], [mate.Mate, getMoostMate], [valido$1.Valido, getMoostValido]);
695
715
  this.unregisteredControllers = [];
696
716
  if (options === null || options === void 0 ? void 0 : options.silent) {
697
- getMoostInfact().silent();
717
+ getMoostInfact().silent(options === null || options === void 0 ? void 0 : options.silent);
698
718
  }
699
719
  }
700
720
  adapter(a) {
@@ -751,12 +771,12 @@ class Moost {
751
771
  }
752
772
  // getInstance - instance factory for resolving SINGLETON and FOR_EVENT instance
753
773
  const getInstance = instance ? () => Promise.resolve(instance) : () => __awaiter(this, void 0, void 0, function* () {
754
- var _c;
774
+ var _c, _d;
755
775
  // if (!instance) {
756
- infact.silent();
776
+ infact.silent(((_c = this.options) === null || _c === void 0 ? void 0 : _c.silent) || 'logs');
757
777
  const { restoreCtx } = eventCore.useEventContext();
758
778
  const instance = yield infact.get(controller, Object.assign(Object.assign({}, infactOpts), { syncContextFn: restoreCtx }));
759
- infact.silent(!!((_c = this.options) === null || _c === void 0 ? void 0 : _c.silent));
779
+ infact.silent(!!((_d = this.options) === null || _d === void 0 ? void 0 : _d.silent));
760
780
  // }
761
781
  return instance;
762
782
  });
@@ -938,7 +958,7 @@ const validatePipe = (opts) => {
938
958
  else if (level === 'CLASS') {
939
959
  meta = metas.classMeta || {};
940
960
  }
941
- 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);
961
+ const result = yield valido.validateParam(_value, meta, metas.key, undefined, metas.instance, undefined, 0, 0, (opts === null || opts === void 0 ? void 0 : opts.errorLimit) || DEFAULT_ERROR_LIMIT, restoreCtx);
942
962
  if (result !== true) {
943
963
  const message = typeof result === 'string' ? result : firstString(result);
944
964
  if (opts === null || opts === void 0 ? void 0 : opts.errorCb) {
@@ -960,6 +980,7 @@ exports.Controller = Controller;
960
980
  exports.Dto = Dto;
961
981
  exports.Id = Id;
962
982
  exports.ImportController = ImportController;
983
+ exports.Inherit = Inherit;
963
984
  exports.Inject = Inject;
964
985
  exports.Injectable = Injectable;
965
986
  exports.Intercept = Intercept;
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 { TProstoMetadata } from '@prostojs/mate';
4
- import { TProstoParamsMetadata } from '@prostojs/mate';
5
3
  import { TProvideFn } from '@prostojs/infact';
6
4
  import { TProvideRegistry } from '@prostojs/infact';
7
5
  import { TValidoDtoOptions } from '@prostojs/valido';
@@ -34,7 +32,7 @@ export declare const genericTypesCastPipe: (strict?: boolean) => TPipeFn;
34
32
 
35
33
  export declare function getMoostInfact(): Infact<TMoostMetadata, TMoostMetadata, TMoostParamsMetadata, TCustom>;
36
34
 
37
- export declare function getMoostMate(): Mate<TMoostMetadata>;
35
+ export declare function getMoostMate<Class extends TObject = TEmpty, Prop extends TObject = TEmpty, Param extends TObject = TEmpty>(): Mate<TMoostMetadata & Class, TMoostMetadata & Prop, TMoostMetadata & TMoostParamsMetadata & Param>;
38
36
 
39
37
  export declare function getNewMoostInfact(): Infact<TMoostMetadata, TMoostMetadata, TMoostParamsMetadata, TCustom>;
40
38
 
@@ -57,6 +55,8 @@ export declare function ImportController(controller: TFunction | TObject, provid
57
55
  */
58
56
  export declare function ImportController(prefix: string, controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
59
57
 
58
+ export declare const Inherit: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
59
+
60
60
  export declare function Inject(type: string | TClassConstructor): ParameterDecorator;
61
61
 
62
62
  /**
@@ -184,8 +184,13 @@ declare interface TCommonMetaFields {
184
184
  }
185
185
 
186
186
  declare interface TCommonMoostMeta {
187
+ inherit?: boolean;
187
188
  pipes?: TPipeData[];
188
- resolver?: (metas: TPipeMetas, level: TDecoratorLevel) => unknown;
189
+ resolver?: (metas: TPipeMetas<TAny>, level: TDecoratorLevel) => unknown;
190
+ type?: TFunction;
191
+ validators?: TValidoFn<TAny>[];
192
+ validatorsOfItem?: TValidoFn<TAny>[];
193
+ arrayType?: true | TValidateArrayOptions<TAny>;
189
194
  }
190
195
 
191
196
  declare interface TCustom {
@@ -250,8 +255,7 @@ export declare type TMoostHandler<T extends object> = T & {
250
255
  type: string;
251
256
  };
252
257
 
253
- export declare interface TMoostMetadata extends TProstoAndCommonMetadata {
254
- inherit?: boolean;
258
+ export declare interface TMoostMetadata extends TCommonMetaFields, TCommonMoostMeta {
255
259
  dto?: TValidoDtoOptions;
256
260
  requiredProps?: (string | symbol)[];
257
261
  controller?: {
@@ -266,9 +270,6 @@ export declare interface TMoostMetadata extends TProstoAndCommonMetadata {
266
270
  injectable?: true | TInjectableScope;
267
271
  interceptors?: TInterceptorData[];
268
272
  handlers?: TMoostHandler<TAny>[];
269
- validators?: TValidoFn[];
270
- validatorsOfItem?: TValidoFn[];
271
- arrayType?: true | TValidateArrayOptions;
272
273
  provide?: TProvideRegistry;
273
274
  }
274
275
 
@@ -277,12 +278,9 @@ export declare interface TMoostOptions {
277
278
  silent?: boolean;
278
279
  }
279
280
 
280
- export declare interface TMoostParamsMetadata extends TProstoParamsAndCommonMetadata {
281
- validators?: TValidoFn[];
282
- validatorsOfItem?: TValidoFn[];
283
- arrayType?: true | TValidateArrayOptions;
281
+ export declare interface TMoostParamsMetadata extends TCommonMetaFields, TCommonMoostMeta {
284
282
  circular?: () => TAny;
285
- inject?: string | symbol;
283
+ inject?: string | symbol | TClassConstructor;
286
284
  }
287
285
 
288
286
  declare type TObject = object;
@@ -318,10 +316,6 @@ export declare enum TPipePriority {
318
316
  AFTER_VALIDATE = 8
319
317
  }
320
318
 
321
- declare type TProstoAndCommonMetadata<T extends TMoostParamsMetadata = TMoostParamsMetadata> = TProstoMetadata<T> & TCommonMetaFields & TCommonMoostMeta;
322
-
323
- declare type TProstoParamsAndCommonMetadata = TProstoParamsMetadata & TCommonMetaFields & TCommonMoostMeta;
324
-
325
319
  export declare interface TValidateArrayOptions<T = unknown> {
326
320
  itemType?: (item: T, index: number) => Promise<TFunction> | TFunction;
327
321
  itemValidators?: () => (PropertyDecorator | ParameterDecorator)[];
package/dist/index.mjs CHANGED
@@ -202,6 +202,15 @@ const moostMate = new Mate(METADATA_WORKSPACE, {
202
202
  readType: true,
203
203
  readReturnType: true,
204
204
  collectPropKeys: true,
205
+ inherit(classMeta, targetMeta, level) {
206
+ if (level === 'CLASS') {
207
+ return !!(classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit);
208
+ }
209
+ if (level === 'PROP') {
210
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit) || !!((classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit) && !targetMeta);
211
+ }
212
+ return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit);
213
+ },
205
214
  });
206
215
  function getMoostMate() {
207
216
  return moostMate;
@@ -258,9 +267,18 @@ function getInstanceOwnMethods(instance) {
258
267
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
259
268
  const proto = Object.getPrototypeOf(instance);
260
269
  return [
270
+ ...getParentProps(getConstructor(instance)),
261
271
  ...Object.getOwnPropertyNames(proto),
262
272
  ...Object.getOwnPropertyNames(instance),
263
273
  ].filter(m => typeof instance[m] === 'function');
274
+ }
275
+ const fnProto = Object.getPrototypeOf(Function);
276
+ function getParentProps(constructor) {
277
+ const parent = Object.getPrototypeOf(constructor);
278
+ if (typeof parent === 'function' && parent !== fnProto && parent !== constructor && parent.prototype) {
279
+ return [...getParentProps(parent), ...Object.getOwnPropertyNames(parent.prototype)];
280
+ }
281
+ return [];
264
282
  }
265
283
 
266
284
  const banner = () => `[${"moost"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
@@ -278,18 +296,18 @@ function panic(error) {
278
296
  return new Error(error);
279
297
  }
280
298
 
281
- function getCallableFn(targetInstance, fn, restoreCtx) {
299
+ function getCallableFn(targetInstance, fn, restoreCtx, pipes, silent) {
282
300
  return __awaiter(this, void 0, void 0, function* () {
283
301
  const mate = getMoostMate();
284
302
  const meta = mate.read(fn);
285
303
  if (meta === null || meta === void 0 ? void 0 : meta.injectable) {
286
304
  const infact = getMoostInfact();
287
- infact.silent(meta.injectable === 'FOR_EVENT');
305
+ infact.silent(silent || (meta.injectable === 'FOR_EVENT' ? 'logs' : false));
288
306
  const instance = yield infact.getForInstance(targetInstance, fn, {
289
307
  syncContextFn: () => { restoreCtx && restoreCtx(); },
290
- customData: { pipes: meta.pipes },
308
+ customData: { pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority) },
291
309
  });
292
- infact.silent(false);
310
+ infact.silent(!!silent);
293
311
  return ((...args) => {
294
312
  return instance.handler(...args);
295
313
  });
@@ -376,6 +394,7 @@ function bindControllerMethods(options) {
376
394
  const methodMeta = getMoostMate().read(fakeInstance, method) || {};
377
395
  if (!methodMeta.handlers || !methodMeta.handlers.length)
378
396
  continue;
397
+ const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])].sort((a, b) => a.priority - b.priority);
379
398
  // preparing interceptors
380
399
  const interceptors = [...(opts.interceptors || []), ...(meta.interceptors || []), ...(methodMeta.interceptors || [])].sort((a, b) => a.priority - b.priority);
381
400
  const getIterceptorHandler = () => {
@@ -387,7 +406,7 @@ function bindControllerMethods(options) {
387
406
  const { restoreCtx } = useHttpContext();
388
407
  const targetInstance = yield getInstance();
389
408
  restoreCtx();
390
- return (yield getCallableFn(targetInstance, handler, restoreCtx))(...args);
409
+ return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, options.silent))(...args);
391
410
  }));
392
411
  }
393
412
  else {
@@ -397,7 +416,6 @@ function bindControllerMethods(options) {
397
416
  return Promise.resolve(new InterceptorHandler(interceptorHandlers));
398
417
  };
399
418
  // preparing pipes
400
- const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])];
401
419
  const argsPipes = [];
402
420
  for (const p of methodMeta.params || []) {
403
421
  argsPipes.push({
@@ -455,7 +473,7 @@ function Required() {
455
473
  const mate = getMoostMate();
456
474
  return mate.apply(mate.decorate('required', true),
457
475
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
458
- mate.decorateClass((meta, key, index) => {
476
+ mate.decorateClass((meta, level, key, index) => {
459
477
  if (typeof index !== 'number' && meta && ['string', 'symbol'].includes(typeof key)) {
460
478
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
461
479
  meta.requiredProps = meta.requiredProps || [];
@@ -623,6 +641,8 @@ function IsBoolean(...args) {
623
641
  return Validate(validoIsBoolean(...args));
624
642
  }
625
643
 
644
+ const Inherit = () => getMoostMate().decorate('inherit', true);
645
+
626
646
  var TPipePriority;
627
647
  (function (TPipePriority) {
628
648
  TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
@@ -692,7 +712,7 @@ class Moost {
692
712
  this.provide = createProvideRegistry([Infact, getMoostInfact], [Mate, getMoostMate], [Valido, getMoostValido]);
693
713
  this.unregisteredControllers = [];
694
714
  if (options === null || options === void 0 ? void 0 : options.silent) {
695
- getMoostInfact().silent();
715
+ getMoostInfact().silent(options === null || options === void 0 ? void 0 : options.silent);
696
716
  }
697
717
  }
698
718
  adapter(a) {
@@ -749,12 +769,12 @@ class Moost {
749
769
  }
750
770
  // getInstance - instance factory for resolving SINGLETON and FOR_EVENT instance
751
771
  const getInstance = instance ? () => Promise.resolve(instance) : () => __awaiter(this, void 0, void 0, function* () {
752
- var _c;
772
+ var _c, _d;
753
773
  // if (!instance) {
754
- infact.silent();
774
+ infact.silent(((_c = this.options) === null || _c === void 0 ? void 0 : _c.silent) || 'logs');
755
775
  const { restoreCtx } = useEventContext();
756
776
  const instance = yield infact.get(controller, Object.assign(Object.assign({}, infactOpts), { syncContextFn: restoreCtx }));
757
- infact.silent(!!((_c = this.options) === null || _c === void 0 ? void 0 : _c.silent));
777
+ infact.silent(!!((_d = this.options) === null || _d === void 0 ? void 0 : _d.silent));
758
778
  // }
759
779
  return instance;
760
780
  });
@@ -936,7 +956,7 @@ const validatePipe = (opts) => {
936
956
  else if (level === 'CLASS') {
937
957
  meta = metas.classMeta || {};
938
958
  }
939
- 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);
959
+ const result = yield valido.validateParam(_value, meta, metas.key, undefined, metas.instance, undefined, 0, 0, (opts === null || opts === void 0 ? void 0 : opts.errorLimit) || DEFAULT_ERROR_LIMIT, restoreCtx);
940
960
  if (result !== true) {
941
961
  const message = typeof result === 'string' ? result : firstString(result);
942
962
  if (opts === null || opts === void 0 ? void 0 : opts.errorCb) {
@@ -952,4 +972,4 @@ const validatePipe = (opts) => {
952
972
  return pipe;
953
973
  };
954
974
 
955
- export { Circular, Const, Controller, Dto, Id, ImportController, Inject, Injectable, Intercept, InterceptorHandler, IsArray, IsBoolean, IsNumber, IsString, IsTypeOf, Label, Moost, Optional, Param, Params, Provide, Required, Resolve, TInterceptorPriority, TPipePriority, Validate, genericTypesCastPipe, getMoostInfact, getMoostMate, getNewMoostInfact, resolvePipe, useControllerMeta, validatePipe };
975
+ export { Circular, Const, Controller, Dto, Id, ImportController, Inherit, Inject, Injectable, Intercept, InterceptorHandler, IsArray, IsBoolean, IsNumber, IsString, IsTypeOf, Label, Moost, Optional, Param, Params, Provide, Required, Resolve, TInterceptorPriority, TPipePriority, Validate, genericTypesCastPipe, getMoostInfact, getMoostMate, getNewMoostInfact, resolvePipe, useControllerMeta, validatePipe };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moost",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
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.1.4",
26
- "@prostojs/mate": "^0.1.12",
25
+ "@prostojs/infact": "^0.1.7",
26
+ "@prostojs/mate": "^0.1.13",
27
27
  "@prostojs/valido": "^0.0.2",
28
- "@wooksjs/event-core": "^0.2.6",
29
- "wooks": "^0.2.6"
28
+ "@wooksjs/event-core": "^0.2.7",
29
+ "wooks": "^0.2.7"
30
30
  },
31
31
  "author": "Artem Maltsev",
32
32
  "license": "MIT",