Skip to content

Commit

Permalink
search-core: add cloudChoice enum and config support
Browse files Browse the repository at this point in the history
This PR adds the new cloudChoice config property, to allow
choosing either all consumer serving regions, or just the ones
backed by GCP. By default the Multi option will be used, so
default behavior is unchanged.

J=WAT-4374
TEST=auto

Added new test class
  • Loading branch information
Fondryext committed Sep 10, 2024
1 parent 8f0e835 commit b128449
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 11 deletions.
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "@yext/search-core",
"version": "2.5.3",
"version": "2.5.4",
"description": "Typescript Networking Library for the Yext Search API",
"main": "./dist/commonjs/src/index.js",
"module": "./dist/esm/src/index.js",
Expand Down
9 changes: 9 additions & 0 deletions src/models/core/CloudChoice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Defines the cloud choice of the API domains.
*
* @public
*/
export enum CloudChoice {
GLOBAL_MULTI = 'GLOBAL-MULTI', //All available cloud regions

Check warning on line 7 in src/models/core/CloudChoice.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Expected indentation of 2 spaces but found 4

Check warning on line 7 in src/models/core/CloudChoice.ts

View workflow job for this annotation

GitHub Actions / linting / linting

Expected indentation of 2 spaces but found 4

Check warning on line 7 in src/models/core/CloudChoice.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Expected indentation of 2 spaces but found 4
GLOBAL_GCP = 'GLOBAL-GCP', //Only available GCP-backed cloud regions

Check warning on line 8 in src/models/core/CloudChoice.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Expected indentation of 2 spaces but found 4

Check warning on line 8 in src/models/core/CloudChoice.ts

View workflow job for this annotation

GitHub Actions / linting / linting

Expected indentation of 2 spaces but found 4

Check warning on line 8 in src/models/core/CloudChoice.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Expected indentation of 2 spaces but found 4
}
11 changes: 9 additions & 2 deletions src/models/core/SearchConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Endpoints } from './Endpoints';
import { Visitor } from './Visitor';
import { Environment } from './Environment';
import { CloudRegion } from './CloudRegion';
import {CloudChoice} from "./CloudChoice";

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required before '}'

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Strings must use singlequote

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / linting / linting

A space is required after '{'

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / linting / linting

A space is required before '}'

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / linting / linting

Strings must use singlequote

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required before '}'

Check warning on line 5 in src/models/core/SearchConfig.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Strings must use singlequote

/**
* The configuration options for getting the endpoints.
Expand All @@ -20,7 +21,13 @@ export interface ServingConfig {
*
* @public
*/
cloudRegion?: CloudRegion
cloudRegion?: CloudRegion,
/**
* {@inheritDoc CloudChoice}
*
* @public
*/
cloudChoice?: CloudChoice
}

/**
Expand Down Expand Up @@ -119,4 +126,4 @@ export type SearchConfig = SearchConfigWithApiKey | SearchConfigWithToken;
*
* @internal
*/
export type SearchConfigWithDefaulting = SearchConfig & { endpoints: Required<Endpoints> };
export type SearchConfigWithDefaulting = SearchConfig & { endpoints: Required<Endpoints> };
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
} from './core/AdditionalHttpHeaders';
export { Environment } from './core/Environment';
export { CloudRegion } from './core/CloudRegion';
export { CloudChoice } from './core/CloudChoice';

// Autocomplete service
export * from './autocompleteservice/AutocompleteRequest';
Expand Down
16 changes: 10 additions & 6 deletions src/provideEndpoints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Endpoints } from './models/core/Endpoints';
import { Environment } from './models/core/Environment';
import { CloudRegion } from './models/core/CloudRegion';
import { ServingConfig } from './models/core/SearchConfig';
import {Endpoints} from './models/core/Endpoints';

Check warning on line 1 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'

Check warning on line 1 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required before '}'

Check warning on line 1 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / linting / linting

A space is required after '{'

Check warning on line 1 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / linting / linting

A space is required before '}'

Check warning on line 1 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'

Check warning on line 1 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required before '}'
import {Environment} from './models/core/Environment';

Check warning on line 2 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'

Check warning on line 2 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required before '}'

Check warning on line 2 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / linting / linting

A space is required after '{'

Check warning on line 2 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / linting / linting

A space is required before '}'

