@rsdk/nest-tools 3.4.1 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,16 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.6.0](https://github.com/R-Vision/rsdk/compare/v3.6.0-next.8...v3.6.0) (2023-10-20)
7
+
8
+ **Note:** Version bump only for package @rsdk/nest-tools
9
+
10
+ ## [3.6.0-next.0](https://github.com/R-Vision/rsdk/compare/v3.5.0...v3.6.0-next.0) (2023-10-06)
11
+
12
+ ### Features
13
+
14
+ * discover config from inject section in dynamic provider ([b20c41c](https://github.com/R-Vision/rsdk/commit/b20c41c08802c2ece7c3fe24614959722cae12b4))
15
+
6
16
  ## [3.4.1](https://github.com/R-Vision/rsdk/compare/v3.4.1-next.0...v3.4.1) (2023-09-01)
7
17
 
8
18
  **Note:** Version bump only for package @rsdk/nest-tools
@@ -0,0 +1,5 @@
1
+ import type { Abstract, ClassProvider, DynamicModule, ExistingProvider, FactoryProvider, Type, ValueProvider } from '@nestjs/common';
2
+ export declare class Assert {
3
+ static isObjectOrCtor(nestDefinition: unknown): nestDefinition is DynamicModule | Type<any> | ClassProvider<any> | ValueProvider<any> | FactoryProvider<any> | ExistingProvider<any> | Abstract<any> | Function;
4
+ static isObject(v: unknown): v is object;
5
+ }
package/dist/assert.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Assert = void 0;
4
+ class Assert {
5
+ static isObjectOrCtor(nestDefinition) {
6
+ return (['object', 'function'].includes(typeof nestDefinition) &&
7
+ nestDefinition !== null);
8
+ }
9
+ static isObject(v) {
10
+ return typeof v === 'object';
11
+ }
12
+ }
13
+ exports.Assert = Assert;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { NestDefinitionType } from './constants';
2
2
  export { NestDefinitionIterator } from './nest-definition.iterator';
3
3
  export { NestDefinition } from './nest-definition';
4
+ export { Assert } from './assert';
package/dist/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NestDefinition = exports.NestDefinitionIterator = exports.NestDefinitionType = void 0;
3
+ exports.Assert = exports.NestDefinition = exports.NestDefinitionIterator = exports.NestDefinitionType = void 0;
4
4
  var constants_1 = require("./constants");
5
5
  Object.defineProperty(exports, "NestDefinitionType", { enumerable: true, get: function () { return constants_1.NestDefinitionType; } });
6
6
  var nest_definition_iterator_1 = require("./nest-definition.iterator");
7
7
  Object.defineProperty(exports, "NestDefinitionIterator", { enumerable: true, get: function () { return nest_definition_iterator_1.NestDefinitionIterator; } });
8
8
  var nest_definition_1 = require("./nest-definition");
9
9
  Object.defineProperty(exports, "NestDefinition", { enumerable: true, get: function () { return nest_definition_1.NestDefinition; } });
10
+ var assert_1 = require("./assert");
11
+ Object.defineProperty(exports, "Assert", { enumerable: true, get: function () { return assert_1.Assert; } });
@@ -1,4 +1,4 @@
1
- import type { DynamicModule, InjectionToken, Provider, Type } from '@nestjs/common';
1
+ import type { Abstract, ClassProvider, DynamicModule, ExistingProvider, FactoryProvider, InjectionToken, OptionalFactoryDependency, Provider, Type, ValueProvider } from '@nestjs/common';
2
2
  export type NestModuleDefinition = DynamicModule | Type;
3
3
  /**
4
4
  * Хелпер для работы с объявлением модуля nest.
@@ -7,6 +7,9 @@ export type NestModuleDefinition = DynamicModule | Type;
7
7
  export declare class NestDefinition {
8
8
  private definition;
9
9
  constructor(definition: NestModuleDefinition);
10
+ static extractDeps(value: DynamicModule | ClassProvider<any> | ValueProvider<any> | FactoryProvider<any> | ExistingProvider<any> | Type<any> | Abstract<any> | Function): (InjectionToken | OptionalFactoryDependency)[];
11
+ static extractInjectDeps(value: DynamicModule | ClassProvider<any> | ValueProvider<any> | FactoryProvider<any> | ExistingProvider<any> | Type<any> | Abstract<any> | Function): (InjectionToken | OptionalFactoryDependency)[];
12
+ static hasInjectDeps(value: DynamicModule | ClassProvider<any> | ValueProvider<any> | FactoryProvider<any> | ExistingProvider<any> | Type<any> | Abstract<any> | Function): value is FactoryProvider & Required<Pick<FactoryProvider, 'inject'>>;
10
13
  getProviders(): Provider[] | undefined;
11
14
  getImports(): NestModuleDefinition[] | undefined;
12
15
  getControllers(): Type[] | undefined;
@@ -1,11 +1,7 @@
1
- import type { Abstract, ClassProvider, DynamicModule, ExistingProvider, FactoryProvider, InjectionToken, Provider, Type, ValueProvider } from '@nestjs/common';
1
+ import type { InjectionToken, Provider } from '@nestjs/common';
2
2
  import type { Promisable } from 'type-fest';
3
3
  import { NestDefinitionType } from './constants';
4
4
  import type { NestModuleDefinition } from './nest-definition';
5
- export declare class Assert {
6
- static isObjectOrCtor(nestDefinition: DynamicModule | Type<any> | ClassProvider<any> | ValueProvider<any> | FactoryProvider<any> | ExistingProvider<any> | string | symbol | Abstract<any>): nestDefinition is DynamicModule | Type<any> | ClassProvider<any> | ValueProvider<any> | FactoryProvider<any> | ExistingProvider<any> | Abstract<any> | Function;
7
- static isObject(v: unknown): v is object;
8
- }
9
5
  export declare class NestDefinitionIterator {
10
6
  private nestModuleDefinition;
11
7
  constructor(nestModuleDefinition: Promisable<NestModuleDefinition>);
@@ -1,18 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NestDefinitionIterator = exports.Assert = void 0;
3
+ exports.NestDefinitionIterator = void 0;
4
+ const assert_1 = require("./assert");
4
5
  const constants_1 = require("./constants");
5
6
  const exception_1 = require("./exception");
6
7
  const nest_definition_1 = require("./nest-definition");
7
- class Assert {
8
- static isObjectOrCtor(nestDefinition) {
9
- return ['object', 'function'].includes(typeof nestDefinition);
10
- }
11
- static isObject(v) {
12
- return typeof v === 'object';
13
- }
14
- }
15
- exports.Assert = Assert;
16
8
  class NestDefinitionIterator {
17
9
  nestModuleDefinition;
18
10
  constructor(nestModuleDefinition) {
@@ -64,7 +56,7 @@ class NestDefinitionIterator {
64
56
  }
65
57
  async find(predicate, type) {
66
58
  for await (const definition of this.iterate(type)) {
67
- if (!Assert.isObjectOrCtor(definition)) {
59
+ if (!assert_1.Assert.isObjectOrCtor(definition)) {
68
60
  throw new exception_1.UnexpectedNestModuleDefinitionValue(definition);
69
61
  }
70
62
  const isNeeded = predicate(definition);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NestDefinition = void 0;
4
4
  const constants_1 = require("@nestjs/common/constants");
5
5
  const scanner_1 = require("@nestjs/core/scanner");
6
+ const assert_1 = require("./assert");
6
7
  /**
7
8
  * Хелпер для работы с объявлением модуля nest.
8
9
  * Получения провайдеров и импортов
@@ -12,6 +13,30 @@ class NestDefinition {
12
13
  constructor(definition) {
13
14
  this.definition = definition;
14
15
  }
16
+ static extractDeps(value) {
17
+ const designParamTypes = Reflect.getMetadata('design:paramtypes', value);
18
+ if (designParamTypes) {
19
+ return designParamTypes;
20
+ }
21
+ if (this.hasInjectDeps(value)) {
22
+ return this.extractInjectDeps(value);
23
+ }
24
+ return [];
25
+ }
26
+ static extractInjectDeps(value) {
27
+ if (!this.hasInjectDeps(value)) {
28
+ throw new Error('Incorrect argument, expected FactoryProvider');
29
+ }
30
+ return value.inject.map((obj) => {
31
+ if (assert_1.Assert.isObjectOrCtor(obj) && 'token' in obj) {
32
+ return obj.token;
33
+ }
34
+ return obj;
35
+ });
36
+ }
37
+ static hasInjectDeps(value) {
38
+ return 'inject' in value && Boolean(value.inject);
39
+ }
15
40
  getProviders() {
16
41
  return scanner_1.DependenciesScanner.prototype.isDynamicModule(this.definition)
17
42
  ? [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsdk/nest-tools",
3
- "version": "3.4.1",
3
+ "version": "3.6.0",
4
4
  "license": "Apache License 2.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -10,5 +10,5 @@
10
10
  "@nestjs/common": "^10.1.3",
11
11
  "@nestjs/core": "^10.1.3"
12
12
  },
13
- "gitHead": "54dcdaa65d9a9f6c19bf6c6abefcb48921bacf18"
13
+ "gitHead": "cc75fb4595bd49e877974f6eeeef66f30743011c"
14
14
  }
package/src/assert.ts ADDED
@@ -0,0 +1,31 @@
1
+ import type {
2
+ Abstract,
3
+ ClassProvider,
4
+ DynamicModule,
5
+ ExistingProvider,
6
+ FactoryProvider,
7
+ Type,
8
+ ValueProvider,
9
+ } from '@nestjs/common';
10
+
11
+ export class Assert {
12
+ static isObjectOrCtor(nestDefinition: unknown): nestDefinition is
13
+ | DynamicModule
14
+ | Type<any>
15
+ | ClassProvider<any>
16
+ | ValueProvider<any>
17
+ | FactoryProvider<any>
18
+ | ExistingProvider<any>
19
+ | Abstract<any>
20
+ // eslint-disable-next-line @typescript-eslint/ban-types
21
+ | Function {
22
+ return (
23
+ ['object', 'function'].includes(typeof nestDefinition) &&
24
+ nestDefinition !== null
25
+ );
26
+ }
27
+
28
+ static isObject(v: unknown): v is object {
29
+ return typeof v === 'object';
30
+ }
31
+ }
package/src/index.ts CHANGED
@@ -3,3 +3,4 @@ export { NestDefinitionType } from './constants';
3
3
  export { NestDefinitionIterator } from './nest-definition.iterator';
4
4
 
5
5
  export { NestDefinition } from './nest-definition';
6
+ export { Assert } from './assert';
@@ -1,16 +1,7 @@
1
- import type {
2
- Abstract,
3
- ClassProvider,
4
- DynamicModule,
5
- ExistingProvider,
6
- FactoryProvider,
7
- InjectionToken,
8
- Provider,
9
- Type,
10
- ValueProvider,
11
- } from '@nestjs/common';
1
+ import type { InjectionToken, Provider } from '@nestjs/common';
12
2
  import type { Promisable } from 'type-fest';
13
3
 
4
+ import { Assert } from './assert';
14
5
  import { NestDefinitionType } from './constants';
15
6
  import {
16
7
  FalsyValueInNestModuleTree,
@@ -19,36 +10,6 @@ import {
19
10
  import type { NestModuleDefinition } from './nest-definition';
20
11
  import { NestDefinition } from './nest-definition';
21
12
 
22
- export class Assert {
23
- static isObjectOrCtor(
24
- nestDefinition:
25
- | DynamicModule
26
- | Type<any>
27
- | ClassProvider<any>
28
- | ValueProvider<any>
29
- | FactoryProvider<any>
30
- | ExistingProvider<any>
31
- | string
32
- | symbol
33
- | Abstract<any>,
34
- ): nestDefinition is
35
- | DynamicModule
36
- | Type<any>
37
- | ClassProvider<any>
38
- | ValueProvider<any>
39
- | FactoryProvider<any>
40
- | ExistingProvider<any>
41
- | Abstract<any>
42
- // eslint-disable-next-line @typescript-eslint/ban-types
43
- | Function {
44
- return ['object', 'function'].includes(typeof nestDefinition);
45
- }
46
-
47
- static isObject(v: unknown): v is object {
48
- return typeof v === 'object';
49
- }
50
- }
51
-
52
13
  export class NestDefinitionIterator {
53
14
  constructor(private nestModuleDefinition: Promisable<NestModuleDefinition>) {}
54
15
 
@@ -1,12 +1,20 @@
1
1
  import type {
2
+ Abstract,
3
+ ClassProvider,
2
4
  DynamicModule,
5
+ ExistingProvider,
6
+ FactoryProvider,
3
7
  InjectionToken,
8
+ OptionalFactoryDependency,
4
9
  Provider,
5
10
  Type,
11
+ ValueProvider,
6
12
  } from '@nestjs/common';
7
13
  import { MODULE_METADATA } from '@nestjs/common/constants';
8
14
  import { DependenciesScanner } from '@nestjs/core/scanner';
9
15
 
16
+ import { Assert } from './assert';
17
+
10
18
  export type NestModuleDefinition = DynamicModule | Type;
11
19
 
12
20
  /**
@@ -16,6 +24,66 @@ export type NestModuleDefinition = DynamicModule | Type;
16
24
  export class NestDefinition {
17
25
  constructor(private definition: NestModuleDefinition) {}
18
26
 
27
+ static extractDeps(
28
+ value:
29
+ | DynamicModule
30
+ | ClassProvider<any>
31
+ | ValueProvider<any>
32
+ | FactoryProvider<any>
33
+ | ExistingProvider<any>
34
+ | Type<any>
35
+ | Abstract<any>
36
+ // eslint-disable-next-line @typescript-eslint/ban-types
37
+ | Function,
38
+ ): (InjectionToken | OptionalFactoryDependency)[] {
39
+ const designParamTypes = Reflect.getMetadata('design:paramtypes', value);
40
+ if (designParamTypes) {
41
+ return designParamTypes;
42
+ }
43
+ if (this.hasInjectDeps(value)) {
44
+ return this.extractInjectDeps(value);
45
+ }
46
+ return [];
47
+ }
48
+
49
+ static extractInjectDeps(
50
+ value:
51
+ | DynamicModule
52
+ | ClassProvider<any>
53
+ | ValueProvider<any>
54
+ | FactoryProvider<any>
55
+ | ExistingProvider<any>
56
+ | Type<any>
57
+ | Abstract<any>
58
+ // eslint-disable-next-line @typescript-eslint/ban-types
59
+ | Function,
60
+ ): (InjectionToken | OptionalFactoryDependency)[] {
61
+ if (!this.hasInjectDeps(value)) {
62
+ throw new Error('Incorrect argument, expected FactoryProvider');
63
+ }
64
+ return value.inject.map((obj) => {
65
+ if (Assert.isObjectOrCtor(obj) && 'token' in obj) {
66
+ return obj.token as InjectionToken;
67
+ }
68
+ return obj;
69
+ });
70
+ }
71
+
72
+ static hasInjectDeps(
73
+ value:
74
+ | DynamicModule
75
+ | ClassProvider<any>
76
+ | ValueProvider<any>
77
+ | FactoryProvider<any>
78
+ | ExistingProvider<any>
79
+ | Type<any>
80
+ | Abstract<any>
81
+ // eslint-disable-next-line @typescript-eslint/ban-types
82
+ | Function,
83
+ ): value is FactoryProvider & Required<Pick<FactoryProvider, 'inject'>> {
84
+ return 'inject' in value && Boolean(value.inject);
85
+ }
86
+
19
87
  getProviders(): Provider[] | undefined {
20
88
  return DependenciesScanner.prototype.isDynamicModule(this.definition)
21
89
  ? [