1.0.0 • Published 7 years ago

gacrux-lib-restify v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

gacrux-lib-restify

Travis Codecov

restify manager that uses inversify under the hood

For more information about this project, see the main repo

Installation

Currently this is not available on npm. Won take long until it gets there :)

Usage

Before you can boot up your restify server, you need to do some steps.

Step 1: Create a Controller

Every controller class needs the @Controller decorator. Inside the decorator, you need to set the URL. So if you want the class to answer the request on /foo you need to insert /foo. For the response, you need to return an object that matches the Response object from this library. The response object is either an object or a promise. Per default the response status is 200. The data field is required.

This controller would answer on the route: GET /foo with the status 200 and the body containing bar. When using POST the server would answer with the status 200, and the body { 'foo': 'bar' }.

import { Controller, Get, Post, Response } from 'gacrux-lib-restify';
import { injectable } from 'inversify';
import { Rquest } from 'restify';

@injectable()
@Controller('/foo')
export class FooController {

  @Get('/')
  public get(): Response {
    return {
      data: 'bar',
      json: false,
      status: 200
    }
  }

  @Post('/')
  public post(request: Request): Promise<Response> {
    return new Promise<Response>((resolve, reject) => {
      // do something with the request
      // like saving it in the database
      resolve({
        data: {
          foo: 'bar'
        }
      });
    });
  }
}

Step 2: Register the Controller

There two ways. The first method is to self-register the controller in the container. The second method is to add the controller over the manager class. In the end, both methods do exactly the same.

Method 1. The manual way

import { Container, interfaces } from 'inversify';
import { CONTROLLER, ControllerInterface, RestifyManager } from 'gacrux-lib-restify';
import { FooController } from './foo';

const container: interfaces.Container = new Container();

container.bind<ControllerInterface>(CONTROLLER).to(FooController).whenTargetNamed('MyTag');

const restifyManager: RestifyManager = new RestifyManager(container, { port: 8080 });

Method 2. Using the manager class

Using this method the tag is optional. Per default it will take the class name.

import { Container, interfaces } from 'inversify';
import { FooController } from './foo';
import { RestifyManager } from 'gacrux-lib-restify';

const container: interfaces.Container = new Container();
const restifyManager: RestifyManager = new RestifyManager(container, { port: 8080 });

restifyManager.registerController(FooController, 'MyTag');

Step 3: Starting the server

The last step is to start the server.

import { Container, interfaces } from 'inversify';
import { FooController } from './foo';
import { RestifyManager } from 'gacrux-lib-restify';

const container: interfaces.Container = new Container();
const restifyManager: RestifyManager = new RestifyManager(container, { port: 8080 });

restifyManager.registerController(FooController);
restifyManager.init()
.then(() => {
  console.log('Server up and running');
});

API

RestifyManager

This class is used for starting the restify server, adding controllers and shutting down the server.

new RestifyManager(container: Container, options: RestifyOptions)

Creates a new instance of the RestifyManager.

  • container -> inversify container. The library will add all controllers to this container
  • options -> currently only the port is in here. See the examples above to see how it works

.init(): Promise<RestifyManager>

Starts the restify server. Listens to the port, given in the options. Per default the middleware BodyParser is initialized.

Important note: It is recommended to add all controllers BEFORE you start the server.

Gives back a Promise containing an instance of the class.

.cleanup(): Promise<RestifyManager>

Shutsdown the server.

Gives back a Promise containing an instance of the class.

.registerController(clazz [, tag: string]): void

Registeres a new controller.

  • clazz -> simple the name of the class
  • tag -> optional, per default it will take the name of the class

.getApp(): Server

Gets the instance of the server.

Returns the restify server instance BEFORE it started listening.

TODO

  • allow to add middleware
  • add restify server options
  • 100% test coverage