Check warning on line 2 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'

Check warning on line 2 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required before '}'
import {CloudRegion} from './models/core/CloudRegion';

Check warning on line 3 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'

Check warning on line 3 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / linting / linting

A space is required after '{'

Check warning on line 3 in src/provideEndpoints.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

A space is required after '{'
import {ServingConfig} from './models/core/SearchConfig';
import {CloudChoice} from "./models/core/CloudChoice";

export const defaultApiVersion = 20220511;

Expand All @@ -13,15 +14,18 @@ export const defaultApiVersion = 20220511;
export class EndpointsFactory {
private readonly environment: Environment;
private readonly cloudRegion: CloudRegion;
private readonly cloudChoice: CloudChoice;

constructor(config?: ServingConfig) {
this.environment = config?.environment || Environment.PROD;
this.cloudRegion = config?.cloudRegion || CloudRegion.US;
this.cloudChoice = config?.cloudChoice || CloudChoice.GLOBAL_MULTI;
}

/** Provides the domain based on environment and cloud region. */
getDomain() {
return `https://${this.environment}-cdn.${this.cloudRegion}.yextapis.com`;
const cloudChoiceSuffix = this.cloudChoice === CloudChoice.GLOBAL_GCP ? '-gcp' : '';
return `https://${this.environment}-cdn${cloudChoiceSuffix}.${this.cloudRegion}.yextapis.com`;
}

/** Provides all endpoints based on environment and cloud region. */
Expand All @@ -46,5 +50,5 @@ export class EndpointsFactory {
* @public
*/
export const SandboxEndpoints: Required<Endpoints> =
new EndpointsFactory({ environment: Environment.SANDBOX, cloudRegion: CloudRegion.US })
new EndpointsFactory({ environment: Environment.SANDBOX, cloudRegion: CloudRegion.US, cloudChoice: CloudChoice.GLOBAL_MULTI })
.getEndpoints();
47 changes: 47 additions & 0 deletions tests/provideEndpointsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {EndpointsFactory} from '../src/provideEndpoints';
import {CloudChoice, CloudRegion, Environment} from "../src";

it('Sandbox, US, Multi produces expected endpoint', () => {
const endPoints = new EndpointsFactory({
environment: Environment.SANDBOX,
cloudRegion: CloudRegion.US,
cloudChoice: CloudChoice.GLOBAL_MULTI
}).getEndpoints();
expect(endPoints).toHaveProperty('universalSearch', 'https://sbx-cdn.us.yextapis.com/v2/accounts/me/search/query');
});

it('Prod, US, Multi produces expected endpoint', () => {
const endPoints = new EndpointsFactory({
environment: Environment.PROD,
cloudRegion: CloudRegion.US,
cloudChoice: CloudChoice.GLOBAL_MULTI
}).getEndpoints();
expect(endPoints).toHaveProperty('verticalSearch', 'https://prod-cdn.us.yextapis.com/v2/accounts/me/search/vertical/query');
});

it('Prod, US, GCP produces expected endpoint', () => {
const endPoints = new EndpointsFactory({
environment: Environment.PROD,
cloudRegion: CloudRegion.US,
cloudChoice: CloudChoice.GLOBAL_GCP
}).getEndpoints();
expect(endPoints).toHaveProperty('universalAutocomplete', 'https://prod-cdn-gcp.us.yextapis.com/v2/accounts/me/search/autocomplete');
});

it('Prod, EU, Multi produces expected endpoint', () => {
const endPoints = new EndpointsFactory({
environment: Environment.PROD,
cloudRegion: CloudRegion.EU,
cloudChoice: CloudChoice.GLOBAL_MULTI
}).getEndpoints();
expect(endPoints).toHaveProperty('verticalAutocomplete', 'https://prod-cdn.eu.yextapis.com/v2/accounts/me/search/vertical/autocomplete');
});

it('Prod, EU, GCP produces expected endpoint', () => {
const endPoints = new EndpointsFactory({
environment: Environment.PROD,
cloudRegion: CloudRegion.EU,
cloudChoice: CloudChoice.GLOBAL_GCP
}).getEndpoints();
expect(endPoints).toHaveProperty('filterSearch', 'https://prod-cdn-gcp.eu.yextapis.com/v2/accounts/me/search/filtersearch');
});

0 comments on commit b128449

Please sign in to comment.