meteorjs-decorators 1.0.2 → 1.0.3

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.
@@ -3,21 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Controller = Controller;
4
4
  require("reflect-metadata");
5
5
  const container_1 = require("../utils/container");
6
+ const forward_ref_1 = require("../utils/forward-ref");
7
+ const inject_decorator_1 = require("./inject.decorator");
6
8
  function Controller() {
7
9
  return function (constructor) {
8
10
  const methods = constructor.prototype.__methods || [];
9
11
  // Get the constructor parameters
10
12
  const paramTypes = Reflect.getMetadata('design:paramtypes', constructor) || [];
13
+ const injectTokens = (0, inject_decorator_1.getInjectTokens)(constructor);
11
14
  // Create a factory function that will create instances with dependencies (services)
12
15
  const factory = () => {
13
- const args = paramTypes.map((type) => {
16
+ const args = paramTypes.map((type, index) => {
14
17
  // Get the module's container from the prototype chain
15
18
  let current = constructor;
16
19
  while (current && !current.__container) {
17
20
  current = Object.getPrototypeOf(current);
18
21
  }
19
22
  const container = current?.__container || container_1.Container;
20
- return container.get(type.name);
23
+ const injectionToken = resolveInjectionToken(type, injectTokens[index], constructor.name, index);
24
+ return container.get(injectionToken);
21
25
  });
22
26
  return new constructor(...args);
23
27
  };
@@ -27,7 +31,7 @@ function Controller() {
27
31
  current = Object.getPrototypeOf(current);
28
32
  }
29
33
  const container = current?.__container || container_1.Container;
30
- container.register(constructor.name, factory);
34
+ container.register(constructor, factory);
31
35
  methods.forEach(({ name, method }) => {
32
36
  console.log('registering method: ', name);
33
37
  Meteor.methods({
@@ -38,7 +42,7 @@ function Controller() {
38
42
  current = Object.getPrototypeOf(current);
39
43
  }
40
44
  const container = current?.__container || container_1.Container;
41
- const instance = container.get(constructor.name);
45
+ const instance = container.get(constructor);
42
46
  // Merge the Meteor method context into the instance
43
47
  instance.__context = this; // __context now is the meteor context for methods
44
48
  // Call the method with the merged context
@@ -48,3 +52,22 @@ function Controller() {
48
52
  });
49
53
  };
50
54
  }
55
+ function resolveInjectionToken(reflectedType, customToken, targetName, index) {
56
+ if (customToken) {
57
+ return unwrapToken(customToken);
58
+ }
59
+ if (!reflectedType) {
60
+ throw new Error(`Cannot resolve dependency for ${targetName}. Parameter at index ${index} is undefined. ` +
61
+ 'Consider using @Inject with forwardRef to resolve circular dependencies.');
62
+ }
63
+ return reflectedType;
64
+ }
65
+ function unwrapToken(token) {
66
+ if (typeof token === 'string') {
67
+ return token;
68
+ }
69
+ if (token instanceof forward_ref_1.ForwardRef) {
70
+ return token.get();
71
+ }
72
+ return token();
73
+ }
@@ -30,7 +30,7 @@ function Injectable() {
30
30
  current = Object.getPrototypeOf(current);
31
31
  }
32
32
  const container = current?.__container || container_1.Container;
33
- container.register(target.name, factory);
33
+ container.register(target, factory);
34
34
  return target;
35
35
  };
36
36
  }
@@ -42,14 +42,14 @@ function resolveInjectionToken(reflectedType, customToken, targetName, index) {
42
42
  throw new Error(`Cannot resolve dependency for ${targetName}. Parameter at index ${index} is undefined. ` +
43
43
  'Consider using @Inject with forwardRef to resolve circular dependencies.');
44
44
  }
45
- return reflectedType.name;
45
+ return reflectedType;
46
46
  }
47
47
  function unwrapToken(token) {
48
48
  if (typeof token === 'string') {
49
49
  return token;
50
50
  }
51
51
  if (token instanceof forward_ref_1.ForwardRef) {
52
- return token.get().name;
52
+ return token.get();
53
53
  }
54
- return token().name;
54
+ return token();
55
55
  }
@@ -41,14 +41,14 @@ function resolveInjectionToken(reflectedType, customToken, targetName, index) {
41
41
  throw new Error(`Cannot resolve dependency for ${targetName}. Parameter at index ${index} is undefined. ` +
42
42
  'Consider using @Inject with forwardRef to resolve circular dependencies.');
43
43
  }
44
- return reflectedType.name;
44
+ return reflectedType;
45
45
  }
46
46
  function unwrapToken(token) {
47
47
  if (typeof token === 'string') {
48
48
  return token;
49
49
  }
50
50
  if (token instanceof forward_ref_1.ForwardRef) {
51
- return token.get().name;
51
+ return token.get();
52
52
  }
53
- return token().name;
53
+ return token();
54
54
  }
@@ -2,6 +2,7 @@ import { ForwardRef } from './forward-ref';
2
2
  export interface ContainerAware {
3
3
  __container?: ModuleContainer;
4
4
  }
5
+ export type ContainerToken<T = any> = string | symbol | (new (...args: any[]) => T);
5
6
  export declare class ModuleContainer {
6
7
  private instances;
7
8
  private factories;
@@ -9,18 +10,19 @@ export declare class ModuleContainer {
9
10
  private resolving;
10
11
  private parent?;
11
12
  constructor(parent?: ModuleContainer);
12
- set<T>(token: string, value: T | ForwardRef<T>): void;
13
- register<T>(token: string, factory: () => T): void;
14
- get<T>(token: string): T;
15
- has(token: string): boolean;
13
+ set<T>(token: ContainerToken<T>, value: T | ForwardRef<T>): void;
14
+ register<T>(token: ContainerToken<T>, factory: () => T): void;
15
+ get<T>(token: ContainerToken<T>): T;
16
+ has(token: ContainerToken): boolean;
16
17
  clear(): void;
17
18
  private getProxy;
19
+ private tokenLabel;
18
20
  }
19
21
  export declare class Container {
20
22
  private static rootContainer;
21
- static set<T>(token: string, value: T | ForwardRef<T>): void;
22
- static register<T>(token: string, factory: () => T): void;
23
- static get<T>(token: string): T;
24
- static has(token: string): boolean;
23
+ static set<T>(token: ContainerToken<T>, value: T | ForwardRef<T>): void;
24
+ static register<T>(token: ContainerToken<T>, factory: () => T): void;
25
+ static get<T>(token: ContainerToken<T>): T;
26
+ static has(token: ContainerToken): boolean;
25
27
  static createChildContainer(): ModuleContainer;
26
28
  }
@@ -54,7 +54,7 @@ class ModuleContainer {
54
54
  get: (_target, prop) => {
55
55
  const instance = this.instances.get(token);
56
56
  if (!instance) {
57
- throw new Error(`Circular dependency detected for token ${token}.`);
57
+ throw new Error(`Circular dependency detected for token ${this.tokenLabel(token)}.`);
58
58
  }
59
59
  const value = instance[prop];
60
60
  return typeof value === 'function' ? value.bind(instance) : value;
@@ -62,7 +62,7 @@ class ModuleContainer {
62
62
  set: (_target, prop, value) => {
63
63
  const instance = this.instances.get(token);
64
64
  if (!instance) {
65
- throw new Error(`Circular dependency detected for token ${token}.`);
65
+ throw new Error(`Circular dependency detected for token ${this.tokenLabel(token)}.`);
66
66
  }
67
67
  instance[prop] = value;
68
68
  return true;
@@ -72,6 +72,15 @@ class ModuleContainer {
72
72
  }
73
73
  return this.proxies.get(token);
74
74
  }
75
+ tokenLabel(token) {
76
+ if (typeof token === 'string') {
77
+ return token;
78
+ }
79
+ if (typeof token === 'symbol') {
80
+ return token.toString();
81
+ }
82
+ return token.name || '<anonymous>';
83
+ }
75
84
  }
76
85
  exports.ModuleContainer = ModuleContainer;
77
86
  // Global container for backward compatibility
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Module = Module;
4
4
  require("reflect-metadata");
5
5
  const container_1 = require("./container");
6
+ const forward_ref_1 = require("./forward-ref");
7
+ const inject_decorator_1 = require("../decorators/inject.decorator");
6
8
  function Module(config) {
7
9
  return function (target) {
8
10
  // Create a container for this module
@@ -12,22 +14,20 @@ function Module(config) {
12
14
  config.imports.forEach(importedModule => {
13
15
  // Don't instantiate modules directly, just register them
14
16
  if (importedModule.forwardRef) {
15
- console.log('importedModule4: ', importedModule.forwardRef.get());
16
- moduleContainer.set(importedModule.forwardRef.get().name, importedModule.forwardRef.get());
17
+ moduleContainer.set(importedModule.forwardRef.get(), importedModule.forwardRef.get());
17
18
  }
18
19
  else {
19
- moduleContainer.set(importedModule.name, importedModule);
20
+ moduleContainer.set(importedModule, importedModule);
20
21
  }
21
22
  });
22
23
  }
23
24
  const resolveProvider = (providerClass) => {
24
- if (container_1.Container.has(providerClass.name)) {
25
- moduleContainer.set(providerClass.name, container_1.Container.get(providerClass.name));
25
+ if (container_1.Container.has(providerClass)) {
26
+ moduleContainer.set(providerClass, container_1.Container.get(providerClass));
26
27
  return;
27
28
  }
28
- const paramTypes = Reflect.getMetadata('design:paramtypes', providerClass) || [];
29
- const args = paramTypes.map((type) => moduleContainer.get(type.name));
30
- moduleContainer.set(providerClass.name, new providerClass(...args));
29
+ const args = resolveArguments(providerClass, moduleContainer);
30
+ moduleContainer.set(providerClass, new providerClass(...args));
31
31
  };
32
32
  // Then handle providers
33
33
  if (config.providers) {
@@ -45,15 +45,14 @@ function Module(config) {
45
45
  config.controllers.forEach(controller => {
46
46
  if (typeof controller === 'function') {
47
47
  let instance;
48
- if (container_1.Container.has(controller.name)) {
49
- instance = container_1.Container.get(controller.name);
48
+ if (container_1.Container.has(controller)) {
49
+ instance = container_1.Container.get(controller);
50
50
  }
51
51
  else {
52
- const paramTypes = Reflect.getMetadata('design:paramtypes', controller) || [];
53
- const args = paramTypes.map((type) => moduleContainer.get(type.name));
52
+ const args = resolveArguments(controller, moduleContainer);
54
53
  instance = new controller(...args);
55
54
  }
56
- moduleContainer.set(controller.name, instance);
55
+ moduleContainer.set(controller, instance);
57
56
  controller.__container = moduleContainer;
58
57
  }
59
58
  });
@@ -62,3 +61,30 @@ function Module(config) {
62
61
  target.__container = moduleContainer;
63
62
  };
64
63
  }
64
+ function resolveArguments(targetClass, container) {
65
+ const paramTypes = Reflect.getMetadata('design:paramtypes', targetClass) || [];
66
+ const injectTokens = (0, inject_decorator_1.getInjectTokens)(targetClass);
67
+ return paramTypes.map((type, index) => {
68
+ const token = resolveInjectionToken(type, injectTokens[index], targetClass.name, index);
69
+ return container.get(token);
70
+ });
71
+ }
72
+ function resolveInjectionToken(reflectedType, customToken, targetName, index) {
73
+ if (customToken) {
74
+ return unwrapToken(customToken);
75
+ }
76
+ if (!reflectedType) {
77
+ throw new Error(`Cannot resolve dependency for ${targetName}. Parameter at index ${index} is undefined. ` +
78
+ 'Consider using @Inject with forwardRef to resolve circular dependencies.');
79
+ }
80
+ return reflectedType;
81
+ }
82
+ function unwrapToken(token) {
83
+ if (typeof token === 'string') {
84
+ return token;
85
+ }
86
+ if (token instanceof forward_ref_1.ForwardRef) {
87
+ return token.get();
88
+ }
89
+ return token();
90
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "meteorjs-decorators",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Meteor decorators to make your Meteor app with NestJS-style pattern.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,4 +23,4 @@
23
23
  "mongodb": "^6.13.1",
24
24
  "typescript": "^5.4.5"
25
25
  }
26
- }
26
+ }