0.0.1 • Published 11 years ago
resource-agent v0.0.1
resource-agent
Node module to create simple API clients for endpoints that expose a common REST-like interface.
Install
$ npm install resource-agent
Usage
Create a "user" resource and do some basic http "CRUD" operations.
var resource = require('resource');
// create a new "users" instance
var users = resource('/users');
// GET /users
users.all(function(err, result){
if (err) throw err;
console.log('%j', result);
});
// GET /users?tag=awesome
users.all({ tag: 'whatever' }, function(err, result){
if (err) throw err;
console.log('%j', result);
});
// GET /users/123456789
users.one(123456789, function(err, result){
if (err) throw err;
console.log('%j', result);
});
// POST /users
users.create({
name: 'garrett',
tag: 'whatever'
}, function(err, result){
if (err) throw err;
console.log('%j', result);
});
// PUT /users/123456789
users.update(123456789, {
tag: 'stuff'
}, function(err, result){
if (err) throw err;
console.log('%j', result);
});
// DELETE /users/123456789
users.remove(123456789, function(err, result){
if (err) throw err;
console.log('%j', result);
});
Custom Methods
Now not all methods that are provided implicity will get you what you need. For that, you can pass an additional parameter to the constructor to "decorate" the resource prototype.
var users = resource('/users', {
tagged: function(value, fn){
return this.all({ tagged: value }, fn);
}
});
// GET /users?tagged=awesome
users.tagged('awesome', function(err, result){
if (err) throw err;
console.log('%j', result);
});
Options
You can set global options on the constructor it self. However, not that it will populate the value to all instances. Otherwise, just pass it into the constructor.
var resource = require('resource-agent');
resource.host = 'http://localhost:3000';
resource.headers = {
secret: 'top-secret-header'
};
TODO
- tests
- response parsing
License
MIT
0.0.1
11 years ago