@punks/cli 2.6.1 → 3.0.0-beta.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/AGENTS.md +1 -1
- package/README.md +42 -24
- package/dist/BASELINE_CHANGELOG.md +57 -0
- package/dist/CHANGELOG.md +80 -0
- package/dist/data/scripts/harness-projection/adapters/claude.d.mts +3 -0
- package/dist/data/scripts/harness-projection/adapters/claude.mjs +124 -0
- package/dist/data/scripts/harness-projection/adapters/codex.d.mts +3 -0
- package/dist/data/scripts/harness-projection/adapters/codex.mjs +137 -0
- package/dist/data/scripts/harness-projection/adapters/cursor.d.mts +5 -0
- package/dist/data/scripts/harness-projection/adapters/cursor.mjs +106 -0
- package/dist/data/scripts/harness-projection/adapters/opencode.d.mts +5 -0
- package/dist/data/scripts/harness-projection/adapters/opencode.mjs +100 -0
- package/dist/data/scripts/harness-projection/apply.d.mts +12 -0
- package/dist/data/scripts/harness-projection/apply.mjs +96 -0
- package/dist/data/scripts/harness-projection/core.d.mts +8 -0
- package/dist/data/scripts/harness-projection/core.mjs +239 -0
- package/dist/data/scripts/harness-projection/test-fixtures/application-context-plan.json +27 -0
- package/dist/data/scripts/harness-projection/test-fixtures/claude/actions.json +109 -0
- package/dist/data/scripts/harness-projection/test-fixtures/claude/context-plan.json +150 -0
- package/dist/data/scripts/harness-projection/test-fixtures/claude/matrix.json +23 -0
- package/dist/data/scripts/harness-projection/test-fixtures/claude/traces.json +15 -0
- package/dist/data/scripts/harness-projection/test-fixtures/codex/actions.json +124 -0
- package/dist/data/scripts/harness-projection/test-fixtures/codex/context-plan.json +150 -0
- package/dist/data/scripts/harness-projection/test-fixtures/codex/matrix.json +23 -0
- package/dist/data/scripts/harness-projection/test-fixtures/codex/traces.json +15 -0
- package/dist/data/scripts/harness-projection/test-fixtures/cursor/compiler-input.json +93 -0
- package/dist/data/scripts/harness-projection/test-fixtures/cursor/context-plan.json +108 -0
- package/dist/data/scripts/harness-projection/test-fixtures/cursor/matrix.json +19 -0
- package/dist/data/scripts/harness-projection/test-fixtures/cursor/projection.json +54 -0
- package/dist/data/scripts/harness-projection/test-fixtures/mixed-context-plan.json +35 -0
- package/dist/data/scripts/harness-projection/test-fixtures/opencode/compiler-input.json +93 -0
- package/dist/data/scripts/harness-projection/test-fixtures/opencode/context-plan.json +108 -0
- package/dist/data/scripts/harness-projection/test-fixtures/opencode/matrix.json +19 -0
- package/dist/data/scripts/harness-projection/test-fixtures/opencode/projection.json +51 -0
- package/dist/data/scripts/sync-subagents.mjs +1113 -33
- package/dist/data/subagents/manifest.mjs +5 -6
- package/dist/index.js +1373 -62672
- package/dist/skills/agnostic/docs/docs-onboarding/SKILL.md +1 -1
- package/dist/skills/agnostic/docs/docs-onboarding/references/artifact-contract.md +2 -2
- package/dist/skills/agnostic/planning/create-plan/SKILL.md +11 -10
- package/dist/skills/agnostic/planning/create-plan/references/grill-ambiguity.md +10 -31
- package/dist/skills/agnostic/planning/create-plan/references/plan-schema.md +2 -2
- package/dist/skills/agnostic/planning/create-spec/SKILL.md +3 -5
- package/dist/skills/agnostic/planning/create-spec/assets/SPEC-TEMPLATE.md +1 -0
- package/dist/skills/agnostic/planning/create-spec/references/backlog-sync.md +2 -2
- package/dist/skills/agnostic/planning/create-spec/references/grill-phase.md +4 -9
- package/dist/skills/agnostic/planning/create-spec/references/spec-quality-bar.md +1 -0
- package/dist/skills/agnostic/planning/implement-spec/references/lifecycle.md +1 -1
- package/dist/skills/agnostic/requirements/grilling/LICENSE +21 -0
- package/dist/skills/agnostic/requirements/grilling/SKILL.md +6 -30
- package/dist/skills/agnostic/requirements/grilling/UPSTREAM.md +16 -0
- package/dist/skills/agnostic/requirements/requirements-grill/SKILL.md +3 -5
- package/dist/skills/agnostic/requirements/requirements-grill/references/artifact-output.md +40 -16
- package/dist/skills/agnostic/requirements/requirements-grill/references/grilling-flow.md +3 -16
- package/dist/skills/agnostic/requirements/write-backlog/REFERENCE.md +21 -15
- package/dist/skills/agnostic/requirements/write-backlog/SKILL.md +14 -14
- package/dist/skills/agnostic/requirements/write-backlog/assets/concepts/backlog-model.md +28 -9
- package/dist/skills/agnostic/requirements/write-backlog/assets/providers/azure-devops-create-payload.md +45 -5
- package/dist/skills/agnostic/requirements/write-backlog/assets/providers/github-projects-create-payload.md +48 -28
- package/dist/skills/agnostic/requirements/write-backlog/assets/providers/linear-create-payload.md +22 -6
- package/dist/skills/agnostic/requirements/write-backlog/assets/providers/monday-create-payload.md +47 -22
- package/dist/skills/frameworks/effect/effect/LICENSE +21 -0
- package/dist/skills/frameworks/effect/effect/SKILL.md +100 -0
- package/dist/skills/frameworks/effect/effect/UPSTREAM.md +11 -0
- package/dist/skills/frameworks/effect/effect/references/CACHING.md +76 -0
- package/dist/skills/frameworks/effect/effect/references/CONFIG.md +67 -0
- package/dist/skills/frameworks/effect/effect/references/HTTP_CLIENTS.md +101 -0
- package/dist/skills/frameworks/effect/{effect-authoring/references/otel-patterns.md → effect/references/OBSERVABILITY.md} +4 -29
- package/dist/skills/frameworks/effect/effect/references/SCHEDULING.md +135 -0
- package/dist/skills/frameworks/effect/effect/references/SCHEMA.md +130 -0
- package/dist/skills/frameworks/effect/effect/references/SERVICES_LAYERS.md +168 -0
- package/dist/skills/frameworks/effect/effect/references/STREAMS.md +135 -0
- package/dist/skills/frameworks/effect/effect/references/TESTING.md +118 -0
- package/dist/skills/frameworks/effect/effect-backend-structure/SKILL.md +7 -8
- package/dist/skills/frameworks/effect/effect-recoverable-actions/SKILL.md +1 -1
- package/dist/skills/frameworks/nestjs/nestjs-best-practices/.gitignore +29 -0
- package/dist/skills/misc/secrets-cli/SKILL.md +54 -0
- package/dist/skills/misc/secrets-cli/references/command-reference.md +107 -0
- package/dist/skills/misc/secrets-cli/references/safety-and-errors.md +54 -0
- package/dist/skills/misc/secrets-cli/references/workflows.md +106 -0
- package/dist/skills/phases/design-phase/phases/grill.md +1 -1
- package/dist/skills/phases/requirements-phase/SKILL.md +2 -4
- package/dist/skills/phases/review-phase/REFERENCE.md +13 -5
- package/dist/skills/phases/review-phase/SKILL.md +3 -2
- package/package.json +1 -1
- package/dist/skills/agnostic/planning/create-spec/references/handoff.md +0 -47
- package/dist/skills/frameworks/effect/effect-authoring/SKILL.md +0 -116
- package/dist/skills/frameworks/effect/effect-authoring/references/branded-types.md +0 -98
- package/dist/skills/frameworks/effect/effect-authoring/references/effect-primitives.md +0 -144
- package/dist/skills/frameworks/effect/effect-authoring/references/error-patterns.md +0 -156
- package/dist/skills/frameworks/effect/effect-authoring/references/test-patterns.md +0 -146
- package/dist/skills/frameworks/effect/effect-best-practices/SKILL.md +0 -517
- package/dist/skills/frameworks/effect/effect-best-practices/references/anti-patterns.md +0 -392
- package/dist/skills/frameworks/effect/effect-best-practices/references/effect-atom-patterns.md +0 -653
- package/dist/skills/frameworks/effect/effect-best-practices/references/error-patterns.md +0 -464
- package/dist/skills/frameworks/effect/effect-best-practices/references/language-server.md +0 -287
- package/dist/skills/frameworks/effect/effect-best-practices/references/layer-patterns.md +0 -495
- package/dist/skills/frameworks/effect/effect-best-practices/references/observability-patterns.md +0 -342
- package/dist/skills/frameworks/effect/effect-best-practices/references/rpc-cluster-patterns.md +0 -418
- package/dist/skills/frameworks/effect/effect-best-practices/references/schema-patterns.md +0 -353
- package/dist/skills/frameworks/effect/effect-best-practices/references/service-patterns.md +0 -299
- /package/dist/skills/frameworks/effect/{effect-authoring/references/effect-atom-patterns.md → effect/references/EFFECT_ATOM.md} +0 -0
|
@@ -1,495 +0,0 @@
|
|
|
1
|
-
# Layer Patterns
|
|
2
|
-
|
|
3
|
-
Layers are dependency application. An Effect requirement is like a function parameter: it stays unsatisfied and type-visible until a layer provides it. A good backend keeps this boundary explicit so production and tests use the same composition mechanism.
|
|
4
|
-
|
|
5
|
-
## Dependencies in Effect.Service
|
|
6
|
-
|
|
7
|
-
**Critical rule:** Always declare dependencies in the `dependencies` array of `Effect.Service`. This ensures proper composition and avoids "leaked dependencies" that require manual wiring at usage sites.
|
|
8
|
-
|
|
9
|
-
### Correct Pattern
|
|
10
|
-
|
|
11
|
-
```typescript
|
|
12
|
-
export class OrderService extends Effect.Service<OrderService>()("OrderService", {
|
|
13
|
-
accessors: true,
|
|
14
|
-
dependencies: [
|
|
15
|
-
UserService.Default,
|
|
16
|
-
ProductService.Default,
|
|
17
|
-
InventoryService.Default,
|
|
18
|
-
PaymentService.Default,
|
|
19
|
-
],
|
|
20
|
-
effect: Effect.gen(function* () {
|
|
21
|
-
const users = yield* UserService
|
|
22
|
-
const products = yield* ProductService
|
|
23
|
-
const inventory = yield* InventoryService
|
|
24
|
-
const payments = yield* PaymentService
|
|
25
|
-
|
|
26
|
-
// Service implementation...
|
|
27
|
-
return { /* methods */ }
|
|
28
|
-
}),
|
|
29
|
-
}) {}
|
|
30
|
-
|
|
31
|
-
// At app root - simple, flat composition
|
|
32
|
-
const AppLive = Layer.mergeAll(
|
|
33
|
-
OrderService.Default,
|
|
34
|
-
// Other top-level services
|
|
35
|
-
NotificationService.Default,
|
|
36
|
-
AnalyticsService.Default,
|
|
37
|
-
)
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### Wrong Pattern (Leaked Dependencies)
|
|
41
|
-
|
|
42
|
-
```typescript
|
|
43
|
-
// WRONG - Dependencies not declared
|
|
44
|
-
export class OrderService extends Effect.Service<OrderService>()("OrderService", {
|
|
45
|
-
accessors: true,
|
|
46
|
-
effect: Effect.gen(function* () {
|
|
47
|
-
const users = yield* UserService // Not in dependencies!
|
|
48
|
-
// ...
|
|
49
|
-
}),
|
|
50
|
-
}) {}
|
|
51
|
-
|
|
52
|
-
// Now every usage requires manual wiring
|
|
53
|
-
const program = OrderService.create(input).pipe(
|
|
54
|
-
Effect.provide(
|
|
55
|
-
OrderService.Default.pipe(
|
|
56
|
-
Layer.provide(UserService.Default),
|
|
57
|
-
Layer.provide(ProductService.Default),
|
|
58
|
-
// Easy to forget one, causes runtime errors
|
|
59
|
-
)
|
|
60
|
-
),
|
|
61
|
-
)
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
## Infrastructure Layers
|
|
65
|
-
|
|
66
|
-
Infrastructure layers (Database, Redis, HTTP clients) are **acceptable** to leave as "leaked" dependencies because:
|
|
67
|
-
|
|
68
|
-
1. They're provided once at the application root
|
|
69
|
-
2. They don't change between test/production (different implementations, same interface)
|
|
70
|
-
3. They're true infrastructure, not business logic
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
// Infrastructure can be provided at app root
|
|
74
|
-
import { PgClient } from "@effect/sql-pg"
|
|
75
|
-
|
|
76
|
-
const DatabaseLive = PgClient.layer({
|
|
77
|
-
host: Config.string("DB_HOST"),
|
|
78
|
-
port: Config.integer("DB_PORT"),
|
|
79
|
-
database: Config.string("DB_NAME"),
|
|
80
|
-
username: Config.string("DB_USER"),
|
|
81
|
-
password: Config.redacted("DB_PASSWORD"),
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
// Services use database but don't declare it in dependencies
|
|
85
|
-
export class UserRepo extends Effect.Service<UserRepo>()("UserRepo", {
|
|
86
|
-
accessors: true,
|
|
87
|
-
// No dependencies array - PgClient provided at app root
|
|
88
|
-
effect: Effect.gen(function* () {
|
|
89
|
-
const sql = yield* PgClient.PgClient
|
|
90
|
-
|
|
91
|
-
const findById = Effect.fn("UserRepo.findById")(function* (id: UserId) {
|
|
92
|
-
const rows = yield* sql`SELECT * FROM users WHERE id = ${id}`.pipe(Effect.orDie)
|
|
93
|
-
return rows[0] as User | undefined
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
return { findById }
|
|
97
|
-
}),
|
|
98
|
-
}) {}
|
|
99
|
-
|
|
100
|
-
// App root provides infrastructure once
|
|
101
|
-
const AppLive = Layer.mergeAll(
|
|
102
|
-
OrderService.Default,
|
|
103
|
-
UserService.Default,
|
|
104
|
-
).pipe(
|
|
105
|
-
Layer.provide(DatabaseLive), // Infrastructure provided here
|
|
106
|
-
Layer.provide(RedisLive),
|
|
107
|
-
)
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
## Layer.mergeAll Over Nested Provides
|
|
111
|
-
|
|
112
|
-
**Use `Layer.mergeAll`** for composing layers at the same level:
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
// CORRECT - Flat composition
|
|
116
|
-
const ServicesLive = Layer.mergeAll(
|
|
117
|
-
UserService.Default,
|
|
118
|
-
OrderService.Default,
|
|
119
|
-
ProductService.Default,
|
|
120
|
-
NotificationService.Default,
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
const InfrastructureLive = Layer.mergeAll(
|
|
124
|
-
DatabaseLive,
|
|
125
|
-
RedisLive,
|
|
126
|
-
HttpClientLive,
|
|
127
|
-
)
|
|
128
|
-
|
|
129
|
-
const AppLive = ServicesLive.pipe(
|
|
130
|
-
Layer.provide(InfrastructureLive),
|
|
131
|
-
)
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
```typescript
|
|
135
|
-
// WRONG - Deeply nested, hard to read
|
|
136
|
-
const AppLive = UserService.Default.pipe(
|
|
137
|
-
Layer.provide(
|
|
138
|
-
OrderService.Default.pipe(
|
|
139
|
-
Layer.provide(
|
|
140
|
-
ProductService.Default.pipe(
|
|
141
|
-
Layer.provide(DatabaseLive),
|
|
142
|
-
),
|
|
143
|
-
),
|
|
144
|
-
),
|
|
145
|
-
),
|
|
146
|
-
)
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
## Layer.provideMerge for Sequential Composition
|
|
150
|
-
|
|
151
|
-
**Use `Layer.provideMerge`** when chaining layers that need incremental composition. Unlike `Layer.provide`, `provideMerge` merges the output into the current layer, producing flatter types.
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
// CORRECT - Layer.provideMerge chains for incremental composition
|
|
155
|
-
const MainLive = DatabaseLive.pipe(
|
|
156
|
-
Layer.provideMerge(ProxyConfigService.Default),
|
|
157
|
-
Layer.provideMerge(LoggerLive),
|
|
158
|
-
Layer.provideMerge(CacheLive),
|
|
159
|
-
Layer.provideMerge(TracerLive),
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
// WRONG - Multiple Layer.provide calls create nested types
|
|
163
|
-
const MainLive = DatabaseLive.pipe(
|
|
164
|
-
Layer.provide(ProxyConfigService.Default),
|
|
165
|
-
Layer.provide(LoggerLive), // Each provide creates deeper nesting
|
|
166
|
-
Layer.provide(CacheLive),
|
|
167
|
-
)
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
**Key difference:** `Layer.provide(A, B)` provides B to A but outputs only A's services. `Layer.provideMerge(A, B)` provides B to A and outputs both A's and B's services merged together.
|
|
171
|
-
|
|
172
|
-
## Layer Deduplication Benefits
|
|
173
|
-
|
|
174
|
-
Layers automatically memoize construction - the same service is instantiated only once regardless of how many times it appears in the dependency graph.
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
// Both UserRepo and OrderRepo depend on DatabaseLive
|
|
178
|
-
const RepoLive = Layer.mergeAll(
|
|
179
|
-
UserRepo.Default, // requires DatabaseLive
|
|
180
|
-
OrderRepo.Default, // requires DatabaseLive
|
|
181
|
-
)
|
|
182
|
-
|
|
183
|
-
// With Layer.mergeAll, DatabaseLive is constructed ONCE
|
|
184
|
-
const AppLive = RepoLive.pipe(
|
|
185
|
-
Layer.provide(DatabaseLive), // Single instance shared
|
|
186
|
-
)
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
**`Effect.provide` does NOT deduplicate:**
|
|
190
|
-
|
|
191
|
-
```typescript
|
|
192
|
-
// WRONG - Each provide creates a new instance
|
|
193
|
-
const program = myEffect.pipe(
|
|
194
|
-
Effect.provide(UserRepo.Default),
|
|
195
|
-
Effect.provide(OrderRepo.Default),
|
|
196
|
-
// If both repos need DatabaseLive, and you provide it separately,
|
|
197
|
-
// you may get TWO database connections!
|
|
198
|
-
)
|
|
199
|
-
|
|
200
|
-
// CORRECT - Use layers for deduplication
|
|
201
|
-
const program = myEffect.pipe(
|
|
202
|
-
Effect.provide(AppLive), // Single composed layer
|
|
203
|
-
)
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
## TypeScript LSP Performance
|
|
207
|
-
|
|
208
|
-
Deeply nested `Layer.provide` chains create complex recursive types that slow down the TypeScript Language Server.
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
// PROBLEMATIC - Deep nesting causes slow LSP
|
|
212
|
-
const AppLive = Layer1.pipe(
|
|
213
|
-
Layer.provide(Layer2.pipe(
|
|
214
|
-
Layer.provide(Layer3.pipe(
|
|
215
|
-
Layer.provide(Layer4.pipe(
|
|
216
|
-
Layer.provide(Layer5),
|
|
217
|
-
)),
|
|
218
|
-
)),
|
|
219
|
-
)),
|
|
220
|
-
)
|
|
221
|
-
// Type becomes: Layer<..., Layer<..., Layer<..., Layer<..., ...>>>>
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
```typescript
|
|
225
|
-
// BETTER - Flat composition with mergeAll produces simpler types
|
|
226
|
-
const InfraLive = Layer.mergeAll(Layer3, Layer4, Layer5)
|
|
227
|
-
const AppLive = Layer.mergeAll(Layer1, Layer2).pipe(
|
|
228
|
-
Layer.provide(InfraLive),
|
|
229
|
-
)
|
|
230
|
-
// Type is flatter and LSP responds faster
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
**Recommendations:**
|
|
234
|
-
- Prefer `Layer.mergeAll` for layers at the same level
|
|
235
|
-
- Use `Layer.provideMerge` instead of chained `Layer.provide` calls
|
|
236
|
-
- Group related layers into intermediate compositions
|
|
237
|
-
- Keep nesting depth shallow (ideally 2-3 levels max)
|
|
238
|
-
|
|
239
|
-
## layerConfig Pattern
|
|
240
|
-
|
|
241
|
-
For services that need configuration at construction time, use the `layerConfig` static method pattern:
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
import { Config, ConfigError, Effect, Layer } from "effect"
|
|
245
|
-
|
|
246
|
-
interface EventQueueConfig {
|
|
247
|
-
readonly maxRetries: number
|
|
248
|
-
readonly batchSize: number
|
|
249
|
-
readonly pollInterval: number
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export class ElectricEventQueue extends Effect.Service<ElectricEventQueue>()(
|
|
253
|
-
"ElectricEventQueue",
|
|
254
|
-
{
|
|
255
|
-
accessors: true,
|
|
256
|
-
effect: Effect.gen(function* () {
|
|
257
|
-
// Default implementation
|
|
258
|
-
return { /* methods */ }
|
|
259
|
-
}),
|
|
260
|
-
}
|
|
261
|
-
) {
|
|
262
|
-
// Static method for config-driven layer
|
|
263
|
-
static readonly layerConfig = (
|
|
264
|
-
config: Config.Config.Wrap<EventQueueConfig>,
|
|
265
|
-
): Layer.Layer<ElectricEventQueue, ConfigError.ConfigError> =>
|
|
266
|
-
Layer.unwrapEffect(
|
|
267
|
-
Config.unwrap(config).pipe(
|
|
268
|
-
Effect.map((cfg) =>
|
|
269
|
-
Layer.succeed(
|
|
270
|
-
ElectricEventQueue,
|
|
271
|
-
new ElectricEventQueueImpl(cfg)
|
|
272
|
-
)
|
|
273
|
-
)
|
|
274
|
-
)
|
|
275
|
-
)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// Usage
|
|
279
|
-
const EventQueueLive = ElectricEventQueue.layerConfig({
|
|
280
|
-
maxRetries: Config.integer("EVENT_QUEUE_MAX_RETRIES").pipe(
|
|
281
|
-
Config.withDefault(3)
|
|
282
|
-
),
|
|
283
|
-
batchSize: Config.integer("EVENT_QUEUE_BATCH_SIZE").pipe(
|
|
284
|
-
Config.withDefault(100)
|
|
285
|
-
),
|
|
286
|
-
pollInterval: Config.integer("EVENT_QUEUE_POLL_INTERVAL").pipe(
|
|
287
|
-
Config.withDefault(1000)
|
|
288
|
-
),
|
|
289
|
-
})
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
This pattern:
|
|
293
|
-
- Separates configuration from implementation
|
|
294
|
-
- Returns `ConfigError` for missing/invalid config
|
|
295
|
-
- Allows different configs per environment
|
|
296
|
-
- Integrates cleanly with `Layer.mergeAll` and `Layer.provideMerge`
|
|
297
|
-
|
|
298
|
-
## Layer Naming Conventions
|
|
299
|
-
|
|
300
|
-
Use suffixes to indicate layer type:
|
|
301
|
-
|
|
302
|
-
- `ServiceLive` - Production implementation
|
|
303
|
-
- `ServiceTest` - Test/mock implementation
|
|
304
|
-
- `ServiceLayer` - Generic layer (rare)
|
|
305
|
-
|
|
306
|
-
```typescript
|
|
307
|
-
// Production
|
|
308
|
-
export const UserServiceLive = UserService.Default
|
|
309
|
-
|
|
310
|
-
// Test with mocks
|
|
311
|
-
export const UserServiceTest = Layer.succeed(
|
|
312
|
-
UserService,
|
|
313
|
-
UserService.of({
|
|
314
|
-
findById: (id) => Effect.succeed(mockUser),
|
|
315
|
-
create: (input) => Effect.succeed({ id: UserId.make("test-id"), ...input }),
|
|
316
|
-
})
|
|
317
|
-
)
|
|
318
|
-
|
|
319
|
-
// Test with in-memory state
|
|
320
|
-
export class UserServiceInMemory extends Effect.Service<UserService>()("UserService", {
|
|
321
|
-
accessors: true,
|
|
322
|
-
effect: Effect.gen(function* () {
|
|
323
|
-
const store = new Map<string, User>()
|
|
324
|
-
|
|
325
|
-
return {
|
|
326
|
-
findById: Effect.fn("UserService.findById")(function* (id) {
|
|
327
|
-
const user = store.get(id)
|
|
328
|
-
if (!user) return yield* Effect.fail(new UserNotFoundError({ userId: id }))
|
|
329
|
-
return user
|
|
330
|
-
}),
|
|
331
|
-
create: Effect.fn("UserService.create")(function* (input) {
|
|
332
|
-
const user = { id: UserId.make(crypto.randomUUID()), ...input }
|
|
333
|
-
store.set(user.id, user)
|
|
334
|
-
return user
|
|
335
|
-
}),
|
|
336
|
-
}
|
|
337
|
-
}),
|
|
338
|
-
}) {}
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
## Layer.unwrapEffect for Config-Dependent Layers
|
|
342
|
-
|
|
343
|
-
When a layer needs async configuration:
|
|
344
|
-
|
|
345
|
-
```typescript
|
|
346
|
-
import { Config, Effect, Layer } from "effect"
|
|
347
|
-
|
|
348
|
-
// Layer that depends on config
|
|
349
|
-
const ApiClientLive = Layer.unwrapEffect(
|
|
350
|
-
Effect.gen(function* () {
|
|
351
|
-
const apiKey = yield* Config.string("API_KEY")
|
|
352
|
-
const baseUrl = yield* Config.string("API_BASE_URL")
|
|
353
|
-
const timeout = yield* Config.integer("API_TIMEOUT").pipe(
|
|
354
|
-
Config.withDefault(5000)
|
|
355
|
-
)
|
|
356
|
-
|
|
357
|
-
return Layer.succeed(
|
|
358
|
-
ApiClient,
|
|
359
|
-
new ApiClientImpl({ apiKey, baseUrl, timeout })
|
|
360
|
-
)
|
|
361
|
-
})
|
|
362
|
-
)
|
|
363
|
-
|
|
364
|
-
// Layer that validates config
|
|
365
|
-
const ValidatedConfigLive = Layer.unwrapEffect(
|
|
366
|
-
Effect.gen(function* () {
|
|
367
|
-
const config = yield* Config.all({
|
|
368
|
-
dbUrl: Config.string("DATABASE_URL"),
|
|
369
|
-
redisUrl: Config.string("REDIS_URL"),
|
|
370
|
-
port: Config.integer("PORT"),
|
|
371
|
-
})
|
|
372
|
-
|
|
373
|
-
// Validate config
|
|
374
|
-
if (!config.dbUrl.startsWith("postgresql://")) {
|
|
375
|
-
return yield* Effect.fail(new ConfigError({ message: "Invalid DATABASE_URL" }))
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
return Layer.succeed(AppConfig, config)
|
|
379
|
-
})
|
|
380
|
-
)
|
|
381
|
-
```
|
|
382
|
-
|
|
383
|
-
## Scoped Layers
|
|
384
|
-
|
|
385
|
-
For resources that need cleanup:
|
|
386
|
-
|
|
387
|
-
```typescript
|
|
388
|
-
import { Effect, Layer, Scope } from "effect"
|
|
389
|
-
|
|
390
|
-
// Resource that needs cleanup
|
|
391
|
-
const DatabaseConnectionLive = Layer.scoped(
|
|
392
|
-
DatabaseConnection,
|
|
393
|
-
Effect.acquireRelease(
|
|
394
|
-
Effect.gen(function* () {
|
|
395
|
-
const pool = yield* createPool(config)
|
|
396
|
-
yield* Effect.log("Database pool created")
|
|
397
|
-
return pool
|
|
398
|
-
}),
|
|
399
|
-
(pool) =>
|
|
400
|
-
Effect.gen(function* () {
|
|
401
|
-
yield* pool.end()
|
|
402
|
-
yield* Effect.log("Database pool closed")
|
|
403
|
-
}).pipe(Effect.orDie)
|
|
404
|
-
)
|
|
405
|
-
)
|
|
406
|
-
|
|
407
|
-
// Service using scoped resource
|
|
408
|
-
export class UserRepo extends Effect.Service<UserRepo>()("UserRepo", {
|
|
409
|
-
accessors: true,
|
|
410
|
-
effect: Effect.gen(function* () {
|
|
411
|
-
const db = yield* DatabaseConnection
|
|
412
|
-
|
|
413
|
-
return {
|
|
414
|
-
findById: Effect.fn("UserRepo.findById")(function* (id) {
|
|
415
|
-
return yield* db.query("SELECT * FROM users WHERE id = $1", [id])
|
|
416
|
-
}),
|
|
417
|
-
}
|
|
418
|
-
}),
|
|
419
|
-
}) {}
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
## Testing Layer Composition
|
|
423
|
-
|
|
424
|
-
Test layers replace implementations at the service boundary. Prefer this over module mocks because the compiler still checks the service shape, required environment, and error channel.
|
|
425
|
-
|
|
426
|
-
```typescript
|
|
427
|
-
// test/setup.ts
|
|
428
|
-
import { Layer } from "effect"
|
|
429
|
-
|
|
430
|
-
export const TestLive = Layer.mergeAll(
|
|
431
|
-
UserServiceTest,
|
|
432
|
-
OrderServiceTest,
|
|
433
|
-
ProductServiceTest,
|
|
434
|
-
).pipe(
|
|
435
|
-
Layer.provide(InMemoryDatabaseLive),
|
|
436
|
-
)
|
|
437
|
-
|
|
438
|
-
// test/user.test.ts
|
|
439
|
-
import { Effect } from "effect"
|
|
440
|
-
import { TestLive } from "./setup"
|
|
441
|
-
|
|
442
|
-
describe("UserService", () => {
|
|
443
|
-
it("creates users", async () => {
|
|
444
|
-
const program = Effect.gen(function* () {
|
|
445
|
-
const user = yield* UserService.create({
|
|
446
|
-
email: "test@example.com",
|
|
447
|
-
name: "Test User",
|
|
448
|
-
})
|
|
449
|
-
expect(user.email).toBe("test@example.com")
|
|
450
|
-
})
|
|
451
|
-
|
|
452
|
-
await Effect.runPromise(program.pipe(Effect.provide(TestLive)))
|
|
453
|
-
})
|
|
454
|
-
})
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
## Layer.effect vs Layer.succeed
|
|
458
|
-
|
|
459
|
-
```typescript
|
|
460
|
-
// Layer.succeed - for static values (no effects)
|
|
461
|
-
const ConfigLive = Layer.succeed(AppConfig, {
|
|
462
|
-
port: 3000,
|
|
463
|
-
env: "development",
|
|
464
|
-
})
|
|
465
|
-
|
|
466
|
-
// Layer.effect - when construction needs effects
|
|
467
|
-
const LoggerLive = Layer.effect(
|
|
468
|
-
Logger,
|
|
469
|
-
Effect.gen(function* () {
|
|
470
|
-
const config = yield* AppConfig
|
|
471
|
-
const transport = config.env === "production"
|
|
472
|
-
? createCloudTransport()
|
|
473
|
-
: createConsoleTransport()
|
|
474
|
-
return new LoggerImpl(transport)
|
|
475
|
-
})
|
|
476
|
-
)
|
|
477
|
-
```
|
|
478
|
-
|
|
479
|
-
## Lazy Layers
|
|
480
|
-
|
|
481
|
-
For expensive initialization that should be deferred:
|
|
482
|
-
|
|
483
|
-
```typescript
|
|
484
|
-
const ExpensiveServiceLive = Layer.lazy(() => {
|
|
485
|
-
// This code runs only when the layer is first used
|
|
486
|
-
return Layer.effect(
|
|
487
|
-
ExpensiveService,
|
|
488
|
-
Effect.gen(function* () {
|
|
489
|
-
yield* Effect.log("Initializing expensive service...")
|
|
490
|
-
const client = yield* createExpensiveClient()
|
|
491
|
-
return new ExpensiveServiceImpl(client)
|
|
492
|
-
})
|
|
493
|
-
)
|
|
494
|
-
})
|
|
495
|
-
```
|