项目作者: salper

项目描述 :
A REST client with fluent interface
高级语言: JavaScript
项目地址: git://github.com/salper/wonderest.git
创建时间: 2014-12-19T16:38:55Z
项目社区:https://github.com/salper/wonderest

开源协议:MIT License

下载


:no_entry: [DEPRECATED] Wonderest

Build Status
Dependency Status
devDependency Status

This project is no longer supported, please consider using Axios instead.

Wonderest is a wrapper around superagent, allowing to configure endpoints bit by bit using a chainable API.

Requirements

Wonderest is written in ES6, compiled in ES5 using traceur, and requires traceur-runtime to be loaded.

  1. require('traceur/bin/traceur-runtime');
  2. var wrest = require('wonderest');

Example

  1. var wrest = require('wonderest');
  2. // GET http://example.org/api
  3. wrest('http://example.org/api')
  4. .request().get().then(function(res) {
  5. console.log(res.text);
  6. });
  7. // GET http://example.org/api
  8. wrest('http://example.org').resource('api')
  9. .request().get().then(function(res) {
  10. console.log(res.text;
  11. });
  12. // GET http://example.org/api?limit=10&offset=1
  13. wrest('http://example.org').data('limit', 10)
  14. .resource('api').data('offset', 1)
  15. .request().get().then(function (res) {
  16. console.log(res.text);
  17. });

API

wrest(url)

Wonderest client factory. A client is a top level resource, thus it exposes the same API as sub resources.

  1. var wrest = require('wonderest');
  2. var client = wrest('http://example.com');

Resource.path

The path of the resource is dynamically resolved and returned.

  1. wrest('http://example.com').resource('api').resource('users').path;
  2. // => http://example.com/api/users

Resource.parent

The resource parent if any. A client has not parent.

  1. wrest('http://example.com').resource('api').resource('users').parent.path;
  2. // => http://example.com/api

Resource.client

The top level resource.

  1. wrest('http://example.com').resource('api').resource('users').client.path;
  2. // => http://example.com

Resource.resource(path)

Sub resource factory.

  1. wrest('http://example.com').resource('api').path;
  2. // => http://example.com/api

Resource.headers()

Sets or gets resource headers (dynamically resolved).

  1. var client = wrest('http://example.com');
  2. client.headers()
  3. // => {}
  4. client.headers({ foo: 'bar' }).headers();
  5. // => { foo: 'bar' }
  6. client.headers('foo');
  7. // => 'bar'
  8. client.resource('api').headers('bar', 'baz').headers();
  9. // => { foo: 'bar', bar => 'baz' }

Resource.data()

Sets or gets resource data (dynamically resolved). Depending on the request verb (GET, POST, PUT, etc.), data is used as request query or body.

  1. var client = wrest('http://example.com');
  2. client.data()
  3. // => {}
  4. client.data({ foo: 'bar' }).data();
  5. // => { foo: 'bar' }
  6. client.data('foo');
  7. // => 'bar'
  8. client.resource('api').data('bar', 'baz').data();
  9. // => { foo: 'bar', bar => 'baz' }

Resource.options()

Sets or gets resource options (dynamically resolved). There are no options currently used by wonderest, it is more a placeholder for some applicative options, like authentication credentials, which can be used in request hooks.

  1. var client = wrest('http://example.com');
  2. client.options()
  3. // => {}
  4. client.options({ foo: 'bar' }).options();
  5. // => { foo: 'bar' }
  6. client.options('foo');
  7. // => 'bar'
  8. client.resource('api').options('bar', 'baz').options();
  9. // => { foo: 'bar', bar => 'baz' }

Resource.request()

Request factory. Implements available HTTP verbs - see methods

  1. wrest('http://example.com').request().post(data, headers).then(function (res) {
  2. // ...
  3. });

Resource.hook(name, handler)

Hook registrar. Wonderest provides two hooks per request (request and response). request is executed before delegating to superagent, and response is executed after superagent response.

The order of execution is from the client to the resource (top to bottom), and handlers must return the same positional arguments as they were given in an array form (to allow spreading).

  1. // Example of authenticated API.
  2. var client = wrest('http://example.com/api');
  3. client.hook('request', function (resource) {
  4. // ...
  5. return [resource];
  6. });
  7. client.hook('response', function (resource, res, retry) {
  8. if (403 !== res.status)
  9. return [].slice.call(arguments);
  10. return resource.client.resource('authenticate')
  11. .data(resource.options('authentication'))
  12. .request().post()
  13. .tap(function (res) {
  14. resource.headers('Authorization', res.text);
  15. })
  16. .then(function () {
  17. return retry();
  18. })
  19. .then(function (res) {
  20. return [resource, res, retry];
  21. });
  22. });
  23. client.resource('users').request().get().then(function (res) {
  24. console.log(res.text);
  25. });

Licence

MIT