Restify project seed with typescript and unit test

Sul Aga photo
0
Last Updated
Feb 05, 2017
Source Code

Introduction

This article explains the restify-typescript-seed project. What is it contain and how to utilise it to your needs.

Background

I have created this seed project because I couldn't find a simple project that has restify, TypeScript and unit test in one place so I thought it would help whoever is looking to have a clean and simple project to start with restify and TypeScript.

Project structure

Image 1 shows the project structure.

Project Structure

The project features an app folder and test folder.

The app folder contains the following sub-folders:

  • routes: contains files which defines the routes that your API is exposing. Here is an example. As you can see from the code snippet below. I am exposing a GET route which will be handled by sampleRouteController

    import * as restify from 'restify';
    import sampleRouteController from '../controllers/SampleRouteController'
    
    function sampleRoute(api:restify.Server) {
      let routeCtrl = new sampleRouteController();
      api.get('/api/ping', routeCtrl.get);
    }
    
    module.exports.routes = sampleRoute;
  • controllers: Contains classes that can handle routes exposed in the routes folder. Here is an example. The get method is used in the previous code snippet to handle GET route.

    import * as restify from 'restify';
    import { logger } from '../services/logger';
    
    export default class SampleRouteController {
      public get(req: restify.Request, res: restify.Response, next: restify.Next) {
        logger.info('accessing ping route');
        res.json(200, 'pong');
        return next();
      }
    }
  • config: Contains the config file for the project. It can include other config files depending on the current process.env.NODE_ENV for example.

  • services: This folder contains code that is used by all the application. For instance it includes logger but it can also include other services like auditing. The Logger service has an info and error methods. The info method will output to the console while the error method will write to a file called error.log which will be created on the root.

TypeScript

You can compile the TypeScript files using TypeScript compiler tsc or using npm task compile. You need to be in root folder for this to work

# Using npm task
npm run compile

# using TypeScript compiler (tsc) which is installed globally
tsc

# using TypeScript compiler (tsc) which is installed for this project only
./node_modules/.bin/tsc

The resulted *.js and *.js.map files will be generated in the same place as their *.ts counterpart. For development purposes you can hide these files. I am using Visual Studio Code which has a feature to exclude files you don't want to see in the project tree like generated files. The source code include this settings in a folder called .vscode. If you load the project in Visual Studio Code then the settings in this folder will be loaded.

The custom types are included in the types.d.ts file which is located in the app folder. The type definition files for the libraries are now loaded using npm. tslint.json file is also included in the root.

Unit and Integration Tests

The seed project contains unit and integration tests to give you a head start on creating your own. Tests are utilising Mocha as a test runner, sinon as a mocking library and chai for assertions.

Tests are using supertest to make API calls. The difference between unit and integration tests is that in unit test I am stubbing the Logger service using sinon whereas in the integration test I am using the real implementation of Logger service.

Conclusion

This restify-typescript-seed project gives a simple and clear view on how to start http service with restify and TypeScript.

Comments