From b2fc63dc51e441f88e21194a31568e3fa70a5eb1 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Sat, 22 Aug 2020 20:34:42 +0200 Subject: [PATCH] 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. --- .../client/source/src/store/modules/api/actions.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/www/client/source/src/store/modules/api/actions.js b/lib/www/client/source/src/store/modules/api/actions.js index 3722737..eee00f8 100644 --- a/lib/www/client/source/src/store/modules/api/actions.js +++ b/lib/www/client/source/src/store/modules/api/actions.js @@ -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 {