Skip to main content

Blog

Stripe SDKをTypeScriptであつかう

Posted over 2 years ago
この記事をシェア:

こういうSDKを扱うのがメインのものは可能な限りTypeScript化したいフレンズです。

準備

まずはもろもろのセットアップから。

$ npm init -y
$ yarn add -D typescript stripe @types/stripe
$ ./node_modules/.bin/tsc --init

Stripe SDKは型ファイルがないので、@types/stripeが必要です。

index.ts

とりあえずお手軽版で作ります。

import Stripe from 'stripe';
const stripe = new Stripe("sk_test_xxxxx")

stripe.invoices.list()
  .then((invoices: Stripe.IList<Stripe.invoices.IInvoice>) => {
    console.log(invoices)
    return stripe.invoices.retrieve('in_xxxxx')
  }).then((data: Stripe.invoices.IInvoice) => {
    console.log(data)
  })

動かし方

ビルドしてから実行です。&&の後ろはビルドされた出力ファイル名に適宜変更してください。

$ ./node_modules/.bin/tsc && node index.js

ポイント

  • @typesが必要
  • list系メソッドはStripe.IList<Stripe.XXXX>という書き方になる
  • 結構extendされているので、引数については公式ドキュメント見たほうが楽。

Tools to Support Stripe Development

We provide helpful tools to extend the Stripe Dashboard and streamline development and testing.

View All Tools

Support This Project

If you find this content helpful, consider supporting the project through GitHub Sponsors. Your support helps maintain and improve these tools.