meilisearch-admin/app/utils/query.js

35 lines
990 B
JavaScript
Raw Permalink Normal View History

2022-08-20 12:47:02 +00:00
import { instance } from '../instance-initializers/application';
export default function query(path, options = {}) {
const session = instance.lookup('service:session');
2022-09-21 11:11:29 +00:00
const toaster = instance.lookup('service:toaster');
2022-08-20 12:47:02 +00:00
const { url, key } = session.data.authenticated;
const defaultHeaders = { 'Content-Type': 'application/json' };
const headers = new Headers(Object.assign(defaultHeaders, options.headers));
if (key) {
headers.append('Authorization', `Bearer ${key}`);
}
const req = new Request(
new URL(path, url),
Object.assign(options, { headers })
2022-05-03 09:49:10 +00:00
);
2022-08-20 12:47:02 +00:00
2023-03-02 07:57:10 +00:00
return fetch(req).then((response) => {
if (response.status === 204) {
return;
} else if (response.ok) {
return response.json();
} else {
2022-11-17 11:32:38 +00:00
return response.json().then((reason) => {
2022-09-21 11:11:29 +00:00
toaster.dangerToast({
header: reason.type,
text: reason.message,
});
2023-03-02 07:57:10 +00:00
// throw new Error('Network response was not OK');
2022-09-21 11:11:29 +00:00
});
2023-03-02 07:57:10 +00:00
}
});
2022-05-03 09:49:10 +00:00
}