ts-ioc-container 55.7.0 → 55.8.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.
@@ -5,6 +5,7 @@ const IInjector_1 = require("./IInjector");
5
5
  const basic_1 = require("../utils/basic");
6
6
  const parameter_1 = require("../metadata/parameter");
7
7
  const toToken_1 = require("../token/toToken");
8
+ const FunctionToken_1 = require("../token/FunctionToken");
8
9
  class MetadataInjector extends IInjector_1.Injector {
9
10
  createInstance(scope, Target, { args: deps = [] } = {}) {
10
11
  const args = (0, exports.resolveArgs)(Target)(scope, { args: deps });
@@ -13,8 +14,12 @@ class MetadataInjector extends IInjector_1.Injector {
13
14
  }
14
15
  exports.MetadataInjector = MetadataInjector;
15
16
  const hookMetaKey = (methodName = 'constructor') => `inject:${methodName}`;
16
- const inject = (fn) => (target, propertyKey, parameterIndex) => {
17
- (0, parameter_1.addParamMeta)(hookMetaKey(propertyKey), () => (0, toToken_1.toToken)(fn))(basic_1.Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
17
+ const inject = (fn, selectFn = (instance) => instance) => (target, propertyKey, parameterIndex) => {
18
+ const toSelectedToken = () => {
19
+ const token = (0, toToken_1.toToken)(fn);
20
+ return new FunctionToken_1.FunctionToken((scope, options) => selectFn(token.resolve(scope, options)));
21
+ };
22
+ (0, parameter_1.addParamMeta)(hookMetaKey(propertyKey), toSelectedToken)(basic_1.Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
18
23
  };
19
24
  exports.inject = inject;
20
25
  const argsFn = (predicate) => (c, { args = [] }) => args.find((value, index) => predicate(value, index));
@@ -2,6 +2,7 @@ import { Injector } from './IInjector.js';
2
2
  import { Is } from '../utils/basic.js';
3
3
  import { getParamMeta, addParamMeta } from '../metadata/parameter.js';
4
4
  import { argToToken, toToken } from '../token/toToken.js';
5
+ import { FunctionToken } from '../token/FunctionToken.js';
5
6
  export class MetadataInjector extends Injector {
6
7
  createInstance(scope, Target, { args: deps = [] } = {}) {
7
8
  const args = resolveArgs(Target)(scope, { args: deps });
@@ -9,8 +10,12 @@ export class MetadataInjector extends Injector {
9
10
  }
10
11
  }
11
12
  const hookMetaKey = (methodName = 'constructor') => `inject:${methodName}`;
12
- export const inject = (fn) => (target, propertyKey, parameterIndex) => {
13
- addParamMeta(hookMetaKey(propertyKey), () => toToken(fn))(Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
13
+ export const inject = (fn, selectFn = (instance) => instance) => (target, propertyKey, parameterIndex) => {
14
+ const toSelectedToken = () => {
15
+ const token = toToken(fn);
16
+ return new FunctionToken((scope, options) => selectFn(token.resolve(scope, options)));
17
+ };
18
+ addParamMeta(hookMetaKey(propertyKey), toSelectedToken)(Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
14
19
  };
15
20
  export const argsFn = (predicate) => (c, { args = [] }) => args.find((value, index) => predicate(value, index));
16
21
  export const args = (index) => argsFn((value, i) => i === index);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "55.7.0",
3
+ "version": "55.8.0",
4
4
  "description": "Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -7,7 +7,7 @@ import { InjectFn } from '../hooks/hook.js';
7
7
  export declare class MetadataInjector extends Injector implements IInjector {
8
8
  protected createInstance<T>(scope: IContainer, Target: constructor<T>, { args: deps }?: InjectOptions): T;
9
9
  }
10
- export declare const inject: <T>(fn: InjectionToken<T> | InjectFn<T> | symbol | string | constructor<T>) => ParameterDecorator;
10
+ export declare const inject: <T, R = T>(fn: InjectionToken<T> | InjectFn<T> | symbol | string | constructor<T>, selectFn?: (instance: T) => R) => ParameterDecorator;
11
11
  export declare const argsFn: <T = unknown>(predicate: (value: unknown, index: number) => boolean) => InjectFn<T>;
12
- export declare const args: (index: number) => InjectFn;
12
+ export declare const args: <T = unknown>(index: number) => InjectFn<T>;
13
13
  export declare const resolveArgs: (Target: constructor<unknown>, methodName?: string) => (scope: IContainer, { args, lazy }: ProviderOptions) => unknown[];