api-scaffolder
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:
mkdir my-service
cd my-service
git init
npm init --yes
api-scaffolder api.raml -o src/myApi
touch index.ts && vim index.ts
import * as express from 'express';
import { setup } from './src/myApi';
export const app = express();
const { middleware, controller } = setup();
app.use('/', middleware);
app.listen(9919, () => {
console.log('Listening in port 9919');
});
Try it
- Install the package:
$ npm install -g api-scaffolder
- 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
- Modify the file
api.raml
- Execute the generator. This will generate the api implementation on
api
folder.
$ api-scaffolder api.raml -o api
- 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:
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
})
}
}