base-ao
v0.1.0
Published
A base API object with built-in validators.
Downloads
2
Readme
js-base-ao
A base API object with built-in validators.
Usage
npm install base-ao
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;
}
};
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.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