Files
hsp-gdh/modules/fetchManager.js
Lightemerald 0ddbc437b9 First part of backend rework
- Added the base data structure for the new database
- Added the new routes for the new database
- Reworked the users endpoints
2024-02-26 10:20:29 +01:00

71 lines
1.4 KiB
JavaScript

import fetch from 'node-fetch';
import { error } from './logManager';
async function get(url, token = 'none') {
const options = {
method: 'GET',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
async function post(url, body, token = 'none') {
const options = {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
body: JSON.stringify(body),
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
async function patch(url, body, token = 'none') {
const options = {
method: 'PATCH',
mode: 'cors',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
body: JSON.stringify(body),
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
async function put(url, body, token = 'none') {
const options = {
method: 'PUT',
mode: 'cors',
headers: { 'Content-Type': 'application/json', authorization: `${token}` },
body: JSON.stringify(body),
};
return await fetch(url, options)
.then(res => res.json())
.then(json => {
return json;
})
.catch(err => error(err));
}
export {
get,
post,
patch,
put,
};