Skip to content

Commit 53cd9e6

Browse files
Yostratonyxiao
authored andcommitted
rebase cleanup
1 parent 92f80c1 commit 53cd9e6

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

packages/openapi/listFnResolver.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

210210
function 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
279293
function 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

packages/source-stripe/src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ const source = {
117117
const resolved = await resolveOpenApiSpec({
118118
apiVersion: config.api_version ?? '2020-08-27',
119119
})
120-
const registry = buildResourceRegistry(resolved.spec, config.api_key, resolved.apiVersion)
120+
const registry = buildResourceRegistry(
121+
resolved.spec,
122+
config.api_key,
123+
resolved.apiVersion,
124+
config.base_url
125+
)
121126
try {
122127
const parser = new SpecParser()
123128
const parsed = parser.parse(resolved.spec, {
@@ -172,7 +177,12 @@ const source = {
172177
const resolved = await resolveOpenApiSpec({
173178
apiVersion: config.api_version ?? '2020-08-27',
174179
})
175-
const registry = buildResourceRegistry(resolved.spec, config.api_key, resolved.apiVersion)
180+
const registry = buildResourceRegistry(
181+
resolved.spec,
182+
config.api_key,
183+
resolved.apiVersion,
184+
config.base_url
185+
)
176186
const streamNames = new Set(catalog.streams.map((s) => s.stream.name))
177187

178188
// Event-driven mode: iterate over incoming webhook inputs

packages/source-stripe/src/resourceRegistry.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export const RESOURCE_TABLE_NAME_MAP: Record<string, string> = Object.fromEntrie
6464
export function buildResourceRegistry(
6565
spec: OpenApiSpec,
6666
apiKey: string,
67-
apiVersion?: string
67+
apiVersion?: string,
68+
baseUrl?: string
6869
): Record<string, ResourceConfig> {
6970
const endpoints = discoverListEndpoints(spec)
7071
const nestedEndpoints = discoverNestedEndpoints(spec, endpoints)
@@ -91,8 +92,8 @@ export function buildResourceRegistry(
9192
supportsLimit: endpoint.supportsLimit,
9293
sync: true,
9394
dependencies: [],
94-
listFn: buildListFn(apiKey, endpoint.apiPath, apiVersion),
95-
retrieveFn: buildRetrieveFn(apiKey, endpoint.apiPath, apiVersion),
95+
listFn: buildListFn(apiKey, endpoint.apiPath, apiVersion, baseUrl),
96+
retrieveFn: buildRetrieveFn(apiKey, endpoint.apiPath, apiVersion, baseUrl),
9697
nestedResources: children.length > 0 ? children : undefined,
9798
}
9899
registry[tableName] = config

0 commit comments

Comments
 (0)