This commit is contained in:
@@ -93,6 +93,14 @@ export default {
|
||||
body: JSON.stringify(data)
|
||||
};
|
||||
},
|
||||
fetchPutOptions(data) {
|
||||
return {
|
||||
method: "put",
|
||||
mode: "cors",
|
||||
headers: this.postAuthorizedHeaders(),
|
||||
body: JSON.stringify(data)
|
||||
};
|
||||
},
|
||||
fetchGetOptions() {
|
||||
/* GET WITH AUTH */
|
||||
return {
|
||||
@@ -134,6 +142,9 @@ export default {
|
||||
}
|
||||
return store.state.apiUrl + apiPath;
|
||||
},
|
||||
/////////////////////////////
|
||||
// ENCODE QUERY STRING
|
||||
//
|
||||
buildQuery(obj, sep, eq, name) {
|
||||
sep = sep || "&";
|
||||
eq = eq || "=";
|
||||
@@ -166,6 +177,9 @@ export default {
|
||||
encodeURIComponent(stringifyPrimitive(obj))
|
||||
);
|
||||
},
|
||||
///////////////////////////////////
|
||||
// GET DATA FROM API SERVER
|
||||
//
|
||||
get(route) {
|
||||
var that = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
@@ -195,6 +209,49 @@ export default {
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
///////////////////////////////////
|
||||
// POST / PUT DATA TO API SERVER
|
||||
//
|
||||
upsert(route, data) {
|
||||
var that = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
//determine if this is a new or existing record
|
||||
var fetchOptions = undefined;
|
||||
if (data.concurrencyToken) {
|
||||
fetchOptions = that.fetchPutOptions(data);
|
||||
} else {
|
||||
fetchOptions = that.fetchPostOptions(data);
|
||||
}
|
||||
fetch(that.APIUrl(route), fetchOptions)
|
||||
.then(that.status)
|
||||
.then(that.json)
|
||||
.then(response => {
|
||||
//Note: response.error indicates there is an error, however this is not an unusual condition
|
||||
//it could be validation errors or other general error so we need to treat it here like it's normal
|
||||
//and let the caller deal with it appropriately
|
||||
resolve(response);
|
||||
})
|
||||
.catch(function(error) {
|
||||
//fundamental error, can't proceed with this call
|
||||
|
||||
var errorMessage =
|
||||
"API error: UPSERT route =" + route + ", message =" + error.message;
|
||||
store.commit("logItem", errorMessage);
|
||||
|
||||
if (error.message && error.message.includes("401")) {
|
||||
store.commit(
|
||||
"logItem",
|
||||
"User is not authorized, redirecting to login"
|
||||
);
|
||||
auth.logout();
|
||||
router.push("/login");
|
||||
} else {
|
||||
//alert("Error: " + errorMessage);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//new functions above here
|
||||
|
||||
Reference in New Issue
Block a user