commit inicial
This commit is contained in:
78
src/utils/utils.js
Normal file
78
src/utils/utils.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import fetch from 'node-fetch';
|
||||
import { Op } from 'sequelize';
|
||||
|
||||
import config from '../services/config.json' assert {type: 'json'};
|
||||
import { interval } from 'date-fns';
|
||||
|
||||
const dataUrbano = config.dataUrbano;
|
||||
const dataInfracommerce = config.dataInfracommerce;
|
||||
|
||||
const sendData = async (url, token = null, payload) => {
|
||||
|
||||
try {
|
||||
const headers = {
|
||||
// 'Authorization': `Bearer ${token}`,
|
||||
'user': dataUrbano.userHeader,
|
||||
'pass': dataUrbano.passHeader,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
};
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: `json=${JSON.stringify(payload)}`
|
||||
};
|
||||
|
||||
const response = await fetch(url, options);
|
||||
|
||||
const result = await response.json();
|
||||
return result;
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return error;
|
||||
}
|
||||
};
|
||||
|
||||
const sendRequestInfracommerce = async (payload, url) => {
|
||||
// if (!omsId) {
|
||||
// throw new Error('omsId no proporcionado');
|
||||
// }
|
||||
|
||||
// const url = dataEnvioEstados.url.replace(':oms_id', omsId);
|
||||
const { AppKey, AppToken } = dataInfracommerce;
|
||||
|
||||
console.log(AppKey, AppToken)
|
||||
|
||||
try {
|
||||
const headers = {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'OMS-AppKey': AppKey,
|
||||
'OMS-AppTok': AppToken
|
||||
};
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const res = await response.json();
|
||||
const data = JSON.stringify(res, null, 2);
|
||||
throw new Error(`Error en la solicitud: ${response.status} ${data}`);
|
||||
}
|
||||
const result = await response.json();
|
||||
return result;
|
||||
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export {
|
||||
sendData,
|
||||
sendRequestInfracommerce
|
||||
}
|
||||
Reference in New Issue
Block a user