@@ -38,6 +38,24 @@ import type {
38
38
SDKModule ,
39
39
} from './sdk-types' ;
40
40
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
+
41
59
export class ClientSDK {
42
60
private stateManager : StateManager ;
43
61
private coreSdk : CoreSDK ;
@@ -79,6 +97,7 @@ export class ClientSDK {
79
97
timeout : config . timeout ,
80
98
events : config . events ,
81
99
navbarItems : config . navbarItems ,
100
+ getAccessToken : config . getAccessToken ,
82
101
} ;
83
102
const client = new ClientSDK ( coreConfig ) ;
84
103
// Run the handshake. If this throws, the promise is rejected.
@@ -145,7 +164,10 @@ export class ClientSDK {
145
164
return Array . from ( this . modules . keys ( ) ) ;
146
165
}
147
166
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 } {
149
171
let request = this . coreSdk . request . bind ( this . coreSdk ) ; // Bind to preserve context
150
172
let operation = keyOrOperationKey + ':' + type ; // Default operation name
151
173
@@ -355,7 +377,62 @@ export class ClientSDK {
355
377
}
356
378
}
357
379
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 > {
359
436
const url = new URL ( input . url ) ;
360
437
const path = url . pathname + url . search + url . hash ;
361
438
@@ -383,6 +460,21 @@ export class ClientSDK {
383
460
} ) ;
384
461
}
385
462
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
+
386
478
async logout ( ) : Promise < void > {
387
479
await this . coreSdk . request ( 'host.logout' ) ;
388
480
}
0 commit comments