ts-ioc-container 70.0.0 → 71.0.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/README.md CHANGED
@@ -1795,7 +1795,7 @@ Constructor parameters that should pick up positional args from `ProviderOptions
1795
1795
 
1796
1796
  `argsFn(predicate)` is the general form: it iterates the runtime `args` array and returns the **first argument matching** `predicate(value, index)` — think `args.find(predicate)`. `arg(index)` is just a shortcut for matching by position: `arg(0)` is `argsFn((value, index) => index === 0)`. `args` is `(scope, options) => options.args`, i.e. it returns the runtime args array as-is. Every `InjectFn` receives `(scope, options)`, where `options.args` is the runtime args array.
1797
1797
 
1798
- `findOrFail(predicate)` is the strict, variadic counterpart for places that take the raw args list — `singleton(findOrFail(isUserId))` keys a per-argument singleton, for example. It returns the first argument matching `predicate(value)` and throws `ArgumentNotFoundError` when none does, instead of silently handing out `undefined`.
1798
+ `findOrFail(predicate)` is the strict counterpart for `singleton()` cache keys — `singleton(findOrFail(isUserId))` keys a per-argument singleton, for example. It receives the full runtime `args` array, returns the first argument matching `predicate(value)`, and throws `ArgumentNotFoundError` when none does, instead of silently handing out `undefined`.
1799
1799
 
1800
1800
  ### Runtime args flow through tokens
1801
1801
 
@@ -2024,7 +2024,7 @@ describe('IProvider', function () {
2024
2024
 
2025
2025
  @register(
2026
2026
  bindTo(EntityManagerToken),
2027
- singleton((repository) => (repository as IRepository).name), // Cache unique instance per repository type
2027
+ singleton(([repository]) => (repository as IRepository).name), // Cache unique instance per repository type
2028
2028
  )
2029
2029
  class EntityManager {
2030
2030
  constructor(@inject(arg(0)) public repository: IRepository) {}
@@ -32,7 +32,7 @@ class Provider {
32
32
  if (!this.getKey) {
33
33
  return this.resolveDep(scope, options);
34
34
  }
35
- const key = (0, basic_1.toString)(this.getKey(...(options.args ?? [])));
35
+ const key = (0, basic_1.toString)(this.getKey(options.args ?? []));
36
36
  if (!this.cache.has(key)) {
37
37
  this.cache.set(key, this.resolveDep(scope, options));
38
38
  }
@@ -6,8 +6,8 @@ class ClassToken extends InjectionToken_1.InjectionToken {
6
6
  target;
7
7
  _getArgsFn;
8
8
  _isLazy;
9
- constructor(target, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false } = {}) {
10
- super();
9
+ constructor(target, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false, tags = [], } = {}) {
10
+ super(tags);
11
11
  this.target = target;
12
12
  this._getArgsFn = getArgsFn;
13
13
  this._isLazy = isLazy;
@@ -26,6 +26,7 @@ class ClassToken extends InjectionToken_1.InjectionToken {
26
26
  return new ClassToken(this.target, {
27
27
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
28
28
  isLazy: this._isLazy,
29
+ tags: this.getTags(),
29
30
  });
30
31
  }
31
32
  argsFn(fn) {
@@ -33,12 +34,21 @@ class ClassToken extends InjectionToken_1.InjectionToken {
33
34
  return new ClassToken(this.target, {
34
35
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
35
36
  isLazy: this._isLazy,
37
+ tags: this.getTags(),
36
38
  });
37
39
  }
38
40
  lazy() {
39
41
  return new ClassToken(this.target, {
40
42
  getArgsFn: this._getArgsFn,
41
43
  isLazy: true,
44
+ tags: this.getTags(),
45
+ });
46
+ }
47
+ addTags(...tags) {
48
+ return new ClassToken(this.target, {
49
+ getArgsFn: this._getArgsFn,
50
+ isLazy: this._isLazy,
51
+ tags: [...this.getTags(), ...tags],
42
52
  });
43
53
  }
44
54
  toString() {
@@ -5,8 +5,8 @@ const InjectionToken_1 = require("./InjectionToken");
5
5
  const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError");
6
6
  class ConstantToken extends InjectionToken_1.InjectionToken {
7
7
  token;
8
- constructor(token) {
9
- super();
8
+ constructor(token, { tags = [] } = {}) {
9
+ super(tags);
10
10
  this.token = token;
11
11
  }
12
12
  resolve(s) {
@@ -21,6 +21,9 @@ class ConstantToken extends InjectionToken_1.InjectionToken {
21
21
  lazy() {
22
22
  throw new MethodNotImplementedError_1.MethodNotImplementedError('not implemented');
23
23
  }
24
+ addTags(...tags) {
25
+ return new ConstantToken(this.token, { tags: [...this.getTags(), ...tags] });
26
+ }
24
27
  toString() {
25
28
  throw new MethodNotImplementedError_1.MethodNotImplementedError('not implemented');
26
29
  }
@@ -7,8 +7,8 @@ class FunctionToken extends InjectionToken_1.InjectionToken {
7
7
  fn;
8
8
  _getArgsFn;
9
9
  _isLazy;
10
- constructor(fn, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false } = {}) {
11
- super();
10
+ constructor(fn, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false, tags = [], } = {}) {
11
+ super(tags);
12
12
  this.fn = fn;
13
13
  this._getArgsFn = getArgsFn;
14
14
  this._isLazy = isLazy;
@@ -24,6 +24,7 @@ class FunctionToken extends InjectionToken_1.InjectionToken {
24
24
  return new FunctionToken(this.fn, {
25
25
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
26
26
  isLazy: this._isLazy,
27
+ tags: this.getTags(),
27
28
  });
28
29
  }
29
30
  argsFn(fn) {
@@ -31,12 +32,21 @@ class FunctionToken extends InjectionToken_1.InjectionToken {
31
32
  return new FunctionToken(this.fn, {
32
33
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
33
34
  isLazy: this._isLazy,
35
+ tags: this.getTags(),
34
36
  });
35
37
  }
36
38
  lazy() {
37
39
  return new FunctionToken(this.fn, {
38
40
  getArgsFn: this._getArgsFn,
39
41
  isLazy: true,
42
+ tags: this.getTags(),
43
+ });
44
+ }
45
+ addTags(...tags) {
46
+ return new FunctionToken(this.fn, {
47
+ getArgsFn: this._getArgsFn,
48
+ isLazy: this._isLazy,
49
+ tags: [...this.getTags(), ...tags],
40
50
  });
41
51
  }
42
52
  toString() {
@@ -6,8 +6,8 @@ class GroupAliasToken extends InjectionToken_1.InjectionToken {
6
6
  token;
7
7
  _getArgsFn;
8
8
  _isLazy;
9
- constructor(token, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false } = {}) {
10
- super();
9
+ constructor(token, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false, tags = [], } = {}) {
10
+ super(tags);
11
11
  this.token = token;
12
12
  this._getArgsFn = getArgsFn;
13
13
  this._isLazy = isLazy;
@@ -29,6 +29,7 @@ class GroupAliasToken extends InjectionToken_1.InjectionToken {
29
29
  return new GroupAliasToken(this.token, {
30
30
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
31
31
  isLazy: this._isLazy,
32
+ tags: this.getTags(),
32
33
  });
33
34
  }
34
35
  argsFn(fn) {
@@ -36,12 +37,21 @@ class GroupAliasToken extends InjectionToken_1.InjectionToken {
36
37
  return new GroupAliasToken(this.token, {
37
38
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
38
39
  isLazy: this._isLazy,
40
+ tags: this.getTags(),
39
41
  });
40
42
  }
41
43
  lazy() {
42
44
  return new GroupAliasToken(this.token, {
43
45
  getArgsFn: this._getArgsFn,
44
46
  isLazy: true,
47
+ tags: this.getTags(),
48
+ });
49
+ }
50
+ addTags(...tags) {
51
+ return new GroupAliasToken(this.token, {
52
+ getArgsFn: this._getArgsFn,
53
+ isLazy: this._isLazy,
54
+ tags: [...this.getTags(), ...tags],
45
55
  });
46
56
  }
47
57
  toString() {
@@ -5,10 +5,11 @@ const InjectionToken_1 = require("./InjectionToken");
5
5
  const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError");
6
6
  class GroupInstanceToken extends InjectionToken_1.InjectionToken {
7
7
  predicate;
8
- isCascade = true;
9
- constructor(predicate) {
10
- super();
8
+ isCascade;
9
+ constructor(predicate, { tags = [], isCascade = true } = {}) {
10
+ super(tags);
11
11
  this.predicate = predicate;
12
+ this.isCascade = isCascade;
12
13
  }
13
14
  select(fn) {
14
15
  return (s) => this.resolve(s).map(fn);
@@ -29,6 +30,9 @@ class GroupInstanceToken extends InjectionToken_1.InjectionToken {
29
30
  resolve(c) {
30
31
  return c.getInstances(this.isCascade).filter(this.predicate);
31
32
  }
33
+ addTags(...tags) {
34
+ return new GroupInstanceToken(this.predicate, { tags: [...this.getTags(), ...tags], isCascade: this.isCascade });
35
+ }
32
36
  toString() {
33
37
  throw new MethodNotImplementedError_1.MethodNotImplementedError('not implemented');
34
38
  }
@@ -6,8 +6,24 @@ const basic_1 = require("../utils/basic");
6
6
  const forwardArgs = (_, { args = [] } = {}) => args;
7
7
  exports.forwardArgs = forwardArgs;
8
8
  class InjectionToken {
9
+ tags;
10
+ constructor(tags = []) {
11
+ this.tags = new Set(tags);
12
+ }
13
+ hasTag(tag) {
14
+ return this.tags.has(tag);
15
+ }
16
+ getTags() {
17
+ return [...this.tags];
18
+ }
9
19
  }
10
20
  exports.InjectionToken = InjectionToken;
11
21
  function isInjectionToken(target) {
12
- return basic_1.Is.object(target) && 'resolve' in target && 'args' in target && 'argsFn' in target && 'lazy' in target;
22
+ return (basic_1.Is.object(target) &&
23
+ 'resolve' in target &&
24
+ 'args' in target &&
25
+ 'argsFn' in target &&
26
+ 'lazy' in target &&
27
+ 'hasTag' in target &&
28
+ 'addTags' in target);
13
29
  }
@@ -6,8 +6,8 @@ class SingleAliasToken extends InjectionToken_1.InjectionToken {
6
6
  token;
7
7
  _getArgsFn;
8
8
  _isLazy;
9
- constructor(token, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false } = {}) {
10
- super();
9
+ constructor(token, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false, tags = [], } = {}) {
10
+ super(tags);
11
11
  this.token = token;
12
12
  this._getArgsFn = getArgsFn;
13
13
  this._isLazy = isLazy;
@@ -29,6 +29,7 @@ class SingleAliasToken extends InjectionToken_1.InjectionToken {
29
29
  return new SingleAliasToken(this.token, {
30
30
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
31
31
  isLazy: this._isLazy,
32
+ tags: this.getTags(),
32
33
  });
33
34
  }
34
35
  argsFn(fn) {
@@ -36,12 +37,21 @@ class SingleAliasToken extends InjectionToken_1.InjectionToken {
36
37
  return new SingleAliasToken(this.token, {
37
38
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
38
39
  isLazy: this._isLazy,
40
+ tags: this.getTags(),
39
41
  });
40
42
  }
41
43
  lazy() {
42
44
  return new SingleAliasToken(this.token, {
43
45
  getArgsFn: this._getArgsFn,
44
46
  isLazy: true,
47
+ tags: this.getTags(),
48
+ });
49
+ }
50
+ addTags(...tags) {
51
+ return new SingleAliasToken(this.token, {
52
+ getArgsFn: this._getArgsFn,
53
+ isLazy: this._isLazy,
54
+ tags: [...this.getTags(), ...tags],
45
55
  });
46
56
  }
47
57
  toString() {
@@ -6,8 +6,8 @@ class SingleToken extends InjectionToken_1.InjectionToken {
6
6
  token;
7
7
  _getArgsFn;
8
8
  _isLazy;
9
- constructor(token, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false } = {}) {
10
- super();
9
+ constructor(token, { getArgsFn = InjectionToken_1.forwardArgs, isLazy = false, tags = [], } = {}) {
10
+ super(tags);
11
11
  this.token = token;
12
12
  this._getArgsFn = getArgsFn;
13
13
  this._isLazy = isLazy;
@@ -29,6 +29,7 @@ class SingleToken extends InjectionToken_1.InjectionToken {
29
29
  return new SingleToken(this.token, {
30
30
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
31
31
  isLazy: this._isLazy,
32
+ tags: this.getTags(),
32
33
  });
33
34
  }
34
35
  argsFn(fn) {
@@ -36,12 +37,21 @@ class SingleToken extends InjectionToken_1.InjectionToken {
36
37
  return new SingleToken(this.token, {
37
38
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
38
39
  isLazy: this._isLazy,
40
+ tags: this.getTags(),
39
41
  });
40
42
  }
41
43
  lazy() {
42
44
  return new SingleToken(this.token, {
43
45
  getArgsFn: this._getArgsFn,
44
46
  isLazy: true,
47
+ tags: this.getTags(),
48
+ });
49
+ }
50
+ addTags(...tags) {
51
+ return new SingleToken(this.token, {
52
+ getArgsFn: this._getArgsFn,
53
+ isLazy: this._isLazy,
54
+ tags: [...this.getTags(), ...tags],
45
55
  });
46
56
  }
47
57
  toString() {
@@ -8,7 +8,7 @@ exports.Filter = {
8
8
  return (v) => !excludeSet.has(v);
9
9
  },
10
10
  };
11
- const findOrFail = (predicate) => (...args) => {
11
+ const findOrFail = (predicate) => (args = []) => {
12
12
  const index = args.findIndex((arg) => predicate(arg));
13
13
  if (index === -1) {
14
14
  throw new ArgumentNotFoundError_1.ArgumentNotFoundError();
@@ -29,7 +29,7 @@ export class Provider {
29
29
  if (!this.getKey) {
30
30
  return this.resolveDep(scope, options);
31
31
  }
32
- const key = toString(this.getKey(...(options.args ?? [])));
32
+ const key = toString(this.getKey(options.args ?? []));
33
33
  if (!this.cache.has(key)) {
34
34
  this.cache.set(key, this.resolveDep(scope, options));
35
35
  }
@@ -3,8 +3,8 @@ export class ClassToken extends InjectionToken {
3
3
  target;
4
4
  _getArgsFn;
5
5
  _isLazy;
6
- constructor(target, { getArgsFn = forwardArgs, isLazy = false } = {}) {
7
- super();
6
+ constructor(target, { getArgsFn = forwardArgs, isLazy = false, tags = [], } = {}) {
7
+ super(tags);
8
8
  this.target = target;
9
9
  this._getArgsFn = getArgsFn;
10
10
  this._isLazy = isLazy;
@@ -23,6 +23,7 @@ export class ClassToken extends InjectionToken {
23
23
  return new ClassToken(this.target, {
24
24
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
25
25
  isLazy: this._isLazy,
26
+ tags: this.getTags(),
26
27
  });
27
28
  }
28
29
  argsFn(fn) {
@@ -30,12 +31,21 @@ export class ClassToken extends InjectionToken {
30
31
  return new ClassToken(this.target, {
31
32
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
32
33
  isLazy: this._isLazy,
34
+ tags: this.getTags(),
33
35
  });
34
36
  }
35
37
  lazy() {
36
38
  return new ClassToken(this.target, {
37
39
  getArgsFn: this._getArgsFn,
38
40
  isLazy: true,
41
+ tags: this.getTags(),
42
+ });
43
+ }
44
+ addTags(...tags) {
45
+ return new ClassToken(this.target, {
46
+ getArgsFn: this._getArgsFn,
47
+ isLazy: this._isLazy,
48
+ tags: [...this.getTags(), ...tags],
39
49
  });
40
50
  }
41
51
  toString() {
@@ -2,8 +2,8 @@ import { InjectionToken } from './InjectionToken.js';
2
2
  import { MethodNotImplementedError } from '../errors/MethodNotImplementedError.js';
3
3
  export class ConstantToken extends InjectionToken {
4
4
  token;
5
- constructor(token) {
6
- super();
5
+ constructor(token, { tags = [] } = {}) {
6
+ super(tags);
7
7
  this.token = token;
8
8
  }
9
9
  resolve(s) {
@@ -18,6 +18,9 @@ export class ConstantToken extends InjectionToken {
18
18
  lazy() {
19
19
  throw new MethodNotImplementedError('not implemented');
20
20
  }
21
+ addTags(...tags) {
22
+ return new ConstantToken(this.token, { tags: [...this.getTags(), ...tags] });
23
+ }
21
24
  toString() {
22
25
  throw new MethodNotImplementedError('not implemented');
23
26
  }
@@ -4,8 +4,8 @@ export class FunctionToken extends InjectionToken {
4
4
  fn;
5
5
  _getArgsFn;
6
6
  _isLazy;
7
- constructor(fn, { getArgsFn = forwardArgs, isLazy = false } = {}) {
8
- super();
7
+ constructor(fn, { getArgsFn = forwardArgs, isLazy = false, tags = [], } = {}) {
8
+ super(tags);
9
9
  this.fn = fn;
10
10
  this._getArgsFn = getArgsFn;
11
11
  this._isLazy = isLazy;
@@ -21,6 +21,7 @@ export class FunctionToken extends InjectionToken {
21
21
  return new FunctionToken(this.fn, {
22
22
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
23
23
  isLazy: this._isLazy,
24
+ tags: this.getTags(),
24
25
  });
25
26
  }
26
27
  argsFn(fn) {
@@ -28,12 +29,21 @@ export class FunctionToken extends InjectionToken {
28
29
  return new FunctionToken(this.fn, {
29
30
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
30
31
  isLazy: this._isLazy,
32
+ tags: this.getTags(),
31
33
  });
32
34
  }
33
35
  lazy() {
34
36
  return new FunctionToken(this.fn, {
35
37
  getArgsFn: this._getArgsFn,
36
38
  isLazy: true,
39
+ tags: this.getTags(),
40
+ });
41
+ }
42
+ addTags(...tags) {
43
+ return new FunctionToken(this.fn, {
44
+ getArgsFn: this._getArgsFn,
45
+ isLazy: this._isLazy,
46
+ tags: [...this.getTags(), ...tags],
37
47
  });
38
48
  }
39
49
  toString() {
@@ -3,8 +3,8 @@ export class GroupAliasToken extends InjectionToken {
3
3
  token;
4
4
  _getArgsFn;
5
5
  _isLazy;
6
- constructor(token, { getArgsFn = forwardArgs, isLazy = false } = {}) {
7
- super();
6
+ constructor(token, { getArgsFn = forwardArgs, isLazy = false, tags = [], } = {}) {
7
+ super(tags);
8
8
  this.token = token;
9
9
  this._getArgsFn = getArgsFn;
10
10
  this._isLazy = isLazy;
@@ -26,6 +26,7 @@ export class GroupAliasToken extends InjectionToken {
26
26
  return new GroupAliasToken(this.token, {
27
27
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
28
28
  isLazy: this._isLazy,
29
+ tags: this.getTags(),
29
30
  });
30
31
  }
31
32
  argsFn(fn) {
@@ -33,12 +34,21 @@ export class GroupAliasToken extends InjectionToken {
33
34
  return new GroupAliasToken(this.token, {
34
35
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
35
36
  isLazy: this._isLazy,
37
+ tags: this.getTags(),
36
38
  });
37
39
  }
38
40
  lazy() {
39
41
  return new GroupAliasToken(this.token, {
40
42
  getArgsFn: this._getArgsFn,
41
43
  isLazy: true,
44
+ tags: this.getTags(),
45
+ });
46
+ }
47
+ addTags(...tags) {
48
+ return new GroupAliasToken(this.token, {
49
+ getArgsFn: this._getArgsFn,
50
+ isLazy: this._isLazy,
51
+ tags: [...this.getTags(), ...tags],
42
52
  });
43
53
  }
44
54
  toString() {
@@ -2,10 +2,11 @@ import { InjectionToken } from './InjectionToken.js';
2
2
  import { MethodNotImplementedError } from '../errors/MethodNotImplementedError.js';
3
3
  export class GroupInstanceToken extends InjectionToken {
4
4
  predicate;
5
- isCascade = true;
6
- constructor(predicate) {
7
- super();
5
+ isCascade;
6
+ constructor(predicate, { tags = [], isCascade = true } = {}) {
7
+ super(tags);
8
8
  this.predicate = predicate;
9
+ this.isCascade = isCascade;
9
10
  }
10
11
  select(fn) {
11
12
  return (s) => this.resolve(s).map(fn);
@@ -26,6 +27,9 @@ export class GroupInstanceToken extends InjectionToken {
26
27
  resolve(c) {
27
28
  return c.getInstances(this.isCascade).filter(this.predicate);
28
29
  }
30
+ addTags(...tags) {
31
+ return new GroupInstanceToken(this.predicate, { tags: [...this.getTags(), ...tags], isCascade: this.isCascade });
32
+ }
29
33
  toString() {
30
34
  throw new MethodNotImplementedError('not implemented');
31
35
  }
@@ -1,7 +1,23 @@
1
1
  import { Is } from '../utils/basic.js';
2
2
  export const forwardArgs = (_, { args = [] } = {}) => args;
3
3
  export class InjectionToken {
4
+ tags;
5
+ constructor(tags = []) {
6
+ this.tags = new Set(tags);
7
+ }
8
+ hasTag(tag) {
9
+ return this.tags.has(tag);
10
+ }
11
+ getTags() {
12
+ return [...this.tags];
13
+ }
4
14
  }
5
15
  export function isInjectionToken(target) {
6
- return Is.object(target) && 'resolve' in target && 'args' in target && 'argsFn' in target && 'lazy' in target;
16
+ return (Is.object(target) &&
17
+ 'resolve' in target &&
18
+ 'args' in target &&
19
+ 'argsFn' in target &&
20
+ 'lazy' in target &&
21
+ 'hasTag' in target &&
22
+ 'addTags' in target);
7
23
  }
@@ -3,8 +3,8 @@ export class SingleAliasToken extends InjectionToken {
3
3
  token;
4
4
  _getArgsFn;
5
5
  _isLazy;
6
- constructor(token, { getArgsFn = forwardArgs, isLazy = false } = {}) {
7
- super();
6
+ constructor(token, { getArgsFn = forwardArgs, isLazy = false, tags = [], } = {}) {
7
+ super(tags);
8
8
  this.token = token;
9
9
  this._getArgsFn = getArgsFn;
10
10
  this._isLazy = isLazy;
@@ -26,6 +26,7 @@ export class SingleAliasToken extends InjectionToken {
26
26
  return new SingleAliasToken(this.token, {
27
27
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
28
28
  isLazy: this._isLazy,
29
+ tags: this.getTags(),
29
30
  });
30
31
  }
31
32
  argsFn(fn) {
@@ -33,12 +34,21 @@ export class SingleAliasToken extends InjectionToken {
33
34
  return new SingleAliasToken(this.token, {
34
35
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
35
36
  isLazy: this._isLazy,
37
+ tags: this.getTags(),
36
38
  });
37
39
  }
38
40
  lazy() {
39
41
  return new SingleAliasToken(this.token, {
40
42
  getArgsFn: this._getArgsFn,
41
43
  isLazy: true,
44
+ tags: this.getTags(),
45
+ });
46
+ }
47
+ addTags(...tags) {
48
+ return new SingleAliasToken(this.token, {
49
+ getArgsFn: this._getArgsFn,
50
+ isLazy: this._isLazy,
51
+ tags: [...this.getTags(), ...tags],
42
52
  });
43
53
  }
44
54
  toString() {
@@ -3,8 +3,8 @@ export class SingleToken extends InjectionToken {
3
3
  token;
4
4
  _getArgsFn;
5
5
  _isLazy;
6
- constructor(token, { getArgsFn = forwardArgs, isLazy = false } = {}) {
7
- super();
6
+ constructor(token, { getArgsFn = forwardArgs, isLazy = false, tags = [], } = {}) {
7
+ super(tags);
8
8
  this.token = token;
9
9
  this._getArgsFn = getArgsFn;
10
10
  this._isLazy = isLazy;
@@ -26,6 +26,7 @@ export class SingleToken extends InjectionToken {
26
26
  return new SingleToken(this.token, {
27
27
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
28
28
  isLazy: this._isLazy,
29
+ tags: this.getTags(),
29
30
  });
30
31
  }
31
32
  argsFn(fn) {
@@ -33,12 +34,21 @@ export class SingleToken extends InjectionToken {
33
34
  return new SingleToken(this.token, {
34
35
  getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
35
36
  isLazy: this._isLazy,
37
+ tags: this.getTags(),
36
38
  });
37
39
  }
38
40
  lazy() {
39
41
  return new SingleToken(this.token, {
40
42
  getArgsFn: this._getArgsFn,
41
43
  isLazy: true,
44
+ tags: this.getTags(),
45
+ });
46
+ }
47
+ addTags(...tags) {
48
+ return new SingleToken(this.token, {
49
+ getArgsFn: this._getArgsFn,
50
+ isLazy: this._isLazy,
51
+ tags: [...this.getTags(), ...tags],
42
52
  });
43
53
  }
44
54
  toString() {
@@ -5,7 +5,7 @@ export const Filter = {
5
5
  return (v) => !excludeSet.has(v);
6
6
  },
7
7
  };
8
- export const findOrFail = (predicate) => (...args) => {
8
+ export const findOrFail = (predicate) => (args = []) => {
9
9
  const index = args.findIndex((arg) => predicate(arg));
10
10
  if (index === -1) {
11
11
  throw new ArgumentNotFoundError();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "70.0.0",
3
+ "version": "71.0.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",
@@ -13,7 +13,7 @@ export type ScopeAccessOptions = {
13
13
  };
14
14
  export type ScopeAccessRule = (options: ScopeAccessOptions, prev: boolean) => boolean;
15
15
  export type ArgsFn = (l: IContainer, options?: InjectOptions) => unknown[];
16
- export type GetCacheKey = (...args: unknown[]) => string | Serializable;
16
+ export type GetCacheKey = (args: unknown[]) => string | Serializable;
17
17
  export type DecorateFn<Instance = any> = (dep: Instance, scope: IContainer) => Instance;
18
18
  export type ProviderHook = (dependency: unknown, scope: IContainer) => void;
19
19
  export interface IProvider<T = any> {
@@ -6,14 +6,16 @@ export declare class ClassToken<T = any> extends InjectionToken<T> implements Se
6
6
  private readonly target;
7
7
  private readonly _getArgsFn;
8
8
  private readonly _isLazy;
9
- constructor(target: constructor<T>, { getArgsFn, isLazy }?: {
9
+ constructor(target: constructor<T>, { getArgsFn, isLazy, tags, }?: {
10
10
  getArgsFn?: ArgsFn;
11
11
  isLazy?: boolean;
12
+ tags?: string[];
12
13
  });
13
14
  select<R>(fn: (target: T) => R): (s: IContainer) => R;
14
15
  resolve(s: IContainer, { args, lazy }?: ProviderOptions): T;
15
16
  args(...newArgs: unknown[]): ClassToken<T>;
16
17
  argsFn(fn: (s: IContainer) => unknown[]): ClassToken<T>;
17
18
  lazy(): ClassToken<T>;
19
+ addTags(...tags: string[]): ClassToken<T>;
18
20
  toString(): string;
19
21
  }
@@ -3,10 +3,13 @@ import { InjectionToken } from './InjectionToken.js';
3
3
  import { Serializable } from '../utils/basic.js';
4
4
  export declare class ConstantToken<T = any> extends InjectionToken<T> implements Serializable {
5
5
  private readonly token;
6
- constructor(token: T);
6
+ constructor(token: T, { tags }?: {
7
+ tags?: string[];
8
+ });
7
9
  resolve(s: IContainer): T;
8
10
  args(...deps: unknown[]): InjectionToken<T>;
9
11
  argsFn(getArgsFn: (s: IContainer) => unknown[]): InjectionToken<T>;
10
12
  lazy(): InjectionToken<T>;
13
+ addTags(...tags: string[]): ConstantToken<T>;
11
14
  toString(): string;
12
15
  }
@@ -7,13 +7,15 @@ export declare class FunctionToken<T = any> extends InjectionToken<T> implements
7
7
  private readonly fn;
8
8
  private readonly _getArgsFn;
9
9
  private readonly _isLazy;
10
- constructor(fn: InjectFn<T>, { getArgsFn, isLazy }?: {
10
+ constructor(fn: InjectFn<T>, { getArgsFn, isLazy, tags, }?: {
11
11
  getArgsFn?: ArgsFn;
12
12
  isLazy?: boolean;
13
+ tags?: string[];
13
14
  });
14
15
  resolve(s: IContainer, { args, lazy }?: ProviderOptions): T;
15
16
  args(...newArgs: unknown[]): InjectionToken<T>;
16
17
  argsFn(fn: (s: IContainer) => unknown[]): InjectionToken<T>;
17
18
  lazy(): InjectionToken<T>;
19
+ addTags(...tags: string[]): FunctionToken<T>;
18
20
  toString(): string;
19
21
  }
@@ -8,9 +8,10 @@ export declare class GroupAliasToken<T = any> extends InjectionToken<T[]> implem
8
8
  readonly token: DependencyKey;
9
9
  private readonly _getArgsFn;
10
10
  private readonly _isLazy;
11
- constructor(token: DependencyKey, { getArgsFn, isLazy }?: {
11
+ constructor(token: DependencyKey, { getArgsFn, isLazy, tags, }?: {
12
12
  getArgsFn?: ArgsFn;
13
13
  isLazy?: boolean;
14
+ tags?: string[];
14
15
  });
15
16
  select<R>(fn: (target: T[]) => R[]): (s: IContainer) => R[];
16
17
  resolve(s: IContainer, { args, lazy }?: ProviderOptions): T[];
@@ -18,6 +19,7 @@ export declare class GroupAliasToken<T = any> extends InjectionToken<T[]> implem
18
19
  args(...newArgs: unknown[]): GroupAliasToken<T>;
19
20
  argsFn(fn: (s: IContainer) => unknown[]): GroupAliasToken<T>;
20
21
  lazy(): GroupAliasToken<T>;
22
+ addTags(...tags: string[]): GroupAliasToken<T>;
21
23
  toString(): string;
22
24
  }
23
25
  export declare const toGroupAlias: <T>(token: DependencyKey) => GroupAliasToken<T>;
@@ -5,12 +5,16 @@ export type InstancePredicate = (dep: unknown) => boolean;
5
5
  export declare class GroupInstanceToken extends InjectionToken<Instance[]> implements Serializable {
6
6
  private predicate;
7
7
  private isCascade;
8
- constructor(predicate: InstancePredicate);
8
+ constructor(predicate: InstancePredicate, { tags, isCascade }?: {
9
+ tags?: string[];
10
+ isCascade?: boolean;
11
+ });
9
12
  select<R>(fn: (target: Instance) => R): (s: IContainer) => R[];
10
13
  args(...deps: unknown[]): this;
11
14
  argsFn(getArgsFn: (s: IContainer) => unknown[]): InjectionToken<Instance[]>;
12
15
  lazy(): InjectionToken<Instance[]>;
13
16
  cascade(isTrue: boolean): this;
14
17
  resolve(c: IContainer): Instance[];
18
+ addTags(...tags: string[]): GroupInstanceToken;
15
19
  toString(): string;
16
20
  }
@@ -1,10 +1,15 @@
1
- import { type IContainer } from '../container/IContainer.js';
1
+ import { type IContainer, type Tag } from '../container/IContainer.js';
2
2
  import { ArgsFn, ProviderOptions } from '../provider/IProvider.js';
3
3
  export declare const forwardArgs: ArgsFn;
4
4
  export declare abstract class InjectionToken<T = any> {
5
+ private readonly tags;
6
+ protected constructor(tags?: Tag[]);
5
7
  abstract resolve(s: IContainer, options?: ProviderOptions): T;
6
8
  abstract args(...deps: unknown[]): InjectionToken<T>;
7
9
  abstract argsFn(getArgsFn: (s: IContainer) => unknown[]): InjectionToken<T>;
8
10
  abstract lazy(): InjectionToken<T>;
11
+ abstract addTags(...tags: Tag[]): InjectionToken<T>;
12
+ hasTag(tag: Tag): boolean;
13
+ protected getTags(): Tag[];
9
14
  }
10
- export declare function isInjectionToken(target: unknown): target is InjectionToken;
15
+ export declare function isInjectionToken<T = any>(target: unknown): target is InjectionToken<T>;
@@ -8,9 +8,10 @@ export declare class SingleAliasToken<T = any> extends InjectionToken<T> impleme
8
8
  readonly token: DependencyKey;
9
9
  private readonly _getArgsFn;
10
10
  private readonly _isLazy;
11
- constructor(token: DependencyKey, { getArgsFn, isLazy }?: {
11
+ constructor(token: DependencyKey, { getArgsFn, isLazy, tags, }?: {
12
12
  getArgsFn?: ArgsFn;
13
13
  isLazy?: boolean;
14
+ tags?: string[];
14
15
  });
15
16
  select<R>(fn: (target: T) => R): (s: IContainer) => R;
16
17
  resolve(s: IContainer, { args, lazy }?: ResolveOneOptions): T;
@@ -18,6 +19,7 @@ export declare class SingleAliasToken<T = any> extends InjectionToken<T> impleme
18
19
  args(...newArgs: unknown[]): SingleAliasToken<T>;
19
20
  argsFn(fn: (s: IContainer) => unknown[]): SingleAliasToken<T>;
20
21
  lazy(): SingleAliasToken<T>;
22
+ addTags(...tags: string[]): SingleAliasToken<T>;
21
23
  toString(): string;
22
24
  }
23
25
  export declare const toSingleAlias: <T>(token: DependencyKey) => SingleAliasToken<T>;
@@ -7,9 +7,10 @@ export declare class SingleToken<T = any> extends InjectionToken<T> implements S
7
7
  token: DependencyKey;
8
8
  private readonly _getArgsFn;
9
9
  private readonly _isLazy;
10
- constructor(token: DependencyKey, { getArgsFn, isLazy }?: {
10
+ constructor(token: DependencyKey, { getArgsFn, isLazy, tags, }?: {
11
11
  getArgsFn?: ArgsFn;
12
12
  isLazy?: boolean;
13
+ tags?: string[];
13
14
  });
14
15
  select<R>(fn: (target: T) => R): (s: IContainer) => R;
15
16
  resolve(s: IContainer, { args, lazy }?: ProviderOptions): T;
@@ -17,5 +18,6 @@ export declare class SingleToken<T = any> extends InjectionToken<T> implements S
17
18
  args(...newArgs: unknown[]): SingleToken<T>;
18
19
  argsFn(fn: (s: IContainer) => unknown[]): SingleToken<T>;
19
20
  lazy(): SingleToken<T>;
21
+ addTags(...tags: string[]): SingleToken<T>;
20
22
  toString(): string;
21
23
  }
@@ -5,5 +5,5 @@ import { type constructor } from '../utils/basic.js';
5
5
  import { type MapFn } from '../utils/fp.js';
6
6
  export type Injectable<T = any> = InjectFn<T> | InjectionToken<T> | DependencyKey | constructor<T>;
7
7
  export declare const toToken: <T = any>(token: Injectable<T>) => InjectionToken<T>;
8
- export declare const argToToken: (v: unknown) => InjectionToken<unknown>;
8
+ export declare const argToToken: <T = unknown>(v: T) => InjectionToken<T>;
9
9
  export declare const toMappedToken: <T = any, R = T>(token: Injectable<T>, mappers: MapFn<any, any>[]) => InjectionToken<R>;
@@ -2,4 +2,4 @@ export type Predicate<T> = (value: T) => boolean;
2
2
  export declare const Filter: {
3
3
  exclude: <T>(arr: Set<T> | T[]) => (v: T) => boolean;
4
4
  };
5
- export declare const findOrFail: <T>(predicate: Predicate<T>) => (...args: unknown[]) => T;
5
+ export declare const findOrFail: <T>(predicate: Predicate<T>) => (args?: unknown[]) => T;