@effect/platform 0.13.9 → 0.13.11

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.
Files changed (55) hide show
  1. package/Http/Body.d.ts +40 -21
  2. package/Http/Body.d.ts.map +1 -1
  3. package/Http/Body.js +14 -8
  4. package/Http/Body.js.map +1 -1
  5. package/Http/Client.d.ts +1 -1
  6. package/Http/Client.d.ts.map +1 -1
  7. package/Http/ClientRequest.d.ts +4 -24
  8. package/Http/ClientRequest.d.ts.map +1 -1
  9. package/Http/ClientRequest.js +1 -7
  10. package/Http/ClientRequest.js.map +1 -1
  11. package/Http/Router.d.ts +29 -25
  12. package/Http/Router.d.ts.map +1 -1
  13. package/Http/Router.js.map +1 -1
  14. package/Http/ServerResponse.d.ts +2 -25
  15. package/Http/ServerResponse.d.ts.map +1 -1
  16. package/Http/ServerResponse.js +2 -13
  17. package/Http/ServerResponse.js.map +1 -1
  18. package/internal/http/body.js +24 -15
  19. package/internal/http/body.js.map +1 -1
  20. package/internal/http/client.d.ts.map +1 -1
  21. package/internal/http/client.js +1 -6
  22. package/internal/http/client.js.map +1 -1
  23. package/internal/http/clientRequest.js +3 -14
  24. package/internal/http/clientRequest.js.map +1 -1
  25. package/internal/http/router.d.ts.map +1 -1
  26. package/internal/http/router.js.map +1 -1
  27. package/internal/http/serverResponse.js +3 -16
  28. package/internal/http/serverResponse.js.map +1 -1
  29. package/mjs/Http/Body.mjs +10 -5
  30. package/mjs/Http/Body.mjs.map +1 -1
  31. package/mjs/Http/ClientRequest.mjs +0 -5
  32. package/mjs/Http/ClientRequest.mjs.map +1 -1
  33. package/mjs/Http/Router.mjs.map +1 -1
  34. package/mjs/Http/ServerResponse.mjs +0 -9
  35. package/mjs/Http/ServerResponse.mjs.map +1 -1
  36. package/mjs/internal/http/body.mjs +20 -12
  37. package/mjs/internal/http/body.mjs.map +1 -1
  38. package/mjs/internal/http/client.mjs +1 -6
  39. package/mjs/internal/http/client.mjs.map +1 -1
  40. package/mjs/internal/http/clientRequest.mjs +2 -11
  41. package/mjs/internal/http/clientRequest.mjs.map +1 -1
  42. package/mjs/internal/http/router.mjs.map +1 -1
  43. package/mjs/internal/http/serverResponse.mjs +2 -13
  44. package/mjs/internal/http/serverResponse.mjs.map +1 -1
  45. package/package.json +1 -1
  46. package/src/Http/Body.ts +46 -24
  47. package/src/Http/Client.ts +1 -1
  48. package/src/Http/ClientRequest.ts +7 -27
  49. package/src/Http/Router.ts +73 -44
  50. package/src/Http/ServerResponse.ts +6 -34
  51. package/src/internal/http/body.ts +22 -22
  52. package/src/internal/http/client.ts +3 -11
  53. package/src/internal/http/clientRequest.ts +15 -39
  54. package/src/internal/http/router.ts +37 -17
  55. package/src/internal/http/serverResponse.ts +22 -52
@@ -8,8 +8,6 @@ import type * as FileSystem from "@effect/platform/FileSystem"
8
8
  import type * as Body from "@effect/platform/Http/Body"
9
9
  import type * as Etag from "@effect/platform/Http/Etag"
10
10
  import type * as Headers from "@effect/platform/Http/Headers"
11
- import type * as Error from "@effect/platform/Http/ServerError"
12
- import type * as ServerRequest from "@effect/platform/Http/ServerRequest"
13
11
  import type * as UrlParams from "@effect/platform/Http/UrlParams"
14
12
  import * as internal from "@effect/platform/internal/http/serverResponse"
15
13
  import type * as Schema from "@effect/schema/Schema"
@@ -39,19 +37,6 @@ export interface ServerResponse extends Pipeable {
39
37
  readonly body: Body.Body
40
38
  }
