├── .gitignore ├── README.md ├── index.coffee.md ├── index.js ├── package.json └── test ├── basic.coffee.md ├── mocha.opts └── promise.coffee.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Note: as I understand, this module is no longer needed. SuperAgent [as of 1.0.0](https://github.com/visionmedia/superagent/blob/master/History.md#100--2015-03-08) should now offer the same semantics as this module, [since 2.0.0](https://github.com/visionmedia/superagent/blob/master/History.md#200-2016-05-29) returns a real `Promise`, and [since 2.3.0](https://github.com/visionmedia/superagent/blob/master/History.md#230-2016-09-20) offers a `.catch` method. 2 | 3 | SuperAgent as Promise(d) 4 | ===================== 5 | 6 | [SuperAgent](http://visionmedia.github.io/superagent/) as Promise. 7 | 8 | The returned Promise will reject both on SuperAgent error, and on error in the response. 9 | 10 | Compare with [the promise plugin](https://github.com/jomaxx/superagent-promise-plugin), which has similar semantics but a different interface. 11 | 12 | Also, SuperAgent 2.0 now returns native Promises from [`req.then`](https://visionmedia.github.io/superagent/#generator-support) (but it sticks to its original semantics). 13 | 14 | Installation 15 | ------------ 16 | 17 | npm install superagent-as-promised 18 | 19 | Usage 20 | ----- 21 | 22 | var request = require('superagent'); 23 | require('superagent-as-promised')(request); 24 | 25 | Then 26 | 27 | request 28 | .get('/location') 29 | .then( function(response) { 30 | console.log("Got "+response.text); 31 | }) 32 | .catch( function(error) { 33 | console.dir(error); 34 | }) 35 | 36 | is syntactic sugar for: 37 | 38 | var promise = request 39 | .get('/location') 40 | .endAsync(); 41 | 42 | promise 43 | .then( function(response) { 44 | console.log("Got "+response.text); 45 | }) 46 | .catch( function(error) { 47 | console.dir(error); 48 | }) 49 | 50 | Options 51 | ------- 52 | 53 | require('superagent-as-promised')(SuperAgent,Promise); 54 | 55 | `SuperAgent` must be a SuperAgent class; it is extended with `endAsync()`, `then`, and `catch` methods. 56 | The optional `Promise` parameter allows you to provide your own Promise class; the native Promise class is used by default. 57 | -------------------------------------------------------------------------------- /index.coffee.md: -------------------------------------------------------------------------------- 1 | SuperAgent as Promised 2 | ====================== 3 | 4 | # Request.Request.prototype.end = Promise.promisify Request.Request.prototype.end 5 | NativePromise = Promise 6 | 7 | module.exports = (Request,Promise = NativePromise) -> 8 | Request.Request.prototype.endAsync = -> 9 | new Promise (resolve,reject) => 10 | try 11 | @end (error, response) -> 12 | if error 13 | reject error 14 | return 15 | if response.error 16 | reject response.error 17 | return 18 | resolve response 19 | catch error 20 | reject error 21 | 22 | Request.Request.prototype.then = -> 23 | @endAsync() 24 | .then arguments... 25 | Request.Request.prototype.catch = -> 26 | @endAsync() 27 | .catch arguments... 28 | 29 | Request 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.10.0 2 | (function() { 3 | var NativePromise; 4 | 5 | NativePromise = Promise; 6 | 7 | module.exports = function(Request, Promise) { 8 | if (Promise == null) { 9 | Promise = NativePromise; 10 | } 11 | Request.Request.prototype.endAsync = function() { 12 | return new Promise((function(_this) { 13 | return function(resolve, reject) { 14 | var error, error1; 15 | try { 16 | return _this.end(function(error, response) { 17 | if (error) { 18 | reject(error); 19 | return; 20 | } 21 | if (response.error) { 22 | reject(response.error); 23 | return; 24 | } 25 | return resolve(response); 26 | }); 27 | } catch (error1) { 28 | error = error1; 29 | return reject(error); 30 | } 31 | }; 32 | })(this)); 33 | }; 34 | Request.Request.prototype.then = function() { 35 | var ref; 36 | return (ref = this.endAsync()).then.apply(ref, arguments); 37 | }; 38 | Request.Request.prototype["catch"] = function() { 39 | var ref; 40 | return (ref = this.endAsync())["catch"].apply(ref, arguments); 41 | }; 42 | return Request; 43 | }; 44 | 45 | }).call(this); 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "superagent-as-promised", 3 | "version": "4.0.0", 4 | "description": "SuperAgent with a Promise twist", 5 | "main": "index.js", 6 | "scripts": { 7 | "prepublish": "coffee -c index.coffee.md", 8 | "pretest": "npm install", 9 | "test": "mocha" 10 | }, 11 | "keywords": [ 12 | "superagent", 13 | "promise" 14 | ], 15 | "author": "Stéphane Alnet (http://stephane.shimaore.net/)", 16 | "license": "MIT", 17 | "devDependencies": { 18 | "bluebird": "^3.4.0", 19 | "chai": "^3.5.0", 20 | "chai-as-promised": "^5.3.0", 21 | "coffee-script": "^1.10.0", 22 | "express": "^4.13.4", 23 | "mocha": "^2.5.3", 24 | "superagent": "^2.0.0" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/basic.coffee.md: -------------------------------------------------------------------------------- 1 | chai = require 'chai' 2 | chai.use require 'chai-as-promised' 3 | chai.should() 4 | 5 | Request = require 'superagent' 6 | (require '..') Request 7 | express = require 'express' 8 | 9 | describe 'SuperAgent as Promise', -> 10 | 11 | server = null 12 | 13 | before -> 14 | app = express() 15 | app.get '/foo.txt', (req,res) -> 16 | res.send 'OK' 17 | app.get '/foo.json', (req,res) -> 18 | res.json ok:true 19 | server = app.listen 3000 20 | 21 | after -> 22 | server.close() 23 | 24 | it 'should be successful', -> 25 | Request 26 | .get 'http://127.0.0.1:3000/foo.txt' 27 | .endAsync() 28 | .should.be.fulfilled 29 | 30 | it 'should return a value', -> 31 | Request 32 | .get 'http://127.0.0.1:3000/foo.txt' 33 | .endAsync() 34 | .then (res) -> 35 | res.text 36 | .should.eventually.equal 'OK' 37 | 38 | it 'should return a json value', -> 39 | Request 40 | .get 'http://127.0.0.1:3000/foo.json' 41 | .accept 'json' 42 | .endAsync() 43 | .then (res) -> 44 | res.body 45 | .should.eventually.deep.equal ok:true 46 | 47 | it 'should fail appropriately', -> 48 | Request 49 | .get 'http://127.0.0.1:3000/unknown' 50 | .endAsync() 51 | .should.be.rejected 52 | 53 | it 'should handle then()', -> 54 | Request 55 | .get 'http://127.0.0.1:3000/foo.json' 56 | .accept 'json' 57 | .then (res) -> 58 | res 59 | .should.eventually.have.property 'ok', true 60 | 61 | it 'should handle then() with body', -> 62 | Request 63 | .get 'http://127.0.0.1:3000/foo.json' 64 | .accept 'json' 65 | .then (res) -> 66 | res.body 67 | .should.eventually.deep.equal ok:true 68 | 69 | it 'should reject on error', -> 70 | Request 71 | .get 'http://127.0.0.1:3000/unknown' 72 | .then -> 73 | null 74 | .should.be.rejected 75 | 76 | it 'should handle catch()', -> 77 | Request 78 | .get 'http://127.0.0.1:3000/unknown' 79 | .catch (error) -> 80 | caught:true 81 | .should.eventually.deep.equal caught:true 82 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers coffee.md:coffee-script/register 2 | --reporter spec 3 | --require chai 4 | --require chai-as-promised 5 | -------------------------------------------------------------------------------- /test/promise.coffee.md: -------------------------------------------------------------------------------- 1 | chai = require 'chai' 2 | chai.use require 'chai-as-promised' 3 | chai.should() 4 | 5 | Request = require 'superagent' 6 | Promise = require 'bluebird' 7 | Promise::foo = 42 8 | (require '..') Request, Promise 9 | express = require 'express' 10 | 11 | describe 'SuperAgent as Promise with a Promise class', -> 12 | 13 | server = null 14 | 15 | before -> 16 | app = express() 17 | app.get '/foo.txt', (req,res) -> 18 | res.send 'OK' 19 | app.get '/foo.json', (req,res) -> 20 | res.json ok:true 21 | server = app.listen 3000 22 | 23 | after -> 24 | server.close() 25 | 26 | it 'should be successful', -> 27 | Request 28 | .get 'http://127.0.0.1:3000/foo.txt' 29 | .endAsync() 30 | .should.be.fulfilled 31 | 32 | it 'should use the provided Promise lib', -> 33 | Request 34 | .get 'http://127.0.0.1:3000/foo.txt' 35 | .endAsync() 36 | .should.have.property 'foo', 42 37 | --------------------------------------------------------------------------------