@effect/platform 0.10.4 → 0.11.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/Effectify.d.ts.map +1 -1
- package/Http/Body.d.ts +129 -0
- package/Http/Body.d.ts.map +1 -0
- package/Http/Body.js +70 -0
- package/Http/Body.js.map +1 -0
- package/Http/Client.d.ts +213 -0
- package/Http/Client.d.ts.map +1 -0
- package/Http/Client.js +124 -0
- package/Http/Client.js.map +1 -0
- package/Http/ClientError.d.ts +70 -0
- package/Http/ClientError.d.ts.map +1 -0
- package/Http/ClientError.js +28 -0
- package/Http/ClientError.js.map +1 -0
- package/Http/ClientRequest.d.ts +317 -0
- package/Http/ClientRequest.d.ts.map +1 -0
- package/Http/ClientRequest.js +202 -0
- package/Http/ClientRequest.js.map +1 -0
- package/Http/ClientResponse.d.ts +36 -0
- package/Http/ClientResponse.d.ts.map +1 -0
- package/Http/ClientResponse.js +29 -0
- package/Http/ClientResponse.js.map +1 -0
- package/Http/FormData.d.ts +56 -0
- package/Http/FormData.d.ts.map +1 -0
- package/Http/FormData.js +13 -0
- package/Http/FormData.js.map +1 -0
- package/Http/Headers.d.ts +38 -0
- package/Http/Headers.d.ts.map +1 -0
- package/Http/Headers.js +54 -0
- package/Http/Headers.js.map +1 -0
- package/Http/IncomingMessage.d.ts +37 -0
- package/Http/IncomingMessage.d.ts.map +1 -0
- package/Http/IncomingMessage.js +30 -0
- package/Http/IncomingMessage.js.map +1 -0
- package/Http/Method.d.ts +10 -0
- package/Http/Method.d.ts.map +1 -0
- package/Http/Method.js +12 -0
- package/Http/Method.js.map +1 -0
- package/Http/UrlParams.d.ts +56 -0
- package/Http/UrlParams.d.ts.map +1 -0
- package/Http/UrlParams.js +74 -0
- package/Http/UrlParams.js.map +1 -0
- package/HttpClient.d.ts +32 -0
- package/HttpClient.d.ts.map +1 -0
- package/HttpClient.js +23 -0
- package/HttpClient.js.map +1 -0
- package/README.md +3 -0
- package/internal/console.js.map +1 -1
- package/internal/effectify.js.map +1 -1
- package/internal/error.js.map +1 -1
- package/internal/http/body.d.ts +2 -0
- package/internal/http/body.d.ts.map +1 -0
- package/internal/http/body.js +101 -0
- package/internal/http/body.js.map +1 -0
- package/internal/http/client.d.ts +3 -0
- package/internal/http/client.d.ts.map +1 -0
- package/internal/http/client.js +147 -0
- package/internal/http/client.js.map +1 -0
- package/internal/http/clientError.d.ts +2 -0
- package/internal/http/clientError.d.ts.map +1 -0
- package/internal/http/clientError.js +24 -0
- package/internal/http/clientError.js.map +1 -0
- package/internal/http/clientRequest.d.ts +2 -0
- package/internal/http/clientRequest.d.ts.map +1 -0
- package/internal/http/clientRequest.js +180 -0
- package/internal/http/clientRequest.js.map +1 -0
- package/internal/http/clientResponse.d.ts +2 -0
- package/internal/http/clientResponse.d.ts.map +1 -0
- package/internal/http/clientResponse.js +101 -0
- package/internal/http/clientResponse.js.map +1 -0
- package/mjs/Http/Body.mjs +52 -0
- package/mjs/Http/Body.mjs.map +1 -0
- package/mjs/Http/Client.mjs +97 -0
- package/mjs/Http/Client.mjs.map +1 -0
- package/mjs/Http/ClientError.mjs +17 -0
- package/mjs/Http/ClientError.mjs.map +1 -0
- package/mjs/Http/ClientRequest.mjs +162 -0
- package/mjs/Http/ClientRequest.mjs.map +1 -0
- package/mjs/Http/ClientResponse.mjs +18 -0
- package/mjs/Http/ClientResponse.mjs.map +1 -0
- package/mjs/Http/FormData.mjs +6 -0
- package/mjs/Http/FormData.mjs.map +1 -0
- package/mjs/Http/Headers.mjs +40 -0
- package/mjs/Http/Headers.mjs.map +1 -0
- package/mjs/Http/IncomingMessage.mjs +19 -0
- package/mjs/Http/IncomingMessage.mjs.map +1 -0
- package/mjs/Http/Method.mjs +5 -0
- package/mjs/Http/Method.mjs.map +1 -0
- package/mjs/Http/UrlParams.mjs +57 -0
- package/mjs/Http/UrlParams.mjs.map +1 -0
- package/mjs/HttpClient.mjs +18 -0
- package/mjs/HttpClient.mjs.map +1 -0
- package/mjs/internal/console.mjs.map +1 -1
- package/mjs/internal/effectify.mjs.map +1 -1
- package/mjs/internal/error.mjs.map +1 -1
- package/mjs/internal/http/body.mjs +83 -0
- package/mjs/internal/http/body.mjs.map +1 -0
- package/mjs/internal/http/client.mjs +120 -0
- package/mjs/internal/http/client.mjs.map +1 -0
- package/mjs/internal/http/clientError.mjs +13 -0
- package/mjs/internal/http/clientError.mjs.map +1 -0
- package/mjs/internal/http/clientRequest.mjs +138 -0
- package/mjs/internal/http/clientRequest.mjs.map +1 -0
- package/mjs/internal/http/clientResponse.mjs +91 -0
- package/mjs/internal/http/clientResponse.mjs.map +1 -0
- package/package.json +13 -8
- package/src/Effectify.ts +65 -65
- package/src/Http/Body.ts +148 -0
- package/src/Http/Client.ts +280 -0
- package/src/Http/ClientError.ts +80 -0
- package/src/Http/ClientRequest.ts +383 -0
- package/src/Http/ClientResponse.ts +42 -0
- package/src/Http/FormData.ts +61 -0
- package/src/Http/Headers.ts +69 -0
- package/src/Http/IncomingMessage.ts +45 -0
- package/src/Http/Method.ts +17 -0
- package/src/Http/UrlParams.ts +106 -0
- package/src/HttpClient.ts +32 -0
- package/src/internal/console.ts +6 -6
- package/src/internal/effectify.ts +14 -14
- package/src/internal/error.ts +2 -2
- package/src/internal/http/body.ts +113 -0
- package/src/internal/http/client.ts +437 -0
- package/src/internal/http/clientError.ts +20 -0
- package/src/internal/http/clientRequest.ts +390 -0
- package/src/internal/http/clientResponse.ts +114 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { dual } from "@effect/data/Function"
|
|
2
|
+
import { pipeArguments } from "@effect/data/Pipeable"
|
|
3
|
+
import type * as Body from "@effect/platform/Http/Body"
|
|
4
|
+
import type * as Error from "@effect/platform/Http/ClientError"
|
|
5
|
+
import type * as ClientRequest from "@effect/platform/Http/ClientRequest"
|
|
6
|
+
import * as Headers from "@effect/platform/Http/Headers"
|
|
7
|
+
import type { Method } from "@effect/platform/Http/Method"
|
|
8
|
+
import * as UrlParams from "@effect/platform/Http/UrlParams"
|
|
9
|
+
import * as internalBody from "@effect/platform/internal/http/body"
|
|
10
|
+
import type * as Schema from "@effect/schema/Schema"
|
|
11
|
+
import type * as Stream from "@effect/stream/Stream"
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export const TypeId: ClientRequest.TypeId = Symbol.for("@effect/platform/Http/ClientRequest") as ClientRequest.TypeId
|
|
15
|
+
|
|
16
|
+
class ClientRequestImpl implements ClientRequest.ClientRequest {
|
|
17
|
+
readonly [TypeId]: ClientRequest.TypeId = TypeId
|
|
18
|
+
constructor(
|
|
19
|
+
readonly method: Method,
|
|
20
|
+
readonly url: string,
|
|
21
|
+
readonly urlParams: UrlParams.UrlParams,
|
|
22
|
+
readonly headers: Headers.Headers,
|
|
23
|
+
readonly body: Body.Body
|
|
24
|
+
) {}
|
|
25
|
+
pipe() {
|
|
26
|
+
return pipeArguments(this, arguments)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @internal */
|
|
31
|
+
export const isClientRequest = (u: unknown): u is ClientRequest.ClientRequest =>
|
|
32
|
+
typeof u === "object" && u !== null && TypeId in u
|
|
33
|
+
|
|
34
|
+
/** @internal */
|
|
35
|
+
export const empty: ClientRequest.ClientRequest = new ClientRequestImpl(
|
|
36
|
+
"GET",
|
|
37
|
+
"",
|
|
38
|
+
UrlParams.empty,
|
|
39
|
+
Headers.empty,
|
|
40
|
+
internalBody.empty
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
/** @internal */
|
|
44
|
+
export const make = (method: Method) =>
|
|
45
|
+
(url: string, options?: {
|
|
46
|
+
readonly url?: string
|
|
47
|
+
readonly urlParams?: UrlParams.Input
|
|
48
|
+
readonly headers?: Headers.Input
|
|
49
|
+
readonly body?: Body.Body
|
|
50
|
+
readonly accept?: string
|
|
51
|
+
readonly acceptJson?: boolean
|
|
52
|
+
}) =>
|
|
53
|
+
modify(empty, {
|
|
54
|
+
method,
|
|
55
|
+
url,
|
|
56
|
+
...(options ?? {})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
/** @internal */
|
|
60
|
+
export const get = make("GET")
|
|
61
|
+
|
|
62
|
+
/** @internal */
|
|
63
|
+
export const post = make("POST")
|
|
64
|
+
|
|
65
|
+
/** @internal */
|
|
66
|
+
export const put = make("PUT")
|
|
67
|
+
|
|
68
|
+
/** @internal */
|
|
69
|
+
export const patch = make("PATCH")
|
|
70
|
+
|
|
71
|
+
/** @internal */
|
|
72
|
+
export const del = make("DELETE")
|
|
73
|
+
|
|
74
|
+
/** @internal */
|
|
75
|
+
export const head = make("HEAD")
|
|
76
|
+
|
|
77
|
+
/** @internal */
|
|
78
|
+
export const options = make("OPTIONS")
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
export const modify = dual<
|
|
82
|
+
(options: {
|
|
83
|
+
readonly method?: Method
|
|
84
|
+
readonly url?: string
|
|
85
|
+
readonly urlParams?: UrlParams.Input
|
|
86
|
+
readonly headers?: Headers.Input
|
|
87
|
+
readonly body?: Body.Body
|
|
88
|
+
readonly accept?: string
|
|
89
|
+
readonly acceptJson?: boolean
|
|
90
|
+
}) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
91
|
+
(self: ClientRequest.ClientRequest, options: {
|
|
92
|
+
readonly method?: Method
|
|
93
|
+
readonly url?: string
|
|
94
|
+
readonly urlParams?: UrlParams.Input
|
|
95
|
+
readonly headers?: Headers.Input
|
|
96
|
+
readonly body?: Body.Body
|
|
97
|
+
readonly accept?: string
|
|
98
|
+
readonly acceptJson?: boolean
|
|
99
|
+
}) => ClientRequest.ClientRequest
|
|
100
|
+
>(2, (self, options) => {
|
|
101
|
+
let result = self
|
|
102
|
+
|
|
103
|
+
if (options.method) {
|
|
104
|
+
result = setMethod(result, options.method)
|
|
105
|
+
}
|
|
106
|
+
if (options.url) {
|
|
107
|
+
result = setUrl(result, options.url)
|
|
108
|
+
}
|
|
109
|
+
if (options.headers) {
|
|
110
|
+
result = setHeaders(result, options.headers)
|
|
111
|
+
}
|
|
112
|
+
if (options.urlParams) {
|
|
113
|
+
result = setUrlParams(result, options.urlParams)
|
|
114
|
+
}
|
|
115
|
+
if (options.body) {
|
|
116
|
+
result = setBody(result, options.body)
|
|
117
|
+
}
|
|
118
|
+
if (options.accept) {
|
|
119
|
+
result = accept(result, options.accept)
|
|
120
|
+
}
|
|
121
|
+
if (options.acceptJson) {
|
|
122
|
+
result = acceptJson(result)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return result
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
/** @internal */
|
|
129
|
+
export const setHeader = dual<
|
|
130
|
+
(key: string, value: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
131
|
+
(self: ClientRequest.ClientRequest, key: string, value: string) => ClientRequest.ClientRequest
|
|
132
|
+
>(3, (self, key, value) =>
|
|
133
|
+
new ClientRequestImpl(
|
|
134
|
+
self.method,
|
|
135
|
+
self.url,
|
|
136
|
+
self.urlParams,
|
|
137
|
+
Headers.set(self.headers, key, value),
|
|
138
|
+
self.body
|
|
139
|
+
))
|
|
140
|
+
|
|
141
|
+
/** @internal */
|
|
142
|
+
export const setHeaders = dual<
|
|
143
|
+
(input: Headers.Input) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
144
|
+
(self: ClientRequest.ClientRequest, input: Headers.Input) => ClientRequest.ClientRequest
|
|
145
|
+
>(2, (self, input) =>
|
|
146
|
+
new ClientRequestImpl(
|
|
147
|
+
self.method,
|
|
148
|
+
self.url,
|
|
149
|
+
self.urlParams,
|
|
150
|
+
Headers.setAll(self.headers, input),
|
|
151
|
+
self.body
|
|
152
|
+
))
|
|
153
|
+
|
|
154
|
+
/** @internal */
|
|
155
|
+
export const basicAuth = dual<
|
|
156
|
+
(username: string, password: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
157
|
+
(self: ClientRequest.ClientRequest, username: string, password: string) => ClientRequest.ClientRequest
|
|
158
|
+
>(3, (self, username, password) => setHeader(self, "Authorization", `Basic ${btoa(`${username}:${password}`)}`))
|
|
159
|
+
|
|
160
|
+
/** @internal */
|
|
161
|
+
export const accept = dual<
|
|
162
|
+
(mediaType: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
163
|
+
(self: ClientRequest.ClientRequest, mediaType: string) => ClientRequest.ClientRequest
|
|
164
|
+
>(2, (self, mediaType) => setHeader(self, "Accept", mediaType))
|
|
165
|
+
|
|
166
|
+
/** @internal */
|
|
167
|
+
export const acceptJson = accept("application/json")
|
|
168
|
+
|
|
169
|
+
/** @internal */
|
|
170
|
+
export const setMethod = dual<
|
|
171
|
+
(method: Method) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
172
|
+
(self: ClientRequest.ClientRequest, method: Method) => ClientRequest.ClientRequest
|
|
173
|
+
>(2, (self, method) =>
|
|
174
|
+
new ClientRequestImpl(
|
|
175
|
+
method,
|
|
176
|
+
self.url,
|
|
177
|
+
self.urlParams,
|
|
178
|
+
self.headers,
|
|
179
|
+
self.body
|
|
180
|
+
))
|
|
181
|
+
|
|
182
|
+
/** @internal */
|
|
183
|
+
export const setUrl = dual<
|
|
184
|
+
(url: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
185
|
+
(self: ClientRequest.ClientRequest, url: string) => ClientRequest.ClientRequest
|
|
186
|
+
>(2, (self, url) =>
|
|
187
|
+
new ClientRequestImpl(
|
|
188
|
+
self.method,
|
|
189
|
+
url,
|
|
190
|
+
self.urlParams,
|
|
191
|
+
self.headers,
|
|
192
|
+
self.body
|
|
193
|
+
))
|
|
194
|
+
|
|
195
|
+
/** @internal */
|
|
196
|
+
export const appendUrl = dual<
|
|
197
|
+
(path: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
198
|
+
(self: ClientRequest.ClientRequest, path: string) => ClientRequest.ClientRequest
|
|
199
|
+
>(2, (self, url) =>
|
|
200
|
+
new ClientRequestImpl(
|
|
201
|
+
self.method,
|
|
202
|
+
self.url + url,
|
|
203
|
+
self.urlParams,
|
|
204
|
+
self.headers,
|
|
205
|
+
self.body
|
|
206
|
+
))
|
|
207
|
+
|
|
208
|
+
/** @internal */
|
|
209
|
+
export const prependUrl = dual<
|
|
210
|
+
(path: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
211
|
+
(self: ClientRequest.ClientRequest, path: string) => ClientRequest.ClientRequest
|
|
212
|
+
>(2, (self, url) =>
|
|
213
|
+
new ClientRequestImpl(
|
|
214
|
+
self.method,
|
|
215
|
+
url + self.url,
|
|
216
|
+
self.urlParams,
|
|
217
|
+
self.headers,
|
|
218
|
+
self.body
|
|
219
|
+
))
|
|
220
|
+
|
|
221
|
+
/** @internal */
|
|
222
|
+
export const updateUrl = dual<
|
|
223
|
+
(f: (url: string) => string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
224
|
+
(self: ClientRequest.ClientRequest, f: (url: string) => string) => ClientRequest.ClientRequest
|
|
225
|
+
>(2, (self, f) =>
|
|
226
|
+
new ClientRequestImpl(
|
|
227
|
+
self.method,
|
|
228
|
+
f(self.url),
|
|
229
|
+
self.urlParams,
|
|
230
|
+
self.headers,
|
|
231
|
+
self.body
|
|
232
|
+
))
|
|
233
|
+
|
|
234
|
+
/** @internal */
|
|
235
|
+
export const appendUrlParam = dual<
|
|
236
|
+
(key: string, value: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
237
|
+
(self: ClientRequest.ClientRequest, key: string, value: string) => ClientRequest.ClientRequest
|
|
238
|
+
>(3, (self, key, value) =>
|
|
239
|
+
new ClientRequestImpl(
|
|
240
|
+
self.method,
|
|
241
|
+
self.url,
|
|
242
|
+
UrlParams.append(self.urlParams, key, value),
|
|
243
|
+
self.headers,
|
|
244
|
+
self.body
|
|
245
|
+
))
|
|
246
|
+
|
|
247
|
+
/** @internal */
|
|
248
|
+
export const appendUrlParams = dual<
|
|
249
|
+
(input: UrlParams.Input) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
250
|
+
(self: ClientRequest.ClientRequest, input: UrlParams.Input) => ClientRequest.ClientRequest
|
|
251
|
+
>(2, (self, input) =>
|
|
252
|
+
new ClientRequestImpl(
|
|
253
|
+
self.method,
|
|
254
|
+
self.url,
|
|
255
|
+
UrlParams.appendAll(self.urlParams, input),
|
|
256
|
+
self.headers,
|
|
257
|
+
self.body
|
|
258
|
+
))
|
|
259
|
+
|
|
260
|
+
/** @internal */
|
|
261
|
+
export const setUrlParam = dual<
|
|
262
|
+
(key: string, value: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
263
|
+
(self: ClientRequest.ClientRequest, key: string, value: string) => ClientRequest.ClientRequest
|
|
264
|
+
>(3, (self, key, value) =>
|
|
265
|
+
new ClientRequestImpl(
|
|
266
|
+
self.method,
|
|
267
|
+
self.url,
|
|
268
|
+
UrlParams.set(self.urlParams, key, value),
|
|
269
|
+
self.headers,
|
|
270
|
+
self.body
|
|
271
|
+
))
|
|
272
|
+
|
|
273
|
+
/** @internal */
|
|
274
|
+
export const setUrlParams = dual<
|
|
275
|
+
(input: UrlParams.Input) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
276
|
+
(self: ClientRequest.ClientRequest, input: UrlParams.Input) => ClientRequest.ClientRequest
|
|
277
|
+
>(2, (self, input) =>
|
|
278
|
+
new ClientRequestImpl(
|
|
279
|
+
self.method,
|
|
280
|
+
self.url,
|
|
281
|
+
UrlParams.setAll(self.urlParams, input),
|
|
282
|
+
self.headers,
|
|
283
|
+
self.body
|
|
284
|
+
))
|
|
285
|
+
|
|
286
|
+
/** @internal */
|
|
287
|
+
export const setBody = dual<
|
|
288
|
+
(body: Body.Body) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
289
|
+
(self: ClientRequest.ClientRequest, body: Body.Body) => ClientRequest.ClientRequest
|
|
290
|
+
>(2, (self, body) => {
|
|
291
|
+
let headers = self.headers
|
|
292
|
+
if (body._tag === "Empty") {
|
|
293
|
+
headers = Headers.remove(Headers.remove(headers, "Content-Type"), "Content-length")
|
|
294
|
+
} else {
|
|
295
|
+
const contentType = body.contentType
|
|
296
|
+
if (contentType) {
|
|
297
|
+
headers = Headers.set(headers, "content-type", contentType)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const contentLength = body.contentLength
|
|
301
|
+
if (contentLength) {
|
|
302
|
+
headers = Headers.set(headers, "content-length", contentLength.toString())
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return new ClientRequestImpl(
|
|
306
|
+
self.method,
|
|
307
|
+
self.url,
|
|
308
|
+
self.urlParams,
|
|
309
|
+
headers,
|
|
310
|
+
body
|
|
311
|
+
)
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
/** @internal */
|
|
315
|
+
export const binaryBody = dual<
|
|
316
|
+
(body: Uint8Array, contentType?: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
317
|
+
(self: ClientRequest.ClientRequest, body: Uint8Array, contentType?: string) => ClientRequest.ClientRequest
|
|
318
|
+
>(
|
|
319
|
+
(args) => isClientRequest(args[0]),
|
|
320
|
+
(self, body, contentType = "application/octet-stream") => setBody(self, internalBody.bytes(body, contentType))
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
/** @internal */
|
|
324
|
+
export const textBody = dual<
|
|
325
|
+
(body: string, contentType?: string) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
326
|
+
(self: ClientRequest.ClientRequest, body: string, contentType?: string) => ClientRequest.ClientRequest
|
|
327
|
+
>(
|
|
328
|
+
(args) => isClientRequest(args[0]),
|
|
329
|
+
(self, body, contentType = "text/plain") => setBody(self, internalBody.text(body, contentType))
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
/** @internal */
|
|
333
|
+
export const jsonBody = dual<
|
|
334
|
+
(body: unknown) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
335
|
+
(self: ClientRequest.ClientRequest, body: string) => ClientRequest.ClientRequest
|
|
336
|
+
>(2, (self, body) => setBody(self, internalBody.json(body)))
|
|
337
|
+
|
|
338
|
+
/** @internal */
|
|
339
|
+
export const schemaBody = <I, A>(schema: Schema.Schema<I, A>): {
|
|
340
|
+
(body: A): (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest
|
|
341
|
+
(self: ClientRequest.ClientRequest, body: A): ClientRequest.ClientRequest
|
|
342
|
+
} => {
|
|
343
|
+
const encode = internalBody.jsonSchema(schema)
|
|
344
|
+
return dual<
|
|
345
|
+
(body: A) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
346
|
+
(self: ClientRequest.ClientRequest, body: A) => ClientRequest.ClientRequest
|
|
347
|
+
>(2, (self, body) => setBody(self, encode(body)))
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** @internal */
|
|
351
|
+
export const urlParamsBody = dual<
|
|
352
|
+
(input: UrlParams.Input) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
353
|
+
(self: ClientRequest.ClientRequest, input: UrlParams.Input) => ClientRequest.ClientRequest
|
|
354
|
+
>(2, (self, body) =>
|
|
355
|
+
setBody(
|
|
356
|
+
self,
|
|
357
|
+
internalBody.text(
|
|
358
|
+
UrlParams.toString(UrlParams.fromInput(body)),
|
|
359
|
+
"application/x-www-form-urlencoded"
|
|
360
|
+
)
|
|
361
|
+
))
|
|
362
|
+
|
|
363
|
+
/** @internal */
|
|
364
|
+
export const formDataBody = dual<
|
|
365
|
+
(body: FormData) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
366
|
+
(self: ClientRequest.ClientRequest, body: FormData) => ClientRequest.ClientRequest
|
|
367
|
+
>(2, (self, body) => setBody(self, internalBody.formData(body)))
|
|
368
|
+
|
|
369
|
+
/** @internal */
|
|
370
|
+
export const streamBody = dual<
|
|
371
|
+
(
|
|
372
|
+
body: Stream.Stream<never, Error.RequestError, Uint8Array>,
|
|
373
|
+
options?: {
|
|
374
|
+
readonly contentType?: string
|
|
375
|
+
readonly contentLength?: number
|
|
376
|
+
}
|
|
377
|
+
) => (self: ClientRequest.ClientRequest) => ClientRequest.ClientRequest,
|
|
378
|
+
(
|
|
379
|
+
self: ClientRequest.ClientRequest,
|
|
380
|
+
body: Stream.Stream<never, Error.RequestError, Uint8Array>,
|
|
381
|
+
options?: {
|
|
382
|
+
readonly contentType?: string
|
|
383
|
+
readonly contentLength?: number
|
|
384
|
+
}
|
|
385
|
+
) => ClientRequest.ClientRequest
|
|
386
|
+
>(
|
|
387
|
+
(args) => isClientRequest(args[0]),
|
|
388
|
+
(self, body, { contentLength, contentType = "application/octet-stream" } = {}) =>
|
|
389
|
+
setBody(self, internalBody.stream(body, contentType, contentLength))
|
|
390
|
+
)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as Effect from "@effect/io/Effect"
|
|
2
|
+
import type * as Error from "@effect/platform/Http/ClientError"
|
|
3
|
+
import type * as ClientRequest from "@effect/platform/Http/ClientRequest"
|
|
4
|
+
import type * as ClientResponse from "@effect/platform/Http/ClientResponse"
|
|
5
|
+
import type * as FormData from "@effect/platform/Http/FormData"
|
|
6
|
+
import * as Headers from "@effect/platform/Http/Headers"
|
|
7
|
+
import * as IncomingMessage from "@effect/platform/Http/IncomingMessage"
|
|
8
|
+
import * as internalError from "@effect/platform/internal/http/clientError"
|
|
9
|
+
import * as Stream from "@effect/stream/Stream"
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const TypeId: ClientResponse.TypeId = Symbol.for("@effect/platform/Http/ClientResponse") as ClientResponse.TypeId
|
|
13
|
+
|
|
14
|
+
/** @internal */
|
|
15
|
+
export const fromWeb = (
|
|
16
|
+
request: ClientRequest.ClientRequest,
|
|
17
|
+
source: globalThis.Response
|
|
18
|
+
): ClientResponse.ClientResponse => new ClientResponseImpl(request, source)
|
|
19
|
+
|
|
20
|
+
class ClientResponseImpl implements ClientResponse.ClientResponse {
|
|
21
|
+
readonly [IncomingMessage.TypeId]: IncomingMessage.TypeId = IncomingMessage.TypeId
|
|
22
|
+
readonly [TypeId]: ClientResponse.TypeId = TypeId
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
private readonly request: ClientRequest.ClientRequest,
|
|
26
|
+
private readonly source: globalThis.Response
|
|
27
|
+
) {}
|
|
28
|
+
|
|
29
|
+
get status(): number {
|
|
30
|
+
return this.source.status
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get headers(): Headers.Headers {
|
|
34
|
+
return Headers.fromInput(this.source.headers)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get stream(): Stream.Stream<never, Error.ResponseError, Uint8Array> {
|
|
38
|
+
return this.source.body
|
|
39
|
+
? Stream.fromReadableStream(() => this.source.body!, (_) =>
|
|
40
|
+
internalError.responseError({
|
|
41
|
+
request: this.request,
|
|
42
|
+
response: this,
|
|
43
|
+
reason: "Decode",
|
|
44
|
+
error: _
|
|
45
|
+
}))
|
|
46
|
+
: Stream.fail(internalError.responseError({
|
|
47
|
+
request: this.request,
|
|
48
|
+
response: this,
|
|
49
|
+
reason: "EmptyBody",
|
|
50
|
+
error: "can not create stream from empty body"
|
|
51
|
+
}))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get json(): Effect.Effect<never, Error.ResponseError, unknown> {
|
|
55
|
+
return Effect.tryPromise({
|
|
56
|
+
try: () => this.source.json(),
|
|
57
|
+
catch: (_) =>
|
|
58
|
+
internalError.responseError({
|
|
59
|
+
request: this.request,
|
|
60
|
+
response: this,
|
|
61
|
+
reason: "Decode",
|
|
62
|
+
error: _
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get text(): Effect.Effect<never, Error.ResponseError, string> {
|
|
68
|
+
return Effect.tryPromise({
|
|
69
|
+
try: () => this.source.text(),
|
|
70
|
+
catch: (_) =>
|
|
71
|
+
internalError.responseError({
|
|
72
|
+
request: this.request,
|
|
73
|
+
response: this,
|
|
74
|
+
reason: "Decode",
|
|
75
|
+
error: _
|
|
76
|
+
})
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get formData(): Effect.Effect<never, Error.ResponseError, FormData> {
|
|
81
|
+
return Effect.tryPromise({
|
|
82
|
+
try: () => this.source.formData(),
|
|
83
|
+
catch: (_) =>
|
|
84
|
+
internalError.responseError({
|
|
85
|
+
request: this.request,
|
|
86
|
+
response: this,
|
|
87
|
+
reason: "Decode",
|
|
88
|
+
error: _
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get formDataStream(): Stream.Stream<never, Error.ResponseError, FormData.Part> {
|
|
94
|
+
return Stream.fail(internalError.responseError({
|
|
95
|
+
request: this.request,
|
|
96
|
+
response: this,
|
|
97
|
+
reason: "Decode",
|
|
98
|
+
error: "not implemented"
|
|
99
|
+
}))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get blob(): Effect.Effect<never, Error.ResponseError, Blob> {
|
|
103
|
+
return Effect.tryPromise({
|
|
104
|
+
try: () => this.source.blob(),
|
|
105
|
+
catch: (_) =>
|
|
106
|
+
internalError.responseError({
|
|
107
|
+
request: this.request,
|
|
108
|
+
response: this,
|
|
109
|
+
reason: "Decode",
|
|
110
|
+
error: _
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
}
|