@effect/platform 0.64.1 → 0.65.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/FetchHttpClient/package.json +6 -0
- package/README.md +173 -195
- package/dist/cjs/FetchHttpClient.js +32 -0
- package/dist/cjs/FetchHttpClient.js.map +1 -0
- package/dist/cjs/HttpApiClient.js +1 -1
- package/dist/cjs/HttpApiClient.js.map +1 -1
- package/dist/cjs/HttpClient.js +5 -36
- package/dist/cjs/HttpClient.js.map +1 -1
- package/dist/cjs/HttpClientRequest.js +11 -11
- package/dist/cjs/HttpClientRequest.js.map +1 -1
- package/dist/cjs/HttpClientResponse.js +2 -61
- package/dist/cjs/HttpClientResponse.js.map +1 -1
- package/dist/cjs/HttpIncomingMessage.js +2 -29
- package/dist/cjs/HttpIncomingMessage.js.map +1 -1
- package/dist/cjs/Runtime.js +1 -1
- package/dist/cjs/Runtime.js.map +1 -1
- package/dist/cjs/Socket.js +42 -53
- package/dist/cjs/Socket.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/internal/fetchHttpClient.js +52 -0
- package/dist/cjs/internal/fetchHttpClient.js.map +1 -0
- package/dist/cjs/internal/httpClient.js +82 -71
- package/dist/cjs/internal/httpClient.js.map +1 -1
- package/dist/cjs/internal/httpClientRequest.js +17 -15
- package/dist/cjs/internal/httpClientRequest.js.map +1 -1
- package/dist/cjs/internal/httpClientResponse.js +1 -33
- package/dist/cjs/internal/httpClientResponse.js.map +1 -1
- package/dist/cjs/internal/worker.js +21 -29
- package/dist/cjs/internal/worker.js.map +1 -1
- package/dist/dts/FetchHttpClient.d.ts +27 -0
- package/dist/dts/FetchHttpClient.d.ts.map +1 -0
- package/dist/dts/HttpApiClient.d.ts +2 -2
- package/dist/dts/HttpClient.d.ts +53 -61
- package/dist/dts/HttpClient.d.ts.map +1 -1
- package/dist/dts/HttpClientRequest.d.ts +16 -15
- package/dist/dts/HttpClientRequest.d.ts.map +1 -1
- package/dist/dts/HttpClientResponse.d.ts +4 -90
- package/dist/dts/HttpClientResponse.d.ts.map +1 -1
- package/dist/dts/HttpIncomingMessage.d.ts +0 -16
- package/dist/dts/HttpIncomingMessage.d.ts.map +1 -1
- package/dist/dts/HttpServer.d.ts +1 -1
- package/dist/dts/Socket.d.ts +3 -3
- package/dist/dts/Socket.d.ts.map +1 -1
- package/dist/dts/index.d.ts +4 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/internal/fetchHttpClient.d.ts +2 -0
- package/dist/dts/internal/fetchHttpClient.d.ts.map +1 -0
- package/dist/esm/FetchHttpClient.js +21 -0
- package/dist/esm/FetchHttpClient.js.map +1 -0
- package/dist/esm/HttpApiClient.js +1 -1
- package/dist/esm/HttpApiClient.js.map +1 -1
- package/dist/esm/HttpClient.js +4 -35
- package/dist/esm/HttpClient.js.map +1 -1
- package/dist/esm/HttpClientRequest.js +10 -10
- package/dist/esm/HttpClientRequest.js.map +1 -1
- package/dist/esm/HttpClientResponse.js +1 -63
- package/dist/esm/HttpClientResponse.js.map +1 -1
- package/dist/esm/HttpIncomingMessage.js +0 -24
- package/dist/esm/HttpIncomingMessage.js.map +1 -1
- package/dist/esm/Runtime.js +1 -1
- package/dist/esm/Runtime.js.map +1 -1
- package/dist/esm/Socket.js +42 -53
- package/dist/esm/Socket.js.map +1 -1
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/fetchHttpClient.js +44 -0
- package/dist/esm/internal/fetchHttpClient.js.map +1 -0
- package/dist/esm/internal/httpClient.js +79 -69
- package/dist/esm/internal/httpClient.js.map +1 -1
- package/dist/esm/internal/httpClientRequest.js +15 -13
- package/dist/esm/internal/httpClientRequest.js.map +1 -1
- package/dist/esm/internal/httpClientResponse.js +0 -24
- package/dist/esm/internal/httpClientResponse.js.map +1 -1
- package/dist/esm/internal/worker.js +21 -29
- package/dist/esm/internal/worker.js.map +1 -1
- package/package.json +11 -3
- package/src/FetchHttpClient.ts +25 -0
- package/src/HttpApiClient.ts +5 -5
- package/src/HttpClient.ts +79 -85
- package/src/HttpClientRequest.ts +26 -25
- package/src/HttpClientResponse.ts +4 -162
- package/src/HttpIncomingMessage.ts +0 -43
- package/src/HttpServer.ts +1 -1
- package/src/Runtime.ts +1 -1
- package/src/Socket.ts +42 -58
- package/src/index.ts +5 -0
- package/src/internal/fetchHttpClient.ts +53 -0
- package/src/internal/httpClient.ts +149 -125
- package/src/internal/httpClientRequest.ts +33 -18
- package/src/internal/httpClientResponse.ts +6 -96
- package/src/internal/worker.ts +40 -55
|
@@ -7,25 +7,23 @@ import type * as Fiber from "effect/Fiber"
|
|
|
7
7
|
import * as FiberRef from "effect/FiberRef"
|
|
8
8
|
import { constFalse, dual } from "effect/Function"
|
|
9
9
|
import { globalValue } from "effect/GlobalValue"
|
|
10
|
+
import * as Inspectable from "effect/Inspectable"
|
|
10
11
|
import * as Layer from "effect/Layer"
|
|
11
12
|
import { pipeArguments } from "effect/Pipeable"
|
|
12
13
|
import * as Predicate from "effect/Predicate"
|
|
13
14
|
import * as Ref from "effect/Ref"
|
|
14
15
|
import type * as Schedule from "effect/Schedule"
|
|
15
16
|
import * as Scope from "effect/Scope"
|
|
16
|
-
import * as Stream from "effect/Stream"
|
|
17
17
|
import * as Cookies from "../Cookies.js"
|
|
18
18
|
import * as Headers from "../Headers.js"
|
|
19
19
|
import type * as Client from "../HttpClient.js"
|
|
20
20
|
import * as Error from "../HttpClientError.js"
|
|
21
21
|
import type * as ClientRequest from "../HttpClientRequest.js"
|
|
22
22
|
import type * as ClientResponse from "../HttpClientResponse.js"
|
|
23
|
-
import * as Method from "../HttpMethod.js"
|
|
24
23
|
import * as TraceContext from "../HttpTraceContext.js"
|
|
25
24
|
import * as UrlParams from "../UrlParams.js"
|
|
26
25
|
import * as internalBody from "./httpBody.js"
|
|
27
26
|
import * as internalRequest from "./httpClientRequest.js"
|
|
28
|
-
import * as internalResponse from "./httpClientResponse.js"
|
|
29
27
|
|
|
30
28
|
/** @internal */
|
|
31
29
|
export const TypeId: Client.TypeId = Symbol.for(
|
|
@@ -33,7 +31,7 @@ export const TypeId: Client.TypeId = Symbol.for(
|
|
|
33
31
|
) as Client.TypeId
|
|
34
32
|
|
|
35
33
|
/** @internal */
|
|
36
|
-
export const tag = Context.GenericTag<Client.HttpClient.
|
|
34
|
+
export const tag = Context.GenericTag<Client.HttpClient.Service>("@effect/platform/HttpClient")
|
|
37
35
|
|
|
38
36
|
/** @internal */
|
|
39
37
|
export const currentTracerDisabledWhen = globalValue(
|
|
@@ -69,57 +67,72 @@ export const withTracerPropagation = dual<
|
|
|
69
67
|
) => Effect.Effect<A, E, R>
|
|
70
68
|
>(2, (self, enabled) => Effect.locally(self, currentTracerPropagation, enabled))
|
|
71
69
|
|
|
72
|
-
|
|
73
|
-
export const currentFetchOptions = globalValue(
|
|
74
|
-
Symbol.for("@effect/platform/HttpClient/currentFetchOptions"),
|
|
75
|
-
() => FiberRef.unsafeMake<RequestInit>({})
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
/** @internal */
|
|
79
|
-
export const withFetchOptions = dual<
|
|
80
|
-
(
|
|
81
|
-
options: RequestInit
|
|
82
|
-
) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>,
|
|
83
|
-
<A, E, R>(
|
|
84
|
-
effect: Effect.Effect<A, E, R>,
|
|
85
|
-
options: RequestInit
|
|
86
|
-
) => Effect.Effect<A, E, R>
|
|
87
|
-
>(2, (self, options) => Effect.locally(self, currentFetchOptions, options))
|
|
88
|
-
|
|
89
|
-
const clientProto = {
|
|
70
|
+
const ClientProto = {
|
|
90
71
|
[TypeId]: TypeId,
|
|
91
72
|
pipe() {
|
|
92
73
|
return pipeArguments(this, arguments)
|
|
74
|
+
},
|
|
75
|
+
...Inspectable.BaseProto,
|
|
76
|
+
toJSON() {
|
|
77
|
+
return {
|
|
78
|
+
_id: "@effect/platform/HttpClient"
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
get(this: Client.HttpClient.Service, url: string | URL, options?: ClientRequest.Options.NoBody) {
|
|
82
|
+
return this.execute(internalRequest.get(url, options))
|
|
83
|
+
},
|
|
84
|
+
head(this: Client.HttpClient.Service, url: string | URL, options?: ClientRequest.Options.NoBody) {
|
|
85
|
+
return this.execute(internalRequest.head(url, options))
|
|
86
|
+
},
|
|
87
|
+
post(this: Client.HttpClient.Service, url: string | URL, options: ClientRequest.Options.NoUrl) {
|
|
88
|
+
return this.execute(internalRequest.post(url, options))
|
|
89
|
+
},
|
|
90
|
+
put(this: Client.HttpClient.Service, url: string | URL, options: ClientRequest.Options.NoUrl) {
|
|
91
|
+
return this.execute(internalRequest.put(url, options))
|
|
92
|
+
},
|
|
93
|
+
patch(this: Client.HttpClient.Service, url: string | URL, options: ClientRequest.Options.NoUrl) {
|
|
94
|
+
return this.execute(internalRequest.patch(url, options))
|
|
95
|
+
},
|
|
96
|
+
del(this: Client.HttpClient.Service, url: string | URL, options?: ClientRequest.Options.NoUrl) {
|
|
97
|
+
return this.execute(internalRequest.del(url, options))
|
|
98
|
+
},
|
|
99
|
+
options(this: Client.HttpClient.Service, url: string | URL, options?: ClientRequest.Options.NoBody) {
|
|
100
|
+
return this.execute(internalRequest.options(url, options))
|
|
93
101
|
}
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
const isClient = (u: unknown): u is Client.HttpClient<unknown, unknown, unknown> => Predicate.hasProperty(u, TypeId)
|
|
97
105
|
|
|
106
|
+
interface HttpClientImpl<A, E, R> extends Client.HttpClient<A, E, R> {
|
|
107
|
+
readonly preprocess: Client.HttpClient.Preprocess<E, R>
|
|
108
|
+
readonly postprocess: Client.HttpClient.Postprocess<A, E, R>
|
|
109
|
+
}
|
|
110
|
+
|
|
98
111
|
/** @internal */
|
|
99
112
|
export const make = <E2, R2, A, E, R>(
|
|
100
|
-
|
|
113
|
+
postprocess: (
|
|
101
114
|
request: Effect.Effect<ClientRequest.HttpClientRequest, E2, R2>
|
|
102
115
|
) => Effect.Effect<A, E, R>,
|
|
103
116
|
preprocess: Client.HttpClient.Preprocess<E2, R2>
|
|
104
117
|
): Client.HttpClient<A, E, R> => {
|
|
105
|
-
|
|
106
|
-
|
|
118
|
+
const self = Object.create(ClientProto)
|
|
119
|
+
self.preprocess = preprocess
|
|
120
|
+
self.postprocess = postprocess
|
|
121
|
+
self.execute = function(request: ClientRequest.HttpClientRequest) {
|
|
122
|
+
return postprocess(preprocess(request))
|
|
107
123
|
}
|
|
108
|
-
|
|
109
|
-
;(client as any).preprocess = preprocess
|
|
110
|
-
;(client as any).execute = execute
|
|
111
|
-
return client as any
|
|
124
|
+
return self
|
|
112
125
|
}
|
|
113
126
|
|
|
114
127
|
/** @internal */
|
|
115
|
-
export const
|
|
128
|
+
export const makeService = (
|
|
116
129
|
f: (
|
|
117
130
|
request: ClientRequest.HttpClientRequest,
|
|
118
131
|
url: URL,
|
|
119
132
|
signal: AbortSignal,
|
|
120
133
|
fiber: Fiber.RuntimeFiber<ClientResponse.HttpClientResponse, Error.HttpClientError>
|
|
121
134
|
) => Effect.Effect<ClientResponse.HttpClientResponse, Error.HttpClientError, Scope.Scope>
|
|
122
|
-
): Client.HttpClient.
|
|
135
|
+
): Client.HttpClient.Service =>
|
|
123
136
|
make((effect) =>
|
|
124
137
|
Effect.flatMap(effect, (request) =>
|
|
125
138
|
Effect.withFiberRuntime((fiber) => {
|
|
@@ -188,52 +201,6 @@ export const makeDefault = (
|
|
|
188
201
|
)
|
|
189
202
|
})), Effect.succeed as Client.HttpClient.Preprocess<never, never>)
|
|
190
203
|
|
|
191
|
-
/** @internal */
|
|
192
|
-
export const Fetch = Context.GenericTag<Client.Fetch, typeof globalThis.fetch>(
|
|
193
|
-
"@effect/platform/HttpClient/Fetch"
|
|
194
|
-
)
|
|
195
|
-
|
|
196
|
-
/** @internal */
|
|
197
|
-
export const fetch: Client.HttpClient.Default = makeDefault((request, url, signal, fiber) => {
|
|
198
|
-
const context = fiber.getFiberRef(FiberRef.currentContext)
|
|
199
|
-
const fetch: typeof globalThis.fetch = context.unsafeMap.get(Fetch.key) ?? globalThis.fetch
|
|
200
|
-
const options = fiber.getFiberRef(currentFetchOptions)
|
|
201
|
-
const headers = new globalThis.Headers(request.headers)
|
|
202
|
-
const send = (body: BodyInit | undefined) =>
|
|
203
|
-
Effect.map(
|
|
204
|
-
Effect.tryPromise({
|
|
205
|
-
try: () =>
|
|
206
|
-
fetch(url, {
|
|
207
|
-
...options,
|
|
208
|
-
method: request.method,
|
|
209
|
-
headers,
|
|
210
|
-
body,
|
|
211
|
-
duplex: request.body._tag === "Stream" ? "half" : undefined,
|
|
212
|
-
signal
|
|
213
|
-
} as any),
|
|
214
|
-
catch: (cause) =>
|
|
215
|
-
new Error.RequestError({
|
|
216
|
-
request,
|
|
217
|
-
reason: "Transport",
|
|
218
|
-
cause
|
|
219
|
-
})
|
|
220
|
-
}),
|
|
221
|
-
(response) => internalResponse.fromWeb(request, response)
|
|
222
|
-
)
|
|
223
|
-
if (Method.hasBody(request.method)) {
|
|
224
|
-
switch (request.body._tag) {
|
|
225
|
-
case "Raw":
|
|
226
|
-
case "Uint8Array":
|
|
227
|
-
return send(request.body.body as any)
|
|
228
|
-
case "FormData":
|
|
229
|
-
return send(request.body.formData)
|
|
230
|
-
case "Stream":
|
|
231
|
-
return Effect.flatMap(Stream.toReadableStreamEffect(request.body.stream), send)
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return send(undefined)
|
|
235
|
-
})
|
|
236
|
-
|
|
237
204
|
/** @internal */
|
|
238
205
|
export const transform = dual<
|
|
239
206
|
<A, E, R, A1, E1, R1>(
|
|
@@ -249,11 +216,13 @@ export const transform = dual<
|
|
|
249
216
|
request: ClientRequest.HttpClientRequest
|
|
250
217
|
) => Effect.Effect<A1, E1, R1>
|
|
251
218
|
) => Client.HttpClient<A1, E | E1, R | R1>
|
|
252
|
-
>(2, (self, f) =>
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
219
|
+
>(2, (self, f) => {
|
|
220
|
+
const client = self as HttpClientImpl<any, any, any>
|
|
221
|
+
return make(
|
|
222
|
+
Effect.flatMap((request) => f(client.postprocess(Effect.succeed(request)), request)),
|
|
223
|
+
client.preprocess
|
|
224
|
+
)
|
|
225
|
+
})
|
|
257
226
|
|
|
258
227
|
/** @internal */
|
|
259
228
|
export const filterStatus = dual<
|
|
@@ -297,12 +266,6 @@ export const filterStatusOk = <E, R>(
|
|
|
297
266
|
})
|
|
298
267
|
))
|
|
299
268
|
|
|
300
|
-
/** @internal */
|
|
301
|
-
export const fetchOk: Client.HttpClient.Default = filterStatusOk(fetch)
|
|
302
|
-
|
|
303
|
-
/** @internal */
|
|
304
|
-
export const layer = Layer.succeed(tag, fetch)
|
|
305
|
-
|
|
306
269
|
/** @internal */
|
|
307
270
|
export const transformResponse = dual<
|
|
308
271
|
<A, E, R, A1, E1, R1>(
|
|
@@ -312,7 +275,10 @@ export const transformResponse = dual<
|
|
|
312
275
|
self: Client.HttpClient<A, E, R>,
|
|
313
276
|
f: (effect: Effect.Effect<A, E, R>) => Effect.Effect<A1, E1, R1>
|
|
314
277
|
) => Client.HttpClient<A1, E1, R1>
|
|
315
|
-
>(2, (self, f) =>
|
|
278
|
+
>(2, (self, f) => {
|
|
279
|
+
const client = self as HttpClientImpl<any, any, any>
|
|
280
|
+
return make((request) => f(client.postprocess(request)), client.preprocess)
|
|
281
|
+
})
|
|
316
282
|
|
|
317
283
|
/** @internal */
|
|
318
284
|
export const catchTag: {
|
|
@@ -510,32 +476,50 @@ export const catchAll: {
|
|
|
510
476
|
)
|
|
511
477
|
|
|
512
478
|
/** @internal */
|
|
513
|
-
export const filterOrElse
|
|
479
|
+
export const filterOrElse: {
|
|
480
|
+
<A, B extends A, C, E2, R2>(
|
|
481
|
+
refinement: Predicate.Refinement<NoInfer<A>, B>,
|
|
482
|
+
orElse: (a: NoInfer<A>) => Effect.Effect<C, E2, R2>
|
|
483
|
+
): <E, R>(self: Client.HttpClient<A, E, R>) => Client.HttpClient<B | C, E | E2, R | R2>
|
|
514
484
|
<A, B, E2, R2>(
|
|
515
|
-
|
|
516
|
-
orElse: (a: A) => Effect.Effect<B, E2, R2>
|
|
517
|
-
)
|
|
485
|
+
predicate: Predicate.Predicate<NoInfer<A>>,
|
|
486
|
+
orElse: (a: NoInfer<A>) => Effect.Effect<B, E2, R2>
|
|
487
|
+
): <E, R>(
|
|
518
488
|
self: Client.HttpClient<A, E, R>
|
|
519
|
-
) => Client.HttpClient<A | B, E2 | E, R2 | R
|
|
489
|
+
) => Client.HttpClient<A | B, E2 | E, R2 | R>
|
|
490
|
+
<A, E, R, B extends A, C, E2, R2>(
|
|
491
|
+
self: Client.HttpClient<A, E, R>,
|
|
492
|
+
refinement: Predicate.Refinement<A, B>,
|
|
493
|
+
orElse: (a: A) => Effect.Effect<C, E2, R2>
|
|
494
|
+
): Client.HttpClient<B | C, E | E2, R | R2>
|
|
520
495
|
<A, E, R, B, E2, R2>(
|
|
521
496
|
self: Client.HttpClient<A, E, R>,
|
|
522
|
-
|
|
497
|
+
predicate: Predicate.Predicate<A>,
|
|
523
498
|
orElse: (a: A) => Effect.Effect<B, E2, R2>
|
|
524
|
-
)
|
|
525
|
-
|
|
499
|
+
): Client.HttpClient<A | B, E2 | E, R2 | R>
|
|
500
|
+
} = dual(3, (self, f, orElse) => transformResponse(self, Effect.filterOrElse(f, orElse)))
|
|
526
501
|
|
|
527
502
|
/** @internal */
|
|
528
|
-
export const filterOrFail
|
|
503
|
+
export const filterOrFail: {
|
|
504
|
+
<A, B extends A, E2>(
|
|
505
|
+
refinement: Predicate.Refinement<NoInfer<A>, B>,
|
|
506
|
+
orFailWith: (a: NoInfer<A>) => E2
|
|
507
|
+
): <E, R>(self: Client.HttpClient<A, E, R>) => Client.HttpClient<B, E | E2, R>
|
|
529
508
|
<A, E2>(
|
|
530
|
-
|
|
509
|
+
predicate: Predicate.Predicate<NoInfer<A>>,
|
|
510
|
+
orFailWith: (a: NoInfer<A>) => E2
|
|
511
|
+
): <E, R>(self: Client.HttpClient<A, E, R>) => Client.HttpClient<A, E2 | E, R>
|
|
512
|
+
<A, B extends A, E, R, E2>(
|
|
513
|
+
self: Client.HttpClient<A, E, R>,
|
|
514
|
+
refinement: Predicate.Refinement<A, B>,
|
|
531
515
|
orFailWith: (a: A) => E2
|
|
532
|
-
)
|
|
516
|
+
): Client.HttpClient<B, E2 | E, R>
|
|
533
517
|
<A, E, R, E2>(
|
|
534
518
|
self: Client.HttpClient<A, E, R>,
|
|
535
|
-
|
|
519
|
+
predicate: Predicate.Predicate<A>,
|
|
536
520
|
orFailWith: (a: A) => E2
|
|
537
|
-
)
|
|
538
|
-
|
|
521
|
+
): Client.HttpClient<A, E2 | E, R>
|
|
522
|
+
} = dual(3, (self, f, orFailWith) => transformResponse(self, Effect.filterOrFail(f, orFailWith)))
|
|
539
523
|
|
|
540
524
|
/** @internal */
|
|
541
525
|
export const map = dual<
|
|
@@ -584,7 +568,10 @@ export const mapRequest = dual<
|
|
|
584
568
|
self: Client.HttpClient<A, E, R>,
|
|
585
569
|
f: (a: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest
|
|
586
570
|
) => Client.HttpClient<A, E, R>
|
|
587
|
-
>(2, (self, f) =>
|
|
571
|
+
>(2, (self, f) => {
|
|
572
|
+
const client = self as HttpClientImpl<any, any, any>
|
|
573
|
+
return make(client.postprocess, (request) => Effect.map(client.preprocess(request), f))
|
|
574
|
+
})
|
|
588
575
|
|
|
589
576
|
/** @internal */
|
|
590
577
|
export const mapRequestEffect = dual<
|
|
@@ -601,10 +588,13 @@ export const mapRequestEffect = dual<
|
|
|
601
588
|
a: ClientRequest.HttpClientRequest
|
|
602
589
|
) => Effect.Effect<ClientRequest.HttpClientRequest, E2, R2>
|
|
603
590
|
) => Client.HttpClient<A, E | E2, R | R2>
|
|
604
|
-
>(2, (self, f) =>
|
|
591
|
+
>(2, (self, f) => {
|
|
592
|
+
const client = self as HttpClientImpl<any, any, any>
|
|
593
|
+
return make(client.postprocess as any, (request) => Effect.flatMap(client.preprocess(request), f))
|
|
594
|
+
})
|
|
605
595
|
|
|
606
596
|
/** @internal */
|
|
607
|
-
export const
|
|
597
|
+
export const mapRequestInput = dual<
|
|
608
598
|
(
|
|
609
599
|
f: (a: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest
|
|
610
600
|
) => <A, E, R>(self: Client.HttpClient<A, E, R>) => Client.HttpClient<A, E, R>,
|
|
@@ -612,10 +602,13 @@ export const mapInputRequest = dual<
|
|
|
612
602
|
self: Client.HttpClient<A, E, R>,
|
|
613
603
|
f: (a: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest
|
|
614
604
|
) => Client.HttpClient<A, E, R>
|
|
615
|
-
>(2, (self, f) =>
|
|
605
|
+
>(2, (self, f) => {
|
|
606
|
+
const client = self as HttpClientImpl<any, any, any>
|
|
607
|
+
return make(client.postprocess, (request) => client.preprocess(f(request)))
|
|
608
|
+
})
|
|
616
609
|
|
|
617
610
|
/** @internal */
|
|
618
|
-
export const
|
|
611
|
+
export const mapRequestInputEffect = dual<
|
|
619
612
|
<E2, R2>(
|
|
620
613
|
f: (
|
|
621
614
|
a: ClientRequest.HttpClientRequest
|
|
@@ -629,17 +622,29 @@ export const mapInputRequestEffect = dual<
|
|
|
629
622
|
a: ClientRequest.HttpClientRequest
|
|
630
623
|
) => Effect.Effect<ClientRequest.HttpClientRequest, E2, R2>
|
|
631
624
|
) => Client.HttpClient<A, E | E2, R | R2>
|
|
632
|
-
>(2, (self, f) =>
|
|
625
|
+
>(2, (self, f) => {
|
|
626
|
+
const client = self as HttpClientImpl<any, any, any>
|
|
627
|
+
return make(client.postprocess as any, (request) => Effect.flatMap(f(request), client.preprocess))
|
|
628
|
+
})
|
|
633
629
|
|
|
634
630
|
/** @internal */
|
|
635
631
|
export const retry: {
|
|
636
|
-
<
|
|
637
|
-
|
|
632
|
+
<E, O extends Effect.Retry.Options<E>>(
|
|
633
|
+
options: O
|
|
634
|
+
): <A, R>(
|
|
635
|
+
self: Client.HttpClient<A, E, R>
|
|
636
|
+
) => Client.Retry.Return<R, E, A, O>
|
|
637
|
+
<B, E, R1>(
|
|
638
|
+
policy: Schedule.Schedule<B, NoInfer<E>, R1>
|
|
638
639
|
): <A, R>(self: Client.HttpClient<A, E, R>) => Client.HttpClient<A, E, R1 | R>
|
|
639
|
-
<A, E
|
|
640
|
+
<A, E, R, O extends Effect.Retry.Options<E>>(
|
|
640
641
|
self: Client.HttpClient<A, E, R>,
|
|
641
|
-
|
|
642
|
-
): Client.
|
|
642
|
+
options: O
|
|
643
|
+
): Client.Retry.Return<R, E, A, O>
|
|
644
|
+
<A, E, R, B, R1>(
|
|
645
|
+
self: Client.HttpClient<A, E, R>,
|
|
646
|
+
policy: Schedule.Schedule<B, E, R1>
|
|
647
|
+
): Client.HttpClient<A, E, R1 | R>
|
|
643
648
|
} = dual(
|
|
644
649
|
2,
|
|
645
650
|
<A, E extends E0, E0, R, R1, B>(
|
|
@@ -683,7 +688,7 @@ export const schemaFunction = dual<
|
|
|
683
688
|
})
|
|
684
689
|
}),
|
|
685
690
|
(body) =>
|
|
686
|
-
self(
|
|
691
|
+
self.execute(
|
|
687
692
|
internalRequest.setBody(
|
|
688
693
|
request,
|
|
689
694
|
internalBody.uint8Array(body, "application/json")
|
|
@@ -714,7 +719,10 @@ export const tapRequest = dual<
|
|
|
714
719
|
self: Client.HttpClient<A, E, R>,
|
|
715
720
|
f: (a: ClientRequest.HttpClientRequest) => Effect.Effect<_, E2, R2>
|
|
716
721
|
) => Client.HttpClient<A, E | E2, R | R2>
|
|
717
|
-
>(2, (self, f) =>
|
|
722
|
+
>(2, (self, f) => {
|
|
723
|
+
const client = self as HttpClientImpl<any, any, any>
|
|
724
|
+
return make(client.postprocess as any, (request) => Effect.tap(client.preprocess(request), f))
|
|
725
|
+
})
|
|
718
726
|
|
|
719
727
|
/** @internal */
|
|
720
728
|
export const withCookiesRef = dual<
|
|
@@ -730,15 +738,16 @@ export const withCookiesRef = dual<
|
|
|
730
738
|
<E, R>(
|
|
731
739
|
self: Client.HttpClient.WithResponse<E, R>,
|
|
732
740
|
ref: Ref.Ref<Cookies.Cookies>
|
|
733
|
-
): Client.HttpClient.WithResponse<E, R> =>
|
|
734
|
-
|
|
741
|
+
): Client.HttpClient.WithResponse<E, R> => {
|
|
742
|
+
const client = self as HttpClientImpl<ClientResponse.HttpClientResponse, E, R>
|
|
743
|
+
return make(
|
|
735
744
|
(request: Effect.Effect<ClientRequest.HttpClientRequest, E, R>) =>
|
|
736
745
|
Effect.tap(
|
|
737
|
-
|
|
746
|
+
client.postprocess(request),
|
|
738
747
|
(response) => Ref.update(ref, (cookies) => Cookies.merge(cookies, response.cookies))
|
|
739
748
|
),
|
|
740
749
|
(request) =>
|
|
741
|
-
Effect.flatMap(
|
|
750
|
+
Effect.flatMap(client.preprocess(request), (request) =>
|
|
742
751
|
Effect.map(
|
|
743
752
|
Ref.get(ref),
|
|
744
753
|
(cookies) =>
|
|
@@ -747,6 +756,7 @@ export const withCookiesRef = dual<
|
|
|
747
756
|
: internalRequest.setHeader(request, "cookie", Cookies.toCookieHeader(cookies))
|
|
748
757
|
))
|
|
749
758
|
)
|
|
759
|
+
}
|
|
750
760
|
)
|
|
751
761
|
|
|
752
762
|
/** @internal */
|
|
@@ -761,15 +771,16 @@ export const followRedirects = dual<
|
|
|
761
771
|
>((args) => isClient(args[0]), <E, R>(
|
|
762
772
|
self: Client.HttpClient.WithResponse<E, R>,
|
|
763
773
|
maxRedirects?: number | undefined
|
|
764
|
-
): Client.HttpClient.WithResponse<E, R> =>
|
|
765
|
-
|
|
774
|
+
): Client.HttpClient.WithResponse<E, R> => {
|
|
775
|
+
const client = self as HttpClientImpl<ClientResponse.HttpClientResponse, E, R>
|
|
776
|
+
return make(
|
|
766
777
|
(request) => {
|
|
767
778
|
const loop = (
|
|
768
779
|
request: ClientRequest.HttpClientRequest,
|
|
769
780
|
redirects: number
|
|
770
781
|
): Effect.Effect<ClientResponse.HttpClientResponse, E, R> =>
|
|
771
782
|
Effect.flatMap(
|
|
772
|
-
|
|
783
|
+
client.postprocess(Effect.succeed(request)),
|
|
773
784
|
(response) =>
|
|
774
785
|
response.status >= 300 && response.status < 400 && response.headers.location &&
|
|
775
786
|
redirects < (maxRedirects ?? 10)
|
|
@@ -784,5 +795,18 @@ export const followRedirects = dual<
|
|
|
784
795
|
)
|
|
785
796
|
return Effect.flatMap(request, (request) => loop(request, 0))
|
|
786
797
|
},
|
|
787
|
-
|
|
788
|
-
)
|
|
798
|
+
client.preprocess
|
|
799
|
+
)
|
|
800
|
+
})
|
|
801
|
+
|
|
802
|
+
/** @internal */
|
|
803
|
+
export const layerMergedContext = <E, R>(effect: Effect.Effect<Client.HttpClient.Service, E, R>) =>
|
|
804
|
+
Layer.effect(
|
|
805
|
+
tag,
|
|
806
|
+
Effect.flatMap(Effect.context<never>(), (context) =>
|
|
807
|
+
Effect.map(effect, (client) =>
|
|
808
|
+
transformResponse(
|
|
809
|
+
client,
|
|
810
|
+
Effect.mapInputContext((input: Context.Context<Scope.Scope>) => Context.merge(context, input))
|
|
811
|
+
)))
|
|
812
|
+
)
|
|
@@ -6,6 +6,7 @@ import * as Effectable from "effect/Effectable"
|
|
|
6
6
|
import { dual } from "effect/Function"
|
|
7
7
|
import * as Inspectable from "effect/Inspectable"
|
|
8
8
|
import * as Option from "effect/Option"
|
|
9
|
+
import * as Redacted from "effect/Redacted"
|
|
9
10
|
import type * as Stream from "effect/Stream"
|
|
10
11
|
import type * as PlatformError from "../Error.js"
|
|
11
12
|
import type * as FileSystem from "../FileSystem.js"
|
|
@@ -21,14 +22,14 @@ import * as internalBody from "./httpBody.js"
|
|
|
21
22
|
export const TypeId: ClientRequest.TypeId = Symbol.for("@effect/platform/HttpClientRequest") as ClientRequest.TypeId
|
|
22
23
|
|
|
23
24
|
/** @internal */
|
|
24
|
-
export const clientTag = Context.GenericTag<HttpClient.
|
|
25
|
+
export const clientTag = Context.GenericTag<HttpClient.Service>("@effect/platform/HttpClient")
|
|
25
26
|
|
|
26
27
|
const Proto = {
|
|
27
28
|
[TypeId]: TypeId,
|
|
28
29
|
...Effectable.CommitPrototype,
|
|
29
30
|
...Inspectable.BaseProto,
|
|
30
31
|
commit(this: ClientRequest.HttpClientRequest) {
|
|
31
|
-
return Effect.flatMap(clientTag, (client) => client(this))
|
|
32
|
+
return Effect.flatMap(clientTag, (client) => client.execute(this))
|
|
32
33
|
},
|
|
33
34
|
toJSON(this: ClientRequest.HttpClientRequest): unknown {
|
|
34
35
|
return {
|
|
@@ -171,17 +172,31 @@ export const setHeaders = dual<
|
|
|
171
172
|
self.body
|
|
172
173
|
))
|
|
173
174
|
|
|
175
|
+
const stringOrRedacted = (value: string | Redacted.Redacted): string =>
|
|
176
|
+
typeof value === "string" ? value : Redacted.value(value)
|
|
177
|
+
|
|
174
178
|
/** @internal */
|
|
175
179
|
export const basicAuth = dual<
|
|
176
|
-
(
|
|
177
|
-
|
|
178
|
-
|
|
180
|
+
(
|
|
181
|
+
username: string | Redacted.Redacted,
|
|
182
|
+
password: string | Redacted.Redacted
|
|
183
|
+
) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
184
|
+
(
|
|
185
|
+
self: ClientRequest.HttpClientRequest,
|
|
186
|
+
username: string | Redacted.Redacted,
|
|
187
|
+
password: string | Redacted.Redacted
|
|
188
|
+
) => ClientRequest.HttpClientRequest
|
|
189
|
+
>(
|
|
190
|
+
3,
|
|
191
|
+
(self, username, password) =>
|
|
192
|
+
setHeader(self, "Authorization", `Basic ${btoa(`${stringOrRedacted(username)}:${stringOrRedacted(password)}`)}`)
|
|
193
|
+
)
|
|
179
194
|
|
|
180
195
|
/** @internal */
|
|
181
196
|
export const bearerToken = dual<
|
|
182
|
-
(token: string) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
183
|
-
(self: ClientRequest.HttpClientRequest, token: string) => ClientRequest.HttpClientRequest
|
|
184
|
-
>(2, (self, token) => setHeader(self, "Authorization", `Bearer ${token}`))
|
|
197
|
+
(token: string | Redacted.Redacted) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
198
|
+
(self: ClientRequest.HttpClientRequest, token: string | Redacted.Redacted) => ClientRequest.HttpClientRequest
|
|
199
|
+
>(2, (self, token) => setHeader(self, "Authorization", `Bearer ${stringOrRedacted(token)}`))
|
|
185
200
|
|
|
186
201
|
/** @internal */
|
|
187
202
|
export const accept = dual<
|
|
@@ -393,7 +408,7 @@ export const setBody = dual<
|
|
|
393
408
|
})
|
|
394
409
|
|
|
395
410
|
/** @internal */
|
|
396
|
-
export const
|
|
411
|
+
export const bodyUint8Array = dual<
|
|
397
412
|
(
|
|
398
413
|
body: Uint8Array,
|
|
399
414
|
contentType?: string
|
|
@@ -405,7 +420,7 @@ export const uint8ArrayBody = dual<
|
|
|
405
420
|
)
|
|
406
421
|
|
|
407
422
|
/** @internal */
|
|
408
|
-
export const
|
|
423
|
+
export const bodyText = dual<
|
|
409
424
|
(body: string, contentType?: string) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
410
425
|
(self: ClientRequest.HttpClientRequest, body: string, contentType?: string) => ClientRequest.HttpClientRequest
|
|
411
426
|
>(
|
|
@@ -414,7 +429,7 @@ export const textBody = dual<
|
|
|
414
429
|
)
|
|
415
430
|
|
|
416
431
|
/** @internal */
|
|
417
|
-
export const
|
|
432
|
+
export const bodyJson = dual<
|
|
418
433
|
(
|
|
419
434
|
body: unknown
|
|
420
435
|
) => (self: ClientRequest.HttpClientRequest) => Effect.Effect<ClientRequest.HttpClientRequest, Body.HttpBodyError>,
|
|
@@ -425,13 +440,13 @@ export const jsonBody = dual<
|
|
|
425
440
|
>(2, (self, body) => Effect.map(internalBody.json(body), (body) => setBody(self, body)))
|
|
426
441
|
|
|
427
442
|
/** @internal */
|
|
428
|
-
export const
|
|
443
|
+
export const bodyUnsafeJson = dual<
|
|
429
444
|
(body: unknown) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
430
445
|
(self: ClientRequest.HttpClientRequest, body: unknown) => ClientRequest.HttpClientRequest
|
|
431
446
|
>(2, (self, body) => setBody(self, internalBody.unsafeJson(body)))
|
|
432
447
|
|
|
433
448
|
/** @internal */
|
|
434
|
-
export const
|
|
449
|
+
export const bodyFile = dual<
|
|
435
450
|
(
|
|
436
451
|
path: string,
|
|
437
452
|
options?: FileSystem.StreamOptions & { readonly contentType?: string }
|
|
@@ -449,13 +464,13 @@ export const fileBody = dual<
|
|
|
449
464
|
)
|
|
450
465
|
|
|
451
466
|
/** @internal */
|
|
452
|
-
export const
|
|
467
|
+
export const bodyFileWeb = dual<
|
|
453
468
|
(file: Body.HttpBody.FileLike) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
454
469
|
(self: ClientRequest.HttpClientRequest, file: Body.HttpBody.FileLike) => ClientRequest.HttpClientRequest
|
|
455
470
|
>(2, (self, file) => setBody(self, internalBody.fileWeb(file)))
|
|
456
471
|
|
|
457
472
|
/** @internal */
|
|
458
|
-
export const
|
|
473
|
+
export const schemaBodyJson = <A, I, R>(schema: Schema.Schema<A, I, R>, options?: ParseOptions | undefined): {
|
|
459
474
|
(
|
|
460
475
|
body: A
|
|
461
476
|
): (self: ClientRequest.HttpClientRequest) => Effect.Effect<ClientRequest.HttpClientRequest, Body.HttpBodyError, R>
|
|
@@ -479,7 +494,7 @@ export const schemaBody = <A, I, R>(schema: Schema.Schema<A, I, R>, options?: Pa
|
|
|
479
494
|
}
|
|
480
495
|
|
|
481
496
|
/** @internal */
|
|
482
|
-
export const
|
|
497
|
+
export const bodyUrlParams = dual<
|
|
483
498
|
(input: UrlParams.Input) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
484
499
|
(self: ClientRequest.HttpClientRequest, input: UrlParams.Input) => ClientRequest.HttpClientRequest
|
|
485
500
|
>(2, (self, body) =>
|
|
@@ -492,13 +507,13 @@ export const urlParamsBody = dual<
|
|
|
492
507
|
))
|
|
493
508
|
|
|
494
509
|
/** @internal */
|
|
495
|
-
export const
|
|
510
|
+
export const bodyFormData = dual<
|
|
496
511
|
(body: FormData) => (self: ClientRequest.HttpClientRequest) => ClientRequest.HttpClientRequest,
|
|
497
512
|
(self: ClientRequest.HttpClientRequest, body: FormData) => ClientRequest.HttpClientRequest
|
|
498
513
|
>(2, (self, body) => setBody(self, internalBody.formData(body)))
|
|
499
514
|
|
|
500
515
|
/** @internal */
|
|
501
|
-
export const
|
|
516
|
+
export const bodyStream = dual<
|
|
502
517
|
(
|
|
503
518
|
body: Stream.Stream<Uint8Array, unknown>,
|
|
504
519
|
options?: {
|