typescript_base_rest_api
TypeScript icon, indicating that this package has built-in type declarations

1.5.6 • Public • Published

Typescript base REST API module

Coverage Status CircleCI This is a lightweight annotation-based restify extension for typescript.

Installation:

This library only works with typescript. Ensure it is installed:

npm install typescript -g

To install typescript base rest api:

npm i typescript_base_rest_api

Get started

1. Define a service router

export class ItemRouter extends RouterConfig {
    protected onConfig(): void {
        this.get("/items", ItemService.getItems);
        this.get("/items/:id", ItemService.findById);
    }
}

2. Create a service

export default class ItemService extends BaseService {
 
    static controller: ItemController = new ItemController();
 
    @RestifyValidation.isAlphanumeric("id")
    public static findById(req: Request, res: Response, next: Next): void {
        // do stuff
    }
    public static getItems(req: Request, res: Response, next: Next): void {
      // do other stuff
    }

3. Configure your server

let options: ServerOptions = new OptionsBuilder()
    .withName(config.get<string>("server.options.name"))
    .withVersion(config.get<string>("server.options.version"))
    .build();
 
export let server: Server = new ServerBuilder()
    .withTimeout(config.get<number>("server.options.timeout"))
    .withOptions(options)
    .withQueryParser(queryParser())
    .withRouter(new PingRouter())
    .withRouter(new ItemRouter())
    .withCORS(false)
    .build();
 
let port = config.get<number>("port");
 
server.listen(port, function () {
    loggerHelper.info("App online on port: " + port);
});

4. Run your application!

The API will be running at http://localhost:8282

Configure your application

You can configure your application using environment variables. Example:

{
  "port": "PORT",
  "security": {
    "enable": "SECURITY_ENABLE",
    "token": "SECURITY_TOKEN"
  },
  "newrelic": {
    "enable": "NEWRELIC_ENABLE",
    "license_key": "NEWRELIC_LICENSE_KEY",
    "app_name": "NEWRELIC_APP_NAME"
  }
}

Examples

Check examples folder to see some basic usage.

Development

To use lint in development, you need to install typescript and tslist globally:

npm install -g typescript tslint

Package Sidebar

Install

npm i typescript_base_rest_api

Weekly Downloads

1

Version

1.5.6

License

MIT

Unpacked Size

336 kB

Total Files

229

Last publish

Collaborators

  • alejodaraio
  • bnovillo
  • gfusca
  • leanazulyoro