diff --git a/src/utils/coverage.js b/src/utils/coverage.js index 4e9c613..fdf837c 100644 --- a/src/utils/coverage.js +++ b/src/utils/coverage.js @@ -1,6 +1,6 @@ import { uniq } from 'lodash'; import settings from '../settings'; -import { JSON_HEADERS } from './fetch'; +import { jsonPost, plainFetch } from './fetch'; import { queryCacheWithFallback } from './localCache'; const { @@ -11,11 +11,8 @@ export const githubUrl = gitCommit => `${GH_GECKO_DEV}/commit/${gitCommit}`; export const codecovUrl = gitCommit => (`${CODECOV_GECKO_DEV}/commit/${gitCommit}`); export const ccovBackendUrl = node => (`${BACKEND}/coverage/changeset/${node}`); -const jsonPost = (url, body) => - fetch(url, { headers: JSON_HEADERS, method: 'POST', body: JSON.stringify(body) }); - const queryChangesetCoverage = node => - fetch(`${CCOV_BACKEND}/coverage/changeset/${node}`, { JSON_HEADERS }); + plainFetch(`${CCOV_BACKEND}/coverage/changeset/${node}`); const queryActiveData = body => jsonPost(`${ACTIVE_DATA}/query`, body); diff --git a/src/utils/fetch.js b/src/utils/fetch.js index e263ea1..21434b4 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -1,6 +1,18 @@ -export const JSON_HEADERS = { +const JSON_HEADERS = { Accept: 'application/json', }; -export const PLAIN_HEADERS = { +const PLAIN_HEADERS = { Accept: 'text/plain', }; + +export const jsonPost = (url, body) => + fetch(url, { headers: JSON_HEADERS, method: 'POST', body: JSON.stringify(body) }); + +export const jsonFetch = async (url) => { + const response = await fetch(url, { JSON_HEADERS }); + const json = await response.json(); + return json; +}; + +export const plainFetch = url => + fetch(url, { PLAIN_HEADERS }); diff --git a/src/utils/hg.js b/src/utils/hg.js index 4a7b8be..a9bdc5b 100644 --- a/src/utils/hg.js +++ b/src/utils/hg.js @@ -1,5 +1,5 @@ import settings from '../settings'; -import { JSON_HEADERS, PLAIN_HEADERS } from './fetch'; +import { jsonFetch, plainFetch } from './fetch'; import { getFromCache, saveInCache } from './localCache'; const { REPO_NAME, HG_HOST } = settings; @@ -22,21 +22,20 @@ const initializedChangeset = (cset, author) => ({ }); export const getDiff = async (node, repoName = REPO_NAME) => { - const text = await fetch(`${HG_HOST}/${repoName}/raw-rev/${node}`, { PLAIN_HEADERS }); + const text = await plainFetch(`${HG_HOST}/${repoName}/raw-rev/${node}`); return text.text(); }; export const getRawFile = (node, filePath, repoName = REPO_NAME) => - fetch(`${HG_HOST}/${repoName}/raw-file/${node}/${filePath}`, { PLAIN_HEADERS }); + plainFetch(`${HG_HOST}/${repoName}/raw-file/${node}/${filePath}`); export const getChangesetMeta = async (node, repoPath = REPO_NAME) => { - const text = await fetch(`${HG_HOST}/${repoPath}/json-rev/${node}`, JSON_HEADERS); - const meta = await text.json(); + const meta = await jsonFetch(`${HG_HOST}/${repoPath}/json-rev/${node}`); return initializedChangeset(meta, meta.user); }; export const getJsonPushes = (repoName = REPO_NAME, date = settings.HG_DAYS_AGO) => - fetch(`${HG_HOST}/${repoName}/json-pushes?version=2&full=1&startdate=${date}`, JSON_HEADERS); + jsonFetch(`${HG_HOST}/${repoName}/json-pushes?version=2&full=1&startdate=${date}`); export const hgDiffUrl = (node, repoName = REPO_NAME) => `${HG_HOST}/${repoName}/rev/${node}`; @@ -109,7 +108,7 @@ const getChangesets = async (repoName = REPO_NAME) => { if (!csets || csets.length === 0) { console.debug('The local cache was not available.'); - const text = await (await getJsonPushes(repoName)).json(); + const text = await getJsonPushes(repoName); csets = await pushesToCsets(text.pushes); } @@ -121,7 +120,7 @@ const getChangesets = async (repoName = REPO_NAME) => { console.error(e); } } else { - const text = await (await getJsonPushes(repoName)).json(); + const text = await getJsonPushes(repoName); csets = await pushesToCsets(text.pushes); } return csets;