@condor-labs/ec-events-gateway
Version:
Client to send events to events gateway API
56 lines (44 loc) • 1.14 kB
JavaScript
const axios = require('@condor-labs/axios');
const validate = require('../validate/validate');
const logger = require('../logger');
const dateFns = require('date-fns');
const { URL_REQUIRED, DATE_FORMAT, PATH } = require('../utils/constants');
const save = async ({
type,
topic,
payload,
url = null,
headers = {},
schema = null
}) => {
try {
const data = {
topic,
type,
data: payload
};
let errors = validate.validateParams(data, schema);
if (errors) return errors;
const eventGatewayUrl = process.env.EVENTS_GATEWAY_URL || url;
if (!eventGatewayUrl) return { error: URL_REQUIRED };
const formattedData = validate.formatData(data);
errors = validate.validateData(formattedData);
if (errors) return errors;
const response = await axios.post(eventGatewayUrl, formattedData, {
headers
});
return response;
} catch (error) {
let filename = dateFns.format(new Date(), DATE_FORMAT);
logger.generateLog(
filename,
type,
'error',
JSON.stringify(error.message),
PATH
);
}
};
module.exports = {
save
};