-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
116 lines (104 loc) · 3.04 KB
/
index.ts
File metadata and controls
116 lines (104 loc) · 3.04 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as nf from 'node-fetch'
export const fetch = nf.default
export type Response = nf.Response
export async function fetchOK(
url: string,
init: nf.RequestInit | undefined,
errorMessage: string,
errorData?: { [key: string]: unknown },
) {
await okResponse(fetch(url, init), errorMessage, errorData)
}
export function fetchJson<T>(
url: string,
init: nf.RequestInit | undefined,
errorMessage: string,
errorData?: { [key: string]: unknown },
) {
return jsonResponse<T>(fetch(url, withType(init, 'application/json')), errorMessage, errorData)
}
export function fetchText(
url: string,
init: nf.RequestInit | undefined,
errorMessage: string,
errorData?: { [key: string]: unknown },
) {
return textResponse(fetch(url, withType(init, 'text/*')), errorMessage, errorData)
}
export async function okResponse(
response: Promise<{
ok?: boolean
status?: number
text: () => Promise<string>
arrayBuffer?: () => Promise<unknown>
}>,
errorMessage: string,
errorData?: { [key: string]: unknown },
) {
const r = await throwOnNotOK(response, errorMessage, errorData)
await (r.arrayBuffer ?? r.text).bind(r)()
}
export async function jsonResponse<T>(
response: Promise<{
ok?: boolean
status?: number
text?: () => Promise<string>
json: () => Promise<unknown>
}>,
errorMessage: string,
errorData?: { [key: string]: unknown },
) {
const r = await throwOnNotOK(response, errorMessage, errorData)
return (await r.json()) as T
}
export async function textResponse(
response: Promise<{
ok?: boolean
status?: number
text: () => Promise<string>
}>,
errorMessage: string,
errorData?: { [key: string]: unknown },
) {
const r = await throwOnNotOK(response, errorMessage, errorData)
return await r.text()
}
export async function throwOnNotOK<
T extends { ok?: boolean; status?: number; text?: () => Promise<string> },
>(response: Promise<T> | T, message: string, data?: { [key: string]: unknown }) {
const r = await response
if (r.ok === false) {
throw Object.assign(new Error(message), {
response: {
status: r.status,
body: limitSize(await r.text?.()),
},
...data,
})
}
return response
}
export function thrownHasStatus(e: unknown, status: number) {
return (e as { response?: { status?: number } } | undefined)?.response?.status === status
}
export function missing(what?: string): never {
throw new Error(what ? `Missing ${what}.` : 'Missing.')
}
function withType(init: nf.RequestInit | undefined, mimeType: string) {
if ((init?.headers as { accept?: string } | undefined)?.accept) {
return init
}
return {
...init,
headers: {
...init?.headers,
accept: mimeType,
},
}
}
function limitSize(text: string | undefined) {
if ((text?.length ?? 0) > 2048) {
return text?.substring(0, 2048)
}
return text
}