-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.ts
60 lines (55 loc) · 1.53 KB
/
http.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
type Fetch = typeof fetch;
type FetchParams = Parameters<Fetch>;
/**
* Simple fetch wrapper for working with JSON HTTP endpoints.
* It will raise error when `response.ok !== true` so you can catch API errors as usual.
*
* @example
* ```ts
* const api = getClient(fetch, { headers: { 'X-Auth-Dog': '123doggod321' } });
* const dogs = await api('https://api.dogs.awesome') // => { dogs: [ { name: 'Rex' }, { name: 'Bella' } ] }
* ````
*/
export function getClient(fetch: Fetch, defaultOpts?: FetchParams[1]) {
return async <T extends Record<string, unknown>>(
...params: FetchParams
): Promise<T | string> => {
let init = params[1];
if (defaultOpts) {
init = { ...defaultOpts, ...init };
}
const res = await fetch(params[0], init);
const isJson = res.headers.get("content-type")?.includes(
"application/json",
);
const result = isJson ? (await res.json() as T) : await res.text();
if (res.ok) {
return result;
} else {
throw new Error(
`Request to ${res.url} failed with status ${res.status} and response "${
isJson ? JSON.stringify(res.body) : result
}".`,
);
}
};
}
/**
* Creates 'Ok' Response
*
* @xample
* ```ts
* return created ? ok(201, 'Created') : ok();
* ```
*/
export const ok = (status = 200, msg = "Ok") => new Response(msg, { status });
/**
* Creates 'Error' Response
*
* @xample
* ```ts
* return err(404, 'Not Found');
* ```
*/
export const err = (status = 400, msg = "Bad Request") =>
new Response(msg, { status });