effect-start 0.15.0 → 0.16.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/src/RouteMount.ts CHANGED
@@ -179,8 +179,8 @@ export namespace RouteMount {
179
179
  export type Items<S> = S extends Builder<any, infer I> ? I : []
180
180
 
181
181
  export type BuilderBindings<S> = S extends Builder<any, infer I>
182
- ? Types.Simplify<WildcardBindings<I>>
183
- : {}
182
+ ? Types.Simplify<WildcardBindings<I>> & { request: Request }
183
+ : { request: Request }
184
184
 
185
185
  type WildcardBindingsItem<T> = T extends Route.Route.Route<
186
186
  { method: "*" },
@@ -251,6 +251,7 @@ export namespace RouteMount {
251
251
  }
252
252
 
253
253
  // Flatten items: merge method into descriptor and accumulate bindings through the chain
254
+ // `request` is omitted from bindings since it's implicit (always available)
254
255
  export type FlattenItems<
255
256
  M extends Method,
256
257
  B,
@@ -259,8 +260,14 @@ export namespace RouteMount {
259
260
  Route.Route.Route<infer D, infer RB, infer A, infer E, infer R>,
260
261
  ...infer Tail extends Route.Route.Tuple,
261
262
  ] ? [
262
- Route.Route.Route<{ method: M } & D, B & RB, A, E, R>,
263
- ...FlattenItems<M, B & RB, Tail>,
263
+ Route.Route.Route<
264
+ Types.Simplify<{ method: M } & D> & {},
265
+ Types.Simplify<Omit<B & RB, "request">>,
266
+ A,
267
+ E,
268
+ R
269
+ >,
270
+ ...FlattenItems<M, Types.Simplify<B & RB>, Tail>,
264
271
  ]
265
272
  : []
266
273
 
@@ -309,5 +316,155 @@ export namespace RouteMount {
309
316
  ...FlattenItems<M, BuilderBindings<S>, Route.RouteSet.Items<C>>,
310
317
  ]
311
318
  >
319
+
320
+ <
321
+ S extends Self,
322
+ A extends Route.RouteSet.Any,
323
+ B extends Route.RouteSet.Any,
324
+ C extends Route.RouteSet.Any,
325
+ D extends Route.RouteSet.Any,
326
+ >(
327
+ this: S,
328
+ ab: (a: EmptySet<M, BuilderBindings<S>>) => A,
329
+ bc: (b: A) => B,
330
+ cd: (c: B) => C,
331
+ de: (d: C) => D,
332
+ ): Builder<
333
+ {},
334
+ [
335
+ ...Items<S>,
336
+ ...FlattenItems<M, BuilderBindings<S>, Route.RouteSet.Items<D>>,
337
+ ]
338
+ >
339
+
340
+ <
341
+ S extends Self,
342
+ A extends Route.RouteSet.Any,
343
+ B extends Route.RouteSet.Any,
344
+ C extends Route.RouteSet.Any,
345
+ D extends Route.RouteSet.Any,
346
+ E extends Route.RouteSet.Any,
347
+ >(
348
+ this: S,
349
+ ab: (a: EmptySet<M, BuilderBindings<S>>) => A,
350
+ bc: (b: A) => B,
351
+ cd: (c: B) => C,
352
+ de: (d: C) => D,
353
+ ef: (e: D) => E,
354
+ ): Builder<
355
+ {},
356
+ [
357
+ ...Items<S>,
358
+ ...FlattenItems<M, BuilderBindings<S>, Route.RouteSet.Items<E>>,
359
+ ]
360
+ >
361
+
362
+ <
363
+ S extends Self,
364
+ A extends Route.RouteSet.Any,
365
+ B extends Route.RouteSet.Any,
366
+ C extends Route.RouteSet.Any,
367
+ D extends Route.RouteSet.Any,
368
+ E extends Route.RouteSet.Any,
369
+ F extends Route.RouteSet.Any,
370
+ >(
371
+ this: S,
372
+ ab: (a: EmptySet<M, BuilderBindings<S>>) => A,
373
+ bc: (b: A) => B,
374
+ cd: (c: B) => C,
375
+ de: (d: C) => D,
376
+ ef: (e: D) => E,
377
+ fg: (f: E) => F,
378
+ ): Builder<
379
+ {},
380
+ [
381
+ ...Items<S>,
382
+ ...FlattenItems<M, BuilderBindings<S>, Route.RouteSet.Items<F>>,
383
+ ]
384
+ >
385
+
386
+ <
387
+ S extends Self,
388
+ A extends Route.RouteSet.Any,
389
+ B extends Route.RouteSet.Any,
390
+ C extends Route.RouteSet.Any,
391
+ D extends Route.RouteSet.Any,
392
+ E extends Route.RouteSet.Any,
393
+ F extends Route.RouteSet.Any,
394
+ G extends Route.RouteSet.Any,
395
+ >(
396
+ this: S,
397
+ ab: (a: EmptySet<M, BuilderBindings<S>>) => A,
398
+ bc: (b: A) => B,
399
+ cd: (c: B) => C,
400
+ de: (d: C) => D,
401
+ ef: (e: D) => E,
402
+ fg: (f: E) => F,
403
+ gh: (g: F) => G,
404
+ ): Builder<
405
+ {},
406
+ [
407
+ ...Items<S>,
408
+ ...FlattenItems<M, BuilderBindings<S>, Route.RouteSet.Items<G>>,
409
+ ]
410
+ >
411
+
412
+ <
413
+ S extends Self,
414
+ A extends Route.RouteSet.Any,
415
+ B extends Route.RouteSet.Any,
416
+ C extends Route.RouteSet.Any,
417
+ D extends Route.RouteSet.Any,
418
+ E extends Route.RouteSet.Any,
419
+ F extends Route.RouteSet.Any,
420
+ G extends Route.RouteSet.Any,
421
+ H extends Route.RouteSet.Any,
422
+ >(
423
+ this: S,
424
+ ab: (a: EmptySet<M, BuilderBindings<S>>) => A,
425
+ bc: (b: A) => B,
426
+ cd: (c: B) => C,
427
+ de: (d: C) => D,
428
+ ef: (e: D) => E,
429
+ fg: (f: E) => F,
430
+ gh: (g: F) => G,
431
+ hi: (h: G) => H,
432
+ ): Builder<
433
+ {},
434
+ [
435
+ ...Items<S>,
436
+ ...FlattenItems<M, BuilderBindings<S>, Route.RouteSet.Items<H>>,
437
+ ]
438
+ >
439
+
440
+ <
441
+ S extends Self,
442
+ A extends Route.RouteSet.Any,
443
+ B extends Route.RouteSet.Any,
444
+ C extends Route.RouteSet.Any,
445
+ D extends Route.RouteSet.Any,
446
+ E extends Route.RouteSet.Any,
447
+ F extends Route.RouteSet.Any,
448
+ G extends Route.RouteSet.Any,
449
+ H extends Route.RouteSet.Any,
450
+ I extends Route.RouteSet.Any,
451
+ >(
452
+ this: S,
453
+ ab: (a: EmptySet<M, BuilderBindings<S>>) => A,
454
+ bc: (b: A) => B,
455
+ cd: (c: B) => C,
456
+ de: (d: C) => D,
457
+ ef: (e: D) => E,
458
+ fg: (f: E) => F,
459
+ gh: (g: F) => G,
460
+ hi: (h: G) => H,
461
+ ij: (i: H) => I,
462
+ ): Builder<
463
+ {},
464
+ [
465
+ ...Items<S>,
466
+ ...FlattenItems<M, BuilderBindings<S>, Route.RouteSet.Items<I>>,
467
+ ]
468
+ >
312
469
  }
