ts-ioc-container 55.1.0 → 55.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
@@ -2462,7 +2462,16 @@ Sometimes you need to invoke methods after construct or dispose of class. This i
2462
2462
 
2463
2463
  ```typescript
2464
2464
  import 'reflect-metadata';
2465
- import { AddOnConstructHookModule, Container, type HookFn, inject, onConstruct, Registration as R } from 'ts-ioc-container';
2465
+ import {
2466
+ AddOnConstructHookModule,
2467
+ Container,
2468
+ type ExecutionContext,
2469
+ type HookFn,
2470
+ type IContainer,
2471
+ inject,
2472
+ onConstruct,
2473
+ Registration as R,
2474
+ } from 'ts-ioc-container';
2466
2475
 
2467
2476
  const execute: HookFn = (ctx) => {
2468
2477
  ctx.invokeMethod({ args: ctx.resolveArgs() });
@@ -2490,6 +2499,64 @@ describe('onConstruct', function () {
2490
2499
  expect(db.isConnected).toBe(true);
2491
2500
  expect(db.connectionString).toBe('postgres://localhost:5432');
2492
2501
  });
2502
+
2503
+ it('should forward hook exceptions to the onException handler with the execution context', function () {
2504
+ const failure = new Error('boom');
2505
+
2506
+ class BrokenService {
2507
+ @onConstruct(() => {
2508
+ throw failure;
2509
+ })
2510
+ init() {}
2511
+ }
2512
+
2513
+ let captured: { ex: unknown; context: ExecutionContext } | undefined;
2514
+ const container = new Container().useModule(
2515
+ new AddOnConstructHookModule((ex, context) => {
2516
+ captured = { ex, context };
2517
+ }),
2518
+ );
2519
+
2520
+ expect(() => container.resolve(BrokenService)).not.toThrow();
2521
+ expect(captured?.ex).toBe(failure);
2522
+ expect(captured?.context.scope).toBe(container);
2523
+ });
2524
+
2525
+ it('should rethrow hook exceptions when no onException handler is provided', function () {
2526
+ const failure = new Error('boom');
2527
+
2528
+ class BrokenService {
2529
+ @onConstruct(() => {
2530
+ throw failure;
2531
+ })
2532
+ init() {}
2533
+ }
2534
+
2535
+ const container = new Container().useModule(new AddOnConstructHookModule());
2536
+
2537
+ expect(() => container.resolve(BrokenService)).toThrow(failure);
2538
+ });
2539
+
2540
+ it('should expose the resolving scope through the execution context', function () {
2541
+ class BrokenService {
2542
+ @onConstruct(() => {
2543
+ throw new Error('boom');
2544
+ })
2545
+ init() {}
2546
+ }
2547
+
2548
+ let scope: IContainer | undefined;
2549
+ const container = new Container().useModule(
2550
+ new AddOnConstructHookModule((_ex, context) => {
2551
+ scope = context.scope;
2552
+ }),
2553
+ );
2554
+ const child = container.createScope();
2555
+
2556
+ child.resolve(BrokenService);
2557
+
2558
+ expect(scope).toBe(child);
2559
+ });
2493
2560
  });
2494
2561
 
2495
2562
  ```
@@ -2601,6 +2668,26 @@ describe('inject property', () => {
2601
2668
  expect(viewModel.greetingService).toBe('Hello');
2602
2669
  expect(viewModel.display()).toBe('Hello User');
2603
2670
  });
2671
+
2672
+ it('should read the applied instance property via getProperty', () => {
2673
+ const onInitHookRunner = new HooksRunner('onInit');
2674
+
2675
+ let injectedValue: unknown;
2676
+
2677
+ class UserViewModel {
2678
+ @hook('onInit', injectProp('GreetingService'), (context) => {
2679
+ injectedValue = context.getProperty();
2680
+ })
2681
+ greetingService!: string;
2682
+ }
2683
+
2684
+ const container = new Container().addRegistration(Registration.fromValue('Hello').bindToKey('GreetingService'));
2685
+
2686
+ const viewModel = container.resolve(UserViewModel);
2687
+ onInitHookRunner.execute(viewModel, { scope: container });
2688
+
2689
+ expect(injectedValue).toBe('Hello');
2690
+ });
2604
2691
  });
2605
2692
 
