api-tailor
v1.1.2
Published
A tool to swiftly tailor a fitting api
Downloads
26
Readme
api-tailor
A tool to swiftly tailor a fitting api
Have a problem? Come chat with us!
Installation
$ [sudo] npm install api-tailor --save
Examples
var tailor = require('api-tailor');
var client = tailor({
host: 'http://yourserver.com/api',
resources: {
customers: {
all: {
method: 'GET',
path: '/'
},
add: {
method: 'POST',
path: '/'
},
byName: {
method: 'GET',
path: '/:name' // url parameter
},
downloadLog: {
method: 'GET',
path: '/log',
stream: true // response will return as stream
},
uploadLog: {
method: 'POST',
path: '/log',
data: 'form' // data given will be treated as form data
}
}
}
});
client.customers.all()
.then(function(customers) {
// customers -> all data from get request to http://yourserver.com/api/customers/all
});
client.customers.add({ name: 'nacho', address: 'nachos home 25, dip mountain, taco-ville' })
.then(function() {
// -> post request to http://yourserver.com/api/customers/
});
client.customers.byName({}, { name: 'nacho' })
.then(function(customer) {
// customer -> get request to http://yourserver.com/api/customers/nacho
});
client.customers.downloadLog()
.then(function(stream) {
stream.pipe(process.stdout); // stream -> get request to http://yourserver.com/api/customers/log
});
client.customers.uploadLog({file: fs.createReadStream('file.txt')})
.then(function() {
// -> post request to http://yourserver.com/api/customers/log
});
Injectors
Use injectors to intercept outgoing or incoming data and manpulate it.
Token Injector Example
var token;
client.inject({
request: function (request) {
request.headers = request.headers || {};
if (token) {
request.headers.Authorization = 'Bearer ' + token;
}
return Q.resolve(request);
}
});
Run Tests
$ npm test
License
we never go out of style