forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
84 lines (75 loc) · 2.15 KB
/
utils.ts
File metadata and controls
84 lines (75 loc) · 2.15 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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 RequesterOptions {
fetch?: typeof fetch
host?: string
response?: (res: Response) => void
}
export type HTTPResponse<ResponseJSON> = (
| {
json(): never
ok: false
}
| {
json(): Promise<ResponseJSON>
ok: true
}
) &
Response
export interface Requester<Params extends object, ResponseJSON> {
(params: Params, opts?: RequesterOptions): Promise<HTTPResponse<ResponseJSON>>
}
export async function fetchJSON<ResponseJSON = unknown>(
method: string,
url: string,
body: object,
opts: RequesterOptions | undefined = {}
): Promise<HTTPResponse<ResponseJSON>> {
let host = opts.host ?? ''
let request = opts.fetch ?? fetch
let response = await request(host + url, {
body: JSON.stringify(body),
credentials: 'include',
headers: {
'Content-Type': 'application/json'
},
method
})
if (opts.response) opts.response(response)
return response as HTTPResponse<ResponseJSON>
}
export function createRequester<Params extends object, ResponseJSON>(
method: string,
getUrl: (params: Params) => string
): Requester<Params, ResponseJSON> {
return (params, opts) => {
return fetchJSON<ResponseJSON>(method, getUrl(params), params, opts)
}
}
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
}