di-sacala 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/LICENSE +21 -0
- package/README.md +126 -0
- package/dist/di-sacala.d.ts +44 -0
- package/dist/di-sacala.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.umd.cjs +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrey Monkin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# di-sacala
|
|
2
|
+
|
|
3
|
+
[](https://github.com/monkin/di-sacala/actions/workflows/test.yml)
|
|
4
|
+
|
|
5
|
+
`di-sacala` is a lightweight, type-safe dependency injection container for TypeScript. It leverages TypeScript's advanced type system to provide a fluent API for service registration and resolution with full type safety and autocompletion.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Full Type Safety**: Get autocompletion and type checks for all your injected services.
|
|
10
|
+
- **Fluent API**: Chainable service registration makes it easy to compose your container.
|
|
11
|
+
- **Container Composition**: Merge multiple containers together to share dependencies across different parts of your application.
|
|
12
|
+
- **Zero Runtime Dependencies**: Extremely lightweight.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install di-sacala
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### 1. Defining a Service
|
|
23
|
+
|
|
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
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { DiService } from 'di-sacala';
|
|
28
|
+
|
|
29
|
+
export class LoggerService implements DiService<"logger"> {
|
|
30
|
+
name = "logger" as const;
|
|
31
|
+
|
|
32
|
+
log(message: string) {
|
|
33
|
+
console.log(`[LOG]: ${message}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. Basic Injection
|
|
39
|
+
|
|
40
|
+
Use `DiContainer` to register and resolve your services.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { DiContainer } from 'di-sacala';
|
|
44
|
+
import { LoggerService } from './LoggerService';
|
|
45
|
+
|
|
46
|
+
const container = new DiContainer()
|
|
47
|
+
.inject(LoggerService);
|
|
48
|
+
|
|
49
|
+
// Access the service directly on the container
|
|
50
|
+
container.logger.log("Service is ready!");
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 3. Services with Dependencies
|
|
54
|
+
|
|
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.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Di, DiService } from 'di-sacala';
|
|
59
|
+
import { LoggerService } from './LoggerService';
|
|
60
|
+
|
|
61
|
+
export class UserService implements DiService<"user"> {
|
|
62
|
+
name = "user" as const;
|
|
63
|
+
|
|
64
|
+
// Use Di<ServiceType> or Di<[Service1, Service2]> for type-safe dependencies
|
|
65
|
+
constructor(private di: Di<LoggerService>) {}
|
|
66
|
+
|
|
67
|
+
getUser(id: string) {
|
|
68
|
+
this.di.logger.log(`Fetching user: ${id}`);
|
|
69
|
+
return { id, name: "User " + id };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const container = new DiContainer()
|
|
74
|
+
.inject(LoggerService)
|
|
75
|
+
.inject(UserService);
|
|
76
|
+
|
|
77
|
+
container.user.getUser("42");
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 4. Merging Containers
|
|
81
|
+
|
|
82
|
+
You can create specialized containers and merge them into a main container using `injectContainer`.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
const authContainer = new DiContainer().inject(AuthService);
|
|
86
|
+
const apiContainer = new DiContainer().inject(ApiService);
|
|
87
|
+
|
|
88
|
+
const appContainer = new DiContainer()
|
|
89
|
+
.injectContainer(authContainer)
|
|
90
|
+
.injectContainer(apiContainer)
|
|
91
|
+
.inject(MainApp);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Development
|
|
95
|
+
|
|
96
|
+
### Installation
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
npm install
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Build
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npm run build
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Test
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
npm test
|
|
112
|
+
npm run test:watch # Watch mode
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Linting & Formatting
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
npm run lint # Run ESLint
|
|
119
|
+
npm run format # Format code with Prettier
|
|
120
|
+
npm run format:check # Check code formatting
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
|
126
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base interface for services in the DI system.
|
|
3
|
+
* The `name` property is used as the key when the service is injected into a DiContainer.
|
|
4
|
+
*/
|
|
5
|
+
export interface DiService<Name extends string> {
|
|
6
|
+
name: Name;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A recursive type transformation that converts a Service (or tuple of Services)
|
|
10
|
+
* into a mapped object type.
|
|
11
|
+
*
|
|
12
|
+
* Example: Service<"logger"> -> { logger: Service<"logger"> }
|
|
13
|
+
* Example: [Service<"a">, Service<"b">] -> { a: Service<"a"> } & { b: Service<"b"> }
|
|
14
|
+
*/
|
|
15
|
+
export type Di<S> = S extends [infer S1, ...infer Tail] ? Di<S1> & Di<Tail> : S extends [] ? unknown : S extends DiService<infer Name> ? {
|
|
16
|
+
[Key in Name]: S;
|
|
17
|
+
} : never;
|
|
18
|
+
/**
|
|
19
|
+
* DiContainer manages service instantiation and dependency resolution.
|
|
20
|
+
* It uses a fluent interface to chain service registrations, dynamically
|
|
21
|
+
* extending its own type with each injected service.
|
|
22
|
+
*/
|
|
23
|
+
export declare class DiContainer {
|
|
24
|
+
constructor();
|
|
25
|
+
/**
|
|
26
|
+
* Registers a new service by instantiating it with the current container instance.
|
|
27
|
+
* The service is then attached to the container using its `name` property.
|
|
28
|
+
*
|
|
29
|
+
* @template S - The type of service being injected.
|
|
30
|
+
* @param dependency - A constructor for the service, which receives the container as its only argument.
|
|
31
|
+
* @returns The container instance, typed with the newly added service.
|
|
32
|
+
*/
|
|
33
|
+
inject<S extends DiService<string>>(dependency: new (dependencies: this) => S): this & Di<S>;
|
|
34
|
+
/**
|
|
35
|
+
* Copies all service properties from another container into this one.
|
|
36
|
+
* Useful for composing containers or providing shared dependencies.
|
|
37
|
+
*
|
|
38
|
+
* @template DC - The type of the other DiContainer.
|
|
39
|
+
* @param other - The source container to copy services from.
|
|
40
|
+
* @returns The current container instance, typed with the merged services.
|
|
41
|
+
*/
|
|
42
|
+
injectContainer<DC extends DiContainer>(other: DC): this & DC;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=di-sacala.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class i {
|
|
2
|
+
constructor() {
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Registers a new service by instantiating it with the current container instance.
|
|
6
|
+
* The service is then attached to the container using its `name` property.
|
|
7
|
+
*
|
|
8
|
+
* @template S - The type of service being injected.
|
|
9
|
+
* @param dependency - A constructor for the service, which receives the container as its only argument.
|
|
10
|
+
* @returns The container instance, typed with the newly added service.
|
|
11
|
+
*/
|
|
12
|
+
inject(n) {
|
|
13
|
+
const t = new n(this);
|
|
14
|
+
return this[t.name] = t, this;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Copies all service properties from another container into this one.
|
|
18
|
+
* Useful for composing containers or providing shared dependencies.
|
|
19
|
+
*
|
|
20
|
+
* @template DC - The type of the other DiContainer.
|
|
21
|
+
* @param other - The source container to copy services from.
|
|
22
|
+
* @returns The current container instance, typed with the merged services.
|
|
23
|
+
*/
|
|
24
|
+
injectContainer(n) {
|
|
25
|
+
for (const t in n)
|
|
26
|
+
Object.prototype.hasOwnProperty.call(n, t) && (this[t] = n[t]);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
i as DiContainer
|
|
32
|
+
};
|
|
@@ -0,0 +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"})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
}
|
|
38
|
+
}
|