-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathutils.ts
61 lines (55 loc) · 1.58 KB
/
utils.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
61
export function isObject(body: unknown): body is object {
return typeof body === 'object' && body !== null
}
export function isEmptyObject(body: unknown): body is Record<never, never> {
return isObject(body) && Object.keys(body).length === 0
}
export function hasKey<Key extends string>(
body: unknown,
key: Key
): body is Record<Key, unknown> {
return isObject(body) && key in body
}
export function hasStringKey<Key extends string>(
body: unknown,
key: Key
): body is Record<Key, string> {
return hasKey(body, key) && typeof body[key] === 'string'
}
export interface FetchOptions {
fetch?: typeof fetch
host?: string
response?: (res: Response) => void
}
export async function fetchJSON<Response = unknown>(
method: string,
url: string,
body: object,
opts: FetchOptions | undefined = {}
): Promise<Response> {
let host = opts.host ?? ''
let request = opts.fetch ?? fetch
let response = await request(host + url, {
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
},
method
})
if (!response.ok) {
throw new Error(await response.text())
}
if (opts.response) opts.response(response)
return response.json() as Response
}
export interface Endpoint<
// Need to put it inside type to pass all types to server in a single variable
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Response,
Request,
UrlParams extends Record<string, string> = Record<never, string>
> {
checkBody(body: unknown, urlParams: UrlParams): false | Request
method: string
parseUrl(url: string): false | UrlParams
}