allow makeAPIRequest() to handle JSON requests
This commit is contained in:
parent
e1ea41b6d1
commit
25eeae34ba
@ -6,13 +6,23 @@
|
|||||||
* @returns a Promise, which resolves when the server can be reached and responds without an error
|
* @returns a Promise, which resolves when the server can be reached and responds without an error
|
||||||
* and rejects otherwise
|
* and rejects otherwise
|
||||||
*/
|
*/
|
||||||
async function makeAPIRequest(endpoint, body) {
|
async function makeAPIRequest(endpoint, body, options={}) {
|
||||||
return new Promise((resolve, reject) => {
|
let fetchOptions = {
|
||||||
const fetchOptions = {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: new URLSearchParams(body)
|
headers: {
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (options["content"] === 'json') {
|
||||||
|
fetchOptions["headers"]["Content-Type"] = 'application/json'
|
||||||
|
fetchOptions["body"] = JSON.stringify(body)
|
||||||
|
} else {
|
||||||
|
fetchOptions["body"] = new URLSearchParams(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
|
fetch("/api/v1/" + endpoint, fetchOptions).then(response => {
|
||||||
const responseCode = response.status;
|
const responseCode = response.status;
|
||||||
if (responseCode >= 500) {
|
if (responseCode >= 500) {
|
||||||
|
Loading…
Reference in New Issue
Block a user