di-sacala 0.1.1 → 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 +28 -7
- package/dist/di-sacala.d.ts +4 -3
- package/dist/di-sacala.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
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
|
|
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 `
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
99
|
+
### 5. Lazy & Singleton
|
|
100
100
|
|
|
101
|
-
Services registered via `inject` are only instantiated when they are first accessed.
|
|
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
|
package/dist/di-sacala.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
|
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.
|
package/dist/di-sacala.d.ts.map
CHANGED
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"di-sacala.d.ts","sourceRoot":"","sources":["../src/di-sac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,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;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,5 +1,5 @@
|
|
|
1
|
-
const r = (n, t) => Object.prototype.hasOwnProperty.call(n, t);
|
|
2
|
-
class
|
|
1
|
+
const s = /* @__PURE__ */ new Set(["inject", "injectContainer"]), r = (n, t) => Object.prototype.hasOwnProperty.call(n, t);
|
|
2
|
+
class o {
|
|
3
3
|
constructor() {
|
|
4
4
|
}
|
|
5
5
|
/**
|
|
@@ -12,7 +12,9 @@ class s {
|
|
|
12
12
|
* @throws {Error} If a service with the same name is already registered.
|
|
13
13
|
*/
|
|
14
14
|
inject(t) {
|
|
15
|
-
const e = t.prototype.
|
|
15
|
+
const e = t.prototype.getServiceName();
|
|
16
|
+
if (s.has(e))
|
|
17
|
+
throw new Error(`Reserved field name: ${e}`);
|
|
16
18
|
if (r(this, e))
|
|
17
19
|
throw new Error(`Duplicate service name: ${e}`);
|
|
18
20
|
let i;
|
|
@@ -45,5 +47,5 @@ class s {
|
|
|
45
47
|
}
|
|
46
48
|
}
|
|
47
49
|
export {
|
|
48
|
-
|
|
50
|
+
o as DiContainer
|
|
49
51
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i
|
|
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"})}));
|