41
39
 
42
- /**
43
- * @since 1.0.0
44
- */
45
- export namespace ServerResponse {
46
- /**
47
- * @since 1.0.0
48
- * @category models
49
- */
50
- export interface NonEffectBody extends ServerResponse {
51
- readonly body: Body.NonEffect
52
- }
53
- }
54
-
55
40
  /**
56
41
  * @since 1.0.0
57
42
  * @category models
@@ -86,14 +71,6 @@ export namespace Options {
86
71
  */
87
72
  export const isServerResponse: (u: unknown) => u is ServerResponse = internal.isServerResponse
88
73
 
89
- /**
90
- * @since 1.0.0
91
- */
92
- export const toNonEffectBody: (
93
- self: ServerResponse
94
- ) => Effect.Effect<ServerRequest.ServerRequest, Error.ResponseError, ServerResponse.NonEffectBody> =
95
- internal.toNonEffectBody
96
-
97
74
  /**
98
75
  * @since 1.0.0
99
76
  * @category constructors
@@ -116,7 +93,10 @@ export const text: (body: string, options?: Options.WithContentType) => ServerRe
116
93
  * @since 1.0.0
117
94
  * @category constructors
118
95
  */
119
- export const json: (body: unknown, options?: Options.WithContent) => ServerResponse = internal.json
96
+ export const json: (
97
+ body: unknown,
98
+ options?: Options.WithContent
99
+ ) => Effect.Effect<never, Body.BodyError, ServerResponse> = internal.json
120
100
 
121
101
  /**
122
102
  * @since 1.0.0
@@ -124,7 +104,8 @@ export const json: (body: unknown, options?: Options.WithContent) => ServerRespo
124
104
  */
125
105
  export const schemaJson: <I, A>(
126
106
  schema: Schema.Schema<I, A>
127
- ) => (body: A, options?: Options.WithContent) => ServerResponse = internal.schemaJson
107
+ ) => (body: A, options?: Options.WithContent) => Effect.Effect<never, Body.BodyError, ServerResponse> =
108
+ internal.schemaJson
128
109
 
129
110
  /**
130
111
  * @since 1.0.0
@@ -138,15 +119,6 @@ export const unsafeJson: (body: unknown, options?: Options.WithContent) => Serve
138
119
  */
139
120
  export const urlParams: (body: UrlParams.Input, options?: Options.WithContent) => ServerResponse = internal.urlParams
140
121
 
141
- /**
142
- * @since 1.0.0
143
- * @category constructors
144
- */
145
- export const effect: (
146
- body: Effect.Effect<never, unknown, Body.NonEffect>,
147
- options?: Options.WithContent
148
- ) => ServerResponse = internal.effect
149
-
150
122
  /**
151
123
  * @since 1.0.0
152
124
  * @category constructors
@@ -1,3 +1,4 @@
1
+ import * as Data from "@effect/data/Data"
1
2
  import * as Effect from "@effect/io/Effect"
2
3
  import type * as PlatformError from "@effect/platform/Error"
3
4
  import * as FileSystem from "@effect/platform/FileSystem"
@@ -10,6 +11,17 @@ export const TypeId: Body.TypeId = Symbol.for(
10
11
  "@effect/platform/Http/Body"
11
12
  ) as Body.TypeId
12
13
 
14
+ /** @internal */
15
+ export const ErrorTypeId: Body.ErrorTypeId = Symbol.for(
16
+ "@effect/platform/Http/Body/BodyError"
17
+ ) as Body.ErrorTypeId
18
+
19
+ const bodyError = Data.tagged<Body.BodyError>("BodyError")
20
+
21
+ /** @internal */
22
+ export const BodyError = (reason: Body.BodyErrorReason): Body.BodyError =>
23
+ bodyError({ [ErrorTypeId]: ErrorTypeId, reason })
24
+
13
25
  class EmptyImpl implements Body.Empty {
14
26
  readonly [TypeId]: Body.TypeId
15
27
  readonly _tag = "Empty"
@@ -59,37 +71,25 @@ export const uint8Array = (body: Uint8Array, contentType?: string): Body.Uint8Ar
59
71
  export const text = (body: string, contentType?: string): Body.Uint8Array =>
60
72
  uint8Array(new TextEncoder().encode(body), contentType ?? "text/plain")
61
73
 
62
- class EffectBodyImpl implements Body.EffectBody {
63
- readonly [TypeId]: Body.TypeId
64
- readonly _tag = "Effect"
65
- constructor(
66
- readonly effect: Effect.Effect<never, unknown, Body.NonEffect>,
67
- readonly contentType?: string
68
- ) {
69
- this[TypeId] = TypeId
70
- }
71
- }
72
-
73
- /** @internal */
74
- export const effect = (
75
- body: Effect.Effect<never, unknown, Body.NonEffect>
76
- ): Body.EffectBody => new EffectBodyImpl(body)
77
-
78
74
  /** @internal */
79
75
  export const unsafeJson = (body: unknown): Body.Uint8Array =>
80
76
  uint8Array(new TextEncoder().encode(JSON.stringify(body)), "application/json")
81
77
 
82
78
  /** @internal */
83
- export const json = (body: unknown): Body.EffectBody => effect(Effect.try(() => unsafeJson(body)))
79
+ export const json = (body: unknown): Effect.Effect<never, Body.BodyError, Body.Uint8Array> =>
80
+ Effect.try({
81
+ try: () => unsafeJson(body),
82
+ catch: (error) => BodyError({ _tag: "JsonError", error })
83
+ })
84
84
 
85
85
  /** @internal */
86
86
  export const jsonSchema = <I, A>(schema: Schema.Schema<I, A>) => {
87
87
  const encode = Schema.encode(schema)
88
- return (body: A): Body.EffectBody =>
89
- effect(Effect.flatMap(
90
- encode(body),
91
- (json) => Effect.try(() => unsafeJson(json))
92
- ))
88
+ return (body: A): Effect.Effect<never, Body.BodyError, Body.Uint8Array> =>
89
+ Effect.flatMap(
90
+ Effect.mapError(encode(body), (error) => BodyError({ _tag: "SchemaError", error })),
91
+ json
92
+ )
93
93
  }
94
94
 
95
95
  /** @internal */
@@ -43,17 +43,9 @@ const setProto = <R, E, A>(
43
43
  /** @internal */
44
44
  export const make = (
45
45
  f: (
46
- request: ClientRequest.ClientRequest.NonEffectBody
46
+ request: ClientRequest.ClientRequest
47
47
  ) => Effect.Effect<never, Error.HttpClientError, ClientResponse.ClientResponse>
48
- ): Client.Client.Default => {
49
- function client(request: ClientRequest.ClientRequest) {
50
- return Effect.flatMap(
51
- internalRequest.resolveBody(request),
52
- (request) => f(request)
53
- )
54
- }
55
- return setProto(client)
56
- }
48
+ ): Client.Client.Default => setProto(f)
57
49
 
58
50
  /** @internal */
59
51
  export const fetch = (
@@ -98,7 +90,7 @@ export const fetch = (
98
90
  )
99
91
  )
100
92
 
101
- const convertBody = (body: Body.NonEffect): BodyInit | undefined => {
93
+ const convertBody = (body: Body.Body): BodyInit | undefined => {
102
94
  switch (body._tag) {
103
95
  case "Empty":
104
96
  return undefined
@@ -10,7 +10,6 @@ import * as Headers from "@effect/platform/Http/Headers"
10
10
  import type { Method } from "@effect/platform/Http/Method"
11
11
  import * as UrlParams from "@effect/platform/Http/UrlParams"
12
12
  import * as internalBody from "@effect/platform/internal/http/body"
13
- import * as internalError from "@effect/platform/internal/http/clientError"
14
13
  import type * as Schema from "@effect/schema/Schema"
15
14
  import type * as Stream from "@effect/stream/Stream"
16
15
 
@@ -313,20 +312,6 @@ export const uint8ArrayBody = dual<
313
312
  (self, body, contentType = "application/octet-stream") => setBody(self, internalBody.uint8Array(body, contentType))
314
313
  )
315
314
 
316
- /** @internal */
317
- export const effectBody = dual<
318
- (
319
- body: Effect.Effect<never, unknown, Body.NonEffect>
320
- ) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
321
- (
322
- self: ClientRequest.ClientRequest,
323
- body: Effect.Effect<never, unknown, Body.NonEffect>
324
- ) => ClientRequest.ClientRequest
325
- >(
326
- 2,
327
- (self, body) => setBody(self, internalBody.effect(body))
328
- )
329
-
330
315
  /** @internal */
331
316
  export const textBody = dual<
332
317
  (body: string, contentType?: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
@@ -338,9 +323,14 @@ export const textBody = dual<
338
323
 
339
324
  /** @internal */
340
325
  export const jsonBody = dual<
341
- (body: unknown) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
342
- (self: ClientRequest.ClientRequest, body: unknown) => ClientRequest.ClientRequest
343
- >(2, (self, body) => setBody(self, internalBody.json(body)))
326
+ (
327
+ body: unknown
328
+ ) => (self: ClientRequest.ClientRequest) => Effect.Effect<never, Body.BodyError, ClientRequest.ClientRequest>,
329
+ (
330
+ self: ClientRequest.ClientRequest,
331
+ body: unknown
332
+ ) => Effect.Effect<never, Body.BodyError, ClientRequest.ClientRequest>
333
+ >(2, (self, body) => Effect.map(internalBody.json(body), (body) => setBody(self, body)))
344
334
 
345
335
  /** @internal */
346
336
  export const unsafeJsonBody = dual<
@@ -368,14 +358,16 @@ export const fileBody = dual<
368
358
 
369
359
  /** @internal */
370
360
  export const schemaBody = <I, A>(schema: Schema.Schema<I, A>): {
371
- (body: A): (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest
372
- (self: ClientRequest.ClientRequest, body: A): ClientRequest.ClientRequest
361
+ (body: A): (self: ClientRequest.ClientRequest) => Effect.Effect<never, Body.BodyError, ClientRequest.ClientRequest>
362
+ (self: ClientRequest.ClientRequest, body: A): Effect.Effect<never, Body.BodyError, ClientRequest.ClientRequest>
373
363
  } => {
374
364
  const encode = internalBody.jsonSchema(schema)
375
365
  return dual<
376
- (body: A) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
377
- (self: ClientRequest.ClientRequest, body: A) => ClientRequest.ClientRequest
378
- >(2, (self, body) => setBody(self, encode(body)))
366
+ (
367
+ body: A
368
+ ) => (self: ClientRequest.ClientRequest) => Effect.Effect<never, Body.BodyError, ClientRequest.ClientRequest>,
369
+ (self: ClientRequest.ClientRequest, body: A) => Effect.Effect<never, Body.BodyError, ClientRequest.ClientRequest>
370
+ >(2, (self, body) => Effect.map(encode(body), (body) => setBody(self, body)))
379
371
  }
380
372
 
381
373
  /** @internal */
@@ -419,19 +411,3 @@ export const streamBody = dual<
419
411
  (self, body, { contentLength, contentType = "application/octet-stream" } = {}) =>
420
412
  setBody(self, internalBody.stream(body, contentType, contentLength))
421
413
  )
422
-
423
- /** @internal */
424
- export const resolveBody = (
425
- self: ClientRequest.ClientRequest
426
- ): Effect.Effect<never, Error.RequestError, ClientRequest.ClientRequest.NonEffectBody> =>
427
- self.body._tag === "Effect"
428
- ? Effect.map(
429
- Effect.mapError(self.body.effect, (error) =>
430
- internalError.requestError({
431
- reason: "Encode",
432
- request: self,
433
- error
434
- })),
435
- (body) => setBody(self, body) as ClientRequest.ClientRequest.NonEffectBody
436
- )
437
- : Effect.succeed(self as ClientRequest.ClientRequest.NonEffectBody)
@@ -12,6 +12,7 @@ import type * as Method from "@effect/platform/Http/Method"
12
12
  import type * as Router from "@effect/platform/Http/Router"
13
13
  import * as Error from "@effect/platform/Http/ServerError"
14
14
  import * as ServerRequest from "@effect/platform/Http/ServerRequest"
15
+ import type * as ServerResponse from "@effect/platform/Http/ServerResponse"
15
16
  import * as Schema from "@effect/schema/Schema"
16
17
  import * as Channel from "@effect/stream/Channel"
17
18
  import * as Sink from "@effect/stream/Sink"
@@ -133,7 +134,7 @@ const toHttpApp = <R, E>(
133
134
  request = sliceRequestUrl(request, route.prefix.value)
134
135
  }
135
136
  return Effect.mapInputContext(
136
- route.handler,
137
+ route.handler as Effect.Effect<Exclude<R, Router.RouteContext>, E, ServerResponse.ServerResponse>,
137
138
  (context) =>
138
139
  Context.add(
139
140
  Context.add(context, ServerRequest.ServerRequest, request),
@@ -241,15 +242,18 @@ export const mountApp = dual<
241
242
  <R1, E1>(
242
243
  path: string,
243
244
  that: App.Default<R1, E1>
244
- ) => <R, E>(self: Router.Router<R, E>) => Router.Router<R | R1, E | E1>,
245
+ ) => <R, E>(
246
+ self: Router.Router<R, E>
247
+ ) => Router.Router<Exclude<R | R1, Router.RouteContext | ServerRequest.ServerRequest>, E | E1>,
245
248
  <R, E, R1, E1>(
246
249
  self: Router.Router<R, E>,
247
250
  path: string,
248
251
  that: App.Default<R1, E1>
249
- ) => Router.Router<R | R1, E | E1>
252
+ ) => Router.Router<Exclude<R | R1, Router.RouteContext | ServerRequest.ServerRequest>, E | E1>
250
253
  >(
251
254
  3,
252
- (self, path, that) => new RouterImpl(self.routes, Chunk.append(self.mounts, [removeTrailingSlash(path), that]) as any)
255
+ (self, path, that) =>
256
+ new RouterImpl<any, any>(self.routes, Chunk.append(self.mounts, [removeTrailingSlash(path), that]))
253
257
  )
254
258
 
255
259
  /** @internal */
@@ -257,23 +261,27 @@ export const route = (method: Method.Method | "*"): {
257
261
  <R1, E1>(
258
262
  path: string,
259
263
  handler: Router.Route.Handler<R1, E1>
260
- ): <R, E>(self: Router.Router<R, E>) => Router.Router<R | Exclude<R1, Router.RouteContext>, E1 | E>
264
+ ): <R, E>(
265
+ self: Router.Router<R, E>
266
+ ) => Router.Router<R | Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest>, E1 | E>
261
267
  <R, E, R1, E1>(
262
268
  self: Router.Router<R, E>,
263
269
  path: string,
264
270
  handler: Router.Route.Handler<R1, E1>
265
- ): Router.Router<R | Exclude<R1, Router.RouteContext>, E | E1>
271
+ ): Router.Router<R | Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest>, E | E1>
266
272
  } =>
267
273
  dual<
268
274
  <R1, E1>(
269
275
  path: string,
270
276
  handler: Router.Route.Handler<R1, E1>
271
- ) => <R, E>(self: Router.Router<R, E>) => Router.Router<R | Exclude<R1, Router.RouteContext>, E | E1>,
277
+ ) => <R, E>(
278
+ self: Router.Router<R, E>
279
+ ) => Router.Router<R | Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest>, E | E1>,
272
280
  <R, E, R1, E1>(
273
281
  self: Router.Router<R, E>,
274
282
  path: string,
275
283
  handler: Router.Route.Handler<R1, E1>
276
- ) => Router.Router<R | Exclude<R1, Router.RouteContext>, E | E1>
284
+ ) => Router.Router<R | Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest>, E | E1>
277
285
  >(3, (self, path, handler) =>
278
286
  new RouterImpl(
279
287
  Chunk.append(self.routes, new RouteImpl(method, path, handler)) as any,
@@ -307,17 +315,17 @@ export const options = route("OPTIONS")
307
315
  /** @internal */
308
316
  export const use = dual<
309
317
  <R, E, R1, E1>(
310
- f: (self: Router.Route.Handler<R, E>) => Router.Route.Handler<R1, E1>
311
- ) => (self: Router.Router<R, E>) => Router.Router<R1, E1>,
318
+ f: (self: Router.Route.Handler<R, E>) => App.Default<R1, E1>
319
+ ) => (self: Router.Router<R, E>) => Router.Router<Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest>, E1>,
312
320
  <R, E, R1, E1>(
313
321
  self: Router.Router<R, E>,
314
- f: (self: Router.Route.Handler<R, E>) => Router.Route.Handler<R1, E1>
315
- ) => Router.Router<R1, E1>
322
+ f: (self: Router.Route.Handler<R, E>) => App.Default<R1, E1>
323
+ ) => Router.Router<Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest>, E1>
316
324
  >(2, (self, f) =>
317
- new RouterImpl(
325
+ new RouterImpl<any, any>(
318
326
  Chunk.map(
319
327
  self.routes,
320
- (route) => new RouteImpl(route.method, route.path, f(route.handler), route.prefix)
328
+ (route) => new RouteImpl(route.method, route.path, f(route.handler) as any, route.prefix)
321
329
  ),
322
330
  Chunk.map(
323
331
  self.mounts,
@@ -412,19 +420,31 @@ export const provideService = dual<
412
420
  tag: T,
413
421
  service: Context.Tag.Service<T>
414
422
  ) => Router.Router<Exclude<R, Context.Tag.Identifier<T>>, E>
415
- >(3, (self, tag, service) => use(self, Effect.provideService(tag, service)))
423
+ >(3, <R, E, T extends Context.Tag<any, any>>(
424
+ self: Router.Router<R, E>,
425
+ tag: T,
426
+ service: Context.Tag.Service<T>
427
+ ): Router.Router<Exclude<R, Context.Tag.Identifier<T>>, E> => use(self, Effect.provideService(tag, service)))
416
428
 
417
429
  /* @internal */
418
430
  export const provideServiceEffect = dual<
419
431
  <T extends Context.Tag<any, any>, R1, E1>(
420
432
  tag: T,
421
433
  effect: Effect.Effect<R1, E1, Context.Tag.Service<T>>
422
- ) => <R, E>(self: Router.Router<R, E>) => Router.Router<R1 | Exclude<R, Context.Tag.Identifier<T>>, E | E1>,
434
+ ) => <R, E>(
435
+ self: Router.Router<R, E>
436
+ ) => Router.Router<
437
+ Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest> | Exclude<R, Context.Tag.Identifier<T>>,
438
+ E | E1
439
+ >,
423
440
  <R, E, T extends Context.Tag<any, any>, R1, E1>(
424
441
  self: Router.Router<R, E>,
425
442
  tag: T,
426
443
  effect: Effect.Effect<R1, E1, Context.Tag.Service<T>>
427
- ) => Router.Router<R1 | Exclude<R, Context.Tag.Identifier<T>>, E | E1>
444
+ ) => Router.Router<
445
+ Exclude<R1, Router.RouteContext | ServerRequest.ServerRequest> | Exclude<R, Context.Tag.Identifier<T>>,
446
+ E | E1
447
+ >
428
448
  >(3, <R, E, T extends Context.Tag<any, any>, R1, E1>(
429
449
  self: Router.Router<R, E>,
430
450
  tag: T,
@@ -6,12 +6,9 @@ import * as FileSystem from "@effect/platform/FileSystem"
6
6
  import type * as Body from "@effect/platform/Http/Body"
7
7
  import * as Etag from "@effect/platform/Http/Etag"
8
8
  import * as Headers from "@effect/platform/Http/Headers"
9
- import type * as Error from "@effect/platform/Http/ServerError"
10
- import * as ServerRequest from "@effect/platform/Http/ServerRequest"
11
9
  import type * as ServerResponse from "@effect/platform/Http/ServerResponse"
12
10
  import * as UrlParams from "@effect/platform/Http/UrlParams"
13
11
  import * as internalBody from "@effect/platform/internal/http/body"
14
- import * as internalError from "@effect/platform/internal/http/serverError"
15
12
  import type * as Schema from "@effect/schema/Schema"
16
13
  import type * as Stream from "@effect/stream/Stream"
17
14
  import * as Mime from "mime"
@@ -51,29 +48,6 @@ class ServerResponseImpl implements ServerResponse.ServerResponse {
51
48
  export const isServerResponse = (u: unknown): u is ServerResponse.ServerResponse =>
52
49
  typeof u === "object" && u !== null && TypeId in u
53
50
 
54
- /** @internal */
55
- export const toNonEffectBody = (
56
- self: ServerResponse.ServerResponse
57
- ): Effect.Effect<ServerRequest.ServerRequest, Error.ResponseError, ServerResponse.ServerResponse.NonEffectBody> =>
58
- self.body._tag === "Effect" ?
59
- Effect.map(
60
- Effect.catchAll(self.body.effect, (error) =>
61
- Effect.flatMap(
62
- ServerRequest.ServerRequest,
63
- (request) =>
64
- Effect.fail(
65
- internalError.responseError({
66
- reason: "Decode",
67
- request,
68
- response: self,
69
- error
70
- })
71
- )
72
- )),
73
- (body) => setBody(self, body) as ServerResponse.ServerResponse.NonEffectBody
74
- ) :
75
- Effect.succeed(self as ServerResponse.ServerResponse.NonEffectBody)
76
-
77
51
  /** @internal */
78
52
  export const empty = (options?: ServerResponse.Options.WithContent): ServerResponse.ServerResponse =>
79
53
  new ServerResponseImpl(
@@ -105,13 +79,17 @@ export const text = (body: string, options?: ServerResponse.Options.WithContentT
105
79
  )
106
80
 
107
81
  /** @internal */
108
- export const json = (body: unknown, options?: ServerResponse.Options.WithContent): ServerResponse.ServerResponse =>
109
- new ServerResponseImpl(
110
- options?.status ?? 200,
111
- options?.statusText,
112
- options?.headers ?? Headers.empty,
113
- internalBody.json(body)
114
- )
82
+ export const json = (
83
+ body: unknown,
84
+ options?: ServerResponse.Options.WithContent
85
+ ): Effect.Effect<never, Body.BodyError, ServerResponse.ServerResponse> =>
86
+ Effect.map(internalBody.json(body), (body) =>
87
+ new ServerResponseImpl(
88
+ options?.status ?? 200,
89
+ options?.statusText,
90
+ options?.headers ?? Headers.empty,
91
+ body
92
+ ))
115
93
 
116
94
  /** @internal */
117
95
  export const unsafeJson = (
@@ -130,13 +108,17 @@ export const schemaJson = <I, A>(
130
108
  schema: Schema.Schema<I, A>
131
109
  ) => {
132
110
  const encode = internalBody.jsonSchema(schema)
133
- return (body: A, options?: ServerResponse.Options.WithContent): ServerResponse.ServerResponse =>
134
- new ServerResponseImpl(
135
- options?.status ?? 200,
136
- options?.statusText,
137
- options?.headers ?? Headers.empty,
138
- encode(body)
139
- )
111
+ return (
112
+ body: A,
113
+ options?: ServerResponse.Options.WithContent
114
+ ): Effect.Effect<never, Body.BodyError, ServerResponse.ServerResponse> =>
115
+ Effect.map(encode(body), (body) =>
116
+ new ServerResponseImpl(
117
+ options?.status ?? 200,
118
+ options?.statusText,
119
+ options?.headers ?? Headers.empty,
120
+ body
121
+ ))
140
122
  }
141
123
 
142
124
  /** @internal */
@@ -189,18 +171,6 @@ export const urlParams = (
189
171
  internalBody.text(UrlParams.toString(UrlParams.fromInput(body)), "application/x-www-form-urlencoded")
190
172
  )
191
173
 
192
- /** @internal */
193
- export const effect = (
194
- body: Effect.Effect<never, unknown, Body.NonEffect>,
195
- options?: ServerResponse.Options.WithContent
196
- ): ServerResponse.ServerResponse =>
197
- new ServerResponseImpl(
198
- options?.status ?? 200,
199
- options?.statusText,
200
- options?.headers ?? Headers.empty,
201
- internalBody.effect(body)
202
- )
203
-
204
174
  /** @internal */
205
175
  export const raw = (body: unknown, options?: ServerResponse.Options): ServerResponse.ServerResponse =>
206
176
  new ServerResponseImpl(