Added stash menu with remove and rename.
This commit is contained in:
52
src/api.js
52
src/api.js
@@ -1,4 +1,5 @@
|
||||
import { parse } from '@brillout/json-serializer/parse'; /* eslint-disable-line import/extensions */
|
||||
import events from '#/src/events.js';
|
||||
|
||||
const postHeaders = {
|
||||
mode: 'cors',
|
||||
@@ -18,39 +19,69 @@ function getQuery(data) {
|
||||
return `?${encodeURI(decodeURIComponent(new URLSearchParams(curatedQuery).toString()))}`; // recode so commas aren't encoded
|
||||
}
|
||||
|
||||
export async function get(path, query = {}) {
|
||||
function showFeedback(isSuccess, options = {}, errorMessage) {
|
||||
if (!isSuccess && typeof options.errorFeedback) {
|
||||
events.emit('feedback', {
|
||||
type: 'error',
|
||||
message: options.appendErrorMessage && errorMessage
|
||||
? `${options.errorFeedback}: ${errorMessage}`
|
||||
: options.errorFeedback,
|
||||
});
|
||||
}
|
||||
|
||||
if (isSuccess && options.successFeedback) {
|
||||
events.emit('feedback', {
|
||||
type: 'success',
|
||||
message: options.successFeedback,
|
||||
});
|
||||
}
|
||||
|
||||
if (isSuccess && options.undoFeedback) {
|
||||
events.emit('feedback', {
|
||||
type: 'undo',
|
||||
message: options.undoFeedback,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function get(path, query = {}, options = {}) {
|
||||
const res = await fetch(`/api${path}${getQuery(query)}`);
|
||||
const body = parse(await res.text());
|
||||
|
||||
if (res.ok) {
|
||||
showFeedback(true, options);
|
||||
return body;
|
||||
}
|
||||
|
||||
showFeedback(false, options, body.statusMessage);
|
||||
throw new Error(body.statusMessage);
|
||||
}
|
||||
|
||||
export async function post(path, data, { query } = {}) {
|
||||
const res = await fetch(`/api${path}${getQuery(query)}`, {
|
||||
export async function post(path, data, options = {}) {
|
||||
const res = await fetch(`/api${path}${getQuery(options.query)}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
...postHeaders,
|
||||
});
|
||||
|
||||
if (res.status === 204) {
|
||||
showFeedback(true, options);
|
||||
return null;
|
||||
}
|
||||
|
||||
const body = parse(await res.text());
|
||||
|
||||
if (res.ok) {
|
||||
showFeedback(true, options);
|
||||
return body;
|
||||
}
|
||||
|
||||
showFeedback(false, options, body.statusMessage);
|
||||
throw new Error(body.statusMessage);
|
||||
}
|
||||
|
||||
export async function patch(path, data, { query } = {}) {
|
||||
const res = await fetch(`/api${path}${getQuery(query)}`, {
|
||||
export async function patch(path, data, options = {}) {
|
||||
const res = await fetch(`/api${path}${getQuery(options.query)}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
...postHeaders,
|
||||
@@ -63,28 +94,33 @@ export async function patch(path, data, { query } = {}) {
|
||||
const body = parse(await res.text());
|
||||
|
||||
if (res.ok) {
|
||||
showFeedback(true, options);
|
||||
return body;
|
||||
}
|
||||
|
||||
showFeedback(false, options, body.statusMessage);
|
||||
throw new Error(body.statusMessage);
|
||||
}
|
||||
|
||||
export async function del(path, { data, query } = {}) {
|
||||
const res = await fetch(`/api${path}${getQuery(query)}`, {
|
||||
export async function del(path, options = {}) {
|
||||
const res = await fetch(`/api${path}${getQuery(options.query)}`, {
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify(data),
|
||||
body: JSON.stringify(options.data),
|
||||
...postHeaders,
|
||||
});
|
||||
|
||||
if (res.status === 204) {
|
||||
showFeedback(true, options);
|
||||
return null;
|
||||
}
|
||||
|
||||
const body = parse(await res.text());
|
||||
|
||||
if (res.ok) {
|
||||
showFeedback(true, options);
|
||||
return body;
|
||||
}
|
||||
|
||||
showFeedback(false, options, body.statusMessage);
|
||||
throw new Error(body.statusMessage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user