0.1.0 • Published 7 years ago

base-ao v0.1.0

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

js-base-ao

A base API object with built-in validators.

Usage

  1. npm install base-ao

  2. Extend BaseAO

const BaseAO = require('base-ao');

module.exports = class MyAO extends BaseAO {

    constructor (json) {
        super(json);

        MyAO.assertIsText(json.name);
        this.name = json.name;
    }

};
  1. Use your API object! It's intended to be used as a data structure, which means that all properties are public. In this example, the only property is a string called name. Instantiating the API object should perform all validations.

  2. If you're writing a web server with express, this package is complemented well by express-deserializer-middleware.

const DeserializerMiddleware = require('express-deserializer-middleware');
const MyAO = require('api/ao/MyAO');

this.post('/',
    DeserializerMiddleware.deserializeBodyToClass(MyAO),
    (req, res, next) => {
        const myAO = req.body;
        myService.myHandler(myAO.name)
            .then(res.json.bind(res))
            .catch(next);
    });

Testing

npm test