2606
2693
  ```
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -26,6 +26,10 @@ class HookContext {
26
26
  // @ts-ignore
27
27
  this.instance[this.methodName] = fn.resolve(this.scope);
28
28
  }
29
+ getProperty() {
30
+ // @ts-ignore
31
+ return this.instance[this.methodName];
32
+ }
29
33
  setInitialArgs(...args) {
30
34
  this.initialArgs = args;
31
35
  return this;
@@ -7,9 +7,21 @@ exports.onConstructHooksRunner = new HooksRunner_1.HooksRunner('onConstruct');
7
7
  const onConstruct = (...fns) => (0, hook_1.hook)('onConstruct', ...fns);
8
8
  exports.onConstruct = onConstruct;
9
9
  class AddOnConstructHookModule {
10
+ onException;
11
+ constructor(onException) {
12
+ this.onException = onException;
13
+ }
10
14
  applyTo(container) {
11
15
  container.addOnConstructHook((instance, scope) => {
12
- exports.onConstructHooksRunner.execute(instance, { scope });
16
+ try {
17
+ exports.onConstructHooksRunner.execute(instance, { scope });
18
+ }
19
+ catch (ex) {
20
+ if (!this.onException) {
21
+ throw ex;
22
+ }
23
+ this.onException(ex, { scope });
24
+ }
13
25
  });
14
26
  }
15
27
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -23,6 +23,10 @@ export class HookContext {
23
23
  // @ts-ignore
24
24
  this.instance[this.methodName] = fn.resolve(this.scope);
25
25
  }
26
+ getProperty() {
27
+ // @ts-ignore
28
+ return this.instance[this.methodName];
29
+ }
26
30
  setInitialArgs(...args) {
27
31
  this.initialArgs = args;
28
32
  return this;
@@ -3,9 +3,21 @@ import { HooksRunner } from './HooksRunner';
3
3
  export const onConstructHooksRunner = new HooksRunner('onConstruct');
4
4
  export const onConstruct = (...fns) => hook('onConstruct', ...fns);
5
5
  export class AddOnConstructHookModule {
6
+ onException;
7
+ constructor(onException) {
8
+ this.onException = onException;
9
+ }
6
10
  applyTo(container) {
7
11
  container.addOnConstructHook((instance, scope) => {
8
- onConstructHooksRunner.execute(instance, { scope });
12
+ try {
13
+ onConstructHooksRunner.execute(instance, { scope });
14
+ }
15
+ catch (ex) {
16
+ if (!this.onException) {
17
+ throw ex;
18
+ }
19
+ this.onException(ex, { scope });
20
+ }
9
21
  });
10
22
  }
11
23
  }
package/esm/index.js CHANGED
@@ -24,7 +24,7 @@ export { CannonSingletonApplyTwiceError } from './errors/CannonSingletonApplyTwi
24
24
  export { getHooks, hook, hasHooks } from './hooks/hook';
25
25
  export { HookContext, createHookContextFactory, createHookContext } from './hooks/HookContext';
26
26
  export { injectProp } from './hooks/injectProp';
27
- export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule } from './hooks/onConstruct';
27
+ export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule, } from './hooks/onConstruct';
28
28
  export { onDisposeHooksRunner, onDispose, AddOnDisposeHookModule } from './hooks/onDispose';
29
29
  export { HooksRunner } from './hooks/HooksRunner';
30
30
  // Tokens
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "55.1.0",
3
+ "version": "55.3.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
  "workspaces": [
6
6
  "docs"
@@ -0,0 +1,4 @@
1
+ import type { IContainer } from './container/IContainer';
2
+ export interface ExecutionContext {
3
+ scope: IContainer;
4
+ }
@@ -9,6 +9,7 @@ export interface IHookContext {
9
9
  args?: unknown[];
10
10
  }): unknown;
11
11
  setProperty(fn: InjectionToken): void;
12
+ getProperty(): unknown;
12
13
  setInitialArgs(...args: unknown[]): this;
13
14
  }
14
15
  export declare class HookContext implements IHookContext {
@@ -22,6 +23,7 @@ export declare class HookContext implements IHookContext {
22
23
  args?: unknown[];
23
24
  }): unknown;
24
25
  setProperty(fn: InjectionToken): void;
26
+ getProperty(): unknown;
25
27
  setInitialArgs(...args: unknown[]): this;
26
28
  }
27
29
  export type CreateHookContext = (Target: object, scope: IContainer, methodName?: string) => IHookContext;
@@ -2,9 +2,13 @@ import { HookClass, HookFn } from './hook';
2
2
  import type { IContainer, IContainerModule } from '../container/IContainer';
3
3
  import { constructor, Instance } from '../utils/basic';
4
4
  import { HooksRunner } from './HooksRunner';
5
+ import type { ExecutionContext } from '../ExecutionContext';
5
6
  export declare const onConstructHooksRunner: HooksRunner;
6
7
  export declare const onConstruct: (...fns: (HookFn | constructor<HookClass>)[]) => (target: object, propertyKey: string | symbol) => void;
7
8
  export type OnConstructHook = (instance: Instance, scope: IContainer) => void;
9
+ export type OnExceptionHandler = (ex: unknown, context: ExecutionContext) => void;
8
10
  export declare class AddOnConstructHookModule implements IContainerModule {
11
+ private readonly onException?;
12
+ constructor(onException?: OnExceptionHandler | undefined);
9
13
  applyTo(container: IContainer): void;
10
14
  }
@@ -20,7 +20,7 @@ export { CannonSingletonApplyTwiceError } from './errors/CannonSingletonApplyTwi
20
20
  export { getHooks, hook, hasHooks, type HookFn, type HookClass, type InjectFn, type HooksOfClass } from './hooks/hook';
21
21
  export { HookContext, createHookContextFactory, createHookContext, type IHookContext } from './hooks/HookContext';
22
22
  export { injectProp } from './hooks/injectProp';
23
- export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule } from './hooks/onConstruct';
23
+ export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule, type OnExceptionHandler, } from './hooks/onConstruct';
24
24
  export { onDisposeHooksRunner, onDispose, AddOnDisposeHookModule } from './hooks/onDispose';
25
25
  export { HooksRunner, type HooksRunnerContext, type MapHookContext } from './hooks/HooksRunner';
26
26
  export { InjectionToken } from './token/InjectionToken';
@@ -39,6 +39,7 @@ export { throttle } from './utils/throttle';
39
39
  export { debounce } from './utils/debounce';
40
40
  export { shallowCache } from './utils/shallowCache';
41
41
  export { once } from './utils/once';
42
+ export { type ExecutionContext } from './ExecutionContext';
42
43
  export { select } from './select';
43
44
  export { pipe, type MapFn } from './utils/fp';
44
45
  export { type constructor, type Instance, Is, resolveConstructor } from './utils/basic';