Build Status

superagent-promise

Simple/dumb promise wrapper for superagent. You must depend on superagent and your favorite Promise library directly.

Usage

var Promise = this.Promise || require('promise');
var agent = require('superagent-promise')(require('superagent'), Promise);

// method, url form with `end`
agent('GET', 'http://google.com')
  .end()
  .then(function onResult(res) {
    // do stuff
  }, function onError(err) {
    //err.response has the response from the server
  });

// method, url form with `then`
agent('GET', 'http://google.com')
  .then(function onResult(res) {
    // do stuff
  });


// helper functions: options, head, get, post, put, patch, del
agent.put('http://myxfoo', 'data')
  .end()
  .then(function(res) {
    // do stuff`
  });

// helper functions: options, head, get, post, put, patch, del
agent.put('http://myxfoo', 'data').
  .then(function(res) {
    // do stuff
  });

Mocking

Now superagent-promise can be mocked using superagent-mock. For the complete example see test/mock.spec.js and test/mock.config.js.

var SUCCESS_BODY = 'Yay! Mocked :)';
var mockedRequest = require('superagent');
var mocks = require('./mock.config')('localhost', SUCCESS_BODY);
require('superagent-mock')(mockedRequest, mocks);
var request = require('../index')(mockedRequest, Promise);


建立状态

superagent-promise

简单/愚蠢承诺的超级代理包装。您必须直接依赖 superagent 和您最喜爱的Promise库。

用法

var Promise = this.Promise || require('promise');
var agent = require('superagent-promise')(require('superagent'), Promise);

// method, url form with end agent('GET', 'http://google.com') .end() .then(function onResult(res) { // do stuff }, function onError(err) { //err.response has the response from the server });

// method, url form with then agent('GET', 'http://google.com') .then(function onResult(res) { // do stuff });

// helper functions: options, head, get, post, put, patch, del agent.put('http://myxfoo', 'data') .end() .then(function(res) { // do stuff` });

// helper functions: options, head, get, post, put, patch, del agent.put('http://myxfoo', 'data'). .then(function(res) { // do stuff });

嘲笑

现在,超级承诺可以使用 superagent-mock 来嘲笑。完整的例子见 test / mock.spec.js test / mock.config.js

var SUCCESS_BODY = 'Yay! Mocked :)';
var mockedRequest = require('superagent');
var mocks = require('./mock.config')('localhost', SUCCESS_BODY);
require('superagent-mock')(mockedRequest, mocks);
var request = require('../index')(mockedRequest, Promise);




相关问题推荐