https://github.com/lightsofapollo/superagent-promise
Simple/dumb promise wrapper for superagent
https://github.com/lightsofapollo/superagent-promise
Last synced: about 1 year ago
JSON representation
Simple/dumb promise wrapper for superagent
- Host: GitHub
- URL: https://github.com/lightsofapollo/superagent-promise
- Owner: lightsofapollo
- License: mit
- Created: 2014-01-28T10:50:27.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2018-06-04T14:36:25.000Z (about 8 years ago)
- Last Synced: 2025-03-30T06:07:20.728Z (about 1 year ago)
- Language: JavaScript
- Size: 387 KB
- Stars: 129
- Watchers: 1
- Forks: 27
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/lightsofapollo/superagent-promise)
superagent-promise
==================
Simple/dumb promise wrapper for superagent. You must depend on `superagent` and your favorite Promise library directly.
## Usage
```js
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`.
```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);
```