di-sacala 0.1.1 → 0.1.3

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,7 +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
+ - **Lazy & Singleton**: Services are instantiated only on demand (when first accessed) and reused for subsequent accesses.
13
13
  - **Zero Runtime Dependencies**: Extremely lightweight.
14
14
 
15
15
  ## Installation
@@ -22,13 +22,13 @@ npm install di-sacala
22
22
 
23
23
  ### 1. Defining a Service
24
24
 
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
+ 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.
26
26
 
27
27
  ```typescript
28
28
  import { DiService } from 'di-sacala';
29
29
 
30
30
  export class LoggerService implements DiService<"logger"> {
31
- getName() {
31
+ getServiceName() {
32
32
  return "logger" as const;
33
33
  }
34
34
 
@@ -55,14 +55,14 @@ container.logger.log("Service is ready!");
55
55
 
56
56
  ### 3. Services with Dependencies
57
57
 
58
- 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.
59
59
 
60
60
  ```typescript
61
61
  import { Di, DiService } from 'di-sacala';
62
62
  import { LoggerService } from './LoggerService';
63
63
 
64
64
  export class UserService implements DiService<"user"> {
65
- getName() {
65
+ getServiceName() {
66
66
  return "user" as const;
67
67
  }
68
68
 
@@ -96,9 +96,9 @@ const appContainer = new DiContainer()
96
96
  .inject(MainApp);
97
97
  ```
98
98
 
99
- ### 5. Lazy Construction
99
+ ### 5. Lazy & Singleton
100
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.
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
102
 
103
103
  ```typescript
104
104
  const container = new DiContainer()
@@ -128,6 +128,27 @@ const container = new DiContainer()
128
128
  container.inject(AnotherLoggerService);
129
129
  ```
130
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
+
131
152
  ## Development
132
153
 
133
154
  ### Installation
@@ -9,7 +9,7 @@ export interface DiService<Name extends string> {
9
9
  *
10
10
  * The method is called without an instance context, so it can be used as a static property.
11
11
  */
12
- getName(this: null): Name;
12
+ getServiceName(this: null): Name;
13
13
  }
14
14
  /**
15
15
  * A recursive type transformation that converts a Service (or tuple of Services)
@@ -21,9 +21,10 @@ export interface DiService<Name extends string> {
21
21
  export type Di<S> = S extends [infer S1, ...infer Tail] ? Di<S1> & Di<Tail> : S extends [] ? unknown : S extends DiService<infer Name> ? {
22
22
  [Key in Name]: S;
23
23
  } : never;
24
- type Append<Container, Service extends DiService<string>> = Service extends DiService<infer Name> ? Container extends {
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 {
25
26
  [Key in Name]: unknown;
26
- } ? `Duplicate service name: ${Name}` : Container & Di<Service> : never;
27
+ } ? `Duplicate service name: ${Name}` : Container & Di<Service>> : never;
27
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}`;
28
29
  /**
29
30
  * DiContainer manages service instantiation and dependency resolution.
@@ -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;;;;;OAKG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;CAC7B;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,KAAK,MAAM,CAAC,SAAS,EAAE,OAAO,SAAS,SAAS,CAAC,MAAM,CAAC,IACpD,OAAO,SAAS,SAAS,CAAC,MAAM,IAAI,CAAC,GAC/B,SAAS,SAAS;KAAG,GAAG,IAAI,IAAI,GAAG,OAAO;CAAE,GACxC,2BAA2B,IAAI,EAAE,GACjC,SAAS,GAAG,EAAE,CAAC,OAAO,CAAC,GAC3B,KAAK,CAAC;AAEhB,KAAK,KAAK,CAAC,GAAG,EAAE,GAAG,IAAI,OAAO,C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(n, t) => Object.prototype.hasOwnProperty.call(n, t);
2
+ function c(n, t) {
3
+ for (const e in n) i(n, e) && t(e);
4
+ }
5
+ const r = (n) => {
6
+ throw new Error(n);
7
+ };
8
+ class a {
3
9
  constructor() {
4
10
  }
5
- /**
6
- * Registers a new service by instantiating it with the current container instance.
7
- * The service is then attached to the container using its `name` property.
8
- *
9
- * @template S - The type of service being injected.
10
- * @param dependency - A constructor for the service, which receives the container as its only argument.
11
- * @returns The container instance, typed with the newly added service.
12
- * @throws {Error} If a service with the same name is already registered.
13
- */
14
11
  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;
12
+ const e = t.prototype.getServiceName.call(null);
13
+ s.has(e) && r(`Reserved field name: ${e}`), i(this, e) && r(`Duplicate service name: ${e}`);
14
+ let o;
15
+ return Object.defineProperty(this, e, { enumerable: !0, get: () => o ?? (o = new t(this)) }), this;
24
16
  }
25
- /**
26
- * Copies all service properties from another container into this one.
27
- * Useful for composing containers or providing shared dependencies.
28
- *
29
- * @template DC - The type of the other DiContainer.
30
- * @param other - The source container to copy services from.
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.
33
- */
34
17
  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
- });
44
- return this;
18
+ return c(t, (e) => {
19
+ i(this, e) && r(`Containers have duplicated keys: ${String(e)}`);
20
+ }), c(t, (e) => {
21
+ Object.defineProperty(this, e, { enumerable: !0, get: () => t[e] });
22
+ }), this;
45
23
  }
46
24
  }
47
25
  export {
48
- s as DiContainer
26
+ a as DiContainer
49
27
  };
@@ -1 +1 @@
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"})}));
1
+ (function(o,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(o=typeof globalThis<"u"?globalThis:o||self,i(o.DiSacala={}))})(this,(function(o){"use strict";const i=new Set(["inject","injectContainer"]),r=(n,t)=>Object.prototype.hasOwnProperty.call(n,t);function c(n,t){for(const e in n)r(n,e)&&t(e)}const s=n=>{throw new Error(n)};class l{constructor(){}inject(t){const e=t.prototype.getServiceName.call(null);i.has(e)&&s(`Reserved field name: ${e}`),r(this,e)&&s(`Duplicate service name: ${e}`);let a;return Object.defineProperty(this,e,{enumerable:!0,get:()=>a??(a=new t(this))}),this}injectContainer(t){return c(t,e=>{r(this,e)&&s(`Containers have duplicated keys: ${String(e)}`)}),c(t,e=>{Object.defineProperty(this,e,{enumerable:!0,get:()=>t[e]})}),this}}o.DiContainer=l,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "di-sacala",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Small type-safe dependency injection lib",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
7
+ "types": "./dist/di-sacala.d.ts",
8
8
  "files": [
9
9
  "dist"
10
10
  ],