Add defaults to Vuex store api action.

It sends Content-Type: application/json by default
and converts the body to string via JSON.stringify
unless it's a string already.
This commit is contained in:
D. Berge
2020-08-22 20:34:42 +02:00
parent 949defd2f6
commit b2fc63dc51

View File

@@ -2,6 +2,17 @@
async function api ({state, commit, dispatch}, [resource, init = {}]) {
try {
// this.loading = true;
if (init && init.hasOwnProperty("body")) {
if (!init.headers) {
// As a convenience, we implicitly add the default content type
init.headers = {
"Content-Type": "application/json"
}
}
if (typeof init.body != "string") {
init.body = JSON.stringify(init.body);
}
}
const res = await fetch(`${state.apiUrl}${resource}`, init);
if (res.ok) {
try {