-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
69 lines (55 loc) · 1.88 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { expect, test } from "vitest";
import * as Effect from "effect/Effect";
import { pipe } from "effect/Function";
import * as Stream from "effect/Stream";
import * as Adapter from "../src/Adapters/Fetch.js";
import * as Fetch from "../src/Fetch.js";
import * as Response from "../src/Response.js";
import { DecodeError } from "../src/internal/error.js";
const base_url = "https://reqres.in/api";
const makeClient = <A, E, R>(effect: Effect.Effect<A, E, R | Fetch.Fetch>) => {
const adapter = Fetch.make(Adapter.fetch);
return Effect.provide(effect, adapter);
};
test("google", async () => {
const program = Effect.flatMap(
Fetch.fetch("https://www.google.com"),
Response.text
);
const result = await Effect.runPromise(makeClient(program));
expect(result).toContain("Google");
});
test("streaming", async () => {
const program = pipe(
Fetch.fetch("https://www.google.com"),
Effect.map((_) =>
_.body == null
? Stream.fail(new DecodeError("Cannot create stream from empty body"))
: Stream.fromReadableStream(
() => _.body!,
(r) => new DecodeError(r)
)
),
Stream.unwrap,
Stream.runFold("", (a, b) => a + new TextDecoder().decode(b))
);
const result = await Effect.runPromise(makeClient(program));
expect(result).toContain("Google");
});
test("should make request", async () => {
const program = Effect.flatMap(
Fetch.fetch(base_url + "/users/2"),
Response.json
);
const result = await Effect.runPromise(makeClient(program));
expect(result.data.id).toBe(2);
});
test("fetch with Effect.gen", async () => {
const program = Effect.gen(function* () {
const fetch = yield* Fetch.Fetch;
const res = yield* fetch(base_url + "/users/2");
return yield* Response.json(res);
});
const result = await Effect.runPromise(makeClient(program));
expect(result.data.id).toBe(2);
});