313
470
  }
@@ -3,9 +3,12 @@ import {
3
3
  Effect,
4
4
  Schema,
5
5
  } from "effect"
6
+ import * as Http from "./Http.ts"
6
7
  import * as Route from "./Route.ts"
8
+ import * as RouteHttp from "./RouteHttp.ts"
7
9
  import * as RouteMount from "./RouteMount.ts"
8
10
  import * as RouteSchema from "./RouteSchema.ts"
11
+ import * as TestLogger from "./testing/TestLogger.ts"
9
12
 
10
13
  test.describe(`${RouteSchema.schemaHeaders.name}()`, () => {
11
14
  test.it(`${RouteSchema.schemaHeaders.name} merges`, () => {
@@ -79,3 +82,346 @@ test.describe(`${RouteSchema.schemaHeaders.name}()`, () => {
79
82
  .toExtend<ExpectedBindings>()
80
83
  })
81
84
  })
85
+
86
+ test.describe(`${RouteSchema.schemaCookies.name}()`, () => {
87
+ test.it("merges cookies from schema", () => {
88
+ type ExpectedBindings = {
89
+ session: string
90
+ token: string
91
+ }
92
+ const route = Route
93
+ .use(
94
+ RouteSchema.schemaCookies(
95
+ Schema.Struct({
96
+ "session": Schema.String,
97
+ }),
98
+ ),
99
+ )
100
+ .get(
101
+ RouteSchema.schemaCookies(
102
+ Schema.Struct({
103
+ "token": Schema.String,
104
+ }),
105
+ ),
106
+ Route.html(function*(ctx) {
107
+ test
108
+ .expectTypeOf(ctx.cookies)
109
+ .toExtend<ExpectedBindings>()
110
+
111
+ return `<h1>Hello, world!</h1>`
112
+ }),
113
+ )
114
+
115
+ test
116
+ .expectTypeOf<Route.Route.Context<typeof route>>()
117
+ .toExtend<{
118
+ cookies: ExpectedBindings
119
+ }>()
120
+ })
121
+
122
+ test.it("parses cookies from request", async () => {
123
+ const handler = RouteHttp.toWebHandler(
124
+ Route.get(
125
+ RouteSchema.schemaCookies(
126
+ Schema.Struct({
127
+ "session": Schema.String,
128
+ }),
129
+ ),
130
+ Route.text(function*(ctx) {
131
+ return `session=${ctx.cookies.session}`
132
+ }),
133
+ ),
134
+ )
135
+ const response = await Http.fetch(handler, {
136
+ path: "/test",
137
+ headers: {
138
+ "Cookie": "session=abc123",
139
+ },
140
+ })
141
+
142
+ test
143
+ .expect(response.status)
144
+ .toBe(200)
145
+ test
146
+ .expect(await response.text())
147
+ .toBe("session=abc123")
148
+ })
149
+ })
150
+
151
+ test.describe(`${RouteSchema.schemaSearchParams.name}()`, () => {
152
+ test.it("merges search params from schema", () => {
153
+ type ExpectedBindings = {
154
+ page: string
155
+ limit: string
156
+ }
157
+ const route = Route
158
+ .use(
159
+ RouteSchema.schemaSearchParams(
160
+ Schema.Struct({
161
+ "page": Schema.String,
162
+ }),
163
+ ),
164
+ )
165
+ .get(
166
+ RouteSchema.schemaSearchParams(
167
+ Schema.Struct({
168
+ "limit": Schema.String,
169
+ }),
170
+ ),
171
+ Route.html(function*(ctx) {
172
+ test
173
+ .expectTypeOf(ctx.searchParams)
174
+ .toExtend<ExpectedBindings>()
175
+
176
+ return `<h1>Hello, world!</h1>`
177
+ }),
178
+ )
179
+
180
+ test
181
+ .expectTypeOf<Route.Route.Context<typeof route>>()
182
+ .toExtend<{
183
+ searchParams: ExpectedBindings
184
+ }>()
185
+ })
186
+
187
+ test.it("parses search params from request", async () => {
188
+ const handler = RouteHttp.toWebHandler(
189
+ Route.get(
190
+ RouteSchema.schemaSearchParams(
191
+ Schema.Struct({
192
+ "page": Schema.NumberFromString,
193
+ "limit": Schema.NumberFromString,
194
+ }),
195
+ ),
196
+ Route.text(function*(ctx) {
197
+ return `page=${ctx.searchParams.page},limit=${ctx.searchParams.limit}`
198
+ }),
199
+ ),
200
+ )
201
+ const response = await Http.fetch(handler, {
202
+ path: "/test?page=2&limit=10",
203
+ })
204
+
205
+ test
206
+ .expect(response.status)
207
+ .toBe(200)
208
+ test
209
+ .expect(await response.text())
210
+ .toBe("page=2,limit=10")
211
+ })
212
+ })
213
+
214
+ test.describe(`${RouteSchema.schemaBodyJson.name}()`, () => {
215
+ test.it("parses JSON body from schema", () => {
216
+ type ExpectedBindings = {
217
+ name: string
218
+ age: number
219
+ }
220
+ const route = Route
221
+ .post(
222
+ RouteSchema.schemaBodyJson(
223
+ Schema.Struct({
224
+ name: Schema.String,
225
+ age: Schema.Number,
226
+ }),
227
+ ),
228
+ Route.json(function*(ctx) {
229
+ test
230
+ .expectTypeOf(ctx.body)
231
+ .toExtend<ExpectedBindings>()
232
+
233
+ return { received: ctx.body }
234
+ }),
235
+ )
236
+
237
+ test
238
+ .expectTypeOf<Route.Route.Context<typeof route>>()
239
+ .toExtend<{
240
+ body: ExpectedBindings
241
+ }>()
242
+ })
243
+
244
+ test.it("parses JSON body from request", async () => {
245
+ const handler = RouteHttp.toWebHandler(
246
+ Route.post(
247
+ RouteSchema.schemaBodyJson(
248
+ Schema.Struct({
249
+ name: Schema.String,
250
+ age: Schema.Number,
251
+ }),
252
+ ),
253
+ Route.json(function*(ctx) {
254
+ return { name: ctx.body.name, age: ctx.body.age }
255
+ }),
256
+ ),
257
+ )
258
+ const response = await Http.fetch(handler, {
259
+ path: "/test",
260
+ method: "POST",
261
+ headers: {
262
+ "Content-Type": "application/json",
263
+ },
264
+ body: JSON.stringify({ name: "Alice", age: 30 }),
265
+ })
266
+
267
+ test
268
+ .expect(response.status)
269
+ .toBe(200)
270
+ test
271
+ .expect(await response.json())
272
+ .toEqual({ name: "Alice", age: 30 })
273
+ })
274
+
275
+ test.it("returns error for invalid JSON body", () =>
276
+ Effect
277
+ .gen(function*() {
278
+ const runtime = yield* Effect.runtime<TestLogger.TestLogger>()
279
+ const handler = RouteHttp.toWebHandlerRuntime(runtime)(
280
+ Route.post(
281
+ RouteSchema.schemaBodyJson(
282
+ Schema.Struct({
283
+ name: Schema.String,
284
+ age: Schema.Number,
285
+ }),
286
+ ),
287
+ Route.json(function*(ctx) {
288
+ return ctx.body
289
+ }),
290
+ ),
291
+ )
292
+ const response = yield* Effect.promise(() =>
293
+ Http.fetch(handler, {
294
+ path: "/test",
295
+ method: "POST",
296
+ headers: {
297
+ "Content-Type": "application/json",
298
+ },
299
+ body: JSON.stringify({ name: "Alice", age: "not a number" }),
300
+ })
301
+ )
302
+
303
+ test
304
+ .expect(response.status)
305
+ .toBe(400)
306
+
307
+ const messages = yield* TestLogger.messages
308
+ test
309
+ .expect(messages.some((m) => m.includes("ParseError")))
310
+ .toBe(true)
311
+ })
312
+ .pipe(Effect.provide(TestLogger.layer()), Effect.runPromise))
313
+ })
314
+
315
+ test.describe(`${RouteSchema.schemaBodyUrlParams.name}()`, () => {
316
+ test.it("parses URL params body from schema", () => {
317
+ type ExpectedBindings = {
318
+ username: string
319
+ password: string
320
+ }
321
+ const route = Route
322
+ .post(
323
+ RouteSchema.schemaBodyUrlParams(
324
+ Schema.Struct({
325
+ username: Schema.String,
326
+ password: Schema.String,
327
+ }),
328
+ ),
329
+ Route.json(function*(ctx) {
330
+ test
331
+ .expectTypeOf(ctx.body)
332
+ .toExtend<ExpectedBindings>()
333
+
334
+ return { received: ctx.body }
335
+ }),
336
+ )
337
+
338
+ test
339
+ .expectTypeOf<Route.Route.Context<typeof route>>()
340
+ .toExtend<{
341
+ body: ExpectedBindings
342
+ }>()
343
+ })
344
+
345
+ test.it("parses URL-encoded body from request", async () => {
346
+ const handler = RouteHttp.toWebHandler(
347
+ Route.post(
348
+ RouteSchema.schemaBodyUrlParams(
349
+ Schema.Struct({
350
+ username: Schema.String,
351
+ password: Schema.String,
352
+ }),
353
+ ),
354
+ Route.json(function*(ctx) {
355
+ return { username: ctx.body.username, password: ctx.body.password }
356
+ }),
357
+ ),
358
+ )
359
+ const response = await Http.fetch(handler, {
360
+ path: "/test",
361
+ method: "POST",
362
+ headers: {
363
+ "Content-Type": "application/x-www-form-urlencoded",
364
+ },
365
+ body: "username=alice&password=secret123",
366
+ })
367
+
368
+ test
369
+ .expect(response.status)
370
+ .toBe(200)
371
+ test
372
+ .expect(await response.json())
373
+ .toEqual({ username: "alice", password: "secret123" })
374
+ })
375
+ })
376
+
377
+ test.describe(`${RouteSchema.schemaBodyMultipart.name}()`, () => {
378
+ test.it("has correct type signature for multipart body", () => {
379
+ const route = Route
380
+ .post(
381
+ RouteSchema.schemaBodyMultipart(
382
+ Schema.Struct({
383
+ name: Schema.String,
384
+ }),
385
+ ),
386
+ Route.json(function*(ctx) {
387
+ test
388
+ .expectTypeOf(ctx.body)
389
+ .toExtend<{ name: string }>()
390
+
391
+ return { name: ctx.body.name }
392
+ }),
393
+ )
394
+
395
+ test
396
+ .expectTypeOf<Route.Route.Context<typeof route>>()
397
+ .toExtend<{
398
+ body: { name: string }
399
+ }>()
400
+ })
401
+ })
402
+
403
+ test.describe(`${RouteSchema.schemaBodyForm.name}()`, () => {
404
+ test.it("has correct type signature for form body", () => {
405
+ const route = Route
406
+ .post(
407
+ RouteSchema.schemaBodyForm(
408
+ Schema.Struct({
409
+ email: Schema.String,
410
+ }),
411
+ ),
412
+ Route.json(function*(ctx) {
413
+ test
414
+ .expectTypeOf(ctx.body)
415
+ .toExtend<{ email: string }>()
416
+
417
+ return { email: ctx.body.email }
418
+ }),
419
+ )
420
+
421
+ test
422
+ .expectTypeOf<Route.Route.Context<typeof route>>()
423
+ .toExtend<{
424
+ body: { email: string }
425
+ }>()
426
+ })
427
+ })