ktvmanager/lib/gotRequest.js

27 lines
746 B
JavaScript
Raw Permalink Normal View History

2021-02-06 11:19:15 +00:00
const got = require('got')
const gotRequest = async (url) => {
let returnResponse = {};
2021-03-26 12:42:13 +00:00
let options = {
timeout: 2000
}
2021-09-20 16:03:43 +00:00
return new Promise(async (resolve, reject) => {
await got(url, options)
.then((response) => {
returnResponse = response;
})
.catch((error) => {
returnResponse = typeof error.response !== 'undefined' ? error.response : error;
2021-02-06 11:19:15 +00:00
2021-09-20 16:03:43 +00:00
if (typeof returnResponse.body === 'string' && returnResponse.body.substring(0, 1) === '{') {
returnResponse.body = JSON.parse(returnResponse.body);
}
});
2021-02-06 11:19:15 +00:00
2021-09-20 16:03:43 +00:00
resolve(returnResponse);
})
2021-02-06 11:19:15 +00:00
};
module.exports = {
gotRequest
}