api-express
Handy libraries to build RESTful API controllers in express.js from ES6 generator callbacks.
Features
- ES6 syntax(generator, class, etc).
- RESTful API
How to use
To config:
const express = ;const app = ; const API = ; // optional, use pre-defined res.ok and res.fail functionsapp;// res.ok(obj) will output structure like { isSuccess: true, result: obj }// res.fail(obj) will output structure like { isSuccess: false, errorCode: ..., errorMessage: ..., errorData: ... } // or self-definedapp;
Quickly build basic CRUD controllers:
const UserModel = ;const api = API; // providing UserModel.someMethod as an ES6 generatorapp;
Build your own controller:
const someController = API;app;
APIs
API.create(Model, params)
Create CRUD methods from an object model params.apiNames default:
{
createOne: 'createOne',
getOne: 'getOne',
updateOne: 'updateOne',
removeOne: 'removeOne',
list: 'list',
getCount: 'getCount'
}
API.builder(callback)
Internal used controller builder
API.hook(params)
Apply with
app.use()
to extend express'sres
object params.apiOk: Boolean|Function Set true to use pre-defined callback params.apiFail: Boolean|Function Set true to use pre-defined callback
ToDos
- Improve doc
- Add example
- Error handling feature