Lines 1-3
Link Here
|
|
|
1 |
import { setError } from "../messages"; |
2 |
|
1 |
class HttpClient { |
3 |
class HttpClient { |
2 |
constructor(options = {}) { |
4 |
constructor(options = {}) { |
3 |
this._baseURL = options.baseURL || ""; |
5 |
this._baseURL = options.baseURL || ""; |
Lines 7-23
class HttpClient {
Link Here
|
7 |
} |
9 |
} |
8 |
|
10 |
|
9 |
async _fetchJSON(endpoint, headers = {}, options = {}) { |
11 |
async _fetchJSON(endpoint, headers = {}, options = {}) { |
10 |
const res = await fetch(this._baseURL + endpoint, { |
12 |
let res; |
|
|
13 |
await fetch(this._baseURL + endpoint, { |
11 |
...options, |
14 |
...options, |
12 |
headers: { ...this._headers, ...headers }, |
15 |
headers: { ...this._headers, ...headers }, |
13 |
}); |
16 |
}) |
14 |
|
17 |
.then(this.checkError) |
15 |
if (!res.ok) throw new Error(res.statusText); |
18 |
.then( |
16 |
|
19 |
(result) => { |
17 |
if (options.parseResponse !== false && res.status !== 204) |
20 |
res = result; |
18 |
return res.json(); |
21 |
}, |
19 |
|
22 |
(error) => { |
20 |
return undefined; |
23 |
setError(error.toString()); |
|
|
24 |
} |
25 |
) |
26 |
.catch((error) => { |
27 |
setError(error); |
28 |
}); |
29 |
return res; |
21 |
} |
30 |
} |
22 |
|
31 |
|
23 |
get(params = {}) { |
32 |
get(params = {}) { |
Lines 51-56
class HttpClient {
Link Here
|
51 |
}); |
60 |
}); |
52 |
} |
61 |
} |
53 |
|
62 |
|
|
|
63 |
checkError(response, return_response = 0) { |
64 |
if (response.status >= 200 && response.status <= 299) { |
65 |
return return_response ? response : response.json(); |
66 |
} else { |
67 |
console.log("Server returned an error:"); |
68 |
console.log(response); |
69 |
throw Error("%s (%s)".format(response.statusText, response.status)); |
70 |
} |
71 |
} |
72 |
|
54 |
//TODO: Implement count method |
73 |
//TODO: Implement count method |
55 |
} |
74 |
} |
56 |
|
75 |
|
57 |
- |
|
|