Skip to content

Commit

Permalink
make region capital and update setupRequestUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Jaffee committed Jan 30, 2024
1 parent e5bd3d6 commit ef32739
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 141 deletions.
9 changes: 0 additions & 9 deletions src/EnumOrString.ts

This file was deleted.

17 changes: 1 addition & 16 deletions src/Environment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import { EnumOrString } from './EnumOrString';
/**
* An enum for the Yext Environments
*
* @remarks
* Affects the domain the requests are sent to.
*
* @public
*/
export enum EnvironmentEnum {
Production = 'PRODUCTION',
/** unsupported in the EU region */
Sandbox = 'SANDBOX'
}

/**
* The Yext Environments
*
Expand All @@ -21,4 +6,4 @@ export enum EnvironmentEnum {
*
* @public
*/
export type Environment = EnumOrString<EnvironmentEnum>;
export type Environment = 'PRODUCTION' | 'SANDBOX';
17 changes: 2 additions & 15 deletions src/Region.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
import { EnumOrString } from './EnumOrString';
/**
* An enum of the physical region the Yext account
*
* @remarks
* Affects the domain the requests are sent to.
*
* @public
*/
export enum RegionEnum {
US = 'us',
EU = 'eu'
}

/**
* The physical region of the Yext account
*
Expand All @@ -20,4 +6,5 @@ export enum RegionEnum {
*
* @public
*/
export type Region = EnumOrString<RegionEnum>;

export type Region = 'US' | 'EU';
17 changes: 1 addition & 16 deletions src/VersionLabel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { EnumOrString } from './EnumOrString';

/**
* An enum for the Search Version Labels
*
Expand All @@ -8,17 +6,4 @@ import { EnumOrString } from './EnumOrString';
*
* @public
*/
export enum VersionLabelEnum {
Production = 'PRODUCTION',
Staging = 'STAGING'
}

/**
* The Search Version Label
*
* @remarks
* Affects the contents of the versionLabel field in the search object field in the event payload.
*
* @public
*/
export type VersionLabel = EnumOrString<VersionLabelEnum>;
export type VersionLabel = 'PRODUCTION' | 'STAGING';
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export * from './AnalyticsEventService';
export * from './Environment';
export * from './Region';
export * from './EventPayload';
export * from './EnumOrString';
export * from './Action';
export * from './VersionLabel';

Expand Down
12 changes: 7 additions & 5 deletions src/setupRequestUrl.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Environment, EnvironmentEnum } from './Environment';
import { Region, RegionEnum } from './Region';
import { Environment } from './Environment';
import { Region } from './Region';

const urlBase = 'yextevents.com/accounts/me/events';

export function setupRequestUrl(env?: Environment, region?: Region): string {
if (env === EnvironmentEnum.Sandbox && region === RegionEnum.EU) {
const isSandbox = env === 'SANDBOX';
const lowerRegion = region?.toLowerCase();
if (isSandbox && lowerRegion === 'eu') {
throw new Error('Sandbox environment is not available in the EU region.');
}
return (
'https://' +
(env === EnvironmentEnum.Sandbox ? 'sbx.' : '') +
(region ?? 'us') +
(isSandbox ? 'sbx.' : '') +
(lowerRegion ?? 'us') +
'.' +
urlBase
);
Expand Down
2 changes: 1 addition & 1 deletion test-cdn/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
const analyticsProvider = window.AnalyticsSDK.analytics({
key: '<INSERT YEXT API KEY HERE>',
sessionTrackingEnabled: false,
region: 'eu'
region: 'EU'
}).with({
action: 'CHAT_LINK_CLICK',
pageUrl: 'http://www.yext-test-pageurl.com',
Expand Down
22 changes: 10 additions & 12 deletions tests/AnalyticsEventReporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { AnalyticsConfig } from '../src/AnalyticsConfig';
import { AnalyticsEventReporter } from '../src/AnalyticsEventReporter';
import { RegionEnum } from '../src/Region';
import { postWithBeacon, postWithFetch, useBeacon } from '../src/post';
import { EnvironmentEnum } from '../src/Environment';
import { getOrSetupSessionId } from '../src/setupSessionId';
import { AnalyticsEventService } from '../src/AnalyticsEventService';

Expand Down Expand Up @@ -96,7 +94,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
region: 'EU',
forceFetch: false
};
const reporter = new AnalyticsEventReporter(config).with({
Expand Down Expand Up @@ -139,7 +137,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'bearer',
authorization: 'bearerToken',
env: EnvironmentEnum.Sandbox
env: 'SANDBOX'
};
const reporter = new AnalyticsEventReporter(config).with({
action: 'c_lowercase_custom_action',
Expand Down Expand Up @@ -354,7 +352,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
region: 'EU',
forceFetch: false
};
const reporter = new AnalyticsEventReporter(config).with({
Expand Down Expand Up @@ -400,7 +398,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
region: 'EU',
forceFetch: false
};

Expand Down Expand Up @@ -448,7 +446,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
region: 'EU',
forceFetch: true
};

Expand Down Expand Up @@ -486,7 +484,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
region: 'EU',
forceFetch: false
};

Expand Down Expand Up @@ -525,7 +523,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
region: 'EU',
forceFetch: false
};

Expand Down Expand Up @@ -601,7 +599,7 @@ describe('Test report function', () => {
const config: AnalyticsConfig = {
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
region: 'EU',
forceFetch: false,
sessionTrackingEnabled: true
};
Expand Down Expand Up @@ -657,7 +655,7 @@ describe('Test report function', () => {
},
searchTerm: 'searchTerm',
sessionId: 'sessionId',
sites: {
pages: {
siteUid: 321,
template: 'template'
},
Expand Down Expand Up @@ -736,7 +734,7 @@ describe('Test report function', () => {
},
searchTerm: 'searchTerm',
sessionId: 'sessionId',
sites: {
pages: {
siteUid: 321,
template: 'template'
},
Expand Down
77 changes: 11 additions & 66 deletions tests/setupRequestUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,30 @@
import { EnvironmentEnum } from '../src/Environment';
import { RegionEnum } from '../src/Region';
import { setupRequestUrl } from '../src/setupRequestUrl';

describe('setUpRequestUrl Test', () => {
it('should set url correctly for env: prod, region: us using their Enums', () => {
const resultUrl = setupRequestUrl(
EnvironmentEnum.Production,
RegionEnum.US
);
it('should set url correctly for env: prod, region: US', () => {
const resultUrl = setupRequestUrl('PRODUCTION', 'US');
const expectedUrl = 'https://us.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly for env: prod, region: us using their string literals', () => {
const resultUrl = setupRequestUrl('PRODUCTION', 'us');
const expectedUrl = 'https://us.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly for env: prod, region: eu using their Enums', () => {
const resultUrl = setupRequestUrl(
EnvironmentEnum.Production,
RegionEnum.EU
);
const expectedUrl = 'https://eu.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly for env: prod, region: eu using their string literals', () => {
const resultUrl = setupRequestUrl('PRODUCTION', 'eu');
it('should set url correctly for env: prod, region: EU', () => {
const resultUrl = setupRequestUrl('PRODUCTION', 'EU');
const expectedUrl = 'https://eu.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly for env: sbx, region: us using their Enums', () => {
const resultUrl = setupRequestUrl(EnvironmentEnum.Sandbox, RegionEnum.US);
it('should set url correctly for env: sbx, region: US', () => {
const resultUrl = setupRequestUrl('SANDBOX', 'US');
const expectedUrl = 'https://sbx.us.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly for env: sbx, region: us using their string literals', () => {
const resultUrl = setupRequestUrl('SANDBOX', 'us');
const expectedUrl = 'https://sbx.us.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should throw error for env: sbx, region: eu using their Enums', () => {
it('should throw error for env: sbx, region: EU', () => {
try {
setupRequestUrl(EnvironmentEnum.Sandbox, RegionEnum.EU);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe(
'Sandbox environment is not available in the EU region.'
);
}
});

it('should throw error for env: sbx, region: eu using their string literals', () => {
try {
setupRequestUrl('SANDBOX', 'eu');
setupRequestUrl('SANDBOX', 'EU');
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
Expand All @@ -82,28 +41,14 @@ describe('setUpRequestUrl Test', () => {
expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly with no env argument passed in using its Enum', () => {
const resultUrl = setupRequestUrl(undefined, RegionEnum.EU);
it('should set url correctly with no env argument passed in', () => {
const resultUrl = setupRequestUrl(undefined, 'EU');
const expectedUrl = 'https://eu.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly with no env argument passed in using their string literals', () => {
const resultUrl = setupRequestUrl(undefined, 'eu');
const expectedUrl = 'https://eu.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly with no region argument passed in using its Enum', () => {
const resultUrl = setupRequestUrl(EnvironmentEnum.Sandbox, undefined);
const expectedUrl = 'https://sbx.us.yextevents.com/accounts/me/events';

expect(resultUrl).toEqual(expectedUrl);
});

it('should set url correctly with no region argument passed in using their string literals', () => {
it('should set url correctly with no region argument passed in', () => {
const resultUrl = setupRequestUrl('SANDBOX', undefined);
const expectedUrl = 'https://sbx.us.yextevents.com/accounts/me/events';

Expand Down

0 comments on commit ef32739

Please sign in to comment.