Skip to content

Commit

Permalink
refactor: (strf-8745) replace fetch with axios
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Dec 4, 2020
1 parent 88fee5d commit 946a012
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 447 deletions.
14 changes: 5 additions & 9 deletions lib/stencil-push.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const async = require('async');
const Inquirer = require('inquirer');
const ProgressBar = require('progress');
const uuid = require('uuid4');
const fetch = require('node-fetch');
const os = require('os');

const { THEME_PATH } = require('../constants');
const Bundle = require('./stencil-bundle');
const themeApiClient = require('./theme-api-client');
const ThemeConfig = require('./theme-config');
const StencilConfigManager = require('./StencilConfigManager');
const { sendApiRequest } = require('./utils/networkUtils');

const themeConfigManager = ThemeConfig.getInstance(THEME_PATH);
const stencilConfigManager = new StencilConfigManager();
Expand Down Expand Up @@ -46,15 +46,12 @@ utils.getStoreHash = async (options) => {
const storeUrlObj = new URL(options.config.normalStoreUrl);

try {
const response = await fetch(`https://${storeUrlObj.host}/admin/oauth/info`);
if (!response.ok) {
throw new Error(response.statusText);
}
const payload = await response.json();
if (!payload.store_hash) {
const url = `https://${storeUrlObj.host}/admin/oauth/info`;
const response = await sendApiRequest({ url });
if (!response.data || !response.data.store_hash) {
throw new Error('Received empty store_hash value in the server response');
}
return { ...options, storeHash: payload.store_hash };
return { ...options, storeHash: response.data.store_hash };
} catch (err) {
err.name = 'StoreHashReadError';
throw err;
Expand Down Expand Up @@ -108,7 +105,6 @@ utils.uploadBundle = async (options) => {
bundleZipPath,
storeHash,
});

return {
...options,
jobId: result.jobId,
Expand Down
12 changes: 6 additions & 6 deletions lib/stencil-push.utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fetchMock = require('node-fetch');
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');

const { getStoreHash } = require('./stencil-push.utils');

// eslint-disable-next-line node/no-unpublished-require,global-require
jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox());
const axiosMock = new MockAdapter(axios);

describe('stencil push utils', () => {
const mockConfig = {
Expand All @@ -14,15 +14,15 @@ describe('stencil push utils', () => {

afterEach(() => {
jest.restoreAllMocks();
fetchMock.mockReset();
axiosMock.reset();
});

describe('.getStoreHash()', () => {
it('should get the store hash', async () => {
const mockResponseData = {
store_hash: 'abc123',
};
fetchMock.mock('*', mockResponseData);
axiosMock.onGet().reply(200, mockResponseData);

const result = await getStoreHash({ config: mockConfig });

Expand All @@ -32,7 +32,7 @@ describe('stencil push utils', () => {

it('should return an error if it fails to retrieve the store hash', async () => {
const mockResponseData = {};
fetchMock.mock('*', mockResponseData);
axiosMock.onGet().reply(200, mockResponseData);

await expect(getStoreHash({ config: mockConfig })).rejects.toThrow(
'Received empty store_hash value in the server response',
Expand Down
15 changes: 6 additions & 9 deletions lib/stencil-start.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('colors');
const BrowserSync = require('browser-sync');
const { promisify } = require('util');
const fetchModule = require('node-fetch');
const fsModule = require('fs');
const path = require('path');

Expand All @@ -14,11 +13,12 @@ const ThemeConfig = require('./theme-config');
const BuildConfigManager = require('./BuildConfigManager');
const fsUtilsModule = require('./utils/fsUtils');
const cliCommonModule = require('./cliCommon');
const networkUtilsModule = require('./utils/networkUtils');

class StencilStart {
constructor({
browserSync = BrowserSync.create(),
fetch = fetchModule,
networkUtils = networkUtilsModule,
fs = fsModule,
fsUtils = fsUtilsModule,
cliCommon = cliCommonModule,
Expand All @@ -30,7 +30,7 @@ class StencilStart {
logger = console,
} = {}) {
this._browserSync = browserSync;
this._fetch = fetch;
this._networkUtils = networkUtils;
this._fs = fs;
this._fsUtils = fsUtils;
this._cliCommon = cliCommon;
Expand Down Expand Up @@ -96,7 +96,7 @@ class StencilStart {
const staplerUrl = stencilConfig.staplerUrl
? stencilConfig.staplerUrl
: stencilConfig.normalStoreUrl;
const reqUrl = new URL(`/stencil-version-check?v=${currentCliVersion}`, staplerUrl);
const url = new URL(`/stencil-version-check?v=${currentCliVersion}`, staplerUrl).toString();
let payload;

const headers = {
Expand All @@ -107,11 +107,8 @@ class StencilStart {
}

try {
const response = await this._fetch(reqUrl, { headers });
if (!response.ok) {
throw new Error(response.statusText);
}
payload = await response.json();
const response = await this._networkUtils.sendApiRequest({ url, headers });
payload = response.data;
if (!payload) {
throw new Error('Empty payload in the server response');
}
Expand Down
9 changes: 5 additions & 4 deletions lib/stencil-start.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fetchMockModule = require('fetch-mock-jest');
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const path = require('path');

const StencilStart = require('./stencil-start');
Expand All @@ -10,7 +11,7 @@ describe('StencilStart unit tests', () => {
watch: jest.fn(),
init: jest.fn(),
});
const getFetchStub = () => fetchMockModule.sandbox();
const getNetworkUtilsStub = () => new MockAdapter(axios);
const getFsStub = () => ({
existsSync: jest.fn(),
});
Expand All @@ -33,9 +34,9 @@ describe('StencilStart unit tests', () => {

const createStencilStartInstance = ({
browserSync,
fetch,
fs,
fsUtils,
networkUtils,
cliCommon,
stencilConfigManager,
themeConfigManager,
Expand All @@ -46,9 +47,9 @@ describe('StencilStart unit tests', () => {
} = {}) => {
const passedArgs = {
browserSync: browserSync || getBrowserSyncStub(),
fetch: fetch || getFetchStub(),
fs: fs || getFsStub(),
fsUtils: fsUtils || getFsUtilsStub(),
networkUtils: networkUtils || getNetworkUtilsStub(),
cliCommon: cliCommon || getCliCommonStub(),
stencilConfigManager: stencilConfigManager || getStencilConfigManagerStub(),
themeConfigManager: themeConfigManager || getThemeConfigManagerStub(),
Expand Down
Loading

0 comments on commit 946a012

Please sign in to comment.