api-scaffolder
v3.0.8
Published
Generate a Typescript API backend from RAML definitions
Downloads
28
Readme
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:
# 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 { 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 overqueryParameters
,uriParameters
,headers
andrequest.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:
- [x] Move queryString & headers interfaces to base.ts
- [x] When json schema is not present, use implicit types from examples.
- [x] 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
})
}
}