di-sacala 0.1.0 → 0.1.2

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
@@ -9,6 +9,7 @@
9
9
  - **Full Type Safety**: Get autocompletion and type checks for all your injected services.
10
10
  - **Fluent API**: Chainable service registration makes it easy to compose your container.
11
11
  - **Container Composition**: Merge multiple containers together to share dependencies across different parts of your application.
12
+ - **Lazy & Singleton**: Services are instantiated only on demand (when first accessed) and reused for subsequent accesses.
12
13
  - **Zero Runtime Dependencies**: Extremely lightweight.
13
14
 
14
15
  ## Installation
@@ -21,13 +22,15 @@ npm install di-sacala
21
22
 
22
23
  ### 1. Defining a Service
23
24
 
24
- A service is a class that implements the `DiService` interface. It must have a `name` property which will be used as the key in the container. Use `as const` to ensure the name is treated as a literal type.
25
+ A service is a class that implements the `DiService` interface. It must implement a `getServiceName()` method which will be used as the key in the container. Use `as const` to ensure the name is treated as a literal type.
25
26
 
26
27
  ```typescript
27
28
  import { DiService } from 'di-sacala';
28
29
 
29
30
  export class LoggerService implements DiService<"logger"> {
30
- name = "logger" as const;
31
+ getServiceName() {
32
+ return "logger" as const;
33
+ }
31
34
 
32
35
  log(message: string) {
33
36
  console.log(`[LOG]: ${message}`);
@@ -52,14 +55,16 @@ container.logger.log("Service is ready!");
52
55
 
53
56
  ### 3. Services with Dependencies
54
57
 
55
- To inject dependencies into a service, define its constructor to accept the container. You can use the `Di` type helper to specify which services are required.
58
+ To inject dependencies into a service, define its constructor to accept the container. You can use the `Di<T>` type helper to specify which services are required. It supports both a single service type or a tuple of multiple services.
56
59
 
57
60
  ```typescript
58
61
  import { Di, DiService } from 'di-sacala';
59
62
  import { LoggerService } from './LoggerService';
60
63
 
