@effect/platform 0.48.16 → 0.48.18
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/README.md +312 -10
- package/dist/cjs/Http/Client.js.map +1 -1
- package/dist/cjs/Http/ClientRequest.js.map +1 -1
- package/dist/cjs/Http/ClientResponse.js.map +1 -1
- package/dist/cjs/Http/Cookies.js +50 -27
- package/dist/cjs/Http/Cookies.js.map +1 -1
- package/dist/cjs/Http/IncomingMessage.js +12 -16
- package/dist/cjs/Http/IncomingMessage.js.map +1 -1
- package/dist/cjs/Http/Multipart.js.map +1 -1
- package/dist/cjs/Http/Router.js +11 -1
- package/dist/cjs/Http/Router.js.map +1 -1
- package/dist/cjs/Http/ServerRequest.js +6 -1
- package/dist/cjs/Http/ServerRequest.js.map +1 -1
- package/dist/cjs/Http/ServerResponse.js.map +1 -1
- package/dist/cjs/Http/UrlParams.js +2 -2
- package/dist/cjs/Http/UrlParams.js.map +1 -1
- package/dist/cjs/internal/http/body.js +2 -2
- package/dist/cjs/internal/http/body.js.map +1 -1
- package/dist/cjs/internal/http/client.js +4 -2
- package/dist/cjs/internal/http/client.js.map +1 -1
- package/dist/cjs/internal/http/clientRequest.js +2 -2
- package/dist/cjs/internal/http/clientRequest.js.map +1 -1
- package/dist/cjs/internal/http/clientResponse.js +8 -8
- package/dist/cjs/internal/http/clientResponse.js.map +1 -1
- package/dist/cjs/internal/http/multipart.js +4 -4
- package/dist/cjs/internal/http/multipart.js.map +1 -1
- package/dist/cjs/internal/http/router.js +34 -7
- package/dist/cjs/internal/http/router.js.map +1 -1
- package/dist/cjs/internal/http/serverRequest.js +22 -15
- package/dist/cjs/internal/http/serverRequest.js.map +1 -1
- package/dist/cjs/internal/http/serverResponse.js +19 -11
- package/dist/cjs/internal/http/serverResponse.js.map +1 -1
- package/dist/dts/Http/Client.d.ts +3 -2
- package/dist/dts/Http/Client.d.ts.map +1 -1
- package/dist/dts/Http/ClientRequest.d.ts +2 -1
- package/dist/dts/Http/ClientRequest.d.ts.map +1 -1
- package/dist/dts/Http/ClientResponse.d.ts +5 -4
- package/dist/dts/Http/ClientResponse.d.ts.map +1 -1
- package/dist/dts/Http/Cookies.d.ts +15 -7
- package/dist/dts/Http/Cookies.d.ts.map +1 -1
- package/dist/dts/Http/Headers.d.ts +1 -1
- package/dist/dts/Http/Headers.d.ts.map +1 -1
- package/dist/dts/Http/IncomingMessage.d.ts +7 -6
- package/dist/dts/Http/IncomingMessage.d.ts.map +1 -1
- package/dist/dts/Http/Multipart.d.ts +3 -2
- package/dist/dts/Http/Multipart.d.ts.map +1 -1
- package/dist/dts/Http/Router.d.ts +29 -3
- package/dist/dts/Http/Router.d.ts.map +1 -1
- package/dist/dts/Http/ServerRequest.d.ts +12 -6
- package/dist/dts/Http/ServerRequest.d.ts.map +1 -1
- package/dist/dts/Http/ServerResponse.d.ts +14 -13
- package/dist/dts/Http/ServerResponse.d.ts.map +1 -1
- package/dist/dts/Http/UrlParams.d.ts +2 -1
- package/dist/dts/Http/UrlParams.d.ts.map +1 -1
- package/dist/dts/internal/http/router.d.ts.map +1 -1
- package/dist/esm/Http/Client.js.map +1 -1
- package/dist/esm/Http/ClientRequest.js.map +1 -1
- package/dist/esm/Http/ClientResponse.js.map +1 -1
- package/dist/esm/Http/Cookies.js +47 -25
- package/dist/esm/Http/Cookies.js.map +1 -1
- package/dist/esm/Http/IncomingMessage.js +12 -15
- package/dist/esm/Http/IncomingMessage.js.map +1 -1
- package/dist/esm/Http/Multipart.js.map +1 -1
- package/dist/esm/Http/Router.js +10 -0
- package/dist/esm/Http/Router.js.map +1 -1
- package/dist/esm/Http/ServerRequest.js +5 -0
- package/dist/esm/Http/ServerRequest.js.map +1 -1
- package/dist/esm/Http/ServerResponse.js.map +1 -1
- package/dist/esm/Http/UrlParams.js +2 -2
- package/dist/esm/Http/UrlParams.js.map +1 -1
- package/dist/esm/internal/http/body.js +2 -2
- package/dist/esm/internal/http/body.js.map +1 -1
- package/dist/esm/internal/http/client.js +4 -2
- package/dist/esm/internal/http/client.js.map +1 -1
- package/dist/esm/internal/http/clientRequest.js +2 -2
- package/dist/esm/internal/http/clientRequest.js.map +1 -1
- package/dist/esm/internal/http/clientResponse.js +8 -8
- package/dist/esm/internal/http/clientResponse.js.map +1 -1
- package/dist/esm/internal/http/multipart.js +4 -4
- package/dist/esm/internal/http/multipart.js.map +1 -1
- package/dist/esm/internal/http/router.js +31 -6
- package/dist/esm/internal/http/router.js.map +1 -1
- package/dist/esm/internal/http/serverRequest.js +20 -14
- package/dist/esm/internal/http/serverRequest.js.map +1 -1
- package/dist/esm/internal/http/serverResponse.js +19 -11
- package/dist/esm/internal/http/serverResponse.js.map +1 -1
- package/package.json +3 -3
- package/src/Http/Client.ts +5 -2
- package/src/Http/ClientRequest.ts +3 -1
- package/src/Http/ClientResponse.ts +16 -7
- package/src/Http/Cookies.ts +60 -37
- package/src/Http/Headers.ts +1 -1
- package/src/Http/IncomingMessage.ts +17 -12
- package/src/Http/Multipart.ts +5 -2
- package/src/Http/Router.ts +50 -3
- package/src/Http/ServerRequest.ts +23 -7
- package/src/Http/ServerResponse.ts +19 -13
- package/src/Http/UrlParams.ts +3 -2
- package/src/internal/http/body.ts +3 -2
- package/src/internal/http/client.ts +10 -5
- package/src/internal/http/clientRequest.ts +3 -2
- package/src/internal/http/clientResponse.ts +9 -8
- package/src/internal/http/multipart.ts +7 -4
- package/src/internal/http/router.ts +80 -6
- package/src/internal/http/serverRequest.ts +31 -15
- package/src/internal/http/serverResponse.ts +32 -22
package/README.md
CHANGED
|
@@ -30,13 +30,13 @@ import { Terminal } from "@effect/platform";
|
|
|
30
30
|
import { NodeRuntime, NodeTerminal } from "@effect/platform-node";
|
|
31
31
|
import { Effect } from "effect";
|
|
32
32
|
|
|
33
|
-
// const
|
|
34
|
-
const
|
|
33
|
+
// const displayMessage: Effect.Effect<void, PlatformError, Terminal.Terminal>
|
|
34
|
+
const displayMessage = Effect.gen(function* (_) {
|
|
35
35
|
const terminal = yield* _(Terminal.Terminal);
|
|
36
36
|
yield* _(terminal.display("a message\n"));
|
|
37
37
|
});
|
|
38
38
|
|
|
39
|
-
NodeRuntime.runMain(
|
|
39
|
+
NodeRuntime.runMain(displayMessage.pipe(Effect.provide(NodeTerminal.layer)));
|
|
40
40
|
// Output: "a message"
|
|
41
41
|
```
|
|
42
42
|
|
|
@@ -47,14 +47,14 @@ import { Terminal } from "@effect/platform";
|
|
|
47
47
|
import { NodeRuntime, NodeTerminal } from "@effect/platform-node";
|
|
48
48
|
import { Console, Effect } from "effect";
|
|
49
49
|
|
|
50
|
-
// const
|
|
51
|
-
const
|
|
50
|
+
// const readLine: Effect.Effect<void, Terminal.QuitException, Terminal.Terminal>
|
|
51
|
+
const readLine = Effect.gen(function* (_) {
|
|
52
52
|
const terminal = yield* _(Terminal.Terminal);
|
|
53
53
|
const input = yield* _(terminal.readLine);
|
|
54
54
|
yield* _(Console.log(`input: ${input}`));
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
-
NodeRuntime.runMain(
|
|
57
|
+
NodeRuntime.runMain(readLine.pipe(Effect.provide(NodeTerminal.layer)));
|
|
58
58
|
// Input: "hello"
|
|
59
59
|
// Output: "input: hello"
|
|
60
60
|
```
|
|
@@ -128,7 +128,14 @@ const loop = (
|
|
|
128
128
|
> =>
|
|
129
129
|
Effect.gen(function* (_) {
|
|
130
130
|
const guess = yield* _(answer);
|
|
131
|
-
return yield* _(
|
|
131
|
+
return yield* _(
|
|
132
|
+
check(
|
|
133
|
+
secret,
|
|
134
|
+
guess,
|
|
135
|
+
end,
|
|
136
|
+
Effect.suspend(() => loop(secret))
|
|
137
|
+
)
|
|
138
|
+
);
|
|
132
139
|
});
|
|
133
140
|
|
|
134
141
|
export const game = Effect.gen(function* (_) {
|
|
@@ -240,8 +247,8 @@ import { FileSystem } from "@effect/platform";
|
|
|
240
247
|
import { NodeFileSystem, NodeRuntime } from "@effect/platform-node";
|
|
241
248
|
import { Effect } from "effect";
|
|
242
249
|
|
|
243
|
-
// const
|
|
244
|
-
const
|
|
250
|
+
// const readFileString: Effect.Effect<void, PlatformError, FileSystem.FileSystem>
|
|
251
|
+
const readFileString = Effect.gen(function* (_) {
|
|
245
252
|
const fs = yield* _(FileSystem.FileSystem);
|
|
246
253
|
|
|
247
254
|
// Reading the content of the same file where this code is written
|
|
@@ -249,5 +256,300 @@ const program = Effect.gen(function* (_) {
|
|
|
249
256
|
console.log(content);
|
|
250
257
|
});
|
|
251
258
|
|
|
252
|
-
NodeRuntime.runMain(
|
|
259
|
+
NodeRuntime.runMain(readFileString.pipe(Effect.provide(NodeFileSystem.layer)));
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
# HTTP Client
|
|
263
|
+
|
|
264
|
+
## Retrieving Data (GET)
|
|
265
|
+
|
|
266
|
+
In this section, we'll explore how to retrieve data using the `HttpClient` module from `@effect/platform`.
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
270
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
271
|
+
import { Console, Effect } from "effect";
|
|
272
|
+
|
|
273
|
+
const getPostAsJson = Http.request
|
|
274
|
+
.get("https://jsonplaceholder.typicode.com/posts/1")
|
|
275
|
+
.pipe(Http.client.fetch(), Http.response.json);
|
|
276
|
+
|
|
277
|
+
NodeRuntime.runMain(
|
|
278
|
+
getPostAsJson.pipe(Effect.andThen((post) => Console.log(typeof post, post)))
|
|
279
|
+
);
|
|
280
|
+
/*
|
|
281
|
+
Output:
|
|
282
|
+
object {
|
|
283
|
+
userId: 1,
|
|
284
|
+
id: 1,
|
|
285
|
+
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
|
|
286
|
+
body: 'quia et suscipit\n' +
|
|
287
|
+
'suscipit recusandae consequuntur expedita et cum\n' +
|
|
288
|
+
'reprehenderit molestiae ut ut quas totam\n' +
|
|
289
|
+
'nostrum rerum est autem sunt rem eveniet architecto'
|
|
290
|
+
}
|
|
291
|
+
*/
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
If you want a response in a different format other than JSON, you can utilize other APIs provided by `Http.response`.
|
|
295
|
+
|
|
296
|
+
In the following example, we fetch the post as text:
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
300
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
301
|
+
import { Console, Effect } from "effect";
|
|
302
|
+
|
|
303
|
+
const getPostAsText = Http.request
|
|
304
|
+
.get("https://jsonplaceholder.typicode.com/posts/1")
|
|
305
|
+
.pipe(Http.client.fetch(), Http.response.text);
|
|
306
|
+
|
|
307
|
+
NodeRuntime.runMain(
|
|
308
|
+
getPostAsText.pipe(Effect.andThen((post) => Console.log(typeof post, post)))
|
|
309
|
+
);
|
|
310
|
+
/*
|
|
311
|
+
Output:
|
|
312
|
+
string {
|
|
313
|
+
userId: 1,
|
|
314
|
+
id: 1,
|
|
315
|
+
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
|
|
316
|
+
body: 'quia et suscipit\n' +
|
|
317
|
+
'suscipit recusandae consequuntur expedita et cum\n' +
|
|
318
|
+
'reprehenderit molestiae ut ut quas totam\n' +
|
|
319
|
+
'nostrum rerum est autem sunt rem eveniet architecto'
|
|
320
|
+
}
|
|
321
|
+
*/
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Here are some APIs you can use to convert the response:
|
|
325
|
+
|
|
326
|
+
| **API** | **Description** |
|
|
327
|
+
| ----------------------------- | ------------------------------------- |
|
|
328
|
+
| `Http.response.arrayBuffer` | Convert to `ArrayBuffer` |
|
|
329
|
+
| `Http.response.formData` | Convert to `FormData` |
|
|
330
|
+
| `Http.response.json` | Convert to JSON |
|
|
331
|
+
| `Http.response.stream` | Convert to a `Stream` of `Uint8Array` |
|
|
332
|
+
| `Http.response.text` | Convert to text |
|
|
333
|
+
| `Http.response.urlParamsBody` | Convert to `Http.urlParams.UrlParams` |
|
|
334
|
+
|
|
335
|
+
### Setting Headers
|
|
336
|
+
|
|
337
|
+
When making HTTP requests, sometimes you need to include additional information in the request headers. You can set headers using the `setHeader` function for a single header or `setHeaders` for multiple headers simultaneously.
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
341
|
+
|
|
342
|
+
const getPost = Http.request
|
|
343
|
+
.get("https://jsonplaceholder.typicode.com/posts/1")
|
|
344
|
+
.pipe(
|
|
345
|
+
// Setting a single header
|
|
346
|
+
Http.request.setHeader("Content-type", "application/json; charset=UTF-8"),
|
|
347
|
+
// Setting multiple headers
|
|
348
|
+
Http.request.setHeaders({
|
|
349
|
+
"Content-type": "application/json; charset=UTF-8",
|
|
350
|
+
Foo: "Bar",
|
|
351
|
+
}),
|
|
352
|
+
Http.client.fetch()
|
|
353
|
+
);
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Decoding Data with Schemas
|
|
357
|
+
|
|
358
|
+
A common use case when fetching data is to validate the received format. For this purpose, the `HttpClient` module is integrated with `@effect/schema`.
|
|
359
|
+
|
|
360
|
+
```ts
|
|
361
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
362
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
363
|
+
import { Schema } from "@effect/schema";
|
|
364
|
+
import { Console, Effect } from "effect";
|
|
365
|
+
|
|
366
|
+
const Post = Schema.struct({
|
|
367
|
+
id: Schema.number,
|
|
368
|
+
title: Schema.string,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
/*
|
|
372
|
+
const getPostAndValidate: Effect.Effect<{
|
|
373
|
+
readonly id: number;
|
|
374
|
+
readonly title: string;
|
|
375
|
+
}, Http.error.HttpClientError | ParseError, never>
|
|
376
|
+
*/
|
|
377
|
+
const getPostAndValidate = Http.request
|
|
378
|
+
.get("https://jsonplaceholder.typicode.com/posts/1")
|
|
379
|
+
.pipe(
|
|
380
|
+
Http.client.fetch(),
|
|
381
|
+
Effect.andThen(Http.response.schemaBodyJson(Post)),
|
|
382
|
+
Effect.scoped
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
NodeRuntime.runMain(getPostAndValidate.pipe(Effect.andThen(Console.log)));
|
|
386
|
+
/*
|
|
387
|
+
Output:
|
|
388
|
+
{
|
|
389
|
+
id: 1,
|
|
390
|
+
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit'
|
|
391
|
+
}
|
|
392
|
+
*/
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
In this example, we define a schema for a post object with properties `id` and `title`. Then, we fetch the data and validate it against this schema using `Http.response.schemaBodyJson`. Finally, we log the validated post object.
|
|
396
|
+
|
|
397
|
+
Note that we use `Effect.scoped` after consuming the response. This ensures that any resources associated with the HTTP request are properly cleaned up once we're done processing the response.
|
|
398
|
+
|
|
399
|
+
### Filtering And Error Handling
|
|
400
|
+
|
|
401
|
+
It's important to note that `Http.client.fetch` doesn't consider non-`200` status codes as errors by default. This design choice allows for flexibility in handling different response scenarios. For instance, you might have a schema union where the status code serves as the discriminator, enabling you to define a schema that encompasses all possible response cases.
|
|
402
|
+
|
|
403
|
+
You can use `Http.client.filterStatusOk`, or `Http.client.fetchOk` to ensure only `2xx` responses are treated as successes.
|
|
404
|
+
|
|
405
|
+
In this example, we attempt to fetch a non-existent page and don't receive any error:
|
|
406
|
+
|
|
407
|
+
```ts
|
|
408
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
409
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
410
|
+
import { Console, Effect } from "effect";
|
|
411
|
+
|
|
412
|
+
const getText = Http.request
|
|
413
|
+
.get("https://jsonplaceholder.typicode.com/non-existing-page")
|
|
414
|
+
.pipe(Http.client.fetch(), Http.response.text);
|
|
415
|
+
|
|
416
|
+
NodeRuntime.runMain(getText.pipe(Effect.andThen(Console.log)));
|
|
417
|
+
/*
|
|
418
|
+
Output:
|
|
419
|
+
{}
|
|
420
|
+
*/
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
However, if we use `Http.client.filterStatusOk`, an error is logged:
|
|
424
|
+
|
|
425
|
+
```ts
|
|
426
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
427
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
428
|
+
import { Console, Effect } from "effect";
|
|
429
|
+
|
|
430
|
+
const getText = Http.request
|
|
431
|
+
.get("https://jsonplaceholder.typicode.com/non-existing-page")
|
|
432
|
+
.pipe(Http.client.filterStatusOk(Http.client.fetch()), Http.response.text);
|
|
433
|
+
|
|
434
|
+
NodeRuntime.runMain(getText.pipe(Effect.andThen(Console.log)));
|
|
435
|
+
/*
|
|
436
|
+
Output:
|
|
437
|
+
timestamp=2024-03-25T10:21:16.972Z level=ERROR fiber=#0 cause="ResponseError: StatusCode error (404 GET https://jsonplaceholder.typicode.com/non-existing-page): non 2xx status code
|
|
438
|
+
*/
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
Note that you can use `Http.client.fetchOk` as a shortcut for `Http.client.filterStatusOk(Http.client.fetch())`:
|
|
442
|
+
|
|
443
|
+
```ts
|
|
444
|
+
const getText = Http.request
|
|
445
|
+
.get("https://jsonplaceholder.typicode.com/non-existing-page")
|
|
446
|
+
.pipe(Http.client.fetchOk(), Http.response.text);
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
You can also create your own status-based filters. In fact, `Http.client.filterStatusOk` is just a shortcut for the following filter:
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
const getText = Http.request
|
|
453
|
+
.get("https://jsonplaceholder.typicode.com/non-existing-page")
|
|
454
|
+
.pipe(
|
|
455
|
+
Http.client.filterStatus(
|
|
456
|
+
Http.client.fetch(),
|
|
457
|
+
(status) => status >= 200 && status < 300
|
|
458
|
+
),
|
|
459
|
+
Http.response.text
|
|
460
|
+
);
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
## POST
|
|
464
|
+
|
|
465
|
+
To make a POST request, you can use the `Http.request.post` function provided by the `HttpClient` module. Here's an example of how to create and send a POST request:
|
|
466
|
+
|
|
467
|
+
```ts
|
|
468
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
469
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
470
|
+
import { Console, Effect } from "effect";
|
|
471
|
+
|
|
472
|
+
const addPost = Http.request
|
|
473
|
+
.post("https://jsonplaceholder.typicode.com/posts")
|
|
474
|
+
.pipe(
|
|
475
|
+
Http.request.jsonBody({
|
|
476
|
+
title: "foo",
|
|
477
|
+
body: "bar",
|
|
478
|
+
userId: 1,
|
|
479
|
+
}),
|
|
480
|
+
Effect.andThen(Http.client.fetch()),
|
|
481
|
+
Http.response.json
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
NodeRuntime.runMain(addPost.pipe(Effect.andThen(Console.log)));
|
|
485
|
+
/*
|
|
486
|
+
Output:
|
|
487
|
+
{ title: 'foo', body: 'bar', userId: 1, id: 101 }
|
|
488
|
+
*/
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
If you need to send data in a format other than JSON, such as plain text, you can use different APIs provided by `Http.request`.
|
|
492
|
+
|
|
493
|
+
In the following example, we send the data as text:
|
|
494
|
+
|
|
495
|
+
```ts
|
|
496
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
497
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
498
|
+
import { Console, Effect } from "effect";
|
|
499
|
+
|
|
500
|
+
const addPost = Http.request
|
|
501
|
+
.post("https://jsonplaceholder.typicode.com/posts")
|
|
502
|
+
.pipe(
|
|
503
|
+
Http.request.textBody(
|
|
504
|
+
JSON.stringify({
|
|
505
|
+
title: "foo",
|
|
506
|
+
body: "bar",
|
|
507
|
+
userId: 1,
|
|
508
|
+
}),
|
|
509
|
+
"application/json; charset=UTF-8"
|
|
510
|
+
),
|
|
511
|
+
Http.client.fetch(),
|
|
512
|
+
Http.response.json
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
NodeRuntime.runMain(Effect.andThen(addPost, Console.log));
|
|
516
|
+
/*
|
|
517
|
+
Output:
|
|
518
|
+
{ title: 'foo', body: 'bar', userId: 1, id: 101 }
|
|
519
|
+
*/
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
### Decoding Data with Schemas
|
|
523
|
+
|
|
524
|
+
A common use case when fetching data is to validate the received format. For this purpose, the `HttpClient` module is integrated with `@effect/schema`.
|
|
525
|
+
|
|
526
|
+
```ts
|
|
527
|
+
import { NodeRuntime } from "@effect/platform-node";
|
|
528
|
+
import * as Http from "@effect/platform/HttpClient";
|
|
529
|
+
import { Schema } from "@effect/schema";
|
|
530
|
+
import { Console, Effect } from "effect";
|
|
531
|
+
|
|
532
|
+
const Post = Schema.struct({
|
|
533
|
+
id: Schema.number,
|
|
534
|
+
title: Schema.string,
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
const addPost = Http.request
|
|
538
|
+
.post("https://jsonplaceholder.typicode.com/posts")
|
|
539
|
+
.pipe(
|
|
540
|
+
Http.request.jsonBody({
|
|
541
|
+
title: "foo",
|
|
542
|
+
body: "bar",
|
|
543
|
+
userId: 1,
|
|
544
|
+
}),
|
|
545
|
+
Effect.andThen(Http.client.fetch()),
|
|
546
|
+
Effect.andThen(Http.response.schemaBodyJson(Post)),
|
|
547
|
+
Effect.scoped
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
NodeRuntime.runMain(addPost.pipe(Effect.andThen(Console.log)));
|
|
551
|
+
/*
|
|
552
|
+
Output:
|
|
553
|
+
{ id: 101, title: 'foo' }
|
|
554
|
+
*/
|
|
253
555
|
```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Client.js","names":["internal","_interopRequireWildcard","require","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","TypeId","exports","Client","tag","Fetch","layer","fetch","fetchOk","catchAll","catchTag","catchTags","filterOrElse","filterOrFail","filterStatus","filterStatusOk","make","makeDefault","transform","transformResponse","map","mapEffect","mapEffectScoped","mapRequest","mapRequestEffect","mapInputRequest","mapInputRequestEffect","retry","scoped","schemaFunction","tap","tapRequest","withCookiesRef"],"sources":["../../../src/Http/Client.ts"],"sourcesContent":[null],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"Client.js","names":["internal","_interopRequireWildcard","require","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","TypeId","exports","Client","tag","Fetch","layer","fetch","fetchOk","catchAll","catchTag","catchTags","filterOrElse","filterOrFail","filterStatus","filterStatusOk","make","makeDefault","transform","transformResponse","map","mapEffect","mapEffectScoped","mapRequest","mapRequestEffect","mapInputRequest","mapInputRequestEffect","retry","scoped","schemaFunction","tap","tapRequest","withCookiesRef"],"sources":["../../../src/Http/Client.ts"],"sourcesContent":[null],"mappings":";;;;;;AAgBA,IAAAA,QAAA,gBAAAC,uBAAA,eAAAC,OAAA;AAAsD,SAAAC,yBAAAC,CAAA;EAAA,yBAAAC,OAAA;EAAA,IAAAC,CAAA,OAAAD,OAAA;IAAAE,CAAA,OAAAF,OAAA;EAAA,QAAAF,wBAAA,YAAAA,CAAAC,CAAA;IAAA,OAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA;EAAA,GAAAF,CAAA;AAAA;AAAA,SAAAH,wBAAAG,CAAA,EAAAE,CAAA;EAAA,KAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA;EAAA,aAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA;IAAAK,OAAA,EAAAL;EAAA;EAAA,IAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA;EAAA,IAAAC,CAAA,IAAAA,CAAA,CAAAG,GAAA,CAAAN,CAAA,UAAAG,CAAA,CAAAI,GAAA,CAAAP,CAAA;EAAA,IAAAQ,CAAA;MAAAC,SAAA;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,gBAAgB,GAAAtB,OAAA,CAAAsB,gBAAA,GAQzB9C,QAAQ,CAAC8C,gBAAgB;AAE7B;;;;AAIO,MAAMC,eAAe,GAAAvB,OAAA,CAAAuB,eAAA,GAKxB/C,QAAQ,CAAC+C,eAAe;AAE5B;;;;AAIO,MAAMC,qBAAqB,GAAAxB,OAAA,CAAAwB,qBAAA,GAQ9BhD,QAAQ,CAACgD,qBAAqB;AAElC;;;;AAIO,MAAMC,KAAK,GAAAzB,OAAA,CAAAyB,KAAA,GAGdjD,QAAQ,CAACiD,KAAK;AAElB;;;;AAIO,MAAMC,MAAM,GAAA1B,OAAA,CAAA0B,MAAA,GAA8ElD,QAAQ,CAACkD,MAAM;AAEhH;;;;AAIO,MAAMC,cAAc,GAAA3B,OAAA,CAAA2B,cAAA,GAgBvBnD,QAAQ,CAACmD,cAAc;AAE3B;;;;AAIO,MAAMC,GAAG,GAAA5B,OAAA,CAAA4B,GAAA,GAGZpD,QAAQ,CAACoD,GAAG;AAEhB;;;;AAIO,MAAMC,UAAU,GAAA7B,OAAA,CAAA6B,UAAA,GAQnBrD,QAAQ,CAACqD,UAAU;AAEvB;;;;AAIO,MAAMC,cAAc,GAAA9B,OAAA,CAAA8B,cAAA,GAGvBtD,QAAQ,CAACsD,cAAc","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientRequest.js","names":["internal","_interopRequireWildcard","require","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","TypeId","exports","Symbol","for","make","post","patch","put","del","head","options","modify","setMethod","setHeader","setHeaders","basicAuth","bearerToken","accept","acceptJson","setUrl","prependUrl","appendUrl","updateUrl","setUrlParam","setUrlParams","appendUrlParam","appendUrlParams","setBody","uint8ArrayBody","textBody","jsonBody","unsafeJsonBody","schemaBody","urlParamsBody","formDataBody","streamBody","fileBody","fileWebBody"],"sources":["../../../src/Http/ClientRequest.ts"],"sourcesContent":[null],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"ClientRequest.js","names":["internal","_interopRequireWildcard","require","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","TypeId","exports","Symbol","for","make","post","patch","put","del","head","options","modify","setMethod","setHeader","setHeaders","basicAuth","bearerToken","accept","acceptJson","setUrl","prependUrl","appendUrl","updateUrl","setUrlParam","setUrlParams","appendUrlParam","appendUrlParams","setBody","uint8ArrayBody","textBody","jsonBody","unsafeJsonBody","schemaBody","urlParamsBody","formDataBody","streamBody","fileBody","fileWebBody"],"sources":["../../../src/Http/ClientRequest.ts"],"sourcesContent":[null],"mappings":";;;;;;AAWA,IAAAA,QAAA,gBAAAC,uBAAA,eAAAC,OAAA;AAA6D,SAAAC,yBAAAC,CAAA;EAAA,yBAAAC,OAAA;EAAA,IAAAC,CAAA,OAAAD,OAAA;IAAAE,CAAA,OAAAF,OAAA;EAAA,QAAAF,wBAAA,YAAAA,CAAAC,CAAA;IAAA,OAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA;EAAA,GAAAF,CAAA;AAAA;AAAA,SAAAH,wBAAAG,CAAA,EAAAE,CAAA;EAAA,KAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA;EAAA,aAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA;IAAAK,OAAA,EAAAL;EAAA;EAAA,IAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA;EAAA,IAAAC,CAAA,IAAAA,CAAA,CAAAG,GAAA,CAAAN,CAAA,UAAAG,CAAA,CAAAI,GAAA,CAAAP,CAAA;EAAA,IAAAQ,CAAA;MAAAC,SAAA;IAAA;IAAAC,CAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA;EAAA,SAAAC,CAAA,IAAAd,CAA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1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientResponse.js","names":["internal","_interopRequireWildcard","require","_IncomingMessage","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","TypeId","exports","fromWeb","schemaJson","schemaNoBody","arrayBuffer","formData","json","stream","text","urlParamsBody","schemaJsonEffect","schemaNoBodyEffect"],"sources":["../../../src/Http/ClientResponse.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"ClientResponse.js","names":["internal","_interopRequireWildcard","require","_IncomingMessage","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","TypeId","exports","fromWeb","schemaJson","schemaNoBody","arrayBuffer","formData","json","stream","text","urlParamsBody","schemaJsonEffect","schemaNoBodyEffect"],"sources":["../../../src/Http/ClientResponse.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,IAAAA,QAAA,gBAAAC,uBAAA,eAAAC,OAAA;AAOA,IAAAC,gBAAA,gBAAAD,OAAA;AA+B6B,SAAAE,yBAAAC,CAAA;EAAA,yBAAAC,OAAA;EAAA,IAAAC,CAAA,OAAAD,OAAA;IAAAE,CAAA,OAAAF,OAAA;EAAA,QAAAF,wBAAA,YAAAA,CAAAC,CAAA;IAAA,OAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA;EAAA,GAAAF,CAAA;AAAA;AAAA,SAAAJ,wBAAAI,CAAA,EAAAE,CAAA;EAAA,KAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA;EAAA,aAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA;IAAAK,OAAA,EAAAL;EAAA;E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,GAAAL,OAAA,CAAAK,QAAA,GAE4D9B,QAAQ,CAAC8B,QAAQ;AAElG;;;;AAIO,MAAMC,IAAI,GAAAN,OAAA,CAAAM,IAAA,GAE+D/B,QAAQ,CAAC+B,IAAI;AAE7F;;;;AAIO,MAAMC,MAAM,GAAAP,OAAA,CAAAO,MAAA,GAEgEhC,QAAQ,CAACgC,MAAM;AAElG;;;;AAIO,MAAMC,IAAI,GAAAR,OAAA,CAAAQ,IAAA,GAE8DjC,QAAQ,CAACiC,IAAI;AAE5F;;;;AAIO,MAAMC,aAAa,GAAAT,OAAA,CAAAS,aAAA,GAEkElC,QAAQ,CAACkC,aAAa;AAElH;;;;AAIO,MAAMC,gBAAgB,GAAAV,OAAA,CAAAU,gBAAA,GAiBzBnC,QAAQ,CAACmC,gBAAgB;AAE7B;;;;AAIO,MAAMC,kBAAkB,GAAAX,OAAA,CAAAW,kBAAA,GAa7BpC,QAAQ,CAACoC,kBAAkB","ignoreList":[]}
|
package/dist/cjs/Http/Cookies.js
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.isEmpty = exports.isCookies = exports.fromSetCookie = exports.
|
|
6
|
+
exports.isEmpty = exports.isCookies = exports.fromSetCookie = exports.fromReadonlyRecord = exports.fromIterable = exports.empty = exports.TypeId = exports.ErrorTypeId = exports.CookiesError = exports.CookieTypeId = void 0;
|
|
7
7
|
exports.makeCookie = makeCookie;
|
|
8
8
|
exports.merge = void 0;
|
|
9
9
|
exports.parseHeader = parseHeader;
|
|
10
10
|
exports.remove = void 0;
|
|
11
11
|
exports.serializeCookie = serializeCookie;
|
|
12
|
-
exports.
|
|
12
|
+
exports.unsafeSetAll = exports.unsafeSet = exports.unsafeMakeCookie = exports.toSetCookieHeaders = exports.toRecord = exports.toCookieHeader = exports.setCookie = exports.setAllCookie = exports.setAll = exports.set = void 0;
|
|
13
13
|
var Duration = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Duration"));
|
|
14
14
|
var Either = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Either"));
|
|
15
15
|
var _Function = /*#__PURE__*/require("effect/Function");
|
|
@@ -17,7 +17,7 @@ var Inspectable = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("ef
|
|
|
17
17
|
var Option = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Option"));
|
|
18
18
|
var _Pipeable = /*#__PURE__*/require("effect/Pipeable");
|
|
19
19
|
var Predicate = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Predicate"));
|
|
20
|
-
var
|
|
20
|
+
var ReadonlyRecord = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/ReadonlyRecord"));
|
|
21
21
|
var _Error = /*#__PURE__*/require("../Error.js");
|
|
22
22
|
function _getRequireWildcardCache(e) {
|
|
23
23
|
if ("function" != typeof WeakMap) return null;
|
|
@@ -85,7 +85,7 @@ const Proto = {
|
|
|
85
85
|
toJSON() {
|
|
86
86
|
return {
|
|
87
87
|
_id: "@effect/platform/Http/Cookies",
|
|
88
|
-
cookies: this.cookies
|
|
88
|
+
cookies: ReadonlyRecord.map(this.cookies, cookie => cookie.toJSON())
|
|
89
89
|
};
|
|
90
90
|
},
|
|
91
91
|
pipe() {
|
|
@@ -98,11 +98,25 @@ const Proto = {
|
|
|
98
98
|
* @since 1.0.0
|
|
99
99
|
* @category constructors
|
|
100
100
|
*/
|
|
101
|
-
const
|
|
101
|
+
const fromReadonlyRecord = cookies => {
|
|
102
102
|
const self = Object.create(Proto);
|
|
103
|
-
self.cookies =
|
|
103
|
+
self.cookies = cookies;
|
|
104
104
|
return self;
|
|
105
105
|
};
|
|
106
|
+
/**
|
|
107
|
+
* Create a Cookies object from an Iterable
|
|
108
|
+
*
|
|
109
|
+
* @since 1.0.0
|
|
110
|
+
* @category constructors
|
|
111
|
+
*/
|
|
112
|
+
exports.fromReadonlyRecord = fromReadonlyRecord;
|
|
113
|
+
const fromIterable = cookies => {
|
|
114
|
+
const record = {};
|
|
115
|
+
for (const cookie of cookies) {
|
|
116
|
+
record[cookie.name] = cookie;
|
|
117
|
+
}
|
|
118
|
+
return fromReadonlyRecord(record);
|
|
119
|
+
};
|
|
106
120
|
/**
|
|
107
121
|
* Create a Cookies object from a set of Set-Cookie headers
|
|
108
122
|
*
|
|
@@ -265,7 +279,7 @@ const empty = exports.empty = /*#__PURE__*/fromIterable([]);
|
|
|
265
279
|
* @since 1.0.0
|
|
266
280
|
* @category refinements
|
|
267
281
|
*/
|
|
268
|
-
const isEmpty = self => self.cookies
|
|
282
|
+
const isEmpty = self => ReadonlyRecord.isEmptyRecord(self.cookies);
|
|
269
283
|
// eslint-disable-next-line no-control-regex
|
|
270
284
|
exports.isEmpty = isEmpty;
|
|
271
285
|
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
@@ -337,63 +351,71 @@ const unsafeMakeCookie = (name, value, options) => Either.getOrThrow(makeCookie(
|
|
|
337
351
|
* @category combinators
|
|
338
352
|
*/
|
|
339
353
|
exports.unsafeMakeCookie = unsafeMakeCookie;
|
|
340
|
-
const
|
|
354
|
+
const setCookie = exports.setCookie = /*#__PURE__*/(0, _Function.dual)(2, (self, cookie) => fromReadonlyRecord(ReadonlyRecord.set(self.cookies, cookie.name, cookie)));
|
|
341
355
|
/**
|
|
342
356
|
* Add multiple cookies to a Cookies object
|
|
343
357
|
*
|
|
344
358
|
* @since 1.0.0
|
|
345
359
|
* @category combinators
|
|
346
360
|
*/
|
|
347
|
-
const
|
|
361
|
+
const setAllCookie = exports.setAllCookie = /*#__PURE__*/(0, _Function.dual)(2, (self, cookies) => {
|
|
362
|
+
const record = {
|
|
363
|
+
...self.cookies
|
|
364
|
+
};
|
|
365
|
+
for (const cookie of cookies) {
|
|
366
|
+
record[cookie.name] = cookie;
|
|
367
|
+
}
|
|
368
|
+
return fromReadonlyRecord(record);
|
|
369
|
+
});
|
|
348
370
|
/**
|
|
349
371
|
* Combine two Cookies objects, removing duplicates from the first
|
|
350
372
|
*
|
|
351
373
|
* @since 1.0.0
|
|
352
374
|
* @category combinators
|
|
353
375
|
*/
|
|
354
|
-
const merge = exports.merge = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
return fromIterable(cookies);
|
|
359
|
-
});
|
|
376
|
+
const merge = exports.merge = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => fromReadonlyRecord({
|
|
377
|
+
...self.cookies,
|
|
378
|
+
...that.cookies
|
|
379
|
+
}));
|
|
360
380
|
/**
|
|
361
381
|
* Remove a cookie by name
|
|
362
382
|
*
|
|
363
383
|
* @since 1.0.0
|
|
364
384
|
* @category combinators
|
|
365
385
|
*/
|
|
366
|
-
const remove = exports.remove = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
386
|
+
const remove = exports.remove = /*#__PURE__*/(0, _Function.dual)(2, (self, name) => fromReadonlyRecord(ReadonlyRecord.remove(self.cookies, name)));
|
|
367
387
|
/**
|
|
368
388
|
* Add a cookie to a Cookies object
|
|
369
389
|
*
|
|
370
390
|
* @since 1.0.0
|
|
371
391
|
* @category combinators
|
|
372
392
|
*/
|
|
373
|
-
const
|
|
393
|
+
const set = exports.set = /*#__PURE__*/(0, _Function.dual)(args => isCookies(args[0]), (self, name, value, options) => Either.map(makeCookie(name, value, options), cookie => fromReadonlyRecord(ReadonlyRecord.set(self.cookies, name, cookie))));
|
|
374
394
|
/**
|
|
375
395
|
* Add a cookie to a Cookies object
|
|
376
396
|
*
|
|
377
397
|
* @since 1.0.0
|
|
378
398
|
* @category combinators
|
|
379
399
|
*/
|
|
380
|
-
const
|
|
400
|
+
const unsafeSet = exports.unsafeSet = /*#__PURE__*/(0, _Function.dual)(args => isCookies(args[0]), (self, name, value, options) => fromReadonlyRecord(ReadonlyRecord.set(self.cookies, name, unsafeMakeCookie(name, value, options))));
|
|
381
401
|
/**
|
|
382
402
|
* Add multiple cookies to a Cookies object
|
|
383
403
|
*
|
|
384
404
|
* @since 1.0.0
|
|
385
405
|
* @category combinators
|
|
386
406
|
*/
|
|
387
|
-
const
|
|
388
|
-
const
|
|
407
|
+
const setAll = exports.setAll = /*#__PURE__*/(0, _Function.dual)(2, (self, cookies) => {
|
|
408
|
+
const record = {
|
|
409
|
+
...self.cookies
|
|
410
|
+
};
|
|
389
411
|
for (const [name, value, options] of cookies) {
|
|
390
412
|
const either = makeCookie(name, value, options);
|
|
391
413
|
if (Either.isLeft(either)) {
|
|
392
414
|
return either;
|
|
393
415
|
}
|
|
394
|
-
|
|
416
|
+
record[name] = either.right;
|
|
395
417
|
}
|
|
396
|
-
return Either.right(
|
|
418
|
+
return Either.right(fromReadonlyRecord(record));
|
|
397
419
|
});
|
|
398
420
|
/**
|
|
399
421
|
* Add multiple cookies to a Cookies object, throwing an error if invalid
|
|
@@ -401,7 +423,7 @@ const addAll = exports.addAll = /*#__PURE__*/(0, _Function.dual)(2, (self, cooki
|
|
|
401
423
|
* @since 1.0.0
|
|
402
424
|
* @category combinators
|
|
403
425
|
*/
|
|
404
|
-
const
|
|
426
|
+
const unsafeSetAll = exports.unsafeSetAll = /*#__PURE__*/(0, _Function.dual)(2, (self, cookies) => Either.getOrThrow(setAll(self, cookies)));
|
|
405
427
|
/**
|
|
406
428
|
* Serialize a cookie into a string
|
|
407
429
|
*
|
|
@@ -474,7 +496,7 @@ function serializeCookie(self) {
|
|
|
474
496
|
* @since 1.0.0
|
|
475
497
|
* @category encoding
|
|
476
498
|
*/
|
|
477
|
-
const toCookieHeader = self => self.cookies.map(cookie => `${cookie.name}=${cookie.valueEncoded}`).join("; ");
|
|
499
|
+
const toCookieHeader = self => Object.values(self.cookies).map(cookie => `${cookie.name}=${cookie.valueEncoded}`).join("; ");
|
|
478
500
|
/**
|
|
479
501
|
* To record
|
|
480
502
|
*
|
|
@@ -484,8 +506,9 @@ const toCookieHeader = self => self.cookies.map(cookie => `${cookie.name}=${cook
|
|
|
484
506
|
exports.toCookieHeader = toCookieHeader;
|
|
485
507
|
const toRecord = self => {
|
|
486
508
|
const record = {};
|
|
487
|
-
|
|
488
|
-
|
|
509
|
+
const cookies = Object.values(self.cookies);
|
|
510
|
+
for (let index = 0; index < cookies.length; index++) {
|
|
511
|
+
const cookie = cookies[index];
|
|
489
512
|
record[cookie.name] = cookie.value;
|
|
490
513
|
}
|
|
491
514
|
return record;
|
|
@@ -497,7 +520,7 @@ const toRecord = self => {
|
|
|
497
520
|
* @category encoding
|
|
498
521
|
*/
|
|
499
522
|
exports.toRecord = toRecord;
|
|
500
|
-
const toSetCookieHeaders = self => self.cookies.map(serializeCookie);
|
|
523
|
+
const toSetCookieHeaders = self => Object.values(self.cookies).map(serializeCookie);
|
|
501
524
|
/**
|
|
502
525
|
* Parse a cookie header into a record of key-value pairs
|
|
503
526
|
*
|