+42
-16
@@ -34,4 +34,9 @@ declare class AlsToken<T = any> { | ||
| type Constructor<T = any> = new (...args: any[]) => T; | ||
| type Factory<T = any> = (requestId?: string) => T; | ||
| type AsyncFactory<T = any> = (requestId?: string) => Promise<T>; | ||
| /** | ||
| * Provider factory. When `Options.deps` is present, the resolved dependencies | ||
| * are passed as positional arguments. Without `deps`, class-token factories | ||
| * retain the legacy `(requestId?: string)` calling convention. | ||
| */ | ||
| type Factory<T = any> = (...args: any[]) => T; | ||
| type AsyncFactory<T = any> = (...args: any[]) => Promise<T>; | ||
| interface OnInit { | ||
@@ -61,3 +66,3 @@ onInit(): void | Promise<void>; | ||
| scope?: Scope; | ||
| deps?: Constructor[]; | ||
| deps?: Token[]; | ||
| factory?: Factory | AsyncFactory; | ||
@@ -81,4 +86,4 @@ metadata?: Metadata; | ||
| scope?: Scope; | ||
| factory?: Factory; | ||
| deps?: Constructor[]; | ||
| factory?: Factory | AsyncFactory; | ||
| deps?: Token[]; | ||
| input?: unknown; | ||
@@ -103,4 +108,4 @@ metadata?: Metadata; | ||
| private container; | ||
| private resolving; | ||
| private instances; | ||
| private initPromises; | ||
| private nameToToken; | ||
@@ -129,2 +134,3 @@ private registry; | ||
| resolve<T>(token: Constructor<T>, requestId?: string): Promise<T>; | ||
| private resolveInContext; | ||
| private resolveBinding; | ||
@@ -143,2 +149,4 @@ private createInstance; | ||
| private checkCircularDep; | ||
| private childContext; | ||
| private checkAvailable; | ||
| private getByName; | ||
@@ -172,3 +180,3 @@ /** | ||
| constructor(container: Container); | ||
| build<T>(ClassRef: Constructor<T>, requestId?: string, explicitDeps?: Constructor[]): Promise<T>; | ||
| build<T>(ClassRef: Constructor<T>, requestId?: string, explicitDeps?: Token[]): Promise<T>; | ||
| private resolveDependencies; | ||
@@ -181,3 +189,3 @@ private injectProperties; | ||
| type AsyncLocalStorageInstance<T> = { | ||
| type AsyncContext<T> = { | ||
| getStore(): T | undefined; | ||
@@ -187,2 +195,8 @@ run<R>(store: T, callback: () => R): R; | ||
| declare const LAZY: unique symbol; | ||
| type LazyValue<T> = { | ||
| readonly [LAZY]: () => T; | ||
| }; | ||
| type StoreInput<T extends Record<string, any>> = { | ||
| [K in keyof T]?: T[K] | LazyValue<T[K]>; | ||
| }; | ||
| /** | ||
@@ -199,8 +213,10 @@ * Wrap a sync factory so the value is constructed on first `get()` of its key | ||
| */ | ||
| declare function lazyValue<T>(factory: () => T): { | ||
| readonly [LAZY]: () => T; | ||
| }; | ||
| declare function lazyValue<T>(factory: () => T): LazyValue<T>; | ||
| declare class AlsStore<T extends Record<string, any> = any> { | ||
| protected readonly als: AsyncLocalStorageInstance<Map<string, any>>; | ||
| run<R>(data: Partial<T>, fn: () => R): R; | ||
| protected readonly als: AsyncContext<Map<PropertyKey, any>>; | ||
| run<R>(data: StoreInput<T>, fn: () => R): R; | ||
| /** @internal Run with a symbol-keyed value that is hidden from `all()`. */ | ||
| runInternal<R>(key: symbol, value: unknown, fn: () => R): R; | ||
| /** @internal Read a symbol-keyed internal context value. */ | ||
| getInternal<V>(key: symbol): V | undefined; | ||
| get<K extends keyof T>(key: K): T[K] | undefined; | ||
@@ -222,2 +238,7 @@ set<K extends keyof T>(key: K, data: T[K]): void; | ||
| readonly requestPromises: Map<string, Map<Token, Promise<any>>>; | ||
| /** @internal Tokens and request scopes currently being torn down. */ | ||
| readonly deletingTokens: Set<Token>; | ||
| readonly closingRequests: Set<string>; | ||
| /** @internal Prevents new resolutions while clear() is settling providers. */ | ||
| isClearing: boolean; | ||
| readonly instantiator: Instantiator; | ||
@@ -279,3 +300,3 @@ private readonly register; | ||
| delete(token: Token): Promise<void>; | ||
| run<R>(data: Partial<TStore>, fn: () => R): R; | ||
| run<R>(data: StoreInput<TStore>, fn: () => R): R; | ||
| isActive(): boolean; | ||
@@ -426,3 +447,3 @@ all(): Partial<TStore> | undefined; | ||
| } | ||
| declare const container: Container; | ||
| declare let container: Container; | ||
| declare function set<T>(token: AlsToken<T>, value: T): Container; | ||
@@ -517,2 +538,3 @@ declare function set<T>(token: string, value: T): Container; | ||
| removeToken(token: Token): void; | ||
| private removeValueIndex; | ||
| clearIndexes(): void; | ||
@@ -573,2 +595,3 @@ } | ||
| cleanupReq(requestId?: string): Promise<void>; | ||
| private settleRequestPromises; | ||
| private destroyRequestScopedInstances; | ||
@@ -580,6 +603,9 @@ private clearRequestMaps; | ||
| private deleteRegistryToken; | ||
| private settleTokenPromises; | ||
| private destroyRegistryValue; | ||
| private cleanTokenFromRequestScopes; | ||
| private removeTokenFromRequestPromises; | ||
| private removeTokenFromMaps; | ||
| clear(): Promise<void>; | ||
| private settleAllPromises; | ||
| private collectRegistryDestroyPromises; | ||
@@ -899,2 +925,2 @@ private collectRequestScopedDestroyPromises; | ||
| export { AlsStore, AlsToken, type AsyncFactory, BaseError, CLASS_TYPE, CONTROLLER, CONTROLLER_PATH, type Constructor, Container, Controller, type ControllerOptions, type CreateInjectOptions, DATABASE, DESIGN_PARAMTYPES, DESIGN_RETURNTYPE, DESIGN_TYPE, DI, DIError, DI_CODES, type DecoratorMetadata, Deleter, type Factory, FactoryWrapper, INJECTABLE, INJECT_PARAMS, INJECT_PROPS, Inject, Injectable, type InjectableOptions, type InjectableType, type InjectionDefinition, Instantiator, METADATA, Meta, MetaHelper, type Metadata, MetadataManager, type MetadataQuery, type NextFunction, type OnBootComplete, type OnDestroy, type OnInit, type Options, type PropertyInjection, type PropsInject, REPOSITORY, type RegEntryOpts, Register, type Registration, type RegistryEntry, Repository, type RepositoryOptions, Resolver, SCAN_PROPS, SCOPE, SERVICE, Scope, Service, type ServiceOptions, Store, type TStore, type Token, Value, alias, boot, container, createAlsToken, createInject, findAll, get, getAll, getClassType, getClasses, getDatabase, getDecoratorMetadata, getDecoratorMetadataValue, getParamTypes, getParameterInjections, getPath, getPropertyInjections, getPropertyType, getReflectType, getReturnType, getScope, has, hasDecoratorMetadata, hasOnBootComplete, hasOnDestroy, hasOnInit, hasSingletonInstance, hasSingletonPending, isAlsToken, isArrayToken, isConstructor, isController, isInjectable, isNamedToken, isRegistryEntry, isRepository, isReqScope, isService, lazyValue, mergeMetadata, reset, resolve, set, setClassType, setInjectable, setScope }; | ||
| export { AlsStore, AlsToken, type AsyncFactory, BaseError, CLASS_TYPE, CONTROLLER, CONTROLLER_PATH, type Constructor, Container, Controller, type ControllerOptions, type CreateInjectOptions, DATABASE, DESIGN_PARAMTYPES, DESIGN_RETURNTYPE, DESIGN_TYPE, DI, DIError, DI_CODES, type DecoratorMetadata, Deleter, type Factory, FactoryWrapper, INJECTABLE, INJECT_PARAMS, INJECT_PROPS, Inject, Injectable, type InjectableOptions, type InjectableType, type InjectionDefinition, Instantiator, type LazyValue, METADATA, Meta, MetaHelper, type Metadata, MetadataManager, type MetadataQuery, type NextFunction, type OnBootComplete, type OnDestroy, type OnInit, type Options, type PropertyInjection, type PropsInject, REPOSITORY, type RegEntryOpts, Register, type Registration, type RegistryEntry, Repository, type RepositoryOptions, Resolver, SCAN_PROPS, SCOPE, SERVICE, Scope, Service, type ServiceOptions, Store, type StoreInput, type TStore, type Token, Value, alias, boot, container, createAlsToken, createInject, findAll, get, getAll, getClassType, getClasses, getDatabase, getDecoratorMetadata, getDecoratorMetadataValue, getParamTypes, getParameterInjections, getPath, getPropertyInjections, getPropertyType, getReflectType, getReturnType, getScope, has, hasDecoratorMetadata, hasOnBootComplete, hasOnDestroy, hasOnInit, hasSingletonInstance, hasSingletonPending, isAlsToken, isArrayToken, isConstructor, isController, isInjectable, isNamedToken, isRegistryEntry, isRepository, isReqScope, isService, lazyValue, mergeMetadata, reset, resolve, set, setClassType, setInjectable, setScope }; |
+7
-2
| { | ||
| "name": "diject", | ||
| "version": "0.1.6", | ||
| "version": "0.1.7", | ||
| "description": "Lightweight TypeScript DI container with decorators, scopes, metadata queries, and AsyncLocalStorage support.", | ||
| "type": "module", | ||
| "engines": { | ||
| "node": ">=18" | ||
| }, | ||
| "files": ["dist"], | ||
@@ -17,3 +20,4 @@ "main": "./dist/index.mjs", | ||
| "scripts": { | ||
| "build": "tsup", | ||
| "build": "npm run typecheck && tsup", | ||
| "typecheck": "tsc -p tsconfig.build.json --noEmit", | ||
| "test": "bun test", | ||
@@ -40,2 +44,3 @@ "test:watch": "bun test --watch", | ||
| "devDependencies": { | ||
| "@types/bun": "^1.3.14", | ||
| "tsup": "^8.0.0", | ||
@@ -42,0 +47,0 @@ "typescript": "^5.7.3", |
+8
-0
@@ -22,2 +22,3 @@ # diject | ||
| `reflect-metadata` is a peer dependency and must be installed by the consumer. | ||
| Node.js 18 or newer is required. | ||
@@ -89,2 +90,6 @@ ## TypeScript setup | ||
| When `deps` is provided, resolved dependencies are passed to `factory` as | ||
| positional arguments. Without `deps`, a class-token factory receives the | ||
| optional request ID for backward compatibility. | ||
| ### Constructor injection examples | ||
@@ -363,2 +368,5 @@ | ||
| Resolving a request-scoped provider without an explicit request ID or an active | ||
| ALS `requestId` throws instead of creating an untracked instance. | ||
| Request cleanup: | ||
@@ -365,0 +373,0 @@ |
Sorry, the diff of this file is too big to display
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
123439
6.93%3241
5.81%753
1.07%4
33.33%2
100%