Skip to content

Commit

Permalink
fix(storefront): strf 9587 api host command is not recognized (#830)
Browse files Browse the repository at this point in the history
* fix: strf-9587 apiHost command is not recognized

- bin/stencil-init.js - option for host added;
- lib/stencil-init.js - apiHost processing;
- constants.js - dev/intg/stg api hosts removed;
- stencil-init.spec.js - tests refactored and some new suits added;
  • Loading branch information
bc-max authored Jan 13, 2022
1 parent 3e8dcfb commit 37fcd49
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 63 deletions.
2 changes: 2 additions & 0 deletions bin/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ program
.option('-u, --url [url]', 'Store URL')
.option('-t, --token [token]', 'Access Token')
.option('-p, --port [port]', 'Port')
.option('-h, --apiHost [host]', 'API Host')
.parse(process.argv);

checkNodeVersion();
Expand All @@ -22,5 +23,6 @@ new StencilInit()
normalStoreUrl: cliOptions.url,
accessToken: cliOptions.token,
port: cliOptions.port,
apiHost: cliOptions.apiHost,
})
.catch(printCliResultErrorAndExit);
6 changes: 0 additions & 6 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ const DEFAULT_CUSTOM_LAYOUTS_CONFIG = {

/// ///////////////////////////////////////// Other //////////////////////////////////////// ///

const DEV_API_HOST = 'https://api.service.bcdev';
const INTG_API_HOST = 'https://api.integration.zone';
const STG_API_HOST = 'https://api.staging.zone';
const API_HOST = 'https://api.bigcommerce.com';

module.exports = {
PACKAGE_INFO,
THEME_PATH,
DEFAULT_CUSTOM_LAYOUTS_CONFIG,
DEV_API_HOST,
INTG_API_HOST,
STG_API_HOST,
API_HOST,
};
54 changes: 20 additions & 34 deletions lib/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ const inquirerModule = require('inquirer');

const serverConfigModule = require('../server/config');
const StencilConfigManager = require('./StencilConfigManager');
const {
DEFAULT_CUSTOM_LAYOUTS_CONFIG,
API_HOST,
INTG_API_HOST,
STG_API_HOST,
DEV_API_HOST,
} = require('../constants');
const { DEFAULT_CUSTOM_LAYOUTS_CONFIG, API_HOST } = require('../constants');

class StencilInit {
/**
Expand Down Expand Up @@ -71,20 +65,21 @@ class StencilInit {
}

/**
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} stencilConfig
* @returns {{port: (number), normalStoreUrl: (string), accessToken: (string)}}
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string), apiHost: (string)}} stencilConfig
* @returns {{port: (number), normalStoreUrl: (string), accessToken: (string), apiHost: (string)}}
*/
getDefaultAnswers(stencilConfig) {
return {
normalStoreUrl: stencilConfig.normalStoreUrl,
accessToken: stencilConfig.accessToken,
port: stencilConfig.port || this._serverConfig.get('/server/port'),
apiHost: API_HOST,
};
}

/**
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} cliOptions
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string), apiHost: (string)}} defaultAnswers
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string), apiHost: (string)}} cliOptions
* @returns {{object[]}}
*/
getQuestions(defaultAnswers, cliOptions) {
Expand All @@ -110,6 +105,15 @@ class StencilInit {
});
}

if (!cliOptions.apiHost) {
prompts.push({
type: 'input',
name: 'apiHost',
message: 'What API Host would you like to use?',
default: defaultAnswers.apiHost,
});
}

if (!cliOptions.port) {
prompts.push({
type: 'input',
Expand Down Expand Up @@ -139,27 +143,12 @@ class StencilInit {
return questions.length ? this._inquirer.prompt(questions) : {};
}

apiHostFromStoreUrl(storeUrl) {
let host = null;
if (storeUrl !== undefined) {
if (storeUrl.includes('service.bcdev')) {
host = DEV_API_HOST;
}
if (storeUrl.includes('my-integration.zone')) {
host = INTG_API_HOST;
}
if (storeUrl.includes('my-staging.zone')) {
host = STG_API_HOST;
}
}

if (host === null) {
host = API_HOST;
}
updateApiHost(stencilConfig, cliOptions) {
const host = cliOptions.apiHost || API_HOST;

console.log('Set API host to: ' + host);

return host;
return { ...stencilConfig, apiHost: host };
}

/**
Expand All @@ -169,13 +158,10 @@ class StencilInit {
* @returns {object}
*/
applyAnswers(stencilConfig, answers, cliOptions) {
const storeUrl =
answers.normalStoreUrl || cliOptions.normalStoreUrl || stencilConfig.normalStoreUrl;

const config = this.updateApiHost(stencilConfig, cliOptions);
return {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
apiHost: this.apiHostFromStoreUrl(storeUrl),
...stencilConfig,
...config,
...cliOptions,
...answers,
};
Expand Down
60 changes: 37 additions & 23 deletions lib/stencil-init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ const getAnswers = () => ({
normalStoreUrl: 'https://url-from-answers.mybigcommerce.com',
port: 3003,
accessToken: 'accessToken_from_answers',
apiHost: API_HOST,
});
const getCliOptions = () => ({
normalStoreUrl: 'https://url-from-cli-options.mybigcommerce.com',
port: 3002,
accessToken: 'accessToken_from_CLI_options',
apiHost: API_HOST,
});
const getQuestions = () => [
{
Expand All @@ -48,6 +50,12 @@ const getQuestions = () => [
default: 'accessToken_from_answers',
filter: (val) => val.trim(),
},
{
type: 'input',
name: 'apiHost',
message: 'What API Host would you like to use?',
default: API_HOST,
},
{
type: 'input',
name: 'port',
Expand All @@ -74,7 +82,6 @@ describe('StencilInit integration tests', () => {
const expectedResult = {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...answers,
apiHost: API_HOST,
};
const stencilConfigManager = new StencilConfigManager({
themePath: './test/_mocks/themes/valid/',
Expand Down Expand Up @@ -112,7 +119,6 @@ describe('StencilInit integration tests', () => {
const expectedResult = {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...cliOptions,
apiHost: API_HOST,
};
const stencilConfigManager = new StencilConfigManager({
themePath: './test/_mocks/themes/valid/',
Expand Down Expand Up @@ -328,27 +334,31 @@ describe('StencilInit unit tests', () => {
});

describe('applyAnswers', () => {
const cliOptions = getCliOptions();

// eslint-disable-next-line jest/expect-expect
it('should not mutate the passed objects', async () => {
const stencilConfig = getStencilConfig();
const answers = getAnswers();

const { instance } = createStencilInitInstance();
await assertNoMutations([stencilConfig, answers], () =>
instance.applyAnswers(stencilConfig, answers),
await assertNoMutations([stencilConfig, answers, cliOptions], () =>
instance.applyAnswers(stencilConfig, answers, cliOptions),
);
});

it('should correctly merge values from the passed objects', async () => {
const stencilConfig = getStencilConfig();
delete cliOptions.apiHost;
const answers = getAnswers();

const { instance } = createStencilInitInstance();
const res = instance.applyAnswers(stencilConfig, answers);
const res = instance.applyAnswers(stencilConfig, answers, cliOptions);

expect(res.normalStoreUrl).toEqual(answers.normalStoreUrl);
expect(res.accessToken).toEqual(answers.accessToken);
expect(res.port).toEqual(answers.port);
expect(res.apiHost).toEqual(answers.apiHost);

expect(res.githubToken).toEqual(stencilConfig.githubToken);
expect(res.customLayouts).toEqual(stencilConfig.customLayouts);
Expand All @@ -359,7 +369,7 @@ describe('StencilInit unit tests', () => {
const answers = getAnswers();

const { instance } = createStencilInitInstance();
const res = instance.applyAnswers(stencilConfig, answers);
const res = instance.applyAnswers(stencilConfig, answers, cliOptions);

expect(res.customLayouts).toEqual(DEFAULT_CUSTOM_LAYOUTS_CONFIG);
// Make sure that other props aren't overwritten:
Expand All @@ -368,34 +378,38 @@ describe('StencilInit unit tests', () => {
});
});

describe('apiHostFromStoreUrl', () => {
it('should return the integration api host for integration stores', async () => {
const storeUrl = 'https://store-url.my-integration.zone';
const expected = 'https://api.integration.zone';

describe('updateApiHost', () => {
const options = getCliOptions();
const config = getStencilConfig();
it('should return the same config if it contains apiHost', async () => {
const { instance } = createStencilInitInstance();
const res = instance.apiHostFromStoreUrl(storeUrl);
delete options.apiHost;
const res = instance.updateApiHost(config, options);

expect(res).toEqual(expected);
expect(res).toEqual(config);
});

it('should return the staging api host for staging stores', async () => {
const storeUrl = 'https://store-url.my-staging.zone';
const expected = 'https://api.staging.zone';

it('should add default apiHost if neither config, nor options contain apiHost', async () => {
const { instance } = createStencilInitInstance();
const res = instance.apiHostFromStoreUrl(storeUrl);
delete config.apiHost;
delete options.apiHost;

const res = instance.updateApiHost(config, options);
const expected = getStencilConfig();

expect(res).toEqual(expected);
});

it('should return the API_HOST constant for all ohter stores', async () => {
const storeUrl = 'https://store-url.mystore.com';

it('should add custom apiHost', async () => {
const { instance } = createStencilInitInstance();
const res = instance.apiHostFromStoreUrl(storeUrl);
delete config.apiHost;
delete options.apiHost;

const cliOptions = { ...options, apiHost: 'https://custom.api.com' };
const expectedConfig = { ...config, apiHost: 'https://custom.api.com' };
const res = instance.updateApiHost(config, cliOptions);

expect(res).toEqual(API_HOST);
expect(res).toEqual(expectedConfig);
});
});
});

0 comments on commit 37fcd49

Please sign in to comment.