Skip to content

Commit 092e0f1

Browse files
PokeApi with Effect.Service
1 parent b749f91 commit 092e0f1

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

src/PokeApi.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
import { Schema } from "@effect/schema";
2-
import { Context, Effect, Layer } from "effect";
2+
import { Effect } from "effect";
33
import { BuildPokeApiUrl } from "./BuildPokeApiUrl";
44
import { FetchError, JsonError } from "./errors";
55
import { PokemonCollection } from "./PokemonCollection";
66
import { Pokemon } from "./schemas";
77

8-
const make = Effect.gen(function* () {
9-
const pokemonCollection = yield* PokemonCollection;
10-
const buildPokeApiUrl = yield* BuildPokeApiUrl;
8+
export class PokeApi extends Effect.Service<PokeApi>()("PokeApi", {
9+
effect: Effect.gen(function* () {
10+
const pokemonCollection = yield* PokemonCollection;
11+
const buildPokeApiUrl = yield* BuildPokeApiUrl;
1112

12-
return {
13-
getPokemon: Effect.gen(function* () {
14-
const requestUrl = buildPokeApiUrl({ name: pokemonCollection[0] });
13+
return {
14+
getPokemon: Effect.gen(function* () {
15+
const requestUrl = buildPokeApiUrl({ name: pokemonCollection[0] });
1516

16-
const response = yield* Effect.tryPromise({
17-
try: () => fetch(requestUrl),
18-
catch: () => new FetchError(),
19-
});
17+
const response = yield* Effect.tryPromise({
18+
try: () => fetch(requestUrl),
19+
catch: () => new FetchError(),
20+
});
2021

21-
if (!response.ok) {
22-
return yield* new FetchError();
23-
}
22+
if (!response.ok) {
23+
return yield* new FetchError();
24+
}
2425

25-
const json = yield* Effect.tryPromise({
26-
try: () => response.json(),
27-
catch: () => new JsonError(),
28-
});
26+
const json = yield* Effect.tryPromise({
27+
try: () => response.json(),
28+
catch: () => new JsonError(),
29+
});
2930

30-
return yield* Schema.decodeUnknown(Pokemon)(json);
31-
}),
32-
};
33-
});
34-
35-
export class PokeApi extends Context.Tag("PokeApi")<
36-
PokeApi,
37-
Effect.Effect.Success<typeof make>
38-
>() {
39-
static readonly Live = Layer.effect(this, make).pipe(
40-
Layer.provide(Layer.mergeAll(PokemonCollection.Live, BuildPokeApiUrl.Live))
41-
);
42-
}
31+
return yield* Schema.decodeUnknown(Pokemon)(json);
32+
}),
33+
};
34+
}),
35+
dependencies: [PokemonCollection.Live, BuildPokeApiUrl.Live],
36+
}) {}

src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const TestConfigProvider = ConfigProvider.fromMap(
1212
);
1313

1414
const ConfigProviderLayer = Layer.setConfigProvider(TestConfigProvider);
15-
const MainLayer = PokeApi.Live.pipe(Layer.provide(ConfigProviderLayer));
15+
const MainLayer = PokeApi.Default.pipe(Layer.provide(ConfigProviderLayer));
1616

1717
const TestingRuntime = ManagedRuntime.make(MainLayer);
1818

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Effect, Layer, ManagedRuntime } from "effect";
22
import { PokeApi } from "./PokeApi";
33

4-
const MainLayer = Layer.mergeAll(PokeApi.Live);
4+
const MainLayer = Layer.mergeAll(PokeApi.Default);
55

66
const PokemonRuntime = ManagedRuntime.make(MainLayer);
77

0 commit comments

Comments
 (0)