@@ -205,7 +205,7 @@ export function isV2Path(apiPath: string): boolean {
205205// HTTP-based list / retrieve builders (no Stripe SDK dependency)
206206// ---------------------------------------------------------------------------
207207
208- const STRIPE_API_BASE = 'https://api.stripe.com'
208+ const DEFAULT_STRIPE_API_BASE = 'https://api.stripe.com'
209209
210210function authHeaders ( apiKey : string ) : Record < string , string > {
211211 return { Authorization : `Bearer ${ apiKey } ` }
@@ -215,7 +215,14 @@ function authHeaders(apiKey: string): Record<string, string> {
215215 * Build a callable list function that hits the Stripe HTTP API directly.
216216 * Supports both v1 (has_more pagination) and v2 (next_page_url pagination).
217217 */
218- export function buildListFn ( apiKey : string , apiPath : string , apiVersion ?: string ) : ListFn {
218+ export function buildListFn (
219+ apiKey : string ,
220+ apiPath : string ,
221+ apiVersion ?: string ,
222+ baseUrl ?: string
223+ ) : ListFn {
224+ const base = baseUrl ?? DEFAULT_STRIPE_API_BASE
225+
219226 if ( isV2Path ( apiPath ) ) {
220227 return async ( params ) => {
221228 const qs = new URLSearchParams ( )
@@ -225,7 +232,7 @@ export function buildListFn(apiKey: string, apiPath: string, apiVersion?: string
225232 const headers = authHeaders ( apiKey )
226233 if ( apiVersion ) headers [ 'Stripe-Version' ] = apiVersion
227234
228- const response = await fetch ( `${ STRIPE_API_BASE } ${ apiPath } ?${ qs } ` , { headers } )
235+ const response = await fetch ( `${ base } ${ apiPath } ?${ qs } ` , { headers } )
229236 const body = ( await response . json ( ) ) as {
230237 data : unknown [ ]
231238 next_page_url ?: string | null
@@ -246,7 +253,7 @@ export function buildListFn(apiKey: string, apiPath: string, apiVersion?: string
246253 }
247254 }
248255
249- const response = await fetch ( `${ STRIPE_API_BASE } ${ apiPath } ?${ qs } ` , {
256+ const response = await fetch ( `${ base } ${ apiPath } ?${ qs } ` , {
250257 headers : authHeaders ( apiKey ) ,
251258 } )
252259 const body = ( await response . json ( ) ) as { data : unknown [ ] ; has_more : boolean }
@@ -257,19 +264,26 @@ export function buildListFn(apiKey: string, apiPath: string, apiVersion?: string
257264/**
258265 * Build a callable retrieve function that hits the Stripe HTTP API directly.
259266 */
260- export function buildRetrieveFn ( apiKey : string , apiPath : string , apiVersion ?: string ) : RetrieveFn {
267+ export function buildRetrieveFn (
268+ apiKey : string ,
269+ apiPath : string ,
270+ apiVersion ?: string ,
271+ baseUrl ?: string
272+ ) : RetrieveFn {
273+ const base = baseUrl ?? DEFAULT_STRIPE_API_BASE
274+
261275 if ( isV2Path ( apiPath ) ) {
262276 return async ( id ) => {
263277 const headers = authHeaders ( apiKey )
264278 if ( apiVersion ) headers [ 'Stripe-Version' ] = apiVersion
265279
266- const response = await fetch ( `${ STRIPE_API_BASE } ${ apiPath } /${ id } ` , { headers } )
280+ const response = await fetch ( `${ base } ${ apiPath } /${ id } ` , { headers } )
267281 return await response . json ( )
268282 }
269283 }
270284
271285 return async ( id ) => {
272- const response = await fetch ( `${ STRIPE_API_BASE } ${ apiPath } /${ id } ` , {
286+ const response = await fetch ( `${ base } ${ apiPath } /${ id } ` , {
273287 headers : authHeaders ( apiKey ) ,
274288 } )
275289 return await response . json ( )
@@ -279,7 +293,7 @@ export function buildRetrieveFn(apiKey: string, apiPath: string, apiVersion?: st
279293function extractPageToken ( nextPageUrl : string | null | undefined ) : string | undefined {
280294 if ( ! nextPageUrl ) return undefined
281295 try {
282- const url = new URL ( nextPageUrl , STRIPE_API_BASE )
296+ const url = new URL ( nextPageUrl , DEFAULT_STRIPE_API_BASE )
283297 return url . searchParams . get ( 'page' ) ?? undefined
284298 } catch {
285299 return undefined
0 commit comments