0.1.0 • Published 8 years ago

api-express v0.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

api-express

npm version

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 = require('express');
const app = express();

const API = require('api-express');

// optional, use pre-defined res.ok and res.fail functions
app.use(API.hook({
    apiOk: true,
    apiFail: true
}));
// 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-defined
app.use(API.hook({
    apiOk: function(obj){ ... },
    apiFail: function(obj){ ... }
}));

Quickly build basic CRUD controllers:

const UserModel = require('path/to/UserModel');
const api = API.create(UserModel, {
    apiNames: {
        'list': 'someMethod'
    }
});

// providing UserModel.someMethod as an ES6 generator
app.get('/users', api.list);

Build your own controller:

const someController = API.builder(function*(){ ... });
app.get('/some/path', someController);

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's res 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