Skip to content

Commit

Permalink
refactor: Send - convert to esm + async
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Aug 14, 2021
1 parent bd7bf35 commit 67f8bab
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions src/send.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require('fs');
const { get } = require('lodash');
const fetch = require('isomorphic-fetch');
import fs from 'fs';
import { get } from 'lodash';
import fetch from 'isomorphic-fetch';

const LOCALES = require('../locales/en');
const { debug } = require('./utils');
import LOCALES from '../locales/en';
import { debug } from './utils';

module.exports = (data, params, config, logger) => {
export default async (data, params, config, logger) => {
const {
key,
endpoint,
Expand Down Expand Up @@ -58,33 +58,37 @@ module.exports = (data, params, config, logger) => {

logger.info('Send stats to RelativeCI', `branch=${branch}`, `commit=${commit}`);

return fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify(payload),
}).then((res) => res.json())
.then((response) => {
debug('Response', response);

if (response.code) {
logger.warn(response);
return;
}

const { res } = response;

if (!res) {
logger.warn(LOCALES.GENERIC_ERROR, response);
return;
}

const buildNumber = get(res, 'job.internalBuildNumber');
const buildSizeInfo = get(response, 'info.message.txt');

logger.info(`Job #${buildNumber} done.`);
logger.info(buildSizeInfo);
})
.catch((err) => logger.warn(err));
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify(payload),
});

const responseData = await response.json();

debug('Response', responseData);

if (responseData.code) {
logger.warn(responseData);
return;
}

const { res } = responseData;

if (!res) {
logger.warn(LOCALES.GENERIC_ERROR, responseData);
return;
}

const buildNumber = get(res, 'job.internalBuildNumber');
const buildSizeInfo = get(responseData, 'info.message.txt');

logger.info(`Job #${buildNumber} done.`);
logger.info(buildSizeInfo);
} catch (err) {
logger.warn(err);
}
};

0 comments on commit 67f8bab

Please sign in to comment.