@enshou/di 0.0.1 → 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/dist/index.d.ts CHANGED
@@ -1,4 +1,22 @@
1
- //#region src/index.d.ts
2
- declare const DI = "DI";
1
+ //#region src/token.d.ts
2
+ type Token<T> = symbol & {
3
+ __type: T;
4
+ };
5
+ declare function createToken<T>(description: string): Token<T>;
3
6
  //#endregion
4
- export { DI };
7
+ //#region src/container.d.ts
8
+ type Class$1<T> = new (...args: any[]) => T;
9
+ type Scope = "singleton" | "transient";
10
+ declare class Container {
11
+ private readonly providers;
12
+ private readonly singletonCache;
13
+ registerValue(token: Token<unknown>, value: unknown): void;
14
+ registerClass(token: Token<unknown>, value: Class$1<any>, scope?: Scope): void;
15
+ resolve<T>(token: Token<T>): T;
16
+ }
17
+ //#endregion
18
+ //#region src/inject.d.ts
19
+ type Class<T> = new (...args: any[]) => T;
20
+ declare function Inject(tokens: Array<Token<any>>): <T extends Class<any>>(target: T, _context?: ClassDecoratorContext<T>) => void;
21
+ //#endregion
22
+ export { Container, Inject, type Scope, type Token, createToken };
package/dist/index.js CHANGED
@@ -1,4 +1,41 @@
1
- //#region src/index.ts
2
- const DI = "DI";
1
+ //#region src/metadata.ts
2
+ const INJECTS_KEY = Symbol("injects");
3
3
  //#endregion
4
- export { DI };
4
+ //#region src/container.ts
5
+ var Container = class {
6
+ providers = /* @__PURE__ */ new Map();
7
+ singletonCache = /* @__PURE__ */ new Map();
8
+ registerValue(token, value) {
9
+ this.singletonCache.set(token, value);
10
+ }
11
+ registerClass(token, value, scope = "singleton") {
12
+ this.providers.set(token, {
13
+ kind: "class",
14
+ useClass: value,
15
+ scope
16
+ });
17
+ }
18
+ resolve(token) {
19
+ if (this.singletonCache.has(token)) return this.singletonCache.get(token);
20
+ const provider = this.providers.get(token);
21
+ if (!provider) throw Error(`No provider for ${String(token)}`);
22
+ const deps = (provider.useClass[INJECTS_KEY] ?? []).map(this.resolve.bind(this));
23
+ const value = new provider.useClass(...deps);
24
+ if (provider.scope === "singleton") this.singletonCache.set(token, value);
25
+ return value;
26
+ }
27
+ };
28
+ //#endregion
29
+ //#region src/inject.ts
30
+ function Inject(tokens) {
31
+ return function(target, _context) {
32
+ target[INJECTS_KEY] = tokens;
33
+ };
34
+ }
35
+ //#endregion
36
+ //#region src/token.ts
37
+ function createToken(description) {
38
+ return Symbol(description);
39
+ }
40
+ //#endregion
41
+ export { Container, Inject, createToken };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enshou/di",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "private": false,
5
5
  "license": "Apache-2.0",
6
6
  "author": "Ivan Popov <iiivanpopov999@gmail.com>",
@@ -20,7 +20,8 @@
20
20
  },
21
21
  "devDependencies": {
22
22
  "@typescript/native-preview": "^7.0.0-dev.20260328.1",
23
- "tsdown": "^0.21.7"
23
+ "tsdown": "^0.21.7",
24
+ "vitest": "^4.1.2"
24
25
  },
25
26
  "scripts": {
26
27
  "build": "tsdown"