61
64
  export class UserService implements DiService<"user"> {
62
- name = "user" as const;
65
+ getServiceName() {
66
+ return "user" as const;
67
+ }
63
68
 
64
69
  // Use Di<ServiceType> or Di<[Service1, Service2]> for type-safe dependencies
65
70
  constructor(private di: Di<LoggerService>) {}
@@ -91,6 +96,59 @@ const appContainer = new DiContainer()
91
96
  .inject(MainApp);
92
97
  ```
93
98
 
99
+ ### 5. Lazy & Singleton
100
+
101
+ Services registered via `inject` are only instantiated when they are first accessed. Once created, the same instance is returned for all subsequent calls. This ensures efficiency and consistent state within the container.
102
+
103
+ ```typescript
104
+ const container = new DiContainer()
105
+ .inject(ExpensiveService);
106
+
107
+ // ExpensiveService is NOT instantiated yet
108
+
109
+ console.log("Container ready");
110
+
111
+ // ExpensiveService is instantiated NOW
112
+ container.expensive.doSomething();
113
+ ```
114
+
115
+ ### 6. Duplicate Service Name Protection
116
+
117
+ `di-sacala` prevents registering multiple services with the same name. This protection works at both compile-time and runtime:
118
+
119
+ - **Type-level Check**: If you try to `inject` a service with a name that already exists in the container, TypeScript will report an error, and the resulting type will be a string literal describing the error.
120
+ - **Runtime Check**: The `inject` and `injectContainer` methods will throw an `Error` if a duplicate key is detected.
121
+
122
+ ```typescript
123
+ const container = new DiContainer()
124
+ .inject(LoggerService);
125
+
126
+ // TypeScript Error: Type '"Duplicate service name: logger"' ...
127
+ // Runtime Error: Duplicate service name: logger
128
+ container.inject(AnotherLoggerService);
129
+ ```
130
+
131
+ ### 7. Reserved Field Names
132
+
133
+ Since `DiContainer` uses a fluent API, certain names are reserved for its internal methods and cannot be used as service names:
134
+
135
+ - `inject`
136
+ - `injectContainer`
137
+
138
+ Similar to duplicate names, attempting to use a reserved name will trigger both a **Type-level Check** and a **Runtime Check**.
139
+
140
+ ```typescript
141
+ class InjectService implements DiService<"inject"> {
142
+ getServiceName() { return "inject" as const; }
143
+ }
144
+
145
+ const container = new DiContainer();
146
+
147
+ // TypeScript Error: Type '"Reserved field name: inject"' ...
148
+ // Runtime Error: Reserved field name: inject
149
+ container.inject(InjectService);
150
+ ```
151
+
94
152
  ## Development
95
153
 
96
154
  ### Installation
@@ -3,7 +3,13 @@
3
3
  * The `name` property is used as the key when the service is injected into a DiContainer.
4
4
  */
5
5
  export interface DiService<Name extends string> {
6
- name: Name;
6
+ /**
7
+ * The name of the service.
8
+ * This is used as the key when the service is injected into a DiContainer.
9
+ *
10
+ * The method is called without an instance context, so it can be used as a static property.
11
+ */
12
+ getServiceName(this: null): Name;
7
13
  }
8
14
  /**
9
15
  * A recursive type transformation that converts a Service (or tuple of Services)
@@ -15,6 +21,11 @@ export interface DiService<Name extends string> {
15
21
  export type Di<S> = S extends [infer S1, ...infer Tail] ? Di<S1> & Di<Tail> : S extends [] ? unknown : S extends DiService<infer Name> ? {
16
22
  [Key in Name]: S;
17
23
  } : never;
24
+ type CheckReservedField<Name, T> = Name extends keyof DiContainer ? `Reserved field name: ${Name}` : T;
25
+ type Append<Container, Service extends DiService<string>> = Service extends DiService<infer Name> ? CheckReservedField<Name, Container extends {
26
+ [Key in Name]: unknown;
27
+ } ? `Duplicate service name: ${Name}` : Container & Di<Service>> : never;
28
+ type Merge<DI1, DI2> = Exclude<keyof DI1, "inject" | "injectContainer"> & Exclude<keyof DI2, "inject" | "injectContainer"> extends never ? DI1 & DI2 : `Containers have duplicated keys: ${(Exclude<keyof DI1, "inject" | "injectContainer"> & Exclude<keyof DI2, "inject" | "injectContainer">) & string}`;
18
29
  /**
19
30
  * DiContainer manages service instantiation and dependency resolution.
20
31
  * It uses a fluent interface to chain service registrations, dynamically
@@ -29,8 +40,9 @@ export declare class DiContainer {
29
40
  * @template S - The type of service being injected.
30
41
  * @param dependency - A constructor for the service, which receives the container as its only argument.
31
42
  * @returns The container instance, typed with the newly added service.
43
+ * @throws {Error} If a service with the same name is already registered.
32
44
  */
33
- inject<S extends DiService<string>>(dependency: new (dependencies: this) => S): this & Di<S>;
45
+ inject<S extends DiService<string>>(dependency: new (dependencies: this) => S): Append<this, S>;
34
46
  /**
35
47
  * Copies all service properties from another container into this one.
36
48
  * Useful for composing containers or providing shared dependencies.
@@ -38,7 +50,9 @@ export declare class DiContainer {
38
50
  * @template DC - The type of the other DiContainer.
39
51
  * @param other - The source container to copy services from.
40
52
  * @returns The current container instance, typed with the merged services.
53
+ * @throws {Error} If any service name from the other container already exists in this container.
41
54
  */
42
- injectContainer<DC extends DiContainer>(other: DC): this & DC;
55
+ injectContainer<DC extends DiContainer>(other: DC): Merge<this, DC>;
43
56
  }
57
+ export {};
44
58
  //# sourceMappingURL=di-sacala.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"di-sacala.d.ts","sourceRoot":"","sources":["../src/di-sacala.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,IAAI,SAAS,MAAM;IAC1C,IAAI,EAAE,IAAI,CAAC;CACd;AAED;;;;;;GAMG;AACH,MAAM,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,GAAG,MAAM,IAAI,CAAC,GACjD,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GACjB,CAAC,SAAS,EAAE,GACV,OAAO,GACP,CAAC,SAAS,SAAS,CAAC,MAAM,IAAI,CAAC,GAC7B;KAAG,GAAG,IAAI,IAAI,GAAG,CAAC;CAAE,GACpB,KAAK,CAAC;AAEhB;;;;GAIG;AACH,qBAAa,WAAW;;IAGpB;;;;;;;OAOG;IACH,MAAM,CAAC,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,KAAK,YAAY,EAAE,IAAI,KAAK,CAAC,GAAG,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC;IAM5F;;;;;;;OAOG;IACH,eAAe,CAAC,EAAE,SAAS,WAAW,EAAE,KAAK,EAAE,EAAE,GAAG,IAAI,GAAG,EAAE;CAQhE"}
1
+ {"version":3,"file":"di-sacala.d.ts","sourceRoot":"","sources":["../src/di-sacala.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,IAAI,SAAS,MAAM;IAC1C;;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CA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,MAAM,CAAC,EAC9B,UAAU,EAAE,KAAK,YAAY,EAAE,IAAI,KAAK,CAAC,GAC1C,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAyBlB;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,SAAS,WAAW,EAAE,KAAK,EAAE,EAAE,GAAG,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;CAoBtE"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- class i {
1
+ const s = /* @__PURE__ */ new Set(["inject", "injectContainer"]), r = (n, t) => Object.prototype.hasOwnProperty.call(n, t);
2
+ class o {
2
3
  constructor() {
3
4
  }
4
5
  /**
@@ -8,10 +9,20 @@ class i {
8
9
  * @template S - The type of service being injected.
9
10
  * @param dependency - A constructor for the service, which receives the container as its only argument.
10
11
  * @returns The container instance, typed with the newly added service.
12
+ * @throws {Error} If a service with the same name is already registered.
11
13
  */
12
- inject(n) {
13
- const t = new n(this);
14
- return this[t.name] = t, this;
14
+ inject(t) {
15
+ const e = t.prototype.getServiceName();
16
+ if (s.has(e))
17
+ throw new Error(`Reserved field name: ${e}`);
18
+ if (r(this, e))
19
+ throw new Error(`Duplicate service name: ${e}`);
20
+ let i;
21
+ return Object.defineProperty(this, e, {
22
+ enumerable: !0,
23
+ configurable: !1,
24
+ get: () => i ?? (i = new t(this))
25
+ }), this;
15
26
  }
16
27
  /**
17
28
  * Copies all service properties from another container into this one.
@@ -20,13 +31,21 @@ class i {
20
31
  * @template DC - The type of the other DiContainer.
21
32
  * @param other - The source container to copy services from.
22
33
  * @returns The current container instance, typed with the merged services.
34
+ * @throws {Error} If any service name from the other container already exists in this container.
23
35
  */
24
- injectContainer(n) {
25
- for (const t in n)
26
- Object.prototype.hasOwnProperty.call(n, t) && (this[t] = n[t]);
36
+ injectContainer(t) {
37
+ for (const e in t)
38
+ if (r(t, e) && r(this, e))
39
+ throw new Error(`Containers have duplicated keys: ${e}`);
40
+ for (const e in t)
41
+ r(t, e) && Object.defineProperty(this, e, {
42
+ enumerable: !0,
43
+ configurable: !1,
44
+ get: () => t[e]
45
+ });
27
46
  return this;
28
47
  }
29
48
  }
30
49
  export {
31
- i as DiContainer
50
+ o as DiContainer
32
51
  };
@@ -1 +1 @@
1
- (function(e,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(e=typeof globalThis<"u"?globalThis:e||self,n(e.DiSacala={}))})(this,(function(e){"use strict";class n{constructor(){}inject(i){const t=new i(this);return this[t.name]=t,this}injectContainer(i){for(const t in i)Object.prototype.hasOwnProperty.call(i,t)&&(this[t]=i[t]);return this}}e.DiContainer=n,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(n,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(n=typeof globalThis<"u"?globalThis:n||self,i(n.DiSacala={}))})(this,(function(n){"use strict";const i=new Set(["inject","injectContainer"]),r=(o,t)=>Object.prototype.hasOwnProperty.call(o,t);class a{constructor(){}inject(t){const e=t.prototype.getServiceName();if(i.has(e))throw new Error(`Reserved field name: ${e}`);if(r(this,e))throw new Error(`Duplicate service name: ${e}`);let s;return Object.defineProperty(this,e,{enumerable:!0,configurable:!1,get:()=>s??(s=new t(this))}),this}injectContainer(t){for(const e in t)if(r(t,e)&&r(this,e))throw new Error(`Containers have duplicated keys: ${e}`);for(const e in t)r(t,e)&&Object.defineProperty(this,e,{enumerable:!0,configurable:!1,get:()=>t[e]});return this}}n.DiContainer=a,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
1
  {
2
- "name": "di-sacala",
3
- "version": "0.1.0",
4
- "description": "Small type-safe dependency injection lib",
5
- "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
- "files": [
9
- "dist"
10
- ],
11
- "scripts": {
12
- "build": "vite build",
13
- "test": "vitest run",
14
- "test:watch": "vitest",
15
- "lint": "eslint src --ext .ts",
16
- "format": "prettier --write \"src/**/*.ts\"",
17
- "format:check": "prettier --check \"src/**/*.ts\""
18
- },
19
- "keywords": [],
20
- "author": {
21
- "name": "Andrei Monkin",
22
- "email": "monkin.andrey@gmail.com",
23
- "url": "https://github.com/monkin"
24
- },
25
- "license": "MIT",
26
- "devDependencies": {
27
- "@eslint/js": "^9.39.2",
28
- "@types/node": "^25.0.9",
29
- "@typescript-eslint/eslint-plugin": "^8.53.0",
30
- "@typescript-eslint/parser": "^8.53.0",
31
- "eslint": "^9.39.2",
32
- "prettier": "^3.8.0",
33
- "typescript": "^5.9.3",
34
- "vite": "^7.3.1",
35
- "vite-plugin-dts": "^4.5.4",
36
- "vitest": "^4.0.17"
37
- }
2
+ "name": "di-sacala",
3
+ "version": "0.1.2",
4
+ "description": "Small type-safe dependency injection lib",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "vite build",
13
+ "test": "vitest run",
14
+ "test:watch": "vitest",
15
+ "lint": "eslint src --ext .ts",
16
+ "format": "prettier --write \"src/**/*.ts\"",
17
+ "format:check": "prettier --check \"src/**/*.ts\""
18
+ },
19
+ "keywords": [],
20
+ "author": {
21
+ "name": "Andrei Monkin",
22
+ "email": "monkin.andrey@gmail.com",
23
+ "url": "https://github.com/monkin"
24
+ },
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "@eslint/js": "^9.39.2",
28
+ "@types/node": "^25.0.9",
29
+ "@typescript-eslint/eslint-plugin": "^8.53.0",
30
+ "@typescript-eslint/parser": "^8.53.0",
31
+ "eslint": "^9.39.2",
32
+ "prettier": "^3.8.0",
33
+ "typescript": "^5.9.3",
34
+ "vite": "^7.3.1",
35
+ "vite-plugin-dts": "^4.5.4",
36
+ "vitest": "^4.0.17"
37
+ }
38
38
  }