Skip to content

Commit

Permalink
Pass CloudRegion to Search Core (#1820)
Browse files Browse the repository at this point in the history
This change uses the CloudRegion injected during the build
process and passes it to the Search Core config. It also
passes the environment using the environment provided from
the config rather than manually constructing the endpoints.

J=BACK-2269
TEST=manual

Manually tested the eu assets with an eu test site
and confirmed the requests were being made correctly. Also
tested the search-bar only integration in eu. Confirmed the
us build still worked as expected.
  • Loading branch information
cea2aj committed May 5, 2023
1 parent fa0e712 commit 277f2cd
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
**/.DS_Store
.idea/
2 changes: 1 addition & 1 deletion THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ CDN

The following NPM package may be included in this product:

- @yext/search-core@2.2.0
- @yext/search-core@2.3.0-beta.1

This package contains the following license and notice below:

Expand Down
11 changes: 2 additions & 9 deletions conf/cloud-regions/constants.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
const CLOUD_REGION = {
US: 'us',
EU: 'eu'
};
const { CloudRegion } = require('@yext/search-core');

exports.CLOUD_REGION = CLOUD_REGION;

exports.DEFAULT_CLOUD_REGION = CLOUD_REGION.US;

exports.ALL_CLOUD_REGIONS = Object.values(CLOUD_REGION);
exports.DEFAULT_CLOUD_REGION = CloudRegion.US;
6 changes: 3 additions & 3 deletions conf/gulp-tasks/library.gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ function allLocaleJSBundles (isSearchBarOnly = false, languages, cloudRegion) {
'answers-umd.min.js'];

return createJSBundlesForLanguages(languages, cloudRegion, isSearchBarOnly).then(() => {
copyAssetsForLocales(assetNames, languages, cloudRegion);
copyAssetsForLocales(assetNames, languages);
});
}

exports.default = function defaultLanguageJSBundles () {
return createJSBundlesForLanguages([DEFAULT_LOCALE]);
return createJSBundlesForLanguages([DEFAULT_LOCALE], DEFAULT_CLOUD_REGION, false);
};

exports.buildLanguages = function allLanguageJSBundles () {
return createJSBundlesForLanguages(ALL_LANGUAGES);
return createJSBundlesForLanguages(ALL_LANGUAGES, DEFAULT_CLOUD_REGION, false);
};

