sandly 0.4.0 → 0.5.1
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 +188 -43
- package/dist/index.d.ts +161 -137
- package/dist/index.js +131 -169
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -407,21 +407,21 @@ type Factory<T, TReg extends AnyTag> = (ctx: ResolutionContext<TReg>) => Promise
|
|
|
407
407
|
*
|
|
408
408
|
* @example Synchronous finalizer
|
|
409
409
|
* ```typescript
|
|
410
|
-
* const
|
|
410
|
+
* const cleanup: Finalizer<FileHandle> = (fileHandle) => {
|
|
411
411
|
* fileHandle.close();
|
|
412
412
|
* };
|
|
413
413
|
* ```
|
|
414
414
|
*
|
|
415
415
|
* @example Asynchronous finalizer
|
|
416
416
|
* ```typescript
|
|
417
|
-
* const
|
|
417
|
+
* const cleanup: Finalizer<DatabaseConnection> = async (connection) => {
|
|
418
418
|
* await connection.disconnect();
|
|
419
419
|
* };
|
|
420
420
|
* ```
|
|
421
421
|
*
|
|
422
422
|
* @example Resilient finalizer
|
|
423
423
|
* ```typescript
|
|
424
|
-
* const
|
|
424
|
+
* const cleanup: Finalizer<HttpServer> = async (server) => {
|
|
425
425
|
* try {
|
|
426
426
|
* await server.close();
|
|
427
427
|
* } catch (error) {
|
|
@@ -437,38 +437,97 @@ type Finalizer<T> = (instance: T) => PromiseOrValue<void>;
|
|
|
437
437
|
/**
|
|
438
438
|
* Type representing a complete dependency lifecycle with both factory and finalizer.
|
|
439
439
|
*
|
|
440
|
-
* This
|
|
440
|
+
* This interface is used when registering dependencies that need cleanup. Instead of
|
|
441
441
|
* passing separate factory and finalizer parameters, you can pass an object
|
|
442
442
|
* containing both.
|
|
443
443
|
*
|
|
444
|
-
*
|
|
444
|
+
* Since this is an interface, you can also implement it as a class for better
|
|
445
|
+
* organization and reuse. This is particularly useful when you have complex
|
|
446
|
+
* lifecycle logic or want to share lifecycle definitions across multiple services.
|
|
447
|
+
*
|
|
448
|
+
* @template T - The instance type
|
|
445
449
|
* @template TReg - Union type of all dependencies available in the container
|
|
446
450
|
*
|
|
447
|
-
* @example Using DependencyLifecycle
|
|
451
|
+
* @example Using DependencyLifecycle as an object
|
|
448
452
|
* ```typescript
|
|
453
|
+
* import { Container, Tag } from 'sandly';
|
|
454
|
+
*
|
|
449
455
|
* class DatabaseConnection extends Tag.Service('DatabaseConnection') {
|
|
450
456
|
* async connect() { return; }
|
|
451
457
|
* async disconnect() { return; }
|
|
452
458
|
* }
|
|
453
459
|
*
|
|
454
|
-
* const lifecycle: DependencyLifecycle<
|
|
455
|
-
*
|
|
460
|
+
* const lifecycle: DependencyLifecycle<DatabaseConnection, never> = {
|
|
461
|
+
* create: async () => {
|
|
456
462
|
* const conn = new DatabaseConnection();
|
|
457
463
|
* await conn.connect();
|
|
458
464
|
* return conn;
|
|
459
465
|
* },
|
|
460
|
-
*
|
|
466
|
+
* cleanup: async (conn) => {
|
|
461
467
|
* await conn.disconnect();
|
|
462
468
|
* }
|
|
463
469
|
* };
|
|
464
470
|
*
|
|
465
471
|
* Container.empty().register(DatabaseConnection, lifecycle);
|
|
466
472
|
* ```
|
|
473
|
+
*
|
|
474
|
+
* @example Implementing DependencyLifecycle as a class with dependencies
|
|
475
|
+
* ```typescript
|
|
476
|
+
* import { Container, Tag, type ResolutionContext } from 'sandly';
|
|
477
|
+
*
|
|
478
|
+
* class Logger extends Tag.Service('Logger') {
|
|
479
|
+
* log(message: string) { console.log(message); }
|
|
480
|
+
* }
|
|
481
|
+
*
|
|
482
|
+
* class DatabaseConnection extends Tag.Service('DatabaseConnection') {
|
|
483
|
+
* constructor(private logger: Logger, private url: string) { super(); }
|
|
484
|
+
* async connect() { this.logger.log('Connected'); }
|
|
485
|
+
* async disconnect() { this.logger.log('Disconnected'); }
|
|
486
|
+
* }
|
|
487
|
+
*
|
|
488
|
+
* class DatabaseLifecycle implements DependencyLifecycle<DatabaseConnection, typeof Logger> {
|
|
489
|
+
* constructor(private url: string) {}
|
|
490
|
+
*
|
|
491
|
+
* async create(ctx: ResolutionContext<typeof Logger>): Promise<DatabaseConnection> {
|
|
492
|
+
* const logger = await ctx.resolve(Logger);
|
|
493
|
+
* const conn = new DatabaseConnection(logger, this.url);
|
|
494
|
+
* await conn.connect();
|
|
495
|
+
* return conn;
|
|
496
|
+
* }
|
|
497
|
+
*
|
|
498
|
+
* async cleanup(conn: DatabaseConnection): Promise<void> {
|
|
499
|
+
* await conn.disconnect();
|
|
500
|
+
* }
|
|
501
|
+
* }
|
|
502
|
+
*
|
|
503
|
+
* const container = Container.empty()
|
|
504
|
+
* .register(Logger, () => new Logger())
|
|
505
|
+
* .register(DatabaseConnection, new DatabaseLifecycle('postgresql://localhost:5432'));
|
|
506
|
+
* ```
|
|
507
|
+
*
|
|
508
|
+
* @example Class with only factory (no cleanup)
|
|
509
|
+
* ```typescript
|
|
510
|
+
* import { Container, Tag } from 'sandly';
|
|
511
|
+
*
|
|
512
|
+
* class SimpleService extends Tag.Service('SimpleService') {}
|
|
513
|
+
*
|
|
514
|
+
* class SimpleServiceLifecycle implements DependencyLifecycle<SimpleService, never> {
|
|
515
|
+
* create(): SimpleService {
|
|
516
|
+
* return new SimpleService();
|
|
517
|
+
* }
|
|
518
|
+
* // cleanup is optional, so it can be omitted
|
|
519
|
+
* }
|
|
520
|
+
*
|
|
521
|
+
* const container = Container.empty().register(
|
|
522
|
+
* SimpleService,
|
|
523
|
+
* new SimpleServiceLifecycle()
|
|
524
|
+
* );
|
|
525
|
+
* ```
|
|
467
526
|
*/
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
}
|
|
527
|
+
interface DependencyLifecycle<T, TReg extends AnyTag> {
|
|
528
|
+
create: Factory<T, TReg>;
|
|
529
|
+
cleanup?: Finalizer<T>;
|
|
530
|
+
}
|
|
472
531
|
/**
|
|
473
532
|
* Union type representing all valid dependency registration specifications.
|
|
474
533
|
*
|
|
@@ -490,8 +549,8 @@ type DependencyLifecycle<T, TReg extends AnyTag> = {
|
|
|
490
549
|
* @example Lifecycle registration
|
|
491
550
|
* ```typescript
|
|
492
551
|
* const spec: DependencySpec<typeof DatabaseConnection, never> = {
|
|
493
|
-
*
|
|
494
|
-
*
|
|
552
|
+
* create: () => new DatabaseConnection(),
|
|
553
|
+
* cleanup: (conn) => conn.close()
|
|
495
554
|
* };
|
|
496
555
|
*
|
|
497
556
|
* Container.empty().register(DatabaseConnection, spec);
|
|
@@ -522,7 +581,6 @@ interface IContainer<TReg extends AnyTag = never> {
|
|
|
522
581
|
exists(tag: AnyTag): boolean;
|
|
523
582
|
resolve: <T extends TReg>(tag: T) => Promise<TagType<T>>;
|
|
524
583
|
resolveAll: <const T extends readonly TReg[]>(...tags: T) => Promise<{ [K in keyof T]: TagType<T[K]> }>;
|
|
525
|
-
merge<TTarget extends AnyTag>(other: IContainer<TTarget>): IContainer<TReg | TTarget>;
|
|
526
584
|
destroy(): Promise<void>;
|
|
527
585
|
}
|
|
528
586
|
/**
|
|
@@ -736,8 +794,8 @@ declare class Container<TReg extends AnyTag> implements IContainer<TReg> {
|
|
|
736
794
|
* and cached for subsequent calls. The method is async-safe and handles concurrent
|
|
737
795
|
* requests for the same dependency correctly.
|
|
738
796
|
*
|
|
739
|
-
* The method performs circular dependency detection
|
|
740
|
-
* the resolution
|
|
797
|
+
* The method performs circular dependency detection by tracking the resolution chain
|
|
798
|
+
* through the resolution context.
|
|
741
799
|
*
|
|
742
800
|
* @template T - The dependency tag type (must be registered in this container)
|
|
743
801
|
* @param tag - The dependency tag to retrieve
|
|
@@ -780,6 +838,12 @@ declare class Container<TReg extends AnyTag> implements IContainer<TReg> {
|
|
|
780
838
|
* ```
|
|
781
839
|
*/
|
|
782
840
|
resolve<T extends TReg>(tag: T): Promise<TagType<T>>;
|
|
841
|
+
/**
|
|
842
|
+
* Internal resolution method that tracks the dependency chain for circular dependency detection.
|
|
843
|
+
* Can be overridden by subclasses (e.g., ScopedContainer) to implement custom resolution logic.
|
|
844
|
+
* @internal
|
|
845
|
+
*/
|
|
846
|
+
protected resolveInternal<T extends TReg>(tag: T, chain: AnyTag[]): Promise<TagType<T>>;
|
|
783
847
|
/**
|
|
784
848
|
* Resolves multiple dependencies concurrently using Promise.all.
|
|
785
849
|
*
|
|
@@ -821,51 +885,16 @@ declare class Container<TReg extends AnyTag> implements IContainer<TReg> {
|
|
|
821
885
|
* ```
|
|
822
886
|
*/
|
|
823
887
|
resolveAll<const T extends readonly TReg[]>(...tags: T): Promise<{ [K in keyof T]: TagType<T[K]> }>;
|
|
824
|
-
/**
|
|
825
|
-
* Copies all registrations from this container to a target container.
|
|
826
|
-
*
|
|
827
|
-
* @internal
|
|
828
|
-
* @param target - The container to copy registrations to
|
|
829
|
-
* @throws {ContainerDestroyedError} If this container has been destroyed
|
|
830
|
-
*/
|
|
831
|
-
copyTo<TTarget extends AnyTag>(target: Container<TTarget>): void;
|
|
832
|
-
/**
|
|
833
|
-
* Creates a new container by merging this container's registrations with another container.
|
|
834
|
-
*
|
|
835
|
-
* This method creates a new container that contains all registrations from both containers.
|
|
836
|
-
* If there are conflicts (same dependency registered in both containers), this
|
|
837
|
-
* container's registration will take precedence.
|
|
838
|
-
*
|
|
839
|
-
* **Important**: Only the registrations are copied, not any cached instances.
|
|
840
|
-
* The new container starts with an empty instance cache.
|
|
841
|
-
*
|
|
842
|
-
* @param other - The container to merge with
|
|
843
|
-
* @returns A new container with combined registrations
|
|
844
|
-
* @throws {ContainerDestroyedError} If this container has been destroyed
|
|
845
|
-
*
|
|
846
|
-
* @example Merging containers
|
|
847
|
-
* ```typescript
|
|
848
|
-
* const container1 = Container.empty()
|
|
849
|
-
* .register(DatabaseService, () => new DatabaseService());
|
|
850
|
-
*
|
|
851
|
-
* const container2 = Container.empty()
|
|
852
|
-
* .register(UserService, () => new UserService());
|
|
853
|
-
*
|
|
854
|
-
* const merged = container1.merge(container2);
|
|
855
|
-
* // merged has both DatabaseService and UserService
|
|
856
|
-
* ```
|
|
857
|
-
*/
|
|
858
|
-
merge<TTarget extends AnyTag>(other: Container<TTarget>): Container<TReg | TTarget>;
|
|
859
888
|
/**
|
|
860
889
|
* Destroys all instantiated dependencies by calling their finalizers and makes the container unusable.
|
|
861
890
|
*
|
|
862
891
|
* **Important: After calling destroy(), the container becomes permanently unusable.**
|
|
863
|
-
* Any subsequent calls to register(), get(), or destroy() will throw a
|
|
892
|
+
* Any subsequent calls to register(), get(), or destroy() will throw a DependencyFinalizationError.
|
|
864
893
|
* This ensures proper cleanup and prevents runtime errors from accessing destroyed resources.
|
|
865
894
|
*
|
|
866
895
|
* All finalizers for instantiated dependencies are called concurrently using Promise.allSettled()
|
|
867
896
|
* for maximum cleanup performance.
|
|
868
|
-
* If any finalizers fail, all errors are collected and a
|
|
897
|
+
* If any finalizers fail, all errors are collected and a DependencyFinalizationError
|
|
869
898
|
* is thrown containing details of all failures.
|
|
870
899
|
*
|
|
871
900
|
* **Finalizer Concurrency:** Finalizers run concurrently, so there are no ordering guarantees.
|
|
@@ -946,35 +975,34 @@ type ErrorDump = {
|
|
|
946
975
|
cause?: unknown;
|
|
947
976
|
};
|
|
948
977
|
};
|
|
949
|
-
declare class BaseError extends Error {
|
|
950
|
-
detail: Record<string, unknown> | undefined;
|
|
951
|
-
constructor(message: string, {
|
|
952
|
-
cause,
|
|
953
|
-
detail
|
|
954
|
-
}?: ErrorProps);
|
|
955
|
-
static ensure(error: unknown): BaseError;
|
|
956
|
-
dump(): ErrorDump;
|
|
957
|
-
dumps(): string;
|
|
958
|
-
}
|
|
959
978
|
/**
|
|
960
|
-
* Base error class for all
|
|
979
|
+
* Base error class for all library errors.
|
|
961
980
|
*
|
|
962
|
-
* This extends the
|
|
963
|
-
* and structured error information across the
|
|
981
|
+
* This extends the native Error class to provide consistent error handling
|
|
982
|
+
* and structured error information across the library.
|
|
964
983
|
*
|
|
965
|
-
* @example Catching
|
|
984
|
+
* @example Catching library errors
|
|
966
985
|
* ```typescript
|
|
967
986
|
* try {
|
|
968
987
|
* await container.resolve(SomeService);
|
|
969
988
|
* } catch (error) {
|
|
970
|
-
* if (error instanceof
|
|
989
|
+
* if (error instanceof SandlyError) {
|
|
971
990
|
* console.error('DI Error:', error.message);
|
|
972
991
|
* console.error('Details:', error.detail);
|
|
973
992
|
* }
|
|
974
993
|
* }
|
|
975
994
|
* ```
|
|
976
995
|
*/
|
|
977
|
-
declare class
|
|
996
|
+
declare class SandlyError extends Error {
|
|
997
|
+
detail: Record<string, unknown> | undefined;
|
|
998
|
+
constructor(message: string, {
|
|
999
|
+
cause,
|
|
1000
|
+
detail
|
|
1001
|
+
}?: ErrorProps);
|
|
1002
|
+
static ensure(error: unknown): SandlyError;
|
|
1003
|
+
dump(): ErrorDump;
|
|
1004
|
+
dumps(): string;
|
|
1005
|
+
}
|
|
978
1006
|
/**
|
|
979
1007
|
* Error thrown when attempting to register a dependency that has already been instantiated.
|
|
980
1008
|
*
|
|
@@ -982,7 +1010,7 @@ declare class ContainerError extends BaseError {}
|
|
|
982
1010
|
* Registration must happen before any instantiation occurs, as cached instances would still be used
|
|
983
1011
|
* by existing dependencies.
|
|
984
1012
|
*/
|
|
985
|
-
declare class DependencyAlreadyInstantiatedError extends
|
|
1013
|
+
declare class DependencyAlreadyInstantiatedError extends SandlyError {}
|
|
986
1014
|
/**
|
|
987
1015
|
* Error thrown when attempting to use a container that has been destroyed.
|
|
988
1016
|
*
|
|
@@ -990,7 +1018,7 @@ declare class DependencyAlreadyInstantiatedError extends ContainerError {}
|
|
|
990
1018
|
* on a container that has already been destroyed. It indicates a programming error where the container
|
|
991
1019
|
* is being used after it has been destroyed.
|
|
992
1020
|
*/
|
|
993
|
-
declare class ContainerDestroyedError extends
|
|
1021
|
+
declare class ContainerDestroyedError extends SandlyError {}
|
|
994
1022
|
/**
|
|
995
1023
|
* Error thrown when attempting to retrieve a dependency that hasn't been registered.
|
|
996
1024
|
*
|
|
@@ -1011,7 +1039,7 @@ declare class ContainerDestroyedError extends ContainerError {}
|
|
|
1011
1039
|
* }
|
|
1012
1040
|
* ```
|
|
1013
1041
|
*/
|
|
1014
|
-
declare class UnknownDependencyError extends
|
|
1042
|
+
declare class UnknownDependencyError extends SandlyError {
|
|
1015
1043
|
/**
|
|
1016
1044
|
* @internal
|
|
1017
1045
|
* Creates an UnknownDependencyError for the given tag.
|
|
@@ -1050,7 +1078,7 @@ declare class UnknownDependencyError extends ContainerError {
|
|
|
1050
1078
|
* }
|
|
1051
1079
|
* ```
|
|
1052
1080
|
*/
|
|
1053
|
-
declare class CircularDependencyError extends
|
|
1081
|
+
declare class CircularDependencyError extends SandlyError {
|
|
1054
1082
|
/**
|
|
1055
1083
|
* @internal
|
|
1056
1084
|
* Creates a CircularDependencyError with the dependency chain information.
|
|
@@ -1066,6 +1094,9 @@ declare class CircularDependencyError extends ContainerError {
|
|
|
1066
1094
|
* This wraps the original error with additional context about which dependency
|
|
1067
1095
|
* failed to be created. The original error is preserved as the `cause` property.
|
|
1068
1096
|
*
|
|
1097
|
+
* When dependencies are nested (A depends on B depends on C), and C's factory throws,
|
|
1098
|
+
* you get nested DependencyCreationErrors. Use `getRootCause()` to get the original error.
|
|
1099
|
+
*
|
|
1069
1100
|
* @example Factory throwing error
|
|
1070
1101
|
* ```typescript
|
|
1071
1102
|
* class DatabaseService extends Tag.Service('DatabaseService') {}
|
|
@@ -1083,8 +1114,22 @@ declare class CircularDependencyError extends ContainerError {
|
|
|
1083
1114
|
* }
|
|
1084
1115
|
* }
|
|
1085
1116
|
* ```
|
|
1117
|
+
*
|
|
1118
|
+
* @example Getting root cause from nested errors
|
|
1119
|
+
* ```typescript
|
|
1120
|
+
* // ServiceA -> ServiceB -> ServiceC (ServiceC throws)
|
|
1121
|
+
* try {
|
|
1122
|
+
* await container.resolve(ServiceA);
|
|
1123
|
+
* } catch (error) {
|
|
1124
|
+
* if (error instanceof DependencyCreationError) {
|
|
1125
|
+
* console.error('Top-level error:', error.message); // "Error creating instance of ServiceA"
|
|
1126
|
+
* const rootCause = error.getRootCause();
|
|
1127
|
+
* console.error('Root cause:', rootCause); // Original error from ServiceC
|
|
1128
|
+
* }
|
|
1129
|
+
* }
|
|
1130
|
+
* ```
|
|
1086
1131
|
*/
|
|
1087
|
-
declare class DependencyCreationError extends
|
|
1132
|
+
declare class DependencyCreationError extends SandlyError {
|
|
1088
1133
|
/**
|
|
1089
1134
|
* @internal
|
|
1090
1135
|
* Creates a DependencyCreationError wrapping the original factory error.
|
|
@@ -1093,6 +1138,27 @@ declare class DependencyCreationError extends ContainerError {
|
|
|
1093
1138
|
* @param error - The original error thrown by the factory function
|
|
1094
1139
|
*/
|
|
1095
1140
|
constructor(tag: AnyTag, error: unknown);
|
|
1141
|
+
/**
|
|
1142
|
+
* Traverses the error chain to find the root cause error.
|
|
1143
|
+
*
|
|
1144
|
+
* When dependencies are nested, each level wraps the error in a DependencyCreationError.
|
|
1145
|
+
* This method unwraps all the layers to get to the original error that started the failure.
|
|
1146
|
+
*
|
|
1147
|
+
* @returns The root cause error (not a DependencyCreationError unless that's the only error)
|
|
1148
|
+
*
|
|
1149
|
+
* @example
|
|
1150
|
+
* ```typescript
|
|
1151
|
+
* try {
|
|
1152
|
+
* await container.resolve(UserService);
|
|
1153
|
+
* } catch (error) {
|
|
1154
|
+
* if (error instanceof DependencyCreationError) {
|
|
1155
|
+
* const rootCause = error.getRootCause();
|
|
1156
|
+
* console.error('Root cause:', rootCause);
|
|
1157
|
+
* }
|
|
1158
|
+
* }
|
|
1159
|
+
* ```
|
|
1160
|
+
*/
|
|
1161
|
+
getRootCause(): unknown;
|
|
1096
1162
|
}
|
|
1097
1163
|
/**
|
|
1098
1164
|
* Error thrown when one or more finalizers fail during container destruction.
|
|
@@ -1113,7 +1179,8 @@ declare class DependencyCreationError extends ContainerError {
|
|
|
1113
1179
|
* }
|
|
1114
1180
|
* ```
|
|
1115
1181
|
*/
|
|
1116
|
-
declare class DependencyFinalizationError extends
|
|
1182
|
+
declare class DependencyFinalizationError extends SandlyError {
|
|
1183
|
+
private readonly errors;
|
|
1117
1184
|
/**
|
|
1118
1185
|
* @internal
|
|
1119
1186
|
* Creates a DependencyFinalizationError aggregating multiple finalizer failures.
|
|
@@ -1121,6 +1188,13 @@ declare class DependencyFinalizationError extends ContainerError {
|
|
|
1121
1188
|
* @param errors - Array of errors thrown by individual finalizers
|
|
1122
1189
|
*/
|
|
1123
1190
|
constructor(errors: unknown[]);
|
|
1191
|
+
/**
|
|
1192
|
+
* Returns the root causes of the errors that occurred during finalization.
|
|
1193
|
+
*
|
|
1194
|
+
* @returns An array of the errors that occurred during finalization.
|
|
1195
|
+
* You can expect at least one error in the array.
|
|
1196
|
+
*/
|
|
1197
|
+
getRootCauses(): unknown[];
|
|
1124
1198
|
}
|
|
1125
1199
|
//#endregion
|
|
1126
1200
|
//#region src/layer.d.ts
|
|
@@ -1547,6 +1621,11 @@ declare class ScopedContainer<TReg extends AnyTag> extends Container<TReg> {
|
|
|
1547
1621
|
* 4. If no parent or parent doesn't have it, throw UnknownDependencyError
|
|
1548
1622
|
*/
|
|
1549
1623
|
resolve<T extends TReg>(tag: T): Promise<TagType<T>>;
|
|
1624
|
+
/**
|
|
1625
|
+
* Internal resolution with delegation logic for scoped containers.
|
|
1626
|
+
* @internal
|
|
1627
|
+
*/
|
|
1628
|
+
protected resolveInternal<T extends TReg>(tag: T, chain: AnyTag[]): Promise<TagType<T>>;
|
|
1550
1629
|
/**
|
|
1551
1630
|
* Destroys this scoped container and its children, preserving the container structure for reuse.
|
|
1552
1631
|
*
|
|
@@ -1559,18 +1638,6 @@ declare class ScopedContainer<TReg extends AnyTag> extends Container<TReg> {
|
|
|
1559
1638
|
* before their dependents.
|
|
1560
1639
|
*/
|
|
1561
1640
|
destroy(): Promise<void>;
|
|
1562
|
-
/**
|
|
1563
|
-
* Creates a new scoped container by merging this container's registrations with another container.
|
|
1564
|
-
*
|
|
1565
|
-
* This method overrides the base Container.merge to return a ScopedContainer instead of a regular Container.
|
|
1566
|
-
* The resulting scoped container contains all registrations from both containers and becomes a root scope
|
|
1567
|
-
* (no parent) with the scope name from this container.
|
|
1568
|
-
*
|
|
1569
|
-
* @param other - The container to merge with
|
|
1570
|
-
* @returns A new ScopedContainer with combined registrations
|
|
1571
|
-
* @throws {ContainerDestroyedError} If this container has been destroyed
|
|
1572
|
-
*/
|
|
1573
|
-
merge<TTarget extends AnyTag>(other: Container<TTarget>): ScopedContainer<TReg | TTarget>;
|
|
1574
1641
|
/**
|
|
1575
1642
|
* Creates a child scoped container.
|
|
1576
1643
|
*
|
|
@@ -1579,49 +1646,6 @@ declare class ScopedContainer<TReg extends AnyTag> extends Container<TReg> {
|
|
|
1579
1646
|
*/
|
|
1580
1647
|
child(scope: Scope): ScopedContainer<TReg>;
|
|
1581
1648
|
}
|
|
1582
|
-
/**
|
|
1583
|
-
* Converts a regular container into a scoped container, copying all registrations.
|
|
1584
|
-
*
|
|
1585
|
-
* This function creates a new ScopedContainer instance and copies all factory functions
|
|
1586
|
-
* and finalizers from the source container. The resulting scoped container becomes a root
|
|
1587
|
-
* scope (no parent) with all the same dependency registrations.
|
|
1588
|
-
*
|
|
1589
|
-
* **Important**: Only the registrations are copied, not any cached instances.
|
|
1590
|
-
* The new scoped container starts with an empty instance cache.
|
|
1591
|
-
*
|
|
1592
|
-
* @param container - The container to convert to a scoped container
|
|
1593
|
-
* @param scope - A string or symbol identifier for this scope (used for debugging)
|
|
1594
|
-
* @returns A new ScopedContainer instance with all registrations copied from the source container
|
|
1595
|
-
* @throws {ContainerDestroyedError} If the source container has been destroyed
|
|
1596
|
-
*
|
|
1597
|
-
* @example Converting a regular container to scoped
|
|
1598
|
-
* ```typescript
|
|
1599
|
-
* import { container, scoped } from 'sandly';
|
|
1600
|
-
*
|
|
1601
|
-
* const appContainer = Container.empty()
|
|
1602
|
-
* .register(DatabaseService, () => new DatabaseService())
|
|
1603
|
-
* .register(ConfigService, () => new ConfigService());
|
|
1604
|
-
*
|
|
1605
|
-
* const scopedAppContainer = scoped(appContainer, 'app');
|
|
1606
|
-
*
|
|
1607
|
-
* // Create child scopes
|
|
1608
|
-
* const requestContainer = scopedAppContainer.child('request');
|
|
1609
|
-
* ```
|
|
1610
|
-
*
|
|
1611
|
-
* @example Copying complex registrations
|
|
1612
|
-
* ```typescript
|
|
1613
|
-
* const baseContainer = Container.empty()
|
|
1614
|
-
* .register(DatabaseService, () => new DatabaseService())
|
|
1615
|
-
* .register(UserService, {
|
|
1616
|
-
* factory: async (ctx) => new UserService(await ctx.resolve(DatabaseService)),
|
|
1617
|
-
* finalizer: (service) => service.cleanup()
|
|
1618
|
-
* });
|
|
1619
|
-
*
|
|
1620
|
-
* const scopedContainer = scoped(baseContainer, 'app');
|
|
1621
|
-
* // scopedContainer now has all the same registrations with finalizers preserved
|
|
1622
|
-
* ```
|
|
1623
|
-
*/
|
|
1624
|
-
declare function scoped<TReg extends AnyTag>(container: Container<TReg>, scope: Scope): ScopedContainer<TReg>;
|
|
1625
1649
|
//#endregion
|
|
1626
1650
|
//#region src/service.d.ts
|
|
1627
1651
|
/**
|
|
@@ -1641,7 +1665,7 @@ type ConstructorParams<T extends ServiceTag<TagId, unknown>> = T extends (new (.
|
|
|
1641
1665
|
*/
|
|
1642
1666
|
type ExtractConstructorDeps<T extends readonly unknown[]> = T extends readonly [] ? never : { [K in keyof T]: T[K] extends {
|
|
1643
1667
|
readonly [ServiceTagIdKey]?: infer Id;
|
|
1644
|
-
} ? Id extends TagId ? ServiceTag<Id, T[K]> : never : ExtractInjectTag<T[K]> extends never ? never : ExtractInjectTag<T[K]> }[number];
|
|
1668
|
+
} ? Id extends TagId ? T[K] extends (new (...args: unknown[]) => infer Instance) ? ServiceTag<Id, Instance> : ServiceTag<Id, T[K]> : never : ExtractInjectTag<T[K]> extends never ? never : ExtractInjectTag<T[K]> }[number];
|
|
1645
1669
|
/**
|
|
1646
1670
|
* Produces an ordered tuple of constructor parameters
|
|
1647
1671
|
* where dependency parameters are replaced with their tag types,
|
|
@@ -1650,7 +1674,7 @@ type ExtractConstructorDeps<T extends readonly unknown[]> = T extends readonly [
|
|
|
1650
1674
|
*/
|
|
1651
1675
|
type InferConstructorDepsTuple<T extends readonly unknown[]> = T extends readonly [] ? never : { [K in keyof T]: T[K] extends {
|
|
1652
1676
|
readonly [ServiceTagIdKey]?: infer Id;
|
|
1653
|
-
} ? Id extends TagId ? ServiceTag<Id, T[K]> : never : ExtractInjectTag<T[K]> extends never ? T[K] : ExtractInjectTag<T[K]> };
|
|
1677
|
+
} ? Id extends TagId ? T[K] extends (new (...args: unknown[]) => infer Instance) ? ServiceTag<Id, Instance> : ServiceTag<Id, T[K]> : never : ExtractInjectTag<T[K]> extends never ? T[K] : ExtractInjectTag<T[K]> };
|
|
1654
1678
|
/**
|
|
1655
1679
|
* Union of all dependency tags a ServiceTag constructor requires.
|
|
1656
1680
|
* Filters out non‑DI parameters.
|
|
@@ -1711,7 +1735,7 @@ declare function service<T extends ServiceTag<TagId, unknown>>(tag: T, spec: Dep
|
|
|
1711
1735
|
*/
|
|
1712
1736
|
type AutoServiceSpec<T extends ServiceTag<TagId, unknown>> = ServiceDepsTuple<T> | {
|
|
1713
1737
|
dependencies: ServiceDepsTuple<T>;
|
|
1714
|
-
|
|
1738
|
+
cleanup?: Finalizer<TagType<T>>;
|
|
1715
1739
|
};
|
|
1716
1740
|
/**
|
|
1717
1741
|
* Creates a service layer with automatic dependency injection by inferring constructor parameters.
|
|
@@ -1806,7 +1830,7 @@ type AutoServiceSpec<T extends ServiceTag<TagId, unknown>> = ServiceDepsTuple<T>
|
|
|
1806
1830
|
* DatabaseService,
|
|
1807
1831
|
* {
|
|
1808
1832
|
* dependencies: ['postgresql://localhost:5432/mydb'],
|
|
1809
|
-
*
|
|
1833
|
+
* cleanup: (service) => service.disconnect() // Finalizer for cleanup
|
|
1810
1834
|
* }
|
|
1811
1835
|
* );
|
|
1812
1836
|
* ```
|
|
@@ -1834,4 +1858,4 @@ declare function autoService<T extends ServiceTag<TagId, unknown>>(tag: T, spec:
|
|
|
1834
1858
|
*/
|
|
1835
1859
|
declare function value<T extends ValueTag<TagId, unknown>>(tag: T, constantValue: TagType<T>): Layer<never, T>;
|
|
1836
1860
|
//#endregion
|
|
1837
|
-
export { type AnyLayer, type AnyTag, CircularDependencyError, Container, ContainerDestroyedError,
|
|
1861
|
+
export { type AnyLayer, type AnyTag, CircularDependencyError, Container, ContainerDestroyedError, DependencyAlreadyInstantiatedError, DependencyCreationError, DependencyFinalizationError, type DependencyLifecycle, type DependencySpec, type Factory, type Finalizer, type IContainer, type Inject, InjectSource, Layer, type PromiseOrValue, type ResolutionContext, SandlyError, type Scope, ScopedContainer, type ServiceDependencies, type ServiceDepsTuple, type ServiceTag, Tag, type TagType, UnknownDependencyError, type ValueTag, autoService, layer, service, value };
|