Switched bot to use nodejs.
This commit is contained in:
71
modules/fetchHandler.js
Normal file
71
modules/fetchHandler.js
Normal file
@@ -0,0 +1,71 @@
|
||||
const fetch = (...args) => import('node-fetch').then(({ default: fth }) => fth(...args));
|
||||
const { error } = require('./logHandler');
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
post,
|
||||
patch,
|
||||
put,
|
||||
};
|
||||
26
modules/logHandler.js
Normal file
26
modules/logHandler.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const pino = require('pino');
|
||||
|
||||
const logger = pino();
|
||||
|
||||
function log(x) {
|
||||
logger.info(x);
|
||||
}
|
||||
|
||||
function debug(x) {
|
||||
logger.debug(x);
|
||||
}
|
||||
|
||||
function warn(x) {
|
||||
logger.warn(x);
|
||||
}
|
||||
|
||||
function error(x) {
|
||||
logger.error(x);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
log,
|
||||
debug,
|
||||
warn,
|
||||
error,
|
||||
};
|
||||
Reference in New Issue
Block a user