stripe / stripe-node

Node.js library for the Stripe API.
https://stripe.com
MIT License
3.81k stars 741 forks source link

feat: Modular API for edge runtimes #2156

Open brettwillis opened 1 month ago

brettwillis commented 1 month ago

Is your feature request related to a problem? Please describe.

In "edge runtimes" such as Cloudflare Workers, Vercel Edge Functions, the size of the application code comes at a premium (as little as 1MB of JS). In such environments it is beneficial to have modular exports/entrypoints that allow importing only the code that is required so that bundlers can remove unused code.

This concept is also generally useful for reducing the startup time of any app, not just edge runtimes.

The Stripe SDK has many modules so it's not small.

Describe the solution you'd like

In addition to the existing API, provide modular exports as an alternative for importing only the required functionality.

This could look like:

import Stripe from 'stripe/core';
import PaymentIntents from 'stripe/payment-intents';

const stripe = new Stripe({...});

const paymentIntents = new PaymentIntents(stripe);

await paymentIntents.retrieve(...);

I notice the SDK is already structured in much the same way as this behind the scenes. But there a a few issues with the above proposed API... eg an app would potentially instantiate many instances of the modules around it's codebase. There are a few ways to improve this but needs careful consideration about the API.

So even better yet, something like this:

import Stripe from 'stripe/core';
import { retrievePaymentIntent } from 'stripe/payment-intents';

const stripe = new Stripe({...});

await retrievePaymentIntent(stripe, ...);

Describe alternatives you've considered

No response

Additional context

No response

helenye-stripe commented 1 month ago

Hi @brettwillis. Thanks for bringing this up! We plan to investigate ways to make the SDK less resource-intensive in the future. We have also thought about using tree-shaking to do this, see https://github.com/stripe/stripe-node/issues/1693. I'll keep this issue open and update once there is progress here.