This makes going fully serverless painful, so far I had to implement the client code for Mailgun, BigQuery and Stripe myself. Each of these has a great SDK for Node, but unfortunately we can’t use those. This is a big downside of going fully serverless: you end up implementing these clients or finding workarounds instead of building your own application.
Fortunately, it turns out that for Stripe it’s easy to write a type-safe client without too much code. This is where Typescript really shines.
First we install the Stripe Node client, we will only use its typings so we can install it as a dev dependency.
npm install --save-dev stripe
Also we’ll be using the
qs NPM package, we use it to turn arbitrarily nested JSON objects into query strings.
npm install --save qs
Writing our own client#
Below is all there is to the client. It currently only implements a single operation (for creating customers), but it’s trivial to add more.
Now we can call the above code like this to create a new customer:
The nice thing is that as we are using Typescript this will actually be typechecked as we are using the typings from the official Stripe NPM package. What that means is that your code-editor can autocomplete the fields, and if you add fields that are invalid the Typescript compiler will tell you.