ra
v0.9.7
Published
Customizing Rest API builder
Downloads
18
Readme
Emerap RA :sunny:
Synopsis
Customizing Rest API builder
API reference
call(req, res, [method])
req
: required Instance of http.IncomingMessageres
: required Instance of http.ServerResponsemethod
: Method name, (default value fromreq.params.method
)
Return object as Promise.
definition(obj)
obj
: required Definition reference
{
name: 'methodName',
args: {
paramName: {
dataType: 'dataTypeName',
},
},
callback: (args) => {
return promise or value
}
}
datatype(obj)
obj
: required Datatype reference
{
type: 'datatypeName',
callback: (value) => {
return promise or value
}
}
Quick start
Open terminal and run command
mkdir ra-test && cd ra-test && npm init -y && touch app.js && npm install express ra --save
Paste code into app.js and save changes
const express = require('express');
const app = express();
const ra = require('ra');
const api = new ra();
api.datatype({
type: 'fooBar',
callback: (value) => {
return `foo bar ${value}`;
}
});
api.definition({
name: 'ra.fooBar',
args: {
user: {
dataType: 'fooBar',
},
},
callback: (args) => {
return Promise.resolve(args);
}
});
app.all('/ra/method/:method', (req, res) => {
api.call(req, res).then((response) => {
res.status(response.code).json(response.data);
});
});
app.listen(3000, () => {
console.log('=====================================================');
console.log('Starting app http://lvh.me:3000/ra/method/ra.version');
console.log('=====================================================');
});
Start app in terminal
node app.js
© Alexander Pokhodyun (Karbunkul) 2017