2024-02-29 04:08:54 +00:00
|
|
|
import { parse } from '@brillout/json-serializer/parse'; /* eslint-disable-line import/extensions */
|
2024-03-26 23:06:03 +00:00
|
|
|
import events from '#/src/events.js';
|
2024-01-07 05:13:40 +00:00
|
|
|
|
2023-12-30 05:29:53 +00:00
|
|
|
const postHeaders = {
|
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'same-origin',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function getQuery(data) {
|
|
|
|
if (!data) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const curatedQuery = Object.fromEntries(Object.entries(data).map(([key, value]) => (value === undefined ? null : [key, value])).filter(Boolean));
|
|
|
|
|
2024-03-31 23:50:24 +00:00
|
|
|
return `?${new URLSearchParams(curatedQuery).toString()}`; // recode so commas aren't encoded
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 23:06:03 +00:00
|
|
|
function showFeedback(isSuccess, options = {}, errorMessage) {
|
2024-05-19 03:07:35 +00:00
|
|
|
if (!isSuccess && (typeof options.errorFeedback === 'string' || options.appendErrorMessage)) {
|
2024-03-26 23:06:03 +00:00
|
|
|
events.emit('feedback', {
|
|
|
|
type: 'error',
|
|
|
|
message: options.appendErrorMessage && errorMessage
|
2024-05-19 03:07:35 +00:00
|
|
|
? `${options.errorFeedback ? `${options.errorFeedback}: ` : ''}${errorMessage}`
|
2024-03-26 23:06:03 +00:00
|
|
|
: options.errorFeedback,
|
|
|
|
});
|
2024-05-19 03:07:35 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isSuccess) {
|
|
|
|
events.emit('feedback', {
|
|
|
|
type: 'error',
|
|
|
|
message: 'Error, please try again',
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
2024-03-26 23:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isSuccess && options.successFeedback) {
|
|
|
|
events.emit('feedback', {
|
|
|
|
type: 'success',
|
|
|
|
message: options.successFeedback,
|
|
|
|
});
|
2024-05-19 03:07:35 +00:00
|
|
|
|
|
|
|
return;
|
2024-03-26 23:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isSuccess && options.undoFeedback) {
|
|
|
|
events.emit('feedback', {
|
|
|
|
type: 'undo',
|
|
|
|
message: options.undoFeedback,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function get(path, query = {}, options = {}) {
|
2024-03-27 01:28:21 +00:00
|
|
|
try {
|
|
|
|
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);
|
|
|
|
} catch (error) {
|
|
|
|
showFeedback(false, options, error.message);
|
|
|
|
throw error;
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 23:06:03 +00:00
|
|
|
export async function post(path, data, options = {}) {
|
2024-03-27 01:28:21 +00:00
|
|
|
try {
|
|
|
|
const res = await fetch(`/api${path}${getQuery(options.query)}`, {
|
|
|
|
method: 'POST',
|
2024-08-31 02:59:05 +00:00
|
|
|
body: data && JSON.stringify(data),
|
2024-03-27 01:28:21 +00:00
|
|
|
...postHeaders,
|
|
|
|
});
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
if (res.status === 204) {
|
|
|
|
showFeedback(true, options);
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
const body = parse(await res.text());
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
if (res.ok) {
|
|
|
|
showFeedback(true, options);
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
showFeedback(false, options, body.statusMessage);
|
|
|
|
throw new Error(body.statusMessage);
|
|
|
|
} catch (error) {
|
|
|
|
showFeedback(false, options, error.message);
|
|
|
|
throw error;
|
|
|
|
}
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 23:06:03 +00:00
|
|
|
export async function patch(path, data, options = {}) {
|
2024-03-27 01:28:21 +00:00
|
|
|
try {
|
|
|
|
const res = await fetch(`/api${path}${getQuery(options.query)}`, {
|
|
|
|
method: 'PATCH',
|
2024-08-31 02:59:05 +00:00
|
|
|
body: data && JSON.stringify(data),
|
2024-03-27 01:28:21 +00:00
|
|
|
...postHeaders,
|
|
|
|
});
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
if (res.status === 204) {
|
2024-09-10 00:47:03 +00:00
|
|
|
showFeedback(true, options);
|
2024-03-27 01:28:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
const body = parse(await res.text());
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
showFeedback(true, options);
|
|
|
|
return body;
|
|
|
|
}
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
showFeedback(false, options, body.statusMessage);
|
|
|
|
throw new Error(body.statusMessage);
|
|
|
|
} catch (error) {
|
|
|
|
showFeedback(false, options, error.message);
|
|
|
|
throw error;
|
|
|
|
}
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 23:06:03 +00:00
|
|
|
export async function del(path, options = {}) {
|
2024-03-27 01:28:21 +00:00
|
|
|
try {
|
|
|
|
const res = await fetch(`/api${path}${getQuery(options.query)}`, {
|
|
|
|
method: 'DELETE',
|
2024-08-31 02:59:05 +00:00
|
|
|
body: options.data && JSON.stringify(options.data),
|
2024-03-27 01:28:21 +00:00
|
|
|
...postHeaders,
|
|
|
|
});
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
if (res.status === 204) {
|
|
|
|
showFeedback(true, options);
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
const body = parse(await res.text());
|
2023-12-30 05:29:53 +00:00
|
|
|
|
2024-03-27 01:28:21 +00:00
|
|
|
if (res.ok) {
|
|
|
|
showFeedback(true, options);
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
showFeedback(false, options, body.statusMessage);
|
|
|
|
throw new Error(body.statusMessage);
|
|
|
|
} catch (error) {
|
|
|
|
showFeedback(false, options, error.message);
|
|
|
|
throw error;
|
|
|
|
}
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|