Compute Services

Building IBM Cloud Functions with TypeScript

Share this post:

Building IBM Cloud Functions with Typescript starter kit

When learning new technology, it’s often helpful to begin by exploring a starter kit. Beyond the proverbial “Hello World,” starter kits are pre-built to focus on a core technology and support libraries and packaging. This helps bring everything together into an opinionated and functioning example.

There are, however, so many development options that no single starter kit will satisfy everyone. And after not finding one for Cloud Functions and Typescript, I’ve created my own. Consider using the starter kit for your next project for the following reasons:

  1. Many serverless examples use native Javascript, but as projects grow in size, better type checking and code modularity are needed. This is why Typescript was chosen for the starter. Consider the Cloud Functions entry point main(params). Is the params.id property a string or a number? With Typescript, you can make this explicit and leverage ES6 syntactic sugar and range of additional programming constructs.
  2. Unit testing might be new to you. Typescript might also be new to you. And if both are new to you, that’s OK! Provided in the starter are examples of testing Cloud Functions using Mocha. Tests are executed using npm run test, which uses the ts-node utility to quickly test without having to build.
  3. The starter kit uses webpack to organize code. By using webpack, you can write multiple actions that are contained in a single project. And any external dependencies used by your actions will be packaged for you automatically. A working webpack.config.js is provided and sample actions declare (global).main = main; to make them compatible. Additionally, the starter will exclude any natively supported NPM modules from the final bundle.
  4. Finally, being declarative about deployment doesn’t mean writing complicated shell scripts—simply use wskdeploy. A sample manifest.yaml has been structured to work with webpack, and it deploys two APIs. See the wskdeploy documentation for more deployment options.

Ready to use the starter? Head over to GitHub and download the code.

IBM Offering Manager

More Compute Services stories
January 14, 2019

Knative Monitoring with Sysdig on IBM Cloud

Learn how to monitor your Knative Kubernetes cluster using IBM Cloud Monitoring with Sysdig.

Continue reading

January 14, 2019

Predicting the future with Monte Carlo simulations over IBM Cloud Functions

We’re going to demonstrate how IBM Cloud Functions can provide a phenomenal boost to Monte Carlo simulations, allowing them to execute in a fraction of the time as compared to a conventional execution model.

Continue reading

January 10, 2019

Knative Log Analysis with LogDNA on IBM Cloud

We're going to show you how to use the IBM Log Analysis with LogDNA service to configure cluster-level logging for an app named Knative-node-app published in IBM Cloud Kubernetes Service.

Continue reading