Réorganisation des sources
This commit is contained in:
+84
-1
@@ -1 +1,84 @@
|
||||
console.log('this is placeholder for routes');
|
||||
const chalk = require('chalk');
|
||||
const utils = {
|
||||
getDateString: () => {
|
||||
let today = new Date();
|
||||
let dateNow = today.toLocaleDateString();
|
||||
return `${dateNow}`;
|
||||
},
|
||||
getTimeString: () => {
|
||||
let today = new Date();
|
||||
let timeNow = today.toLocaleTimeString();
|
||||
return `${timeNow}`;
|
||||
},
|
||||
getDateTimeString: (delimiter = true) => {
|
||||
let today = new Date();
|
||||
let dateString = `${today.toLocaleDateString()} ${today.toLocaleTimeString()}`;
|
||||
if (delimiter) {
|
||||
dateString = `[${dateString}]`;
|
||||
}
|
||||
return dateString;
|
||||
},
|
||||
getDateTimeISOString: (delimiter = true) => {
|
||||
let today = new Date();
|
||||
let dateString = today.toISOString();
|
||||
if (delimiter) {
|
||||
dateString = `[${dateString}]`;
|
||||
}
|
||||
return dateString;
|
||||
},
|
||||
async fetchSkydiverIdApi(path, query) {
|
||||
let limit = 50;
|
||||
let offset = 0;
|
||||
let args = '';
|
||||
if (typeof query.limit !== 'undefined') {
|
||||
limit = query.limit;
|
||||
}
|
||||
if (typeof query.offset !== 'undefined') {
|
||||
offset = ((query.offset/limit)+1);
|
||||
}
|
||||
if (offset > 1) {
|
||||
args += `&page=${offset}`;
|
||||
}
|
||||
if (typeof query.dateRangeStart !== 'undefined' && typeof query.dateRangeEnd !== 'undefined') {
|
||||
args += `&filter[and][0][date][gte]=${query.dateRangeStart}`;
|
||||
args += `&filter[and][0][date][lte]=${query.dateRangeEnd}`;
|
||||
}
|
||||
try {
|
||||
let url = `${process.env.SKYDIVERID_API_URL}/${path}`;
|
||||
if (typeof query.limit !== 'undefined') {
|
||||
url += `?per-page=${limit}${args}`;
|
||||
}
|
||||
/* url pour provoquer une erreur en dev : url = `https://www.goldapi.ioppppp/api/${price.metal}/${price.currency}`; */
|
||||
let options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${process.env.SKYDIVERID_API_TOKEN}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
redirect: 'follow'
|
||||
};
|
||||
/* Pour tester le retour d'erreur : return { errors: { name: 'error name', message: 'error message', type: 'TYPE', code: 'CODE' } }; */
|
||||
//const response = await fetch(url, options);
|
||||
return fetch(url, options).then(function (response) {
|
||||
if (response.status >= 200 && response.status < 400) {
|
||||
console.log(`${utils.getDateTimeISOString()}`, chalk.green(`Réception des sauts via SkydiverId API`), `${response.status} ${response.statusText}`);
|
||||
const data = response.json();
|
||||
console.log(data);
|
||||
return data;
|
||||
} else {
|
||||
console.log(`${utils.getDateTimeISOString()}`, chalk.red(`Erreur ${response.status} ${response.statusText}`));
|
||||
return { errors: { name: response.statusText, message: `Une erreur ${response.status} '${response.statusText}' est survenue lors de la récupération des sauts via SkydiverId API`, type: 'API Error', code: response.status } };
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(`${utils.getDateTimeISOString()}`, chalk.red(`A fetchSkydiverIdApi error occured ${error.code} ${error.type}`));
|
||||
return { errors: { name: error.name, message: `A fetchSkydiverIdApi error occured : ${error.message}`, type: error.type, code: error.code } };
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`${utils.getDateTimeISOString()}`, chalk.red(`fetchSkydiverIdApi error`));
|
||||
return { errors: { name: error.name, message: error.message, type: error.type, code: error.code } };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = utils;
|
||||
|
||||
Reference in New Issue
Block a user