sandly 0.0.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/LICENSE +21 -0
- package/README.md +208 -0
- package/dist/index.d.ts +1255 -0
- package/dist/index.js +983 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Boris Rakovan
|
|
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,208 @@
|
|
|
1
|
+
# Sandly
|
|
2
|
+
|
|
3
|
+
**⚠️ This project is under heavy development and APIs may change.**
|
|
4
|
+
|
|
5
|
+
A dependency injection framework for TypeScript that emphasizes complete type safety, modular architecture, and scope management.
|
|
6
|
+
|
|
7
|
+
*Sandly* stands for **Services & Layers** - reflecting the framework's core concepts of organizing code into composable service layers.
|
|
8
|
+
|
|
9
|
+
## Key Features
|
|
10
|
+
|
|
11
|
+
### Complete Type Safety
|
|
12
|
+
|
|
13
|
+
- **Zero runtime errors**: All dependencies are validated at compile time
|
|
14
|
+
- **Automatic type inference**: Container knows exactly what services are available
|
|
15
|
+
- **Constructor-based DI**: Dependencies are inferred from class constructors
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { container, Tag } from 'sandly';
|
|
19
|
+
|
|
20
|
+
class DatabaseService extends Tag.Class('DatabaseService') {
|
|
21
|
+
query() {
|
|
22
|
+
return ['data'];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const c = container().register(DatabaseService, () => new DatabaseService());
|
|
27
|
+
|
|
28
|
+
// ✅ TypeScript knows DatabaseService is available
|
|
29
|
+
const db = await c.get(DatabaseService);
|
|
30
|
+
|
|
31
|
+
// ❌ Compile error - UserService not registered
|
|
32
|
+
const user = await c.get(UserService); // Type error
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Modular Architecture with Layers
|
|
36
|
+
|
|
37
|
+
Organize dependencies into composable layers that promote clean architecture:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// Infrastructure layer
|
|
41
|
+
const databaseLayer = layer<never, typeof DatabaseService>((container) =>
|
|
42
|
+
container.register(DatabaseService, () => new DatabaseService())
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// Service layer that depends on infrastructure
|
|
46
|
+
const userServiceLayer = layer<typeof DatabaseService, typeof UserService>(
|
|
47
|
+
(container) =>
|
|
48
|
+
container.register(
|
|
49
|
+
UserService,
|
|
50
|
+
async (c) => new UserService(await c.get(DatabaseService))
|
|
51
|
+
)
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// Compose layers
|
|
55
|
+
const appLayer = databaseLayer().provide(userServiceLayer());
|
|
56
|
+
|
|
57
|
+
const app = appLayer.register(container());
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Advanced Scope Management
|
|
61
|
+
|
|
62
|
+
Built-in support for request/runtime scopes with automatic cleanup:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Runtime-scoped dependencies (shared across requests)
|
|
66
|
+
const runtime = scopedContainer('runtime').register(DatabaseService, {
|
|
67
|
+
factory: () => new DatabaseService(),
|
|
68
|
+
finalizer: (db) => db.disconnect(),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Lambda handler example
|
|
72
|
+
export const handler = async (event, context) => {
|
|
73
|
+
// Create request scope for this invocation
|
|
74
|
+
const requestContainer = runtime.child('request').register(
|
|
75
|
+
UserService,
|
|
76
|
+
async (c) => new UserService(await c.get(DatabaseService)) // Uses runtime DB
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const userService = await requestContainer.get(UserService);
|
|
81
|
+
return await userService.handleRequest(event);
|
|
82
|
+
} finally {
|
|
83
|
+
await requestContainer.destroy(); // Cleanup request scope
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Usage Examples
|
|
89
|
+
|
|
90
|
+
### Basic Container Usage
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { container, Tag } from 'sandly';
|
|
94
|
+
|
|
95
|
+
class EmailService extends Tag.Class('EmailService') {
|
|
96
|
+
sendEmail(to: string, subject: string) {
|
|
97
|
+
return { messageId: 'msg-123' };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
class UserService extends Tag.Class('UserService') {
|
|
102
|
+
constructor(private emailService: EmailService) {
|
|
103
|
+
super();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async createUser(email: string) {
|
|
107
|
+
// Create user logic
|
|
108
|
+
await this.emailService.sendEmail(email, 'Welcome!');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const app = container()
|
|
113
|
+
.register(EmailService, () => new EmailService())
|
|
114
|
+
.register(
|
|
115
|
+
UserService,
|
|
116
|
+
async (c) => new UserService(await c.get(EmailService))
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const userService = await app.get(UserService);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Service Pattern with Auto-Composition
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { service, Layer } from 'sandly';
|
|
126
|
+
|
|
127
|
+
const emailService = service(EmailService, () => new EmailService());
|
|
128
|
+
const userService = service(
|
|
129
|
+
UserService,
|
|
130
|
+
async (container) => new UserService(await container.get(EmailService))
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// Automatic dependency resolution
|
|
134
|
+
const app = emailService().provide(userService()).register(container());
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Value Tags for Configuration
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const ApiKeyTag = Tag.of('apiKey')<string>();
|
|
141
|
+
const ConfigTag = Tag.of('config')<{ dbUrl: string }>();
|
|
142
|
+
|
|
143
|
+
class DatabaseService extends Tag.Class('DatabaseService') {
|
|
144
|
+
constructor(
|
|
145
|
+
private config: Inject<typeof ConfigTag>,
|
|
146
|
+
private apiKey: Inject<typeof ApiKeyTag>
|
|
147
|
+
) {
|
|
148
|
+
super();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const app = container()
|
|
153
|
+
.register(ApiKeyTag, () => process.env.API_KEY!)
|
|
154
|
+
.register(ConfigTag, () => ({ dbUrl: 'postgresql://localhost' }))
|
|
155
|
+
.register(
|
|
156
|
+
DatabaseService,
|
|
157
|
+
async (c) =>
|
|
158
|
+
new DatabaseService(await c.get(ConfigTag), await c.get(ApiKeyTag))
|
|
159
|
+
);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Complex Layer Composition
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
// Infrastructure layers
|
|
166
|
+
const databaseLayer = layer<never, typeof DatabaseService>((container) =>
|
|
167
|
+
container.register(DatabaseService, () => new DatabaseService())
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const cacheLayer = layer<never, typeof CacheService>((container) =>
|
|
171
|
+
container.register(CacheService, () => new CacheService())
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
// Business logic layer
|
|
175
|
+
const userServiceLayer = layer<
|
|
176
|
+
typeof DatabaseService | typeof CacheService,
|
|
177
|
+
typeof UserService
|
|
178
|
+
>((container) =>
|
|
179
|
+
container.register(
|
|
180
|
+
UserService,
|
|
181
|
+
async (c) =>
|
|
182
|
+
new UserService(
|
|
183
|
+
await c.get(DatabaseService),
|
|
184
|
+
await c.get(CacheService)
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// Compose everything
|
|
190
|
+
const fullApplication = Layer.merge(databaseLayer(), cacheLayer()).provide(
|
|
191
|
+
userServiceLayer()
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
const app = fullApplication.register(container());
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Benefits
|
|
198
|
+
|
|
199
|
+
- **Type Safety**: Eliminates entire classes of runtime errors
|
|
200
|
+
- **Modular Design**: Layer system naturally guides you toward clean architecture
|
|
201
|
+
- **Performance**: Zero runtime overhead for dependency resolution
|
|
202
|
+
- **Flexibility**: Powerful scope management for any use case (web servers, serverless, etc.)
|
|
203
|
+
- **Developer Experience**: IntelliSense works perfectly, no magic strings
|
|
204
|
+
- **Testing**: Easy to mock dependencies and create isolated test containers
|
|
205
|
+
|
|
206
|
+
## License
|
|
207
|
+
|
|
208
|
+
MIT
|