exports.buildLocales = function (languages = ALL_LANGUAGES, cloudRegion = DEFAULT_CLOUD_REGION) {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@mapbox/mapbox-gl-language": "^0.10.1",
"@yext/answers-storage": "^1.1.0",
"@yext/rtf-converter": "^1.7.1",
"@yext/search-core": "^2.2.0",
"@yext/search-core": "^2.3.0-beta.1",
"bowser": "^2.11.0",
"cross-fetch": "^3.1.5",
"css-vars-ponyfill": "^2.4.3",
Expand Down
1 change: 1 addition & 0 deletions src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const LOCALE = '@@LOCALE';
/** The speech recognition locales supported by Microsoft Edge */
export const SPEECH_RECOGNITION_LOCALES_SUPPORTED_BY_EDGE = '@@SPEECH_RECOGNITION_LOCALES_SUPPORTED_BY_EDGE';

/** The cloud region being used, injected by the build process */
export const CLOUD_REGION = '@@CLOUD_REGION';

/** The identifier of the production environment */
Expand Down
27 changes: 10 additions & 17 deletions src/core/core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/** @module Core */
import { provideCore } from '@yext/search-core/lib/commonjs';
// Using the ESM build for importing the Environment enum due to an issue importing the commonjs version
import { Environment } from '@yext/search-core';
import { generateUUID } from './utils/uuid';
import SearchDataTransformer from './search/searchdatatransformer';

Expand All @@ -17,8 +19,7 @@ import FilterRegistry from './filters/filterregistry';
import DirectAnswer from './models/directanswer';
import AutoCompleteResponseTransformer from './search/autocompleteresponsetransformer';

import { PRODUCTION, ENDPOINTS, LIB_VERSION, CLOUD_REGION } from './constants';
import { getCachedLiveApiUrl, getLiveApiUrl } from './utils/urlutils';
import { PRODUCTION, LIB_VERSION, CLOUD_REGION, SANDBOX } from './constants';
import { SearchParams } from '../ui';
import SearchStates from './storage/searchstates';
import Searcher from './models/searcher';
Expand Down Expand Up @@ -113,6 +114,10 @@ export default class Core {
*/
this._environment = config.environment || PRODUCTION;

/**
* Determines the region of the api endpoints used when making search requests.
* @type {string}
*/
this._cloudRegion = CLOUD_REGION;

/** @type {string} */
Expand All @@ -138,36 +143,24 @@ export default class Core {
* Initializes the {@link Core} by providing it with an instance of the Core library.
*/
init (config) {
const environment = this._environment === SANDBOX ? Environment.SANDBOX : Environment.PROD;
const params = {
...(this._token && { token: this._token }),
...(this._apiKey && { apiKey: this._apiKey }),
experienceKey: this._experienceKey,
locale: this._locale,
experienceVersion: this._experienceVersion,
endpoints: this._getServiceUrls(),
additionalQueryParams: {
jsLibVersion: LIB_VERSION
},
cloudRegion: this._cloudRegion,
environment,
...config
};

this._coreLibrary = provideCore(params);
}

/**
* Get the urls for each service based on the environment.
*/
_getServiceUrls () {
return {
universalSearch: getLiveApiUrl(this._environment) + ENDPOINTS.UNIVERSAL_SEARCH,
verticalSearch: getLiveApiUrl(this._environment) + ENDPOINTS.VERTICAL_SEARCH,
questionSubmission: getLiveApiUrl(this._environment) + ENDPOINTS.QUESTION_SUBMISSION,
universalAutocomplete: getCachedLiveApiUrl(this._environment) + ENDPOINTS.UNIVERSAL_AUTOCOMPLETE,
verticalAutocomplete: getCachedLiveApiUrl(this._environment) + ENDPOINTS.VERTICAL_AUTOCOMPLETE,
filterSearch: getCachedLiveApiUrl(this._environment) + ENDPOINTS.FILTER_SEARCH
};
}

/**
* @returns {boolean} A boolean indicating if the {@link Core} has been
* initailized.
Expand Down
8 changes: 0 additions & 8 deletions src/core/utils/urlutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ export function getLiveApiUrl (env = PRODUCTION) {
return env === SANDBOX ? 'https://liveapi-sandbox.yext.com' : 'https://liveapi.yext.com';
}

/**
* Returns the base url for the live api backend in the desired environment.
* @param {string} env The desired environment.
*/
export function getCachedLiveApiUrl (env = PRODUCTION) {
return env === SANDBOX ? 'https://liveapi-sandbox.yext.com' : 'https://liveapi-cached.yext.com';
}

/**
* Returns the base url for the analytics backend in the desired environment.
* @param {string} env The desired environment.
Expand Down
4 changes: 2 additions & 2 deletions tests/acceptance/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const VERTICAL_SEARCH_URL_REGEX = /v2\/accounts\/me\/answers\/vertical\/query/;
export const UNIVERSAL_SEARCH_URL_REGEX = /v2\/accounts\/me\/answers\/query/;
export const VERTICAL_SEARCH_URL_REGEX = /v2\/accounts\/me\/search\/vertical\/query/;
export const UNIVERSAL_SEARCH_URL_REGEX = /v2\/accounts\/me\/search\/query/;
export const PORT = 9999;

const PAGE_DIR = `http://localhost:${PORT}/tests/acceptance/fixtures/html`;
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/fixtures/responses/filtersearch/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function generateFilterSearchResponse (input) {

export const MockedFilterSearchRequest = RequestMock()
.onRequestTo(async request => {
const urlRegex = /^https:\/\/liveapi-cached.yext.com\/v2\/accounts\/me\/answers\/filtersearch/;
const urlRegex = /^https:\/\/prod-cdn.us.yextapis.com\/v2\/accounts\/me\/search\/filtersearch/;
return urlRegex.test(request.url) && request.method === 'get';
})
.respond((req, res) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function generateAutoCompleteResponse (prompt) {

export const MockedUniversalAutoCompleteRequest = RequestMock()
.onRequestTo(async request => {
const urlRegex = /^https:\/\/liveapi-cached.yext.com\/v2\/accounts\/me\/answers\/autocomplete/;
const urlRegex = /^https:\/\/prod-cdn.us.yextapis.com\/v2\/accounts\/me\/search\/autocomplete/;
return urlRegex.test(request.url) && request.method === 'get';
})
.respond((req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/fixtures/responses/universal/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ function generateUniversalSearchResponse (input) {

export const MockedUniversalSearchRequest = RequestMock()
.onRequestTo(async request => {
const urlRegex = /^https:\/\/liveapi.yext.com\/v2\/accounts\/me\/answers\/query/;
const urlRegex = /^https:\/\/prod-cdn.us.yextapis.com\/v2\/accounts\/me\/search\/query/;
return urlRegex.test(request.url) && request.method === 'get';
})
.respond((req, res) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function generateAutoCompleteResponse (prompt) {

export const MockedVerticalAutoCompleteRequest = RequestMock()
.onRequestTo(async request => {
const urlRegex = /^https:\/\/liveapi-cached.yext.com\/v2\/accounts\/me\/answers\/vertical\/autocomplete/;
const urlRegex = /^https:\/\/prod-cdn.us.yextapis.com\/v2\/accounts\/me\/search\/vertical\/autocomplete/;
return urlRegex.test(request.url) && request.method === 'get';
})
.respond((req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/fixtures/responses/vertical/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function generateVerticalSearchResponse (verticalKey, input, offset, filterParam

export const MockedVerticalSearchRequest = RequestMock()
.onRequestTo(async request => {
const urlRegex = /^https:\/\/liveapi.yext.com\/v2\/accounts\/me\/answers\/vertical\/query/;
const urlRegex = /^https:\/\/prod-cdn.us.yextapis.com\/v2\/accounts\/me\/search\/vertical\/query/;
return urlRegex.test(request.url) && request.method === 'get';
})
.respond((req, res) => {
Expand Down
3 changes: 0 additions & 3 deletions tests/core/utils/urlutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import SearchParams from '../../../src/ui/dom/searchparams';
import { PRODUCTION, SANDBOX } from '../../../src/core/constants';
import {
getLiveApiUrl,
getCachedLiveApiUrl,
getAnalyticsUrl,
replaceUrlParams,
urlWithoutQueryParamsAndHash,
Expand All @@ -15,11 +14,9 @@ const baseUrl = 'https://yext.com/';
describe('getUrlFunctions work', () => {
it('differentiates sandbox from prod', () => {
expect(getLiveApiUrl()).not.toEqual(expect.stringContaining('sandbox'));
expect(getCachedLiveApiUrl()).not.toEqual(expect.stringContaining('sandbox'));
expect(getAnalyticsUrl()).not.toEqual(expect.stringContaining('sandbox'));

expect(getLiveApiUrl(SANDBOX)).toEqual(expect.stringContaining('sandbox'));
expect(getCachedLiveApiUrl(SANDBOX)).toEqual(expect.stringContaining('sandbox'));
expect(getAnalyticsUrl(SANDBOX)).toEqual(expect.stringContaining('sandbox'));
});

Expand Down

0 comments on commit 277f2cd

Please sign in to comment.