|
Lines 1-77
Link Here
|
|
|
1 |
import { |
| 2 |
isSubmitting, |
| 3 |
submitted, |
| 4 |
} from "../messages" |
| 5 |
|
| 1 |
class HttpClient { |
6 |
class HttpClient { |
| 2 |
constructor(options = {}) { |
7 |
constructor(options = {}) { |
| 3 |
this._baseURL = options.baseURL || ""; |
8 |
this._baseURL = options.baseURL || ""; |
| 4 |
this._headers = options.headers || {}; |
9 |
this._headers = options.headers || {}; |
| 5 |
} |
10 |
} |
| 6 |
|
11 |
|
| 7 |
async _fetchJSON(endpoint, options = {}) { |
12 |
async _fetchJSON(endpoint, headers = {}, options = {}) { |
|
|
13 |
// isSubmitting() //FIXME: need to fix below submitted() call |
| 8 |
const res = await fetch(this._baseURL + endpoint, { |
14 |
const res = await fetch(this._baseURL + endpoint, { |
| 9 |
...options, |
15 |
...options, |
| 10 |
headers: this._headers |
16 |
headers: headers |
| 11 |
}); |
17 |
}) |
| 12 |
|
18 |
// .then(() => submitted()); //FIXME: seemingly working but throws error on console? |
|
|
19 |
|
| 13 |
if (!res.ok) throw new Error(res.statusText); |
20 |
if (!res.ok) throw new Error(res.statusText); |
| 14 |
|
21 |
|
| 15 |
if (options.parseResponse !== false && res.status !== 204) |
22 |
if (options.parseResponse !== false && res.status !== 204) |
| 16 |
return res.json(); |
23 |
return res.json(); |
| 17 |
|
24 |
|
| 18 |
return undefined; |
25 |
return undefined; |
| 19 |
} |
26 |
} |
| 20 |
|
27 |
|
| 21 |
setHeader(key, value) { |
28 |
get(endpoint, headers = {}, options = {}) { |
| 22 |
this._headers[key] = value; |
29 |
return this._fetchJSON(endpoint, headers, |
| 23 |
return this; |
30 |
{ |
| 24 |
} |
|
|
| 25 |
|
| 26 |
getHeader(key) { |
| 27 |
return this._headers[key]; |
| 28 |
} |
| 29 |
|
| 30 |
setBasicAuth(username, password) { |
| 31 |
this._headers.Authorization = `Basic ${btoa(`${username}:${password}`)}`; |
| 32 |
return this; |
| 33 |
} |
| 34 |
|
| 35 |
setBearerAuth(token) { |
| 36 |
this._headers.Authorization = `Bearer ${token}`; |
| 37 |
return this; |
| 38 |
} |
| 39 |
|
| 40 |
get(endpoint, options = {}) { |
| 41 |
return this._fetchJSON(endpoint, { |
| 42 |
...options, |
31 |
...options, |
| 43 |
method: "GET" |
32 |
method: "GET" |
| 44 |
}); |
33 |
}); |
| 45 |
} |
34 |
} |
|
|
35 |
|
| 36 |
//TODO: Implement count method |
| 46 |
|
37 |
|
| 47 |
post(endpoint, body, options = {}) { |
38 |
post(endpoint, body, headers = {}, options = {}) { |
| 48 |
return this._fetchJSON(endpoint, { |
39 |
return this._fetchJSON(endpoint, headers, { |
| 49 |
...options, |
40 |
...options, |
| 50 |
body: body ? JSON.stringify(body) : undefined, |
41 |
body: body ? JSON.stringify(body) : undefined, |
| 51 |
method: "POST" |
42 |
method: "POST" |
| 52 |
}); |
43 |
}); |
| 53 |
} |
44 |
} |
| 54 |
|
45 |
|
| 55 |
put(endpoint, body, options = {}) { |
46 |
put(endpoint, body, headers = {}, options = {}) { |
| 56 |
console.log('puting'); |
47 |
return this._fetchJSON(endpoint, headers, { |
| 57 |
return this._fetchJSON(endpoint, { |
|
|
| 58 |
...options, |
48 |
...options, |
| 59 |
body: body ? JSON.stringify(body) : undefined, |
49 |
body: body ? JSON.stringify(body) : undefined, |
| 60 |
method: "PUT" |
50 |
method: "PUT" |
| 61 |
}); |
51 |
}); |
| 62 |
} |
52 |
} |
| 63 |
|
53 |
|
| 64 |
patch(endpoint, operations, options = {}) { |
54 |
delete(endpoint, headers = {}, options = {}) { |
| 65 |
return this._fetchJSON(endpoint, { |
55 |
return this._fetchJSON(endpoint, headers, { |
| 66 |
parseResponse: false, |
|
|
| 67 |
...options, |
| 68 |
body: JSON.stringify(operations), |
| 69 |
method: "PATCH" |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
delete(endpoint, options = {}) { |
| 74 |
return this._fetchJSON(endpoint, { |
| 75 |
parseResponse: false, |
56 |
parseResponse: false, |
| 76 |
...options, |
57 |
...options, |
| 77 |
method: "DELETE" |
58 |
method: "DELETE" |
| 78 |
- |
|
|