Skip to content

Commit 49f9f9c

Browse files
committed
refactor(cursor): extract shared fetchStripe helper to eliminate duplication
1 parent db37ad3 commit 49f9f9c

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

plugins/cursor/plugin.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
}
276276
}
277277

278-
function fetchStripeBalance(ctx, accessToken) {
278+
function fetchStripe(ctx, accessToken) {
279279
var session = buildSessionToken(ctx, accessToken)
280280
if (!session) {
281281
ctx.host.log.warn("stripe: cannot build session token")
@@ -291,38 +291,27 @@
291291
timeoutMs: 10000,
292292
})
293293
if (resp.status < 200 || resp.status >= 300) {
294-
ctx.host.log.warn("stripe balance returned status=" + resp.status)
294+
ctx.host.log.warn("stripe request returned status=" + resp.status)
295295
return null
296296
}
297-
var stripe = ctx.util.tryParseJson(resp.bodyText)
298-
if (!stripe) return null
299-
var customerBalanceCents = Number(stripe.customerBalance)
300-
if (!Number.isFinite(customerBalanceCents)) return null
301-
// Stripe stores customer credits as a negative balance.
302-
return customerBalanceCents < 0 ? Math.abs(customerBalanceCents) : 0
297+
return ctx.util.tryParseJson(resp.bodyText)
303298
} catch (e) {
304-
ctx.host.log.warn("stripe balance fetch failed: " + String(e))
299+
ctx.host.log.warn("stripe request failed: " + String(e))
305300
return null
306301
}
307302
}
308303

304+
function fetchStripeBalance(ctx, accessToken) {
305+
var stripe = fetchStripe(ctx, accessToken)
306+
if (!stripe) return null
307+
var customerBalanceCents = Number(stripe.customerBalance)
308+
if (!Number.isFinite(customerBalanceCents)) return null
309+
// Stripe stores customer credits as a negative balance.
310+
return customerBalanceCents < 0 ? Math.abs(customerBalanceCents) : 0
311+
}
312+
309313
function fetchStripeInfo(ctx, accessToken) {
310-
var session = buildSessionToken(ctx, accessToken)
311-
if (!session) return null
312-
try {
313-
var resp = ctx.util.request({
314-
method: "GET",
315-
url: STRIPE_URL,
316-
headers: {
317-
Cookie: "WorkosCursorSessionToken=" + session.sessionToken,
318-
},
319-
timeoutMs: 10000,
320-
})
321-
if (resp.status < 200 || resp.status >= 300) return null
322-
return ctx.util.tryParseJson(resp.bodyText)
323-
} catch (e) {
324-
return null
325-
}
314+
return fetchStripe(ctx, accessToken)
326315
}
327316

328317
function buildRequestBasedResult(ctx, accessToken, planName, unavailableMessage) {

0 commit comments

Comments
 (0)