-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathhelpers.js
147 lines (139 loc) · 3.37 KB
/
helpers.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* This module contains helper functions to get and post JSON data to the
* backend.
*
* @module api/helpers
* @flow
*/
/**
* Sends an authentiacted JSON-GET request
* @param {string} url of the API
* @param {string} token
*/
export async function getAuthenticatedJson<T>(
url: string,
token: string
): Promise<T> {
const response = await fetch(url, {
method: "GET",
headers: {
"X-Auth-Token": token,
Accept: "application/json",
},
})
return checkStatus(response)
}
/**
* Sends a JSON-POST request with the given payload to the url
* @param {string} url of the API
* @param {T} params to be sent in the request body.
*/
export async function postJson<T, U>(url: string, params: T): Promise<U> {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(params),
})
return checkStatus(response)
}
/**
* Sends an authentiacted JSON-POST request
* @param {string} url of the API
* @param {string} token
* @param {T} params to be sent in the request body.
*/
export async function postAuthenticatedJson<T, U>(
url: string,
token: string,
params: T
): Promise<U> {
const response = await fetch(url, {
method: "POST",
headers: {
"X-Auth-Token": token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(params),
})
return checkStatus(response)
}
/**
* Sends an authentiacted JSON-PATCH request
* @param {string} url of the API
* @param {string} token
* @param {T} params to be sent in the request body.
*/
export async function patchAuthenticatedJson<T, U>(
url: string,
token: string,
params: T
): Promise<U> {
const response = await fetch(url, {
method: "PATCH",
headers: {
"X-Auth-Token": token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(params),
})
return checkStatus(response)
}
/**
* Sends an authentiacted JSON-PUT request
* @param {string} url of the API
* @param {string} token
* @param {T} params to be sent in the request body.
*/
export async function putAuthenticatedJson<T, U>(
url: string,
token: string,
params: T
): Promise<U> {
const response = await fetch(url, {
method: "PUT",
headers: {
"X-Auth-Token": token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(params),
})
return checkStatus(response)
}
/**
* Represents an API error with a status code, textual status and a generic
* body.
*/
export class ApiError<T> {
code: number
statusText: string
body: T
constructor(code: number, statusText: string, body: T) {
this.code = code
this.statusText = statusText
this.body = body
}
}
/**
* Checks the status of a HTTP response and returns the body as json. If the
* request was not successful, throws a new ApiError instance.
* @param {Response} response
*/
export async function checkStatus<T>(response: Response): Promise<T> {
if (response.status >= 200 && response.status < 300) {
return response.json()
} else {
let json
try {
json = await response.json()
} catch (_) {
throw new ApiError(response.status, response.statusText, "")
}
throw new ApiError(response.status, response.statusText, json)
}
}