sandly 0.5.3 → 1.0.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 +65 -14
- package/dist/index.d.ts +477 -400
- package/dist/index.js +245 -146
- package/package.json +68 -74
package/README.md
CHANGED
|
@@ -204,12 +204,12 @@ const container = Container.empty()
|
|
|
204
204
|
For large applications, organizing dependencies into layers helps manage complexity and makes dependencies composable.
|
|
205
205
|
|
|
206
206
|
```typescript
|
|
207
|
-
import { layer, service,
|
|
207
|
+
import { layer, service, constant, Tag, Container } from 'sandly';
|
|
208
208
|
|
|
209
209
|
// Configuration layer - provides primitive values
|
|
210
210
|
const Config = Tag.of('Config')<{ databaseUrl: string }>();
|
|
211
211
|
|
|
212
|
-
const configLayer =
|
|
212
|
+
const configLayer = constant(Config, { databaseUrl: process.env.DATABASE_URL! });
|
|
213
213
|
|
|
214
214
|
// Database layer - depends on config
|
|
215
215
|
class Database extends Tag.Service('Database') {
|
|
@@ -265,7 +265,7 @@ Don't worry if you don't understand everything yet - keep reading and you'll lea
|
|
|
265
265
|
Any value can be a dependency, not just class instances:
|
|
266
266
|
|
|
267
267
|
```typescript
|
|
268
|
-
import { Tag,
|
|
268
|
+
import { Tag, constant, Container } from 'sandly';
|
|
269
269
|
|
|
270
270
|
// Primitive values
|
|
271
271
|
const PortTag = Tag.of('Port')<number>();
|
|
@@ -1319,7 +1319,7 @@ export const databaseLayer = autoService(Database, [ConfigTag]);
|
|
|
1319
1319
|
export const loggerLayer = autoService(Logger, []);
|
|
1320
1320
|
|
|
1321
1321
|
// config.ts
|
|
1322
|
-
export const configLayer =
|
|
1322
|
+
export const configLayer = constant(ConfigTag, loadConfig());
|
|
1323
1323
|
|
|
1324
1324
|
// Infrastructure layer combining all base services
|
|
1325
1325
|
export const infraLayer = Layer.mergeAll(
|
|
@@ -1629,24 +1629,75 @@ const databaseLayer = autoService(Database, {
|
|
|
1629
1629
|
});
|
|
1630
1630
|
```
|
|
1631
1631
|
|
|
1632
|
-
####
|
|
1632
|
+
#### dependency() - Generic Dependency Layer
|
|
1633
1633
|
|
|
1634
|
-
The `
|
|
1634
|
+
The `dependency()` function creates a layer for any tag type (ServiceTag or ValueTag) with fully inferred types. Unlike `service()` and `autoService()`, it doesn't require extending `Tag.Service()`:
|
|
1635
1635
|
|
|
1636
1636
|
```typescript
|
|
1637
|
-
import {
|
|
1637
|
+
import { dependency, Tag } from 'sandly';
|
|
1638
|
+
|
|
1639
|
+
// Simple dependency without requirements
|
|
1640
|
+
const Config = Tag.of('Config')<{ apiUrl: string }>();
|
|
1641
|
+
|
|
1642
|
+
const configDep = dependency(Config, () => ({
|
|
1643
|
+
apiUrl: process.env.API_URL!,
|
|
1644
|
+
}));
|
|
1645
|
+
|
|
1646
|
+
// Dependency with requirements - pass them as the last argument
|
|
1647
|
+
const Database = Tag.of('Database')<DatabaseConnection>();
|
|
1648
|
+
|
|
1649
|
+
const databaseDep = dependency(
|
|
1650
|
+
Database,
|
|
1651
|
+
async (ctx) => {
|
|
1652
|
+
const config = await ctx.resolve(Config);
|
|
1653
|
+
return createConnection(config.apiUrl);
|
|
1654
|
+
},
|
|
1655
|
+
[Config] // Requirements array - enables type inference
|
|
1656
|
+
);
|
|
1657
|
+
```
|
|
1658
|
+
|
|
1659
|
+
With lifecycle (create + cleanup):
|
|
1660
|
+
|
|
1661
|
+
```typescript
|
|
1662
|
+
const databaseDep = dependency(
|
|
1663
|
+
Database,
|
|
1664
|
+
{
|
|
1665
|
+
create: async (ctx) => {
|
|
1666
|
+
const config = await ctx.resolve(Config);
|
|
1667
|
+
return await createConnection(config.apiUrl);
|
|
1668
|
+
},
|
|
1669
|
+
cleanup: async (db) => {
|
|
1670
|
+
await db.disconnect();
|
|
1671
|
+
},
|
|
1672
|
+
},
|
|
1673
|
+
[Config]
|
|
1674
|
+
);
|
|
1675
|
+
```
|
|
1676
|
+
|
|
1677
|
+
The `dependency()` function is useful when:
|
|
1678
|
+
|
|
1679
|
+
- Working with ValueTags that need dependencies
|
|
1680
|
+
- Using third-party classes that can't extend `Tag.Service()`
|
|
1681
|
+
- Wanting cleaner syntax than `layer()` without explicit type parameters
|
|
1682
|
+
|
|
1683
|
+
#### constant() - Constant Value Layer Helper
|
|
1684
|
+
|
|
1685
|
+
The `constant()` function creates a layer that provides a constant value:
|
|
1686
|
+
|
|
1687
|
+
```typescript
|
|
1688
|
+
import { constant, Tag } from 'sandly';
|
|
1638
1689
|
|
|
1639
1690
|
const ApiKeyTag = Tag.of('ApiKey')<string>();
|
|
1640
1691
|
const PortTag = Tag.of('Port')<number>();
|
|
1641
1692
|
|
|
1642
|
-
const apiKeyLayer =
|
|
1643
|
-
const portLayer =
|
|
1693
|
+
const apiKeyLayer = constant(ApiKeyTag, 'my-secret-key');
|
|
1694
|
+
const portLayer = constant(PortTag, 3000);
|
|
1644
1695
|
|
|
1645
|
-
// Combine
|
|
1696
|
+
// Combine constant layers
|
|
1646
1697
|
const configLayer = Layer.mergeAll(
|
|
1647
1698
|
apiKeyLayer,
|
|
1648
1699
|
portLayer,
|
|
1649
|
-
|
|
1700
|
+
constant(Tag.of('Debug')<boolean>(), true)
|
|
1650
1701
|
);
|
|
1651
1702
|
```
|
|
1652
1703
|
|
|
@@ -1751,7 +1802,7 @@ Use when you want to expose multiple layers' services:
|
|
|
1751
1802
|
```typescript
|
|
1752
1803
|
const AppConfigTag = Tag.of('AppConfig')<AppConfig>();
|
|
1753
1804
|
|
|
1754
|
-
const configLayer =
|
|
1805
|
+
const configLayer = constant(AppConfigTag, loadConfig());
|
|
1755
1806
|
const databaseLayer = layer<typeof AppConfigTag, typeof Database>((container) =>
|
|
1756
1807
|
container.register(
|
|
1757
1808
|
Database,
|
|
@@ -1826,8 +1877,8 @@ Merges multiple layers at once:
|
|
|
1826
1877
|
|
|
1827
1878
|
```typescript
|
|
1828
1879
|
const infraLayer = Layer.mergeAll(
|
|
1829
|
-
|
|
1830
|
-
|
|
1880
|
+
constant(ApiKeyTag, 'key'),
|
|
1881
|
+
constant(PortTag, 3000),
|
|
1831
1882
|
databaseLayer,
|
|
1832
1883
|
loggerLayer
|
|
1833
1884
|
);
|