@nzyme/ioc 0.1.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/LICENSE +21 -0
- package/dist/Caller.d.ts +1 -0
- package/dist/Caller.js +6 -0
- package/dist/Caller.js.map +1 -0
- package/dist/Command.d.ts +13 -0
- package/dist/Command.js +17 -0
- package/dist/Command.js.map +1 -0
- package/dist/Container.d.ts +27 -0
- package/dist/Container.js +64 -0
- package/dist/Container.js.map +1 -0
- package/dist/ContainerScope.d.ts +6 -0
- package/dist/ContainerScope.js +7 -0
- package/dist/ContainerScope.js.map +1 -0
- package/dist/Injectable.d.ts +49 -0
- package/dist/Injectable.js +24 -0
- package/dist/Injectable.js.map +1 -0
- package/dist/Interface.d.ts +50 -0
- package/dist/Interface.js +66 -0
- package/dist/Interface.js.map +1 -0
- package/dist/Module.d.ts +5 -0
- package/dist/Module.js +5 -0
- package/dist/Module.js.map +1 -0
- package/dist/Service.d.ts +28 -0
- package/dist/Service.js +29 -0
- package/dist/Service.js.map +1 -0
- package/dist/helpers/constValue.d.ts +6 -0
- package/dist/helpers/constValue.js +12 -0
- package/dist/helpers/constValue.js.map +1 -0
- package/dist/helpers/envVariable.d.ts +16 -0
- package/dist/helpers/envVariable.js +20 -0
- package/dist/helpers/envVariable.js.map +1 -0
- package/dist/helpers/optional.d.ts +1 -0
- package/dist/helpers/optional.js +2 -0
- package/dist/helpers/optional.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/serviceResolve.d.ts +16 -0
- package/dist/serviceResolve.js +83 -0
- package/dist/serviceResolve.js.map +1 -0
- package/package.json +29 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kedrzu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/Caller.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const Caller: import("./Injectable.js").Injectable<import("./Injectable.js").Injectable<unknown> | undefined>;
|
package/dist/Caller.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Caller.js","sourceRoot":"","sources":["../src/Caller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnD,MAAM,CAAC,MAAM,MAAM,GAAG,gBAAgB,CAAC;IACnC,IAAI,EAAE,QAAQ;IACd,OAAO,EAAE,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO;CAC5C,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EmptyObject } from '@nzyme/types';
|
|
2
|
+
import type { ContainerScope } from './ContainerScope.js';
|
|
3
|
+
import type { Interface } from './Interface.js';
|
|
4
|
+
import { type Service, type ServiceConstructor, type ServiceDependencies, type ServiceOptions } from './Service.js';
|
|
5
|
+
export type CommandFunction<P extends any[] = any[], R = unknown> = (...params: P) => R;
|
|
6
|
+
export interface CommandOptions<T extends CommandFunction, TExtend extends T = T> {
|
|
7
|
+
readonly implements?: Interface<T>;
|
|
8
|
+
readonly scope?: ContainerScope;
|
|
9
|
+
readonly setup: ServiceConstructor<TExtend>;
|
|
10
|
+
}
|
|
11
|
+
export type Command<T extends CommandFunction = CommandFunction> = Service<T>;
|
|
12
|
+
export type CommandResult<T extends Command<any>> = T extends Command<infer R> ? Awaited<ReturnType<R>> : never;
|
|
13
|
+
export declare function defineCommand<T extends CommandFunction, TExtend extends T = T, TDeps extends ServiceDependencies = EmptyObject>(options: ServiceOptions<T, TExtend, TDeps>): Service<TExtend, TDeps>;
|
package/dist/Command.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createMemo } from '@nzyme/utils';
|
|
2
|
+
import { defineService, } from './Service.js';
|
|
3
|
+
import { singletonStrategy } from './serviceResolve.js';
|
|
4
|
+
/*#__NO_SIDE_EFFECTS__*/
|
|
5
|
+
export function defineCommand(options) {
|
|
6
|
+
return defineService({
|
|
7
|
+
...options,
|
|
8
|
+
resolution: commandStrategy,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function commandStrategy(params) {
|
|
12
|
+
const memo = createMemo(() => singletonStrategy(params));
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
14
|
+
const command = (...args) => memo()(...args);
|
|
15
|
+
return command;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=Command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Command.js","sourceRoot":"","sources":["../src/Command.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI1C,OAAO,EACH,aAAa,GAKhB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,iBAAiB,EAAgC,MAAM,qBAAqB,CAAC;AAiBtF,wBAAwB;AACxB,MAAM,UAAU,aAAa,CAI3B,OAA0C;IACxC,OAAO,aAAa,CAAoB;QACpC,GAAG,OAAO;QACV,UAAU,EAAE,eAAe;KAC9B,CAAC,CAAC;AACP,CAAC;AAED,SAAS,eAAe,CAAC,MAA+B;IACpD,MAAM,IAAI,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,iBAAiB,CAAC,MAAM,CAAoB,CAAC,CAAC;IAC5E,iEAAiE;IACjE,MAAM,OAAO,GAAoB,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;IAE9D,OAAO,OAAO,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ContainerScope } from './ContainerScope.js';
|
|
2
|
+
import { type Injectable } from './Injectable.js';
|
|
3
|
+
import type { Interface } from './Interface.js';
|
|
4
|
+
import type { Module } from './Module.js';
|
|
5
|
+
import { type Service } from './Service.js';
|
|
6
|
+
export declare const CONTAINER_SYMBOL: unique symbol;
|
|
7
|
+
export type ContainerOptions = {
|
|
8
|
+
parent?: Container;
|
|
9
|
+
scope?: ContainerScope;
|
|
10
|
+
resolve?: (service: Service, scope?: Interface) => unknown;
|
|
11
|
+
createChild?: (scope: ContainerScope) => Container;
|
|
12
|
+
};
|
|
13
|
+
export type Container = {
|
|
14
|
+
readonly parent?: Container;
|
|
15
|
+
readonly scope?: ContainerScope;
|
|
16
|
+
readonly [CONTAINER_SYMBOL]: true;
|
|
17
|
+
addModule<TParams extends unknown[], TResult>(this: void, module: Module<TParams, TResult>, ...params: TParams): void;
|
|
18
|
+
createChild(this: void, scope: ContainerScope): Container;
|
|
19
|
+
get<T>(this: void, injectable: Injectable<T>): T | Injectable<T> | undefined;
|
|
20
|
+
set<T>(this: void, injectable: Injectable<T>, instance: T): void;
|
|
21
|
+
set<T>(this: void, injectable: Injectable<T>, service: Injectable<T>): void;
|
|
22
|
+
resolve<T>(this: void, injectable: Injectable<T>, source?: Interface): T;
|
|
23
|
+
tryResolve<T>(this: void, injectable: Injectable<T>, source?: Interface): T | undefined;
|
|
24
|
+
};
|
|
25
|
+
export declare const Container: Injectable<Container>;
|
|
26
|
+
export declare function createContainer(options?: ContainerOptions): Container;
|
|
27
|
+
export declare function isContainer(value: unknown): value is Container;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { defineInjectable, isInjectable } from './Injectable.js';
|
|
2
|
+
import { isService } from './Service.js';
|
|
3
|
+
export const CONTAINER_SYMBOL = Symbol('container');
|
|
4
|
+
export const Container = defineInjectable({
|
|
5
|
+
name: 'Container',
|
|
6
|
+
resolve: container => container,
|
|
7
|
+
});
|
|
8
|
+
export function createContainer(options) {
|
|
9
|
+
const instances = new Map();
|
|
10
|
+
const injectables = new Map();
|
|
11
|
+
const parent = options?.parent;
|
|
12
|
+
const scope = options?.scope;
|
|
13
|
+
const resolve = options?.resolve ?? ((service, source) => service.resolve(container, source));
|
|
14
|
+
const createChild = options?.createChild ?? (scope => createContainer({ parent: container, scope }));
|
|
15
|
+
const container = {
|
|
16
|
+
[CONTAINER_SYMBOL]: true,
|
|
17
|
+
scope,
|
|
18
|
+
parent,
|
|
19
|
+
addModule: (module, ...params) => module(container, ...params),
|
|
20
|
+
createChild,
|
|
21
|
+
get,
|
|
22
|
+
set,
|
|
23
|
+
resolve: injectable => injectable.resolve(container),
|
|
24
|
+
tryResolve,
|
|
25
|
+
};
|
|
26
|
+
return container;
|
|
27
|
+
function get(injectable) {
|
|
28
|
+
return (instances.get(injectable) ??
|
|
29
|
+
injectables.get(injectable));
|
|
30
|
+
}
|
|
31
|
+
function set(injectable, instanceOrService) {
|
|
32
|
+
if (isInjectable(instanceOrService)) {
|
|
33
|
+
injectables.set(injectable, instanceOrService);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
instances.set(injectable, instanceOrService);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function tryResolve(injectable, source) {
|
|
40
|
+
const instance = instances.get(injectable);
|
|
41
|
+
if (instance) {
|
|
42
|
+
return instance;
|
|
43
|
+
}
|
|
44
|
+
// Try to resolve as a registered service
|
|
45
|
+
const service = injectables.get(injectable);
|
|
46
|
+
if (service) {
|
|
47
|
+
return resolveService(service, source);
|
|
48
|
+
}
|
|
49
|
+
if (isService(injectable)) {
|
|
50
|
+
return resolveService(injectable, source);
|
|
51
|
+
}
|
|
52
|
+
return parent?.tryResolve(injectable, source);
|
|
53
|
+
}
|
|
54
|
+
function resolveService(service, source) {
|
|
55
|
+
if (service.scope !== scope) {
|
|
56
|
+
return parent?.tryResolve(service, source);
|
|
57
|
+
}
|
|
58
|
+
return resolve(service, source);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export function isContainer(value) {
|
|
62
|
+
return typeof value === 'object' && value != null && CONTAINER_SYMBOL in value;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=Container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Container.js","sourceRoot":"","sources":["../src/Container.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAmB,MAAM,iBAAiB,CAAC;AAGlF,OAAO,EAAE,SAAS,EAAgB,MAAM,cAAc,CAAC;AAEvD,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AA0BpD,MAAM,CAAC,MAAM,SAAS,GAAG,gBAAgB,CAAC;IACtC,IAAI,EAAE,WAAW;IACjB,OAAO,EAAE,SAAS,CAAC,EAAE,CAAC,SAAS;CAClC,CAAC,CAAC;AAEH,MAAM,UAAU,eAAe,CAAC,OAA0B;IACtD,MAA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 +1,49 @@
|
|
|
1
|
+
import type { Container } from './Container.js';
|
|
2
|
+
export declare const INJECTABLE_SYMBOL: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* Infers the type of the injectable.
|
|
5
|
+
*/
|
|
6
|
+
export type Injected<T> = T extends Injectable ? ReturnType<T['resolve']> : never;
|
|
7
|
+
/**
|
|
8
|
+
* Options for the injectable.
|
|
9
|
+
*/
|
|
10
|
+
export type InjectableOptions<T> = {
|
|
11
|
+
/**
|
|
12
|
+
* Name of the injectable.
|
|
13
|
+
*/
|
|
14
|
+
readonly name?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Resolve function.
|
|
17
|
+
*/
|
|
18
|
+
readonly resolve: (container: Container, caller?: Injectable) => T;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Injectable is a type that can be injected into other types.
|
|
22
|
+
*/
|
|
23
|
+
export type Injectable<T = unknown> = {
|
|
24
|
+
/**
|
|
25
|
+
* Symbol that indicates that the type is an injectable.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
readonly [INJECTABLE_SYMBOL]: true | symbol;
|
|
29
|
+
/**
|
|
30
|
+
* Name of the injectable.
|
|
31
|
+
*/
|
|
32
|
+
readonly name?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Resolve function.
|
|
35
|
+
*/
|
|
36
|
+
readonly resolve: (container: Container, caller?: Injectable) => T;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Define an injectable.
|
|
40
|
+
* @param options - Options for the injectable.
|
|
41
|
+
* @returns Injectable.
|
|
42
|
+
*/
|
|
43
|
+
export declare function defineInjectable<T>(options: InjectableOptions<T>): Injectable<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Check if the value is an injectable.
|
|
46
|
+
* @param value - Value to check.
|
|
47
|
+
* @returns Whether the value is an injectable.
|
|
48
|
+
*/
|
|
49
|
+
export declare function isInjectable(value: unknown): value is Injectable;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const INJECTABLE_SYMBOL = Symbol('injectable');
|
|
2
|
+
/**
|
|
3
|
+
* Define an injectable.
|
|
4
|
+
* @param options - Options for the injectable.
|
|
5
|
+
* @returns Injectable.
|
|
6
|
+
*/
|
|
7
|
+
export function defineInjectable(options) {
|
|
8
|
+
return {
|
|
9
|
+
...options,
|
|
10
|
+
[INJECTABLE_SYMBOL]: true,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Check if the value is an injectable.
|
|
15
|
+
* @param value - Value to check.
|
|
16
|
+
* @returns Whether the value is an injectable.
|
|
17
|
+
*/
|
|
18
|
+
export function isInjectable(value) {
|
|
19
|
+
if (value == null) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return value[INJECTABLE_SYMBOL] !== undefined;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=Injectable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Injectable.js","sourceRoot":"","sources":["../src/Injectable.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAwCtD;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAI,OAA6B;IAC7D,OAAO;QACH,GAAG,OAAO;QACV,CAAC,iBAAiB,CAAC,EAAE,IAAI;KAC5B,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACvC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAQ,KAAoB,CAAC,iBAAiB,CAAC,KAAK,SAAS,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Injectable } from './Injectable.js';
|
|
2
|
+
/**
|
|
3
|
+
* Options for the interface.
|
|
4
|
+
*/
|
|
5
|
+
export interface InterfaceOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Name of the interface.
|
|
8
|
+
*/
|
|
9
|
+
name?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Interface injectable.
|
|
13
|
+
*/
|
|
14
|
+
export type Interface<T = unknown> = Injectable<T> & {
|
|
15
|
+
/**
|
|
16
|
+
* Make the interface optional.
|
|
17
|
+
* Will resolve to `undefined` if the interface is not registered in the container.
|
|
18
|
+
*/
|
|
19
|
+
optional(): Injectable<T | undefined>;
|
|
20
|
+
/**
|
|
21
|
+
* Set the default value for the interface.
|
|
22
|
+
* Will resolve to the default value if the interface is not registered in the container.
|
|
23
|
+
* @param value - Default value.
|
|
24
|
+
*/
|
|
25
|
+
default(value: T): Injectable<T>;
|
|
26
|
+
/**
|
|
27
|
+
* Set the default value for the interface.
|
|
28
|
+
* Will resolve to the default value if the interface is not registered in the container.
|
|
29
|
+
* @param value - Default value.
|
|
30
|
+
*/
|
|
31
|
+
default<D>(value: D): Injectable<T | D>;
|
|
32
|
+
/**
|
|
33
|
+
* Set the default value for the interface.
|
|
34
|
+
* Will resolve to the default value if the interface is not registered in the container.
|
|
35
|
+
* @param value - Default value.
|
|
36
|
+
*/
|
|
37
|
+
default<D>(injectable: Injectable<D>): Injectable<T | D>;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Define an interface injectable.
|
|
41
|
+
* @param options - Options for the interface.
|
|
42
|
+
* @returns Interface injectable.
|
|
43
|
+
*/
|
|
44
|
+
export declare function defineInterface<T>(options?: InterfaceOptions): Interface<T>;
|
|
45
|
+
/**
|
|
46
|
+
* Check if the value is an interface injectable.
|
|
47
|
+
* @param value - Value to check.
|
|
48
|
+
* @returns True if the value is an interface, false otherwise.
|
|
49
|
+
*/
|
|
50
|
+
export declare function isInterface(value: unknown): value is Interface;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { INJECTABLE_SYMBOL, isInjectable } from './Injectable.js';
|
|
2
|
+
const INTERFACE_SYMBOL = Symbol('interface');
|
|
3
|
+
/**
|
|
4
|
+
* Define an interface injectable.
|
|
5
|
+
* @param options - Options for the interface.
|
|
6
|
+
* @returns Interface injectable.
|
|
7
|
+
*/
|
|
8
|
+
export function defineInterface(options = {}) {
|
|
9
|
+
return {
|
|
10
|
+
[INJECTABLE_SYMBOL]: INTERFACE_SYMBOL,
|
|
11
|
+
name: options.name,
|
|
12
|
+
resolve(container, caller) {
|
|
13
|
+
while (container) {
|
|
14
|
+
const value = container.get(this);
|
|
15
|
+
if (value === undefined) {
|
|
16
|
+
if (container.parent) {
|
|
17
|
+
container = container.parent;
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
if (isInjectable(value)) {
|
|
23
|
+
return value.resolve(container, caller);
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`Interface ${this.name ?? ''} was not found`);
|
|
28
|
+
},
|
|
29
|
+
optional,
|
|
30
|
+
default: defaultValue,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if the value is an interface injectable.
|
|
35
|
+
* @param value - Value to check.
|
|
36
|
+
* @returns True if the value is an interface, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export function isInterface(value) {
|
|
39
|
+
if (value == null) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return value[INJECTABLE_SYMBOL] === INTERFACE_SYMBOL;
|
|
43
|
+
}
|
|
44
|
+
function optional() {
|
|
45
|
+
return this.default(undefined);
|
|
46
|
+
}
|
|
47
|
+
function defaultValue(defaultValue) {
|
|
48
|
+
return {
|
|
49
|
+
[INJECTABLE_SYMBOL]: true,
|
|
50
|
+
name: this.name,
|
|
51
|
+
resolve: (container, caller) => {
|
|
52
|
+
const value = container.get(this);
|
|
53
|
+
if (value === undefined) {
|
|
54
|
+
if (isInjectable(defaultValue)) {
|
|
55
|
+
return defaultValue.resolve(container, caller);
|
|
56
|
+
}
|
|
57
|
+
return defaultValue;
|
|
58
|
+
}
|
|
59
|
+
if (isInjectable(value)) {
|
|
60
|
+
return value.resolve(container, caller);
|
|
61
|
+
}
|
|
62
|
+
return value;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=Interface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Interface.js","sourceRoot":"","sources":["../src/Interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAmB,MAAM,iBAAiB,CAAC;AAEnF,MAAM,gBAAgB,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AAyC7C;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAI,UAA4B,EAAE;IAC7D,OAAO;QACH,CAAC,iBAAiB,CAAC,EAAE,gBAAgB;QACrC,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,OAAO,CAAC,SAAS,EAAE,MAAM;YACrB,OAAO,SAAS,EAAE,CAAC;gBACf,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBAElC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACtB,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;wBACnB,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC;wBAC7B,SAAS;oBACb,CAAC;oBAED,MAAM;gBACV,CAAC;gBAED,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;oBACtB,OAAO,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;gBAC5C,CAAC;gBAED,OAAO,KAAK,CAAC;YACjB,CAAC;YAED,MAAM,IAAI,KAAK,CAAC,aAAa,IAAI,CAAC,IAAI,IAAI,EAAE,gBAAgB,CAAC,CAAC;QAClE,CAAC;QACD,QAAQ;QACR,OAAO,EAAE,YAAY;KACxB,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACtC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAQ,KAAmB,CAAC,iBAAiB,CAAC,KAAK,gBAAgB,CAAC;AACxE,CAAC;AAED,SAAS,QAAQ;IACb,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AACnC,CAAC;AAED,SAAS,YAAY,CAEjB,YAA+B;IAE/B,OAAO;QACH,CAAC,iBAAiB,CAAC,EAAE,IAAI;QACzB,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,OAAO,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE;YAC3B,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAClC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACtB,IAAI,YAAY,CAAC,YAAY,CAAC,EAAE,CAAC;oBAC7B,OAAO,YAAY,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;gBACnD,CAAC;gBAED,OAAO,YAAY,CAAC;YACxB,CAAC;YAED,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;gBACtB,OAAO,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;YAC5C,CAAC;YAED,OAAO,KAAK,CAAC;QACjB,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/dist/Module.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Container } from './Container.js';
|
|
2
|
+
export type Module<TParams extends unknown[] = [], TResult = void> = {
|
|
3
|
+
(container: Container, ...params: TParams): TResult;
|
|
4
|
+
};
|
|
5
|
+
export declare function defineModule<TParams extends unknown[], TResult>(module: Module<TParams, TResult>): Module<TParams, TResult>;
|
package/dist/Module.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Module.js","sourceRoot":"","sources":["../src/Module.ts"],"names":[],"mappings":"AAMA,wBAAwB;AACxB,MAAM,UAAU,YAAY,CAAqC,MAAgC;IAC7F,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { EmptyObject } from '@nzyme/types';
|
|
2
|
+
import type { ContainerScope } from './ContainerScope.js';
|
|
3
|
+
import { type Injectable } from './Injectable.js';
|
|
4
|
+
import type { Interface } from './Interface.js';
|
|
5
|
+
import { type ServiceResolutionStrategy, type ServiceResolutionType } from './serviceResolve.js';
|
|
6
|
+
export type ServiceDependencies = {
|
|
7
|
+
[key: string]: Injectable;
|
|
8
|
+
};
|
|
9
|
+
export type ResolveDependencies<D extends ServiceDependencies> = keyof D extends never ? void : {
|
|
10
|
+
[K in keyof D]: D[K] extends Injectable<infer T> ? T : unknown;
|
|
11
|
+
};
|
|
12
|
+
export interface ServiceOptions<T = unknown, TExtend extends T = T, TDeps extends ServiceDependencies = ServiceDependencies> {
|
|
13
|
+
readonly name?: string;
|
|
14
|
+
readonly implements?: Interface<T>;
|
|
15
|
+
readonly deps?: TDeps;
|
|
16
|
+
readonly resolution?: ServiceResolutionType | ServiceResolutionStrategy;
|
|
17
|
+
readonly scope?: ContainerScope;
|
|
18
|
+
readonly setup: ServiceConstructor<TExtend, TDeps>;
|
|
19
|
+
}
|
|
20
|
+
export type ServiceConstructor<T = unknown, TDeps extends ServiceDependencies = ServiceDependencies> = (deps: ResolveDependencies<TDeps>) => T;
|
|
21
|
+
export type Service<T = unknown, TDeps extends ServiceDependencies = ServiceDependencies> = ServiceConstructor<T, TDeps> & Injectable<T> & {
|
|
22
|
+
readonly implements?: Interface;
|
|
23
|
+
readonly scope?: ContainerScope;
|
|
24
|
+
readonly deps: TDeps;
|
|
25
|
+
};
|
|
26
|
+
export declare function defineService<T, TExtend extends T = T, TDeps extends ServiceDependencies = EmptyObject>(options: ServiceOptions<T, TExtend, TDeps>): Service<TExtend, TDeps>;
|
|
27
|
+
export declare function isService<T>(value: Injectable<T>): value is Service<T>;
|
|
28
|
+
export declare function isService(value: unknown): value is Service;
|
package/dist/Service.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { INJECTABLE_SYMBOL } from './Injectable.js';
|
|
2
|
+
import { getResolutionStrategy, } from './serviceResolve.js';
|
|
3
|
+
const SERVICE_SYMBOL = Symbol('service');
|
|
4
|
+
/*#__NO_SIDE_EFFECTS__*/
|
|
5
|
+
export function defineService(options) {
|
|
6
|
+
const name = options.name ?? options.implements?.name ?? 'UnnamedService';
|
|
7
|
+
const resolution = getResolutionStrategy(options.resolution ?? 'singleton');
|
|
8
|
+
const wrapper = {
|
|
9
|
+
[name](deps) {
|
|
10
|
+
return options.setup(deps);
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
const service = wrapper[name];
|
|
14
|
+
service[INJECTABLE_SYMBOL] = SERVICE_SYMBOL;
|
|
15
|
+
service.implements = options.implements;
|
|
16
|
+
service.scope = options.scope;
|
|
17
|
+
service.deps = options.deps ?? {};
|
|
18
|
+
service.resolve = (container, caller) => resolution({
|
|
19
|
+
service: service,
|
|
20
|
+
options: options,
|
|
21
|
+
container,
|
|
22
|
+
caller,
|
|
23
|
+
});
|
|
24
|
+
return service;
|
|
25
|
+
}
|
|
26
|
+
export function isService(value) {
|
|
27
|
+
return value != null && value[INJECTABLE_SYMBOL] === SERVICE_SYMBOL;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=Service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Service.js","sourceRoot":"","sources":["../src/Service.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBAAiB,EAAmB,MAAM,iBAAiB,CAAC;AAErE,OAAO,EACH,qBAAqB,GAGxB,MAAM,qBAAqB,CAAC;AAC7B,MAAM,cAAc,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAwCzC,wBAAwB;AACxB,MAAM,UAAU,aAAa,CAI3B,OAA0C;IACxC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,UAAU,EAAE,IAAI,IAAI,gBAAgB,CAAC;IAC1E,MAAM,UAAU,GAAG,qBAAqB,CAAC,OAAO,CAAC,UAAU,IAAI,WAAW,CAAC,CAAC;IAE5E,MAAM,OAAO,GAA0D;QACnE,CAAC,IAAI,CAAC,CAAC,IAAI;YACP,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC/B,CAAC;KACJ,CAAC;IAEF,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,CAAiD,CAAC;IAE9E,OAAO,CAAC,iBAAiB,CAAC,GAAG,cAAc,CAAC;IAC5C,OAAO,CAAC,UAAU,GAAG,OAAO,CAAC,UAAuB,CAAC;IACrD,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IAC9B,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAK,EAAY,CAAC;IAC7C,OAAO,CAAC,OAAO,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,CACpC,UAAU,CAAC;QACP,OAAO,EAAE,OAA6B;QACtC,OAAO,EAAE,OAAoC;QAC7C,SAAS;QACT,MAAM;KACT,CAAY,CAAC;IAElB,OAAO,OAAkC,CAAC;AAC9C,CAAC;AAID,MAAM,UAAU,SAAS,CAAC,KAAc;IACpC,OAAO,KAAK,IAAI,IAAI,IAAK,KAAoB,CAAC,iBAAiB,CAAC,KAAK,cAAc,CAAC;AACxF,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineInjectable } from '../Injectable.js';
|
|
2
|
+
/**
|
|
3
|
+
* Define a constant value injectable.
|
|
4
|
+
* @param value - Value to define.
|
|
5
|
+
* @returns Constant value injectable.
|
|
6
|
+
*/
|
|
7
|
+
export function constValue(value) {
|
|
8
|
+
return defineInjectable({
|
|
9
|
+
resolve: () => value,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=constValue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constValue.js","sourceRoot":"","sources":["../../src/helpers/constValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAI,KAAQ;IAClC,OAAO,gBAAgB,CAAC;QACpB,OAAO,EAAE,GAAG,EAAE,CAAC,KAAK;KACvB,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for {@link envVariable}.
|
|
3
|
+
*/
|
|
4
|
+
export type EnvVariableOptions<TRequired extends boolean> = {
|
|
5
|
+
/**
|
|
6
|
+
* Whether the environment variable is required.
|
|
7
|
+
*/
|
|
8
|
+
required?: TRequired;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Define an environment variable injectable.
|
|
12
|
+
* @param name - Name of the environment variable.
|
|
13
|
+
* @param options - Options for the environment variable.
|
|
14
|
+
* @returns Environment variable injectable.
|
|
15
|
+
*/
|
|
16
|
+
export declare function envVariable<TRequired extends boolean = false>(name: string, options?: EnvVariableOptions<TRequired>): import("../Injectable.js").Injectable<TRequired extends true ? string : string | undefined>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineInjectable } from '../Injectable.js';
|
|
2
|
+
/**
|
|
3
|
+
* Define an environment variable injectable.
|
|
4
|
+
* @param name - Name of the environment variable.
|
|
5
|
+
* @param options - Options for the environment variable.
|
|
6
|
+
* @returns Environment variable injectable.
|
|
7
|
+
*/
|
|
8
|
+
export function envVariable(name, options) {
|
|
9
|
+
return defineInjectable({
|
|
10
|
+
name: `env:${name}`,
|
|
11
|
+
resolve: () => {
|
|
12
|
+
const value = process.env[name];
|
|
13
|
+
if (options?.required && !value) {
|
|
14
|
+
throw new Error(`Environment variable ${name} not set`);
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=envVariable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envVariable.js","sourceRoot":"","sources":["../../src/helpers/envVariable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAYpD;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACvB,IAAY,EACZ,OAAuC;IAIvC,OAAO,gBAAgB,CAAS;QAC5B,IAAI,EAAE,OAAO,IAAI,EAAE;QACnB,OAAO,EAAE,GAAG,EAAE;YACV,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAEhC,IAAI,OAAO,EAAE,QAAQ,IAAI,CAAC,KAAK,EAAE,CAAC;gBAC9B,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,UAAU,CAAC,CAAC;YAC5D,CAAC;YAED,OAAO,KAAe,CAAC;QAC3B,CAAC;KACJ,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optional.js","sourceRoot":"","sources":["../../src/helpers/optional.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './Command.js';
|
|
2
|
+
export * from './Container.js';
|
|
3
|
+
export * from './ContainerScope.js';
|
|
4
|
+
export * from './Injectable.js';
|
|
5
|
+
export * from './Interface.js';
|
|
6
|
+
export * from './Module.js';
|
|
7
|
+
export * from './Service.js';
|
|
8
|
+
export * from './Caller.js';
|
|
9
|
+
export * from './helpers/envVariable.js';
|
|
10
|
+
export * from './helpers/constValue.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './Command.js';
|
|
2
|
+
export * from './Container.js';
|
|
3
|
+
export * from './ContainerScope.js';
|
|
4
|
+
export * from './Injectable.js';
|
|
5
|
+
export * from './Interface.js';
|
|
6
|
+
export * from './Module.js';
|
|
7
|
+
export * from './Service.js';
|
|
8
|
+
export * from './Caller.js';
|
|
9
|
+
export * from './helpers/envVariable.js';
|
|
10
|
+
export * from './helpers/constValue.js';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAE5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Container } from './Container.js';
|
|
2
|
+
import type { Injectable } from './Injectable.js';
|
|
3
|
+
import type { Service, ServiceDependencies, ServiceOptions } from './Service.js';
|
|
4
|
+
export type ServiceResolutionParams = {
|
|
5
|
+
service: Service;
|
|
6
|
+
options: ServiceOptions;
|
|
7
|
+
container: Container;
|
|
8
|
+
caller?: Injectable;
|
|
9
|
+
};
|
|
10
|
+
export type ServiceResolutionStrategy = (params: ServiceResolutionParams) => unknown;
|
|
11
|
+
export type ServiceResolutionType = 'transient' | 'singleton' | 'lazy';
|
|
12
|
+
export declare function getResolutionStrategy(strategy: ServiceResolutionType | ServiceResolutionStrategy): ServiceResolutionStrategy;
|
|
13
|
+
export declare function singletonStrategy({ service, options, container, caller, }: ServiceResolutionParams): unknown;
|
|
14
|
+
export declare function transientStrategy({ service, options, container, caller, }: ServiceResolutionParams): unknown;
|
|
15
|
+
export declare function lazyStrategy(params: ServiceResolutionParams): {};
|
|
16
|
+
export declare function resolveDeps(deps: ServiceDependencies, container: Container, caller?: Injectable): Record<string, unknown>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { noop, createMemo } from '@nzyme/utils';
|
|
2
|
+
export function getResolutionStrategy(strategy) {
|
|
3
|
+
if (typeof strategy === 'function') {
|
|
4
|
+
return strategy;
|
|
5
|
+
}
|
|
6
|
+
switch (strategy) {
|
|
7
|
+
case 'transient':
|
|
8
|
+
return transientStrategy;
|
|
9
|
+
case 'singleton':
|
|
10
|
+
return singletonStrategy;
|
|
11
|
+
case 'lazy':
|
|
12
|
+
return lazyStrategy;
|
|
13
|
+
default:
|
|
14
|
+
throw new Error(`Invalid service resolution strategy: ${strategy}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function singletonStrategy({ service, options, container, caller, }) {
|
|
18
|
+
let instance = container.get(service);
|
|
19
|
+
if (instance) {
|
|
20
|
+
return instance;
|
|
21
|
+
}
|
|
22
|
+
while (container.scope !== service.scope) {
|
|
23
|
+
if (!container.parent) {
|
|
24
|
+
throw new Error(`Container with required scope ${service.scope?.name} not found for service ${service.name}`);
|
|
25
|
+
}
|
|
26
|
+
container = container.parent;
|
|
27
|
+
instance = container.get(service);
|
|
28
|
+
if (instance) {
|
|
29
|
+
return instance;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const deps = resolveDeps(service.deps, container, caller);
|
|
33
|
+
instance = options.setup(deps);
|
|
34
|
+
container.set(service, instance);
|
|
35
|
+
if (options.implements && !container.get(options.implements)) {
|
|
36
|
+
container.set(options.implements, instance);
|
|
37
|
+
}
|
|
38
|
+
return instance;
|
|
39
|
+
}
|
|
40
|
+
export function transientStrategy({ service, options, container, caller, }) {
|
|
41
|
+
if (service.scope) {
|
|
42
|
+
while (container.scope !== service.scope) {
|
|
43
|
+
if (!container.parent) {
|
|
44
|
+
throw new Error(`Container with required scope ${service.scope?.name} not found for service ${service.name}`);
|
|
45
|
+
}
|
|
46
|
+
container = container.parent;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const deps = resolveDeps(service.deps, container, caller);
|
|
50
|
+
return options.setup(deps);
|
|
51
|
+
}
|
|
52
|
+
export function lazyStrategy(params) {
|
|
53
|
+
// Short-circuit if already resolved
|
|
54
|
+
const existing = params.container.get(params.service);
|
|
55
|
+
if (existing) {
|
|
56
|
+
return existing;
|
|
57
|
+
}
|
|
58
|
+
const memo = createMemo(() => singletonStrategy(params));
|
|
59
|
+
const proxy = new Proxy(noop, {
|
|
60
|
+
get: (target, prop) => {
|
|
61
|
+
return memo()[prop];
|
|
62
|
+
},
|
|
63
|
+
set: (target, prop, value) => {
|
|
64
|
+
memo()[prop] = value;
|
|
65
|
+
return true;
|
|
66
|
+
},
|
|
67
|
+
has: (target, prop) => {
|
|
68
|
+
return prop in memo();
|
|
69
|
+
},
|
|
70
|
+
apply: (target, thisArg, args) => {
|
|
71
|
+
return memo()(...args);
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
return proxy;
|
|
75
|
+
}
|
|
76
|
+
export function resolveDeps(deps, container, caller) {
|
|
77
|
+
const resolved = {};
|
|
78
|
+
for (const [key, value] of Object.entries(deps)) {
|
|
79
|
+
resolved[key] = value.resolve(container, caller);
|
|
80
|
+
}
|
|
81
|
+
return resolved;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=serviceResolve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serviceResolve.js","sourceRoot":"","sources":["../src/serviceResolve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAgBhD,MAAM,UAAU,qBAAqB,CAAC,QAA2D;IAC7F,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;QACjC,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED,QAAQ,QAAQ,EAAE,CAAC;QACf,KAAK,WAAW;YACZ,OAAO,iBAAiB,CAAC;QA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,CAAC;QACD,GAAG,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;YAClB,OAAO,IAAI,IAAK,IAAI,EAAuC,CAAC;QAChE,CAAC;QACD,KAAK,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,IAAe,EAAE,EAAE;YACxC,OAAQ,IAAI,EAAsC,CAAC,GAAG,IAAI,CAAC,CAAC;QAChE,CAAC;KACJ,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAyB,EAAE,SAAoB,EAAE,MAAmB;IAC5F,MAAM,QAAQ,GAA4B,EAAE,CAAC;IAE7C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9C,QAAQ,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,QAAQ,CAAC;AACpB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nzyme/ioc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"repository": "https://github.com/kedrzu/nzyme.git",
|
|
9
|
+
"author": "Michał Kędrzyński <m.kedrzynski@gmail.com>",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"scripts": {
|
|
15
|
+
"eslint": "eslint . --fix --cache",
|
|
16
|
+
"build": "tsc --build"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@nzyme/types": "^0.1.0",
|
|
20
|
+
"@nzyme/utils": "^0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@nzyme/eslint": "^0.1.0",
|
|
24
|
+
"@nzyme/tsconfig": "^0.1.0",
|
|
25
|
+
"@nzyme/vitest": "^0.1.0",
|
|
26
|
+
"vitest": "^2.1.8"
|
|
27
|
+
},
|
|
28
|
+
"gitHead": "292c98d76c39c9629a99faab3fbfe569bd289ef0"
|
|
29
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@nzyme/tsconfig/base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"tsBuildInfoFile": "./dist/.tsbuildinfo",
|
|
6
|
+
"rootDir": "./src"
|
|
7
|
+
},
|
|
8
|
+
"include": ["src"],
|
|
9
|
+
"exclude": ["src/**/*.test.ts"],
|
|
10
|
+
"references": [
|
|
11
|
+
{
|
|
12
|
+
"path": "../types/tsconfig.json"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"path": "../utils/tsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "../vitest/tsconfig.json"
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|