-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfetch.ts
43 lines (39 loc) · 1019 Bytes
/
fetch.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
import { HTTPClient1 } from '../out/petstore.yaml/client/client';
import { MonadThrow1 } from 'fp-ts/MonadThrow';
import { pipe } from 'fp-ts/function';
import { record } from 'fp-ts';
export const URI = 'Promise' as const;
export type URI = typeof URI;
declare module 'fp-ts/HKT' {
interface URItoKind<A> {
readonly [URI]: Promise<A>;
}
}
export const monadPromise: MonadThrow1<URI> = {
URI,
of: Promise.resolve,
throwError: Promise.reject,
map: (fa, f) => fa.then(a => Promise.resolve(f(a))),
ap: (fab, fa) => fab.then(fn => fa.then(a => fn(a))),
chain: (fa, f) => fa.then(f),
};
export const fetchClient: HTTPClient1<URI> = {
...monadPromise,
request: ({ body, headers, method, query, url }) =>
fetch(query ? `${url}?${query}` : url, {
body: body && JSON.stringify(body),
headers:
headers &&
pipe(
headers,
record.map(val => {
if (Array.isArray(val)) {
return val.join(',');
} else {
return String(val);
}
}),
),
method,
}),
};