(requestType, data, callback = (r) => {
console.log('Fetch Success', r);
}, endpoint = '/api/rest')
| 2 | /* HELPFUL functions to call */ |
| 3 | |
| 4 | function restRequest(requestType, data, callback = (r) => { |
| 5 | console.log('Fetch Success', r); |
| 6 | }, endpoint = '/api/rest') { |
| 7 | const requestData = requestType === 'GET' ? |
| 8 | {method: requestType, headers: {'Content-Type': 'application/json'}} : |
| 9 | {method: requestType, headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)} |
| 10 | |
| 11 | fetch(endpoint, requestData) |
| 12 | .then((response) => { |
| 13 | if (!response.ok) { |
| 14 | throw (response.statusText); |
| 15 | } |
| 16 | return response.text(); |
| 17 | }) |
| 18 | .then((text) => { |
| 19 | try { |
| 20 | callback(JSON.parse(text)); |
| 21 | } catch { |
| 22 | callback(text); |
| 23 | } |
| 24 | }) |
| 25 | .catch((error) => console.error(error)); |
| 26 | } |
| 27 | |
| 28 | function apiV2(requestType, endpoint, body = null, jsonRequest = true) { |
| 29 | let requestBody = { method: requestType }; |
no test coverage detected