di-sacala 0.1.0 → 0.1.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 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 Construction**: Services are instantiated only on demand (when first accessed).
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 `getName()` 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
+ getName() {
32
+ return "logger" as const;
33
+ }
31
34
 
32
35
  log(message: string) {
33
36
  console.log(`[LOG]: ${message}`);
@@ -59,7 +62,9 @@ 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
+ getName() {
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,38 @@ const appContainer = new DiContainer()
91
96
  .inject(MainApp);
92
97
  ```
93
98
 
99
+ ### 5. Lazy Construction
100
+
101
+ Services registered via `inject` are only instantiated when they are first accessed. This allows for efficient container initialization and avoids unnecessary work for services that might not be used in certain execution paths.
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
+
94
131
  ## Development
95
132
 
96
133
  ### 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
+ getName(this: null): Name;
7
13
  }
8
14
  /**
9
15
  * A recursive type transformation that converts a Service (or tuple of Services)
@@ -15,6 +21,10 @@ 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 Append<Container, Service extends DiService<string>> = Service extends DiService<infer Name> ? Container extends {
25
+ [Key in Name]: unknown;
26
+ } ? `Duplicate service name: ${Name}` : Container & Di<Service> : never;
27
+ 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
28
  /**
19
29
  * DiContainer manages service instantiation and dependency resolution.
20
30
  * It uses a fluent interface to chain service registrations, dynamically
@@ -29,8 +39,9 @@ export declare class DiContainer {
29
39
  * @template S - The type of service being injected.
30
40
  * @param dependency - A constructor for the service, which receives the container as its only argument.
31
41
  * @returns The container instance, typed with the newly added service.
42
+ * @throws {Error} If a service with the same name is already registered.
32
43
  */
33
- inject<S extends DiService<string>>(dependency: new (dependencies: this) => S): this & Di<S>;
44
+ inject<S extends DiService<string>>(dependency: new (dependencies: this) => S): Append<this, S>;
34
45
  /**
35
46
  * Copies all service properties from another container into this one.
36
47
  * Useful for composing containers or providing shared dependencies.
@@ -38,7 +49,9 @@ export declare class DiContainer {
38
49
  * @template DC - The type of the other DiContainer.
39
50
  * @param other - The source container to copy services from.
40
51
  * @returns The current container instance, typed with the merged services.
52
+ * @throws {Error} If any service name from the other container already exists in this container.
41
53
  */
42
- injectContainer<DC extends DiContainer>(other: DC): this & DC;
54
+ injectContainer<DC extends DiContainer>(other: DC): Merge<this, DC>;
43
55
  }
56
+ export {};
44
57
  //# 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,C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AiB,CAAC,GACnE,OAAO,CAAC,MAAM,GAAG,EAAE,QAAQ,GAAG,iBAAiB,CAAC,SAAS,KAAK,GAC5D,GAAG,GAAG,GAAG,GACT,oCAAoC,CAAC,OAAO,CACxC,MAAM,GAAG,EACT,QAAQ,GAAG,iBAAiB,CAC/B,GACG,OAAO,CAAC,MAAM,GAAG,EAAE,QAAQ,GAAG,iBAAiB,CAAC,CAAC,GACjD,MAAM,EAAE,CAAC;AAKnB;;;;GAIG;AACH,qBAAa,WAAW;;IAGpB;;;;;;;;OAQG;IACH,MAAM,CAAC,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,EAC9B,UAAU,EAAE,KAAK,YAAY,EAAE,IAAI,KAAK,CAAC,GAC1C,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAoBlB;;;;;;;;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 r = (n, t) => Object.prototype.hasOwnProperty.call(n, t);
2
+ class s {
2
3
  constructor() {
3
4
  }
4
5
  /**
@@ -8,10 +9,18 @@ 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.getName();
16
+ if (r(this, e))
17
+ throw new Error(`Duplicate service name: ${e}`);
18
+ let i;
19
+ return Object.defineProperty(this, e, {
20
+ enumerable: !0,
21
+ configurable: !1,
22
+ get: () => i ?? (i = new t(this))
23
+ }), this;
15
24
  }
16
25
  /**
17
26
  * Copies all service properties from another container into this one.
@@ -20,13 +29,21 @@ class i {
20
29
  * @template DC - The type of the other DiContainer.
21
30
  * @param other - The source container to copy services from.
22
31
  * @returns The current container instance, typed with the merged services.
32
+ * @throws {Error} If any service name from the other container already exists in this container.
23
33
  */
24
- injectContainer(n) {
25
- for (const t in n)
26
- Object.prototype.hasOwnProperty.call(n, t) && (this[t] = n[t]);
34
+ injectContainer(t) {
35
+ for (const e in t)
36
+ if (r(t, e) && r(this, e))
37
+ throw new Error(`Containers have duplicated keys: ${e}`);
38
+ for (const e in t)
39
+ r(t, e) && Object.defineProperty(this, e, {
40
+ enumerable: !0,
41
+ configurable: !1,
42
+ get: () => t[e]
43
+ });
27
44
  return this;
28
45
  }
29
46
  }
30
47
  export {
31
- i as DiContainer
48
+ s as DiContainer
32
49
  };
@@ -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(i,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(i=typeof globalThis<"u"?globalThis:i||self,n(i.DiSacala={}))})(this,(function(i){"use strict";const n=(r,t)=>Object.prototype.hasOwnProperty.call(r,t);class s{constructor(){}inject(t){const e=t.prototype.getName();if(n(this,e))throw new Error(`Duplicate service name: ${e}`);let o;return Object.defineProperty(this,e,{enumerable:!0,configurable:!1,get:()=>o??(o=new t(this))}),this}injectContainer(t){for(const e in t)if(n(t,e)&&n(this,e))throw new Error(`Containers have duplicated keys: ${e}`);for(const e in t)n(t,e)&&Object.defineProperty(this,e,{enumerable:!0,configurable:!1,get:()=>t[e]});return this}}i.DiContainer=s,Object.defineProperty(i,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.1",
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
  }