-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPokeApi.ts
More file actions
35 lines (29 loc) · 1.08 KB
/
PokeApi.ts
File metadata and controls
35 lines (29 loc) · 1.08 KB
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
import { Effect, Schema } from "effect";
import { BuildPokeApiUrl } from "./BuildPokeApiUrl";
import { FetchError, JsonError } from "./errors";
import { PokemonCollection } from "./PokemonCollection";
import { Pokemon } from "./schemas";
export class PokeApi extends Effect.Service<PokeApi>()("PokeApi", {
dependencies: [PokemonCollection.Default, BuildPokeApiUrl.Default],
effect: Effect.gen(function* () {
const pokemonCollection = yield* PokemonCollection;
const buildPokeApiUrl = yield* BuildPokeApiUrl;
return {
getPokemon: Effect.gen(function* () {
const requestUrl = buildPokeApiUrl({ name: pokemonCollection[0] });
const response = yield* Effect.tryPromise({
try: () => fetch(requestUrl),
catch: () => new FetchError(),
});
if (!response.ok) {
return yield* new FetchError();
}
const json = yield* Effect.tryPromise({
try: () => response.json(),
catch: () => new JsonError(),
});
return yield* Schema.decodeUnknown(Pokemon)(json);
}),
};
}),
}) {}