I just discovered CLAUDIA.JS  and I think this toolkit is absolutely awesome. The ability to create/update/deploy AWS lambdas served by API Gateway so easily is amazing. You should check it out immediately if you’re new to it as I was.

Now, I love TypeScript and would like to use it everywhere. This series will go through my findings transpiling TypeScript code and uploading it to AWS. In this first part, we focus on the transpiling process. Uploading to AWS will be covered in the next post.

The first thing to do is to write a Typescript declaration file to build the bridge between the claudia-api-builder module and Typescript. How do we proceed ? Well start by checking if something has been already published:

npm install –save-dev @types/claudia-api-builder

This ends with an error:

npm ERR! code E404
npm ERR! 404 Not Found: @types/claudia-api-builder@latest

So we’re good to go.

Next, examine the existing module structure and the consumer code to choose a template to start with. The structure of the module is like this:

module.exports = function ApiBuilder(options) {...}

The consumer code is like the following:

var ApiBuilder = require('claudia-api-builder'),
api
= new ApiBuilder();

This matches with the following statement in the documentation:

Use module-class.d.ts if your module can be constructed using new

So now we have the template. Copy/Paste it in a new index.d.ts file that is located in a ‘@types/claudia-api-builder’ subfolder.

Following is a minimal file. Disclaimer : It is far from complete and quality ready for publishing.

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] 

/*~ This is the module template file for class modules.
 *~ You should rename it to index.d.ts and place it in a folder with the same name as the module.
 *~ For example, if you were writing a file for "super-greeter", this
 *~ file should be 'super-greeter/index.d.ts'
 */

/*~ Note that ES6 modules cannot directly export class objects.
 *~ This file should be imported using the CommonJS-style:
 *~   import x = require('someLibrary');
 *~
 *~ Refer to the documentation to understand common
 *~ workarounds for this limitation of ES6 modules.
 */

/*~ If this module is a UMD module that exposes a global variable 'myClassLib' when
 *~ loaded outside a module loader environment, declare that global here.
 *~ Otherwise, delete this declaration.
 */
// export as namespace myClassLib;

declare module "claudia-api-builder" {

    /*~ This declaration specifies that the class constructor function
     *~ is the exported object from the file
     */
    export = ApiBuilder;

    /*~ Write your module's methods and properties in this class */
    class ApiBuilder {
        constructor(options?: any);

        get(uri: string, callback: Function): void;
    }

}

We’ll review the various parts of this file:

  1. declare module: A declare modifier is mandatory in a .d.ts file. In our case we declare a module. This will allow us to require it in the .ts source code.
  2. export: As the comment states, this allow the consuming code to use the new keyword.
  3. class: The class should be complete with all available methods and required types. Otherwise it defeats the purpose of typescript that is to have a type system that tells you when there is wrong code a design/compile time.

Now we’re ready to take the sample from the tutorial and convert it to typescript. If you’ve dowloaded the sample, rename the file with a .ts extension. Otherwise, create an app.ts file and copy/paste the javascript code.

In any case, you’ll have to refactor the code until all errors are gone.

The result could be like the following:

import ApiBuilder = require('claudia-api-builder');

const api = new ApiBuilder();

api.get('/hello', function () {
    return 'hello world';
});

export = api;

The first thing to note is that the module is required as explained in the documentation.

The typescript code to define the HTTP GET method is left almost unchanged. Just follow the linter notifications.

Finally the object is exported as a module with:

export = api;

This is the equivalent of the javascript code:

module.exports = api;

In the next part of this series, we’ll learn and adapt from this tutorial to transpile code with the Typescript compiler. Stay tuned.

One thought on “Writing Typescript Lambda deployed with CLAUDIA.JS part 1

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s