Skip to content

Commit

Permalink
feat(headless): enforce organization endpoints usage for headless com…
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored Jun 7, 2024
1 parent d1d098d commit 8bb77af
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 38 deletions.
6 changes: 0 additions & 6 deletions packages/atomic/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,6 @@ export namespace Components {
* Executes the first search after initializing connection to the headless search engine.
*/
"executeFirstSearch": () => Promise<void>;
/**
* Returns the unique, organization-specific endpoint(s).
* @param organizationId
* @param env
*/
"getOrganizationEndpoints": (organizationId: string, env?: PlatformEnvironment) => Promise<{ platform: string; analytics: string; search: string; admin: string; }>;
/**
* the commerce interface i18next instance.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {
PlatformEnvironment,
LogLevel,
Search,
Unsubscribe,
UrlManager,
buildSearch,
getOrganizationEndpoints as getOrganizationEndpointsHeadless,
updateQuery,
CommerceEngine,
CommerceEngineConfiguration,
Expand Down Expand Up @@ -284,18 +282,6 @@ export class AtomicCommerceInterface
this.engine.executeFirstSearch();
}

/**
* Returns the unique, organization-specific endpoint(s).
* @param {string} organizationId
* @param {'prod'|'hipaa'|'staging'|'dev'} [env=Prod]
*/
@Method() public async getOrganizationEndpoints(
organizationId: string,
env: PlatformEnvironment = 'prod'
) {
return getOrganizationEndpointsHeadless(organizationId, env);
}

public get bindings(): CommerceBindings {
return {
engine: this.engine!,
Expand Down
10 changes: 2 additions & 8 deletions packages/atomic/src/pages/examples/commerce-website/engine.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {navContent} from './commerce-nav.mjs';
import {
getOrganizationEndpoints,
buildCommerceEngine,
} from '/build/headless/commerce/headless.esm.js';
import {buildCommerceEngine} from '/build/headless/commerce/headless.esm.js';

export const commerceEngine = buildCommerceEngine({
configuration: {
Expand All @@ -17,6 +14,7 @@ export const commerceEngine = buildCommerceEngine({
},
},
organizationId: 'barcasportsmcy01fvu',
environment: 'dev',
analytics: {
trackingId: 'sports',
},
Expand Down Expand Up @@ -52,9 +50,5 @@ export const commerceEngine = buildCommerceEngine({
},
],
},
organizationEndpoints: await getOrganizationEndpoints(
'barcasportsmcy01fvu',
'dev'
),
},
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {RecordValue, Schema} from '@coveo/bueno';
import {getOrganizationEndpoints} from '../../api/platform-client';
import {RecordValue, Schema, StringValue} from '@coveo/bueno';
import {CartInitialState} from '../../controllers/commerce/context/cart/headless-cart';
import {ContextOptions} from '../../controllers/commerce/context/headless-context';
import {cartDefinition} from '../../features/commerce/context/cart/cart-validation';
import {contextDefinition} from '../../features/commerce/context/context-validation';
import {PlatformEnvironment} from '../../utils/url-utils';
import {
EngineConfiguration,
engineConfigurationDefinitions,
Expand All @@ -17,6 +17,7 @@ import {
export interface CommerceEngineConfiguration extends EngineConfiguration {
context: ContextOptions;
cart?: CartInitialState;
environment?: PlatformEnvironment;
}

export const commerceEngineConfigurationSchema =
Expand All @@ -29,16 +30,19 @@ export const commerceEngineConfigurationSchema =
cart: new RecordValue({
values: cartDefinition,
}),
environment: new StringValue<PlatformEnvironment>({
required: false,
constrainTo: ['prod', 'hipaa', 'stg', 'dev'],
emptyAllowed: false,
default: 'prod',
}),
});

export function getSampleCommerceEngineConfiguration(): CommerceEngineConfiguration {
return {
organizationId: 'fashioncoveodemocomgzh7iep8',
// deepcode ignore HardcodedNonCryptoSecret: Public key freely available for our documentation
accessToken: 'xx149e3ec9-786f-4c6c-b64f-49a403b930de',
organizationEndpoints: getOrganizationEndpoints(
'fashioncoveodemocomgzh7iep8'
),
context: {
language: 'en',
country: 'CA',
Expand Down
34 changes: 34 additions & 0 deletions packages/headless/src/app/commerce-engine/commerce-engine.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {getOrganizationEndpoints} from '../../api/platform-client';
import {createCartKey} from '../../controllers/commerce/context/cart/headless-cart';
import {stateKey} from '../state-key';
import {
Expand Down Expand Up @@ -64,4 +65,37 @@ describe('buildCommerceEngine', () => {
[createCartKey(items[1])]: items[1],
});
});

it('uses organization endpoints when manually configured', () => {
options.configuration.organizationEndpoints = getOrganizationEndpoints(
'my-org-id',
'hipaa'
);
initEngine();

expect(engine.configuration.platformUrl).toBe(
'https://my-org-id.orghipaa.coveo.com'
);
});

it('uses organization endpoints when not manually specified', () => {
options.configuration.organizationEndpoints = undefined;
options.configuration.environment = 'hipaa';
options.configuration.organizationId = 'my-org-id';
initEngine();

expect(engine.configuration.platformUrl).toBe(
'https://my-org-id.orghipaa.coveo.com'
);
});

it('uses organization endpoints defaulting to prod when not manually specified', () => {
options.configuration.organizationEndpoints = undefined;
options.configuration.organizationId = 'my-org-id';
initEngine();

expect(engine.configuration.platformUrl).toBe(
'https://my-org-id.org.coveo.com'
);
});
});
11 changes: 11 additions & 0 deletions packages/headless/src/app/commerce-engine/commerce-engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {StateFromReducersMapObject} from '@reduxjs/toolkit';
import {Logger} from 'pino';
import {CommerceAPIClient} from '../../api/commerce/commerce-api-client';
import {getOrganizationEndpoints} from '../../api/platform-client';
import {NoopPreprocessRequest} from '../../api/preprocess-request';
import {setItems} from '../../features/commerce/context/cart/cart-actions';
import {cartReducer} from '../../features/commerce/context/cart/cart-slice';
Expand Down Expand Up @@ -116,6 +117,16 @@ export function buildCommerceEngine(

const augmentedOptions: EngineOptions<CommerceEngineReducers> = {
...options,
configuration: {
...options.configuration,
organizationEndpoints: {
...getOrganizationEndpoints(
options.configuration.organizationId,
options.configuration.environment
),
...options.configuration.organizationEndpoints,
},
},
reducers: commerceEngineReducers,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
AtomicProductText,
AtomicProductPrice,
AtomicProductDescription,
getOrganizationEndpoints,
buildCommerceEngine,
AtomicCommerceSearchBox,
AtomicCommerceSearchBoxInstantProducts,
Expand All @@ -32,10 +31,7 @@ export const CommerceSearchPage = () => {
configuration: {
accessToken: 'xxc481d5de-16cb-4290-bd78-45345319d94c',
organizationId: 'barcasportsmcy01fvu',
organizationEndpoints: getOrganizationEndpoints(
'barcasportsmcy01fvu',
'dev'
),
environment: 'dev',
analytics: {
trackingId: 'sports',
},
Expand Down

0 comments on commit 8bb77af

Please sign in to comment.