ts-ioc-container 29.1.2 → 29.3.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
@@ -137,11 +137,11 @@ import {
137
137
  Container,
138
138
  DependencyNotFoundError,
139
139
  key,
140
- scope,
141
140
  provider,
142
141
  MetadataInjector,
143
142
  Registration as R,
144
143
  by,
144
+ scope,
145
145
  } from 'ts-ioc-container';
146
146
 
147
147
  @key('ILogger')
@@ -538,7 +538,7 @@ Sometimes you need to resolve provider only from scope which matches to certain
538
538
 
539
539
  ```typescript
540
540
  import 'reflect-metadata';
541
- import { singleton, Container, key, scope, provider, MetadataInjector, Registration as R } from 'ts-ioc-container';
541
+ import { singleton, Container, key, provider, MetadataInjector, Registration as R, scope } from 'ts-ioc-container';
542
542
 
543
543
  @key('ILogger')
544
544
  @provider(singleton(), scope((s) => s.hasTag('root'))) // the same as .pipe(singleton(), scope((s) => s.hasTag('root')))
@@ -611,7 +611,7 @@ import 'reflect-metadata';
611
611
  import { alias, by, Container, inject, provider, MetadataInjector, Registration as R } from 'ts-ioc-container';
612
612
 
613
613
  describe('alias', () => {
614
- const IMiddlewareKey = Symbol('IMiddleware');
614
+ const IMiddlewareKey = 'IMiddleware';
615
615
  const middleware = provider(alias(IMiddlewareKey));
616
616
 
617
617
  interface IMiddleware {
@@ -640,7 +640,7 @@ describe('alias', () => {
640
640
  it('should resolve by some alias', () => {
641
641
  class App implements IApplication {
642
642
  private appliedMiddleware: Set<string> = new Set();
643
- constructor(@inject(by.alias.some(IMiddlewareKey)) public middleware: IMiddleware[]) {}
643
+ constructor(@inject(by.provider((d) => d.hasAlias(IMiddlewareKey))) public middleware: IMiddleware[]) {}
644
644
 
645
645
  markMiddlewareAsApplied(name: string): void {
646
646
  this.appliedMiddleware.add(name);
package/cjm/by.js CHANGED
@@ -4,18 +4,7 @@ exports.by = exports.all = void 0;
4
4
  const all = () => true;
5
5
  exports.all = all;
6
6
  exports.by = {
7
- alias: {
8
- /**
9
- * Get all instances that have at least one of the given aliases
10
- * @param aliases
11
- */
12
- some: (...aliases) => (c, ...args) => c.getTokensByProvider((p) => aliases.some((alias) => p.hasAlias(alias))).map((t) => c.resolve(t, ...args)),
13
- /**
14
- * Get all instances that have all of the given aliases
15
- * @param aliases
16
- */
17
- all: (...aliases) => (c, ...args) => c.getTokensByProvider((p) => aliases.every((alias) => p.hasAlias(alias))).map((t) => c.resolve(t, ...args)),
18
- },
7
+ provider: (predicate) => (c, ...args) => c.getTokensByProvider(predicate).map((t) => c.resolve(t, ...args)),
19
8
  /**
20
9
  * Get all instances that match the given keys
21
10
  * @param keys
@@ -13,7 +13,7 @@ class Container {
13
13
  this.scopes = new Set();
14
14
  this.instances = new Set();
15
15
  this.parent = (_a = options.parent) !== null && _a !== void 0 ? _a : new EmptyContainer_1.EmptyContainer();
16
- this.tags = (_b = options.tags) !== null && _b !== void 0 ? _b : [];
16
+ this.tags = new Set((_b = options.tags) !== null && _b !== void 0 ? _b : []);
17
17
  }
18
18
  register(key, provider) {
19
19
  this.validateContainer();
@@ -65,7 +65,7 @@ class Container {
65
65
  return instances;
66
66
  }
67
67
  hasTag(tag) {
68
- return this.tags.includes(tag);
68
+ return this.tags.has(tag);
69
69
  }
70
70
  use(...modules) {
71
71
  for (const module of modules) {
package/esm/by.js CHANGED
@@ -1,17 +1,6 @@
1
1
  export const all = () => true;
2
2
  export const by = {
3
- alias: {
4
- /**
5
- * Get all instances that have at least one of the given aliases
6
- * @param aliases
7
- */
8
- some: (...aliases) => (c, ...args) => c.getTokensByProvider((p) => aliases.some((alias) => p.hasAlias(alias))).map((t) => c.resolve(t, ...args)),
9
- /**
10
- * Get all instances that have all of the given aliases
11
- * @param aliases
12
- */
13
- all: (...aliases) => (c, ...args) => c.getTokensByProvider((p) => aliases.every((alias) => p.hasAlias(alias))).map((t) => c.resolve(t, ...args)),
14
- },
3
+ provider: (predicate) => (c, ...args) => c.getTokensByProvider(predicate).map((t) => c.resolve(t, ...args)),
15
4
  /**
16
5
  * Get all instances that match the given keys
17
6
  * @param keys
@@ -10,7 +10,7 @@ export class Container {
10
10
  this.scopes = new Set();
11
11
  this.instances = new Set();
12
12
  this.parent = (_a = options.parent) !== null && _a !== void 0 ? _a : new EmptyContainer();
13
- this.tags = (_b = options.tags) !== null && _b !== void 0 ? _b : [];
13
+ this.tags = new Set((_b = options.tags) !== null && _b !== void 0 ? _b : []);
14
14
  }
15
15
  register(key, provider) {
16
16
  this.validateContainer();
@@ -62,7 +62,7 @@ export class Container {
62
62
  return instances;
63
63
  }
64
64
  hasTag(tag) {
65
- return this.tags.includes(tag);
65
+ return this.tags.has(tag);
66
66
  }
67
67
  use(...modules) {
68
68
  for (const module of modules) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "29.1.2",
3
+ "version": "29.3.0",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -60,5 +60,5 @@
60
60
  "ts-node": "^10.9.1",
61
61
  "typescript": "5.4.3"
62
62
  },
63
- "gitHead": "e94b7c0288a941df9679dbee14c9aac5891b34df"
63
+ "gitHead": "0533f30b75ac9adb189e7e67ce181aac87ea95cb"
64
64
  }
package/typings/by.d.ts CHANGED
@@ -1,19 +1,9 @@
1
- import { DependencyKey, IContainer, InjectionToken } from './container/IContainer';
1
+ import { IContainer, InjectionToken } from './container/IContainer';
2
+ import { Aliased } from './provider/IProvider';
2
3
  export type InstancePredicate = (dep: unknown) => boolean;
3
4
  export declare const all: InstancePredicate;
4
5
  export declare const by: {
5
- alias: {
6
- /**
7
- * Get all instances that have at least one of the given aliases
8
- * @param aliases
9
- */
10
- some: (...aliases: DependencyKey[]) => (c: IContainer, ...args: unknown[]) => unknown[];
11
- /**
12
- * Get all instances that have all of the given aliases
13
- * @param aliases
14
- */
15
- all: (...aliases: DependencyKey[]) => (c: IContainer, ...args: unknown[]) => unknown[];
16
- };
6
+ provider: (predicate: (provider: Aliased) => boolean) => (c: IContainer, ...args: unknown[]) => unknown[];
17
7
  /**
18
8
  * Get all instances that match the given keys
19
9
  * @param keys
@@ -1,10 +1,10 @@
1
1
  import { DependencyKey, IContainer, IContainerModule, InjectionToken, Tag } from './IContainer';
2
2
  import { IInjector } from '../injector/IInjector';
3
- import { IProvider } from '../provider/IProvider';
3
+ import { IProvider, ProviderPredicate } from '../provider/IProvider';
4
4
  export declare class Container implements IContainer {
5
5
  private readonly injector;
6
6
  readonly providers: Map<DependencyKey, IProvider<unknown>>;
7
- private readonly tags;
7
+ private tags;
8
8
  private isDisposed;
9
9
  private parent;
10
10
  private scopes;
@@ -15,7 +15,7 @@ export declare class Container implements IContainer {
15
15
  });
16
16
  register(key: DependencyKey, provider: IProvider): this;
17
17
  resolve<T>(token: InjectionToken<T>, ...args: unknown[]): T;
18
- getTokensByProvider(predicate: (provider: IProvider) => boolean): DependencyKey[];
18
+ getTokensByProvider(predicate: ProviderPredicate): DependencyKey[];
19
19
  createScope(...tags: Tag[]): Container;
20
20
  dispose(): void;
21
21
  getInstances(): unknown[];
@@ -1,4 +1,4 @@
1
- import { IProvider } from '../provider/IProvider';
1
+ import { IProvider, ProviderPredicate } from '../provider/IProvider';
2
2
  import { constructor } from '../utils';
3
3
  export type Tag = string;
4
4
  export type DependencyKey = string | symbol;
@@ -11,7 +11,10 @@ export interface Resolvable {
11
11
  export interface IContainerModule {
12
12
  applyTo(container: IContainer): void;
13
13
  }
14
- export interface IContainer extends Resolvable {
14
+ export interface Tagged {
15
+ hasTag(tag: Tag): boolean;
16
+ }
17
+ export interface IContainer extends Resolvable, Tagged {
15
18
  createScope(...tags: Tag[]): IContainer;
16
19
  register(key: DependencyKey, value: IProvider): this;
17
20
  removeScope(child: IContainer): void;
@@ -19,7 +22,6 @@ export interface IContainer extends Resolvable {
19
22
  dispose(): void;
20
23
  use(...modules: IContainerModule[]): this;
21
24
  getAllProviders(): Map<DependencyKey, IProvider>;
22
- getTokensByProvider(predicate: (provider: IProvider) => boolean): DependencyKey[];
25
+ getTokensByProvider(predicate: ProviderPredicate): DependencyKey[];
23
26
  hasDependency(key: DependencyKey): boolean;
24
- hasTag(tag: Tag): boolean;
25
27
  }
@@ -1,12 +1,16 @@
1
- import { DependencyKey, IContainer, Resolvable } from '../container/IContainer';
1
+ import { Resolvable, Tagged } from '../container/IContainer';
2
2
  import { MapFn } from '../utils';
3
3
  export type ResolveDependency<T = unknown> = (container: Resolvable, ...args: unknown[]) => T;
4
- export interface IProvider<T = unknown> {
4
+ export type Alias = string;
5
+ export interface Aliased {
6
+ hasAlias(alias: Alias): boolean;
7
+ }
8
+ export interface IProvider<T = unknown> extends Aliased {
5
9
  clone(): IProvider<T>;
6
10
  resolve(container: Resolvable, ...args: unknown[]): T;
7
- isValid(container: IContainer): boolean;
11
+ isValid(container: Tagged): boolean;
8
12
  pipe(...mappers: MapFn<IProvider<T>>[]): IProvider<T>;
9
- hasAlias(alias: DependencyKey): boolean;
10
- addAliases(...aliases: DependencyKey[]): this;
13
+ addAliases(...aliases: Alias[]): this;
11
14
  }
12
- export declare function alias<T = unknown>(...tokens: DependencyKey[]): MapFn<IProvider<T>>;
15
+ export type ProviderPredicate = (provider: IProvider) => boolean;
16
+ export declare function alias<T = unknown>(...tokens: Alias[]): MapFn<IProvider<T>>;
@@ -1,5 +1,5 @@
1
- import { IProvider, ResolveDependency } from './IProvider';
2
- import { DependencyKey, Resolvable } from '../container/IContainer';
1
+ import { Alias, IProvider, ResolveDependency } from './IProvider';
2
+ import { Resolvable } from '../container/IContainer';
3
3
  import { constructor, MapFn } from '../utils';
4
4
  export declare const provider: (...mappers: MapFn<IProvider>[]) => ClassDecorator;
5
5
  export declare class Provider<T> implements IProvider<T> {
@@ -9,8 +9,8 @@ export declare class Provider<T> implements IProvider<T> {
9
9
  private aliases;
10
10
  constructor(resolveDependency: ResolveDependency<T>);
11
11
  pipe(...mappers: MapFn<IProvider<T>>[]): IProvider<T>;
12
- addAliases(...aliases: DependencyKey[]): this;
13
- hasAlias(alias: DependencyKey): boolean;
12
+ addAliases(...aliases: Alias[]): this;
13
+ hasAlias(alias: Alias): boolean;
14
14
  clone(): Provider<T>;
15
15
  resolve(container: Resolvable, ...args: unknown[]): T;
16
16
  isValid(): boolean;
@@ -1,13 +1,13 @@
1
- import { DependencyKey, IContainer, Resolvable } from '../container/IContainer';
2
- import { IProvider } from './IProvider';
1
+ import { Resolvable, Tagged } from '../container/IContainer';
2
+ import { Alias, IProvider } from './IProvider';
3
3
  import { MapFn } from '../utils';
4
4
  export declare abstract class ProviderDecorator<T> implements IProvider<T> {
5
5
  private decorated;
6
6
  protected constructor(decorated: IProvider<T>);
7
- addAliases(...aliases: DependencyKey[]): this;
8
- hasAlias(alias: DependencyKey): boolean;
7
+ addAliases(...aliases: Alias[]): this;
8
+ hasAlias(alias: Alias): boolean;
9
9
  abstract clone(): IProvider<T>;
10
- isValid(container: IContainer): boolean;
10
+ isValid(container: Tagged): boolean;
11
11
  resolve(container: Resolvable, ...args: any[]): T;
12
12
  pipe(...mappers: MapFn<IProvider<T>>[]): IProvider<T>;
13
13
  }
@@ -1,9 +1,9 @@
1
1
  import { IProvider } from './IProvider';
2
2
  import { ProviderDecorator } from './ProviderDecorator';
3
- import { IContainer } from '../container/IContainer';
3
+ import { IContainer, Tagged } from '../container/IContainer';
4
4
  import { MapFn } from '../utils';
5
5
  type ContainerPredicate = (c: IContainer) => boolean;
6
- export declare function scope<T = unknown>(predicate: ContainerPredicate): MapFn<IProvider<T>>;
6
+ export declare function scope<T = unknown>(predicate: (c: Tagged) => boolean): MapFn<IProvider<T>>;
7
7
  export declare class ScopeProvider<T> extends ProviderDecorator<T> {
8
8
  private provider;
9
9
  private predicate;