stripe-node icon indicating copy to clipboard operation
stripe-node copied to clipboard

Helper method to get item ids

Open nathanhleung opened this issue 2 years ago • 2 comments
trafficstars

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

I'm often writing the same TypeScript code to narrow the types returned by the API to extract item IDs. Example:

const price = await stripe.prices.retrieve(priceId);
const { product } = price;

// this part
const productId = (typeof product === 'string' ? product : product.id);

Describe the solution you'd like

It would be nice if this library included a helper method for this, e.g.

function getId(stripeObject: { id: string } | string) {
  return typeof stripeObject === 'string' ? stripeObject : stripeObject.id;
}

Then I could write

const price = await stripe.prices.retrieve(priceId);
const { product } = price;

const productId = getId(product);

Describe alternatives you've considered

I could just write this function for use in my personal projects, but if this is a common problem/use-case I think including it in the library would be good so others could use it too

Additional context

No response

nathanhleung avatar May 18 '23 04:05 nathanhleung

Hi @nathanhleung, thank you for your suggestion. I'll make it as future to gauge the interest in a helper like this.

pakrym-stripe avatar May 18 '23 18:05 pakrym-stripe

@pakrym-stripe Greetings, I would like to pick this up.

Sadaf-A avatar Aug 23 '23 07:08 Sadaf-A