-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.ts
39 lines (34 loc) · 925 Bytes
/
service.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
import * as E from "fp-ts/Either";
import * as TE from "fp-ts/TaskEither";
import { pipe } from "fp-ts/lib/function";
import axios from "axios";
import * as t from "io-ts";
import { failure } from "io-ts/lib/PathReporter";
import * as C from "fp-ts/Console";
const Todo = t.type({
title: t.string,
});
type Todo = t.TypeOf<typeof Todo>;
const bodyDecoder = (u: unknown) =>
pipe(
Todo.decode(u),
E.mapLeft(
(errors) =>
`Could not parse response body:\n${failure(errors).join("\n")}`
),
TE.fromEither
);
const getTodo = (requestId: string): TE.TaskEither<string, Todo> =>
pipe(
C.info(`calling service {requestId=${requestId}}`),
TE.fromIO,
TE.chain(() =>
TE.tryCatch(
() => axios("https://jsonplaceholder.typicode.com/todos/1"),
String
)
),
TE.map((response) => response.data),
TE.chain(bodyDecoder)
);
export { Todo, getTodo };