@@ -36,6 +36,7 @@ export type ClientInterfaceOptions = {
3636 clientVersion : string ,
3737 // This is a function instead of a string because it might be different based on the environment (for example client vs server)
3838 getBaseUrl : ( ) => string ,
39+ getAnalyticsBaseUrl ?: ( ) => string ,
3940 extraRequestHeaders : Record < string , string > ,
4041 projectId : string ,
4142 prepareRequest ?: ( ) => Promise < void > ,
@@ -60,6 +61,10 @@ export class StackClientInterface {
6061 return this . options . getBaseUrl ( ) + "/api/v1" ;
6162 }
6263
64+ getAnalyticsApiUrl ( ) {
65+ return ( this . options . getAnalyticsBaseUrl ?? this . options . getBaseUrl ) ( ) + "/api/v1" ;
66+ }
67+
6368 public async runNetworkDiagnostics ( session ?: InternalSession | null , requestType ?: "client" | "server" | "admin" ) {
6469 if ( this . pendingNetworkDiagnostics ) {
6570 return await this . pendingNetworkDiagnostics ;
@@ -224,13 +229,14 @@ export class StackClientInterface {
224229 requestOptions : RequestInit ,
225230 session : InternalSession | null ,
226231 requestType : "client" | "server" | "admin" = "client" ,
232+ apiUrlOverride ?: string ,
227233 ) {
228234 session ??= this . createSession ( {
229235 refreshToken : null ,
230236 } ) ;
231237
232238 return await this . _networkRetry (
233- ( ) => this . sendClientRequestInner ( path , requestOptions , session ! , requestType ) ,
239+ ( ) => this . sendClientRequestInner ( path , requestOptions , session ! , requestType , apiUrlOverride ) ,
234240 session ,
235241 requestType ,
236242 ) ;
@@ -259,6 +265,8 @@ export class StackClientInterface {
259265 keepalive : options . keepalive ,
260266 } ,
261267 session ,
268+ "client" ,
269+ this . getAnalyticsApiUrl ( ) ,
262270 ) ;
263271 return Result . ok ( response ) ;
264272 } catch ( e ) {
@@ -281,6 +289,8 @@ export class StackClientInterface {
281289 keepalive : options . keepalive ,
282290 } ,
283291 session ,
292+ "client" ,
293+ this . getAnalyticsApiUrl ( ) ,
284294 ) ;
285295 return Result . ok ( response ) ;
286296 } catch ( e ) {
@@ -319,6 +329,7 @@ export class StackClientInterface {
319329 options : RequestInit ,
320330 session : InternalSession ,
321331 requestType : "client" | "server" | "admin" ,
332+ apiUrlOverride ?: string ,
322333 ) : Promise < Result < Response & {
323334 usedTokens : {
324335 accessToken : AccessToken ,
@@ -353,7 +364,7 @@ export class StackClientInterface {
353364 // all requests should be dynamic to prevent Next.js caching
354365 await this . options . prepareRequest ?.( ) ;
355366
356- let url = this . getApiUrl ( ) + path ;
367+ let url = ( apiUrlOverride ?? this . getApiUrl ( ) ) + path ;
357368 if ( url . endsWith ( "/" ) ) {
358369 url = url . slice ( 0 , - 1 ) ;
359370 }
0 commit comments