node package manager

api-scaffolder

> Check out the slides < POC, NOT FOR PRODUCTION

api-scaffolder Build Status

A RAML based typescript backend generator.

It will generate a base project structure for your API, since it uses Typescript your API will be typed and your RAML contract will be ensured.

The generated code exposes an express middleware, implement it should be very simple:

# create a folder 
mkdir my-service
cd my-service
 
# init git and npm 
git init
npm init --yes
 
# (create an api.raml file) 
 
# scaffold api.raml 
api-scaffolder api.raml -o src/myApi
 
# create and open index.ts 
touch index.ts && vim index.ts
import * as express from 'express';
 
import { registerRouter } from './src/myApi';
 
export const app = express();
 
registerRouter(app);
 
app.listen(9919, () => {
  console.log('Listening in port 9919');
});

Try it

  1. Install the package:
$ npm install -g api-scaffolder
  1. Clone base project (or download it here) and delete api folder.
$ git clone https://github.com/menduz/api-scaffolder-demo . && git checkout tags/base-api  && rm -fr api
  1. Modify the file api.raml
  2. Execute the generator. This will generate the api implementation on api folder.
$ api-scaffolder api.raml -o api
  1. Build & run $ npm install && npm run run

Features

  • Routing: All the controllers automatically register themselves on a router.
  • Method request validation: Internally it uses osprey to perform validations over queryParameters, uriParameters, headers and request.body.
  • Securirity: Provides an easy way (middlewares) to handle security schemas, no more traits for security.
  • Ensures required parameters: If a header or queryParameter is required.
  • Well defined responses: Every response is defined as a class, ex: GetResponse200 | PostResponse403

TODO:

  • Move queryString & headers interfaces to base.ts
  • When json schema is not present, use implicit types from examples.
  • Middleware decorator
import bodyParser = require("body-parser");
 
class Controller extends ns.AbstractHandler {
    @Api.ApplyMiddlewares([
        bodyParser.urlencoded()
    ])
    post(body: any){
        return new ns.GetResponse200({
            parsedBody: this.currentContext.request.body
        })
    }
}