From f8d882da5de9d0fbea25df71834b6a60f0a48a7c Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Tue, 29 Mar 2022 14:03:14 +0200 Subject: [PATCH] Replace `text` parameter by `format` in Vuex API call. Instead of { text: true } as a Fetch option, one can now specify { format: "text" }, as well as any of these other options, which call the corresponding Fetch method: * "arrayBuffer", * "blob", * "formData", * "json", * "text" --- lib/www/client/source/src/store/modules/api/actions.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 1c0ff8e..30dac3a 100644 --- a/lib/www/client/source/src/store/modules/api/actions.js +++ b/lib/www/client/source/src/store/modules/api/actions.js @@ -29,8 +29,16 @@ async function api ({state, getters, commit, dispatch}, [resource, init = {}, cb await dispatch('setCredentials'); + try { - return init.text ? (await res.text()) : (await res.json()); + if (!res.bodyUsed) { // It may have been consumed by a callback + const validFormats = [ "arrayBuffer", "blob", "formData", "json", "text" ]; + if (init.format && validFormats.includes(init.format)) { + return await res[init.format](); + } else { + return await res.json(); + } + } } catch (err) { if (err instanceof SyntaxError) { if (Number(res.headers.get("Content-Length")) === 0) {