fix: Lazy loaded Stripe proxy to avoid crash on BillingService instantiation (#25400)
This commit is contained in:
@@ -6,9 +6,33 @@ import { DI_TOKENS } from "../tokens";
|
||||
|
||||
export const stripeClientModule = createModule();
|
||||
const token = DI_TOKENS.STRIPE_CLIENT;
|
||||
|
||||
/**
|
||||
* Creates a deep proxy that only throws when a Stripe function is invoked.
|
||||
* Property access (e.g. stripe.customers, stripe.customers.create) returns another proxy so that
|
||||
* code doing capability checks (typeof stripe.customers?.create === "function") does not immediately throw.
|
||||
*/
|
||||
function createLazyThrowingStripeStub(error: Error): Stripe {
|
||||
// Using unknown instead of any to satisfy lint while still allowing broad proxying.
|
||||
function makeDeepProxy(): unknown {
|
||||
return new Proxy(function () {}, {
|
||||
get() {
|
||||
// Return another proxy for any nested property (resources/methods)
|
||||
return makeDeepProxy();
|
||||
},
|
||||
apply() {
|
||||
throw error; // Throw only when a function is actually called
|
||||
},
|
||||
});
|
||||
}
|
||||
return makeDeepProxy() as Stripe;
|
||||
}
|
||||
|
||||
stripeClientModule.bind(token).toFactory(() => {
|
||||
if (!process.env.STRIPE_PRIVATE_KEY) {
|
||||
throw new Error("STRIPE_PRIVATE_KEY is not set");
|
||||
const error = new Error("STRIPE_PRIVATE_KEY is not set");
|
||||
// Inject a stub that defers throwing until a method invocation
|
||||
return createLazyThrowingStripeStub(error);
|
||||
}
|
||||
|
||||
return new Stripe(process.env.STRIPE_PRIVATE_KEY!, {
|
||||
|
||||
Reference in New Issue
Block a user