Rese API
The Node.js RESTful API Service
Usage
npm i -S koa rese-api
const app = const reseApi = app configs app
configs.js
Sample totalPopulation: type: Number required: true 'totalPopulation required' country: String zipCode: Number } Person: ... ... } // OR mysql // ref: http://docs.sequelizejs.com/en/v3/docs/models-definition/ schema: City: name: type: 'STRING' allowNull: false defaultValue: '' totalPopulation: type: 'BIGINT' allowNull: false country: type: 'STRING' Image: md5: type: 'STRING' allowNull: false validate: notEmpty: true size: type: 'BIGINT(20)' allowNull: false validate: isNumeric: true tableName: 'picture_info' timestamps: false // http://docs.sequelizejs.com/en/v3/docs/models-definition/#configuration ... } router: prefix: 'api' }
Will generate services for each schema:
'city.find': path: '/api/city' method: 'get' controller: Object 'city.findById': path: '/api/city/:id' method: 'get' controller: Object 'city.add': path: '/api/city' method: 'post' controller: Object 'city.update': path: '/api/city/:id' method: 'put' controller: Object 'city.delete': path: '/api/city/:id' method: 'delete' controller: Object 'person.find': ...
reseApi.services
All services.
reseApi.models
MongoDB: return mongoose models
MySQL: return sequelize models
reseApi.router
Instance of KoaRouter
reseApi.add(name, config)
Add service.
name
:{String}
required, service nameconfig
:{Object}
required, service config
reseApi
reseApi.hook([name], opts)
Hook service
name
:{String|Array}
optional, service name or namesopts
:{Object}
required,{ pre: fn, post: fn}
When no names, will hook all services.
reseApi
Demo
Client request:
Request URL: api/resource?md5=27ad74bfc80ce829fd3fc58410e04f26&limit=30&offset=0&order=update_at|desc
Request Method:GET
Changelog
v0.0.4 2017-05-24 12:34
- Change config style.
- Support more query conditions.
v0.0.3 2017-03-29 21:10
- Support sequelize configuration (http://docs.sequelizejs.com/en/v3/docs/models-definition/#configuration)
v0.0.2 2017-03-29 11:23
- Add MySQL supports. (using: http://docs.sequelizejs.com/en/v3/)