| 26 | } |
| 27 | |
| 28 | function apiV2(requestType, endpoint, body = null, jsonRequest = true) { |
| 29 | let requestBody = { method: requestType }; |
| 30 | if (jsonRequest) { |
| 31 | requestBody.headers = { 'Content-Type': 'application/json' }; |
| 32 | if (body) { |
| 33 | requestBody.body = JSON.stringify(body); |
| 34 | } |
| 35 | } else { |
| 36 | if (body) { |
| 37 | requestBody.body = body; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return new Promise((resolve, reject) => { |
| 42 | fetch(endpoint, requestBody) |
| 43 | .then((response) => { |
| 44 | if (!response.ok) { |
| 45 | reject(response.statusText); |
| 46 | } |
| 47 | return response.text(); |
| 48 | }) |
| 49 | .then((text) => { |
| 50 | try { |
| 51 | resolve(JSON.parse(text)); |
| 52 | } catch { |
| 53 | resolve(text); |
| 54 | } |
| 55 | }); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Given list of abilities, returns list filtered by the following |