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"
This commit is contained in:
D. Berge
2022-03-29 14:03:14 +02:00
parent 808c9987af
commit f8d882da5d

View File

@@ -29,8 +29,16 @@ async function api ({state, getters, commit, dispatch}, [resource, init = {}, cb
await dispatch('setCredentials'); await dispatch('setCredentials');
try { 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) { } catch (err) {
if (err instanceof SyntaxError) { if (err instanceof SyntaxError) {
if (Number(res.headers.get("Content-Length")) === 0) { if (Number(res.headers.get("Content-Length")) === 0) {