@effect/platform 0.13.8 → 0.13.10

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 (51) 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/ServerResponse.d.ts +2 -25
  12. package/Http/ServerResponse.d.ts.map +1 -1
  13. package/Http/ServerResponse.js +2 -13
  14. package/Http/ServerResponse.js.map +1 -1
  15. package/internal/http/body.js +24 -15
  16. package/internal/http/body.js.map +1 -1
  17. package/internal/http/client.d.ts.map +1 -1
  18. package/internal/http/client.js +1 -6
  19. package/internal/http/client.js.map +1 -1
  20. package/internal/http/clientRequest.js +3 -14
  21. package/internal/http/clientRequest.js.map +1 -1
  22. package/internal/http/middleware.js +12 -9
  23. package/internal/http/middleware.js.map +1 -1
  24. package/internal/http/serverResponse.js +3 -16
  25. package/internal/http/serverResponse.js.map +1 -1
  26. package/mjs/Http/Body.mjs +10 -5
  27. package/mjs/Http/Body.mjs.map +1 -1
  28. package/mjs/Http/ClientRequest.mjs +0 -5
  29. package/mjs/Http/ClientRequest.mjs.map +1 -1
  30. package/mjs/Http/ServerResponse.mjs +0 -9
  31. package/mjs/Http/ServerResponse.mjs.map +1 -1
  32. package/mjs/internal/http/body.mjs +20 -12
  33. package/mjs/internal/http/body.mjs.map +1 -1
  34. package/mjs/internal/http/client.mjs +1 -6
  35. package/mjs/internal/http/client.mjs.map +1 -1
  36. package/mjs/internal/http/clientRequest.mjs +2 -11
  37. package/mjs/internal/http/clientRequest.mjs.map +1 -1
  38. package/mjs/internal/http/middleware.mjs +12 -9
  39. package/mjs/internal/http/middleware.mjs.map +1 -1
  40. package/mjs/internal/http/serverResponse.mjs +2 -13
  41. package/mjs/internal/http/serverResponse.mjs.map +1 -1
  42. package/package.json +1 -1
  43. package/src/Http/Body.ts +46 -24
  44. package/src/Http/Client.ts +1 -1
  45. package/src/Http/ClientRequest.ts +7 -27
  46. package/src/Http/ServerResponse.ts +6 -34
  47. package/src/internal/http/body.ts +22 -22
  48. package/src/internal/http/client.ts +3 -11
  49. package/src/internal/http/clientRequest.ts +15 -39
  50. package/src/internal/http/middleware.ts +10 -9
  51. 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)
@@ -8,11 +8,12 @@ import * as ServerRequest from "@effect/platform/Http/ServerRequest"
8
8
  export const make = <M extends Middleware.Middleware>(middleware: M): M => middleware
9
9
 
10
10
  /** @internal */
11
- export const logger = make((httpApp) =>
12
- Effect.withLogSpan(
13
- Effect.flatMap(
14
- ServerRequest.ServerRequest,
15
- (request) =>
11
+ export const logger = make((httpApp) => {
12
+ let counter = 0
13
+ return Effect.flatMap(
14
+ ServerRequest.ServerRequest,
15
+ (request) =>
16
+ Effect.withLogSpan(
16
17
  Effect.tap(
17
18
  Effect.tapErrorCause(httpApp, (cause) =>
18
19
  Effect.annotateLogs(Effect.log(cause), {
@@ -26,11 +27,11 @@ export const logger = make((httpApp) =>
26
27
  "http.url": request.url,
27
28
  "http.status": response.status
28
29
  })
29
- )
30
- ),
31
- "http.span"
30
+ ),
31
+ `http.span.${++counter}`
32
+ )
32
33
  )
33
- )
34
+ })
34
35
 
35
36
  /** @internal */
36
37
  export const tracer = make((httpApp) =>
@@ -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(