stripe / stripe-node

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

Export object interfaces from Typescript definitions #1502

Open volkanunsal opened 1 year ago

volkanunsal commented 1 year ago

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

I need to be able to reference object interfaces defined inside the Stripe namespace, but I'm unable to because these interfaces aren't exported.

// File generated from our OpenAPI spec

declare module 'stripe' {
  namespace Stripe {
    /**
     * The Subscription object.
     */
    interface Subscription {

Describe the solution you'd like

Please modify your generator to add export in front of interface declarations.

// File generated from our OpenAPI spec

declare module 'stripe' {
  namespace Stripe {
    /**
     * The Subscription object.
     */
    export interface Subscription {

Describe alternatives you've considered

I currently have to copy and paste the interfaces into my app.

Additional context

No response

kamil-stripe commented 1 year ago

Hi @volkanunsal! Thank you for your report. We are going to investigate exporting interface definitions from the d.ts files. To unblock you, could you try prefixing the interface names with Stripe namespace? This is our default export from index.d.t.s file.

const stripe = new Stripe("sk_123");

const subscriptions: Stripe.ApiList<Stripe.Subscription> = await stripe.subscriptions.list();
const subscription: Stripe.Subscription = subscriptions.data[0];
console.log(subscription.id);
volkanunsal commented 1 year ago

Strange. I tried that but it didn’t work for me.

volkanunsal commented 1 year ago

Are you using some compiler magic? Do I need to add something to tsconfig.json to make that work?

volkanunsal commented 1 year ago

Some updates. I thought it wasn't working because the types aren't populated when I start typing the type name, but I can actually use them by reference, even though intellisense doesn't know they're there. That's good enough for me.

kamil-stripe commented 1 year ago

No compiler magic. When I tested I used this tsconfig.json. I think moduleResolution may be related to the differences in the experience.

{  
    "compilerOptions": {    
        "target": "ES2022",
        "module": "ES2022",
        "moduleResolution": "node"
    },
    "include": ["index.ts"],
    "exclude": [
        "node_modules"
    ]
}
volkanunsal commented 1 year ago

I still don't see the types in intellisense, but that's ok. It only shows exported types or classes.

Screen Shot 2022-08-08 at 9 05 29 PM
ryantbrown commented 1 year ago

I can't seem to use the types either. Exporting the interfaces directly seems like a no-brainer and it was most libraries do. Also, VSCode has major issues when trying to work with the entire Stripe namespace. It grinds to a halt.

richardm-stripe commented 1 year ago

Thanks for writing in @ryantbrown and continuing to write in @volkanunsal. This is an active area of work for the team right now. in 2023. We're working on a fairly major redesign of how the library is exported/imported and as part of that we will look at this specifically and I'm re-opening the ticket accordingly.

alex-dixon commented 11 months ago

The readme says don't use import * as but it solved this issue for me.

import * as StripeAPI from 'stripe'

const Stripe = Context.Tag<StripeAPI.Stripe>('stripe')

const makeStripeLayer = (stripeAPIKey: string) =>
  Layer.succeed(
    Stripe,
    new StripeAPI.Stripe(stripeAPIKey, {
      apiVersion: '2022-11-15',
      typescript: true, // "Optionally indicate that you are using TypeScript. This currently has no runtime effect other than adding "TypeScript" to your user-agent."
    })
  )

const listPayouts = (args: StripeAPI.Stripe.PayoutListParams) =>
  Effect.flatMap(Stripe, (stripe) =>
    Effect.tryPromise(() => stripe.payouts.list(args))
  )
decoursin commented 1 month ago

Is this still in the works?