Skip to content

Commit d76c8ea

Browse files
author
Mohammod Al Amin Ashik
committed
add ep direct request support
1 parent ea2f21d commit d76c8ea

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

packages/client/src/client.ts

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ import type {
3838
SDKModule,
3939
} from './sdk-types';
4040

41+
// Constants
42+
const DEFAULT_HEADER = { 'sc-resource': 'marketplace' };
43+
const EDGE_PLATFORM_PROXY_URL = 'https://edge-platform.sitecorecloud.io';
44+
45+
/**
46+
* Gets the Edge Platform Proxy URL, preferring EDGE_PLATFORM_PROXY_URL environment variable
47+
* over the default production URL
48+
*/
49+
function getEdgePlatformProxyUrl(): string {
50+
// Prefer environment variable EDGE_PLATFORM_PROXY_URL
51+
if ((window as any).env && (window as any).env.VITE_EDGE_PLATFORM_PROXY_URL) {
52+
return (window as any).env.VITE_EDGE_PLATFORM_PROXY_URL;
53+
}
54+
55+
// Fallback to default production URL when environment variable is not set
56+
return EDGE_PLATFORM_PROXY_URL;
57+
}
58+
4159
export class ClientSDK {
4260
private stateManager: StateManager;
4361
private coreSdk: CoreSDK;
@@ -79,6 +97,7 @@ export class ClientSDK {
7997
timeout: config.timeout,
8098
events: config.events,
8199
navbarItems: config.navbarItems,
100+
getAccessToken: config.getAccessToken,
82101
};
83102
const client = new ClientSDK(coreConfig);
84103
// Run the handshake. If this throws, the promise is rejected.
@@ -145,7 +164,10 @@ export class ClientSDK {
145164
return Array.from(this.modules.keys());
146165
}
147166

148-
private resolveOperation(keyOrOperationKey: string, type: 'query' | 'mutation'): { request: Function; operation: string } {
167+
private resolveOperation(
168+
keyOrOperationKey: string,
169+
type: 'query' | 'mutation',
170+
): { request: Function; operation: string } {
149171
let request = this.coreSdk.request.bind(this.coreSdk); // Bind to preserve context
150172
let operation = keyOrOperationKey + ':' + type; // Default operation name
151173

@@ -355,7 +377,62 @@ export class ClientSDK {
355377
}
356378
}
357379

358-
private async _fetch(input: globalThis.Request): Promise<Response> {
380+
/**
381+
* Makes a direct authenticated fetch request using the configured getAccessToken callback.
382+
* Replicates the behavior of host's handleGenericApiRequest method.
383+
* @param input - The original request object
384+
* @returns Promise that resolves to the fetch response
385+
*/
386+
private async _fetchDirect(input: globalThis.Request): Promise<Response> {
387+
const token = await this.config.getAccessToken!();
388+
const url = new URL(input.url);
389+
const path = url.pathname + url.search + url.hash;
390+
391+
// Get the edge platform proxy URL, preferring EDGE_PLATFORM_PROXY_URL environment variable
392+
const edgePlatformProxyUrl = getEdgePlatformProxyUrl();
393+
394+
// Build the full URL using edge platform proxy
395+
const fullUrl = edgePlatformProxyUrl + (path.startsWith('/') ? path : '/' + path);
396+
397+
// Start with marketplace default header and merge with existing headers
398+
const headers: Record<string, string> = { ...DEFAULT_HEADER };
399+
400+
// Add existing headers from the request
401+
if (input.headers) {
402+
input.headers.forEach((value, key) => {
403+
headers[key] = value;
404+
});
405+
}
406+
407+
// Add authentication header
408+
headers['Authorization'] = `Bearer ${token}`;
409+
410+
// Add context ID header if available (extracted from existing headers)
411+
const contextId = input.headers?.get('x-sitecore-contextid');
412+
if (contextId) {
413+
headers['x-sitecore-contextid'] = contextId;
414+
}
415+
416+
// Make direct fetch request to edge platform proxy
417+
return fetch(fullUrl, {
418+
method: input.method,
419+
headers,
420+
body: input.body,
421+
mode: input.mode,
422+
credentials: input.credentials,
423+
cache: input.cache,
424+
redirect: input.redirect,
425+
referrer: input.referrer,
426+
integrity: input.integrity,
427+
});
428+
}
429+
430+
/**
431+
* Makes a fetch request through the host proxy using the host.request event.
432+
* @param input - The original request object
433+
* @returns Promise that resolves to the proxied response
434+
*/
435+
private async _fetchViaHost(input: globalThis.Request): Promise<Response> {
359436
const url = new URL(input.url);
360437
const path = url.pathname + url.search + url.hash;
361438

@@ -383,6 +460,21 @@ export class ClientSDK {
383460
});
384461
}
385462

463+
/**
464+
* Do not remove this method. It is used by the XMC SDK to make requests to the host.
465+
* When getAccessToken is configured, makes direct authenticated requests.
466+
* Otherwise, uses host.request event.
467+
*/
468+
private async _fetch(input: globalThis.Request): Promise<Response> {
469+
// If getAccessToken is configured, use direct authenticated request
470+
if (this.config.getAccessToken) {
471+
return this._fetchDirect(input);
472+
}
473+
474+
// Otherwise, use host proxy method
475+
return this._fetchViaHost(input);
476+
}
477+
386478
async logout(): Promise<void> {
387479
await this.coreSdk.request('host.logout');
388480
}

packages/client/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export interface ClientSDKConfig extends CoreSDKConfig {
8181
onPageContextUpdate?: (data: any) => void;
8282
};
8383
navbarItems?: NavbarItemsProps;
84+
/** Callback function to retrieve access token for authenticated requests */
85+
getAccessToken?: () => string | Promise<string>;
8486
}
8587

8688
export type ClientSDKInitConfig = {
@@ -90,6 +92,8 @@ export type ClientSDKInitConfig = {
9092
modules?: SDKModule[];
9193
events?: ClientSDKConfig['events'];
9294
navbarItems?: NavbarItemsProps;
95+
/** Callback function to retrieve access token for authenticated requests */
96+
getAccessToken?: () => string | Promise<string>;
9397
};
9498

9599
export { UserInfo, ApplicationContext } from '@sitecore-marketplace-sdk/core';

0 commit comments

Comments
 (0)