Permalink
Browse files

initial commit

  • Loading branch information...
1 parent acd85e8 commit 594d2c458a39a41be2d78c3221f301985dc4fead @lightsofapollo committed Jan 28, 2014
Showing 348 changed files with 45,307 additions and 1 deletion.
View
@@ -1,4 +1,19 @@
superagent-promise
==================
-Simple/dumb promise wrapper for superagent
+Simple/dumb promise wrapper for superagent. Both `superagent` and
+`promise` are peerDependencies. The `.get`, `.del`, etc.. helper methods
+are not present here.
+
+
+## Usage
+
+```js
+var agent = require('superagent-promise');
+
+agent('GET', 'http://google.com').end().then(
+ function onResult() {
+
+ }
+);
+```
View
@@ -0,0 +1,29 @@
+/**
+Promise wrapper for superagent
+*/
+
+var superagent = require('superagent');
+var Request = superagent.Request;
+var Promise = require('promise');
+
+function PromiseRequest() {
+ Request.apply(this, arguments);
+}
+
+PromiseRequest.prototype = Object.create(Request.prototype);
+
+PromiseRequest.prototype.end = function() {
+ var _super = Request.prototype.end;
+ var context = this;
+
+ return new Promise(function(accept, reject) {
+ _super.call(context, function(err, value) {
+ if (err) return reject(err);
+ accept(value);
+ });
+ });
+};
+
+module.exports = function(method, url) {
+ return new PromiseRequest(method, url);
+};
View
@@ -0,0 +1,41 @@
+suite('superagent-promise', function() {
+ var assert = require('assert');
+ var agent = require('./');
+ var http = require('http');
+
+ // start the server
+ var server;
+ var body = 'woot';
+ setup(function(done) {
+ server = http.createServer(function(req, res) {
+ res.writeHead(200, {
+ 'Content-Length': body.length,
+ 'Content-Type': 'text/plain'
+ });
+ res.end(body);
+ });
+
+ server.listen(0, done);
+ });
+
+ teardown(function(done) {
+ server.close(done);
+ });
+
+
+ test('issue request', function(done) {
+ var addr = server.address();
+ var url = 'http://' + addr.address + ':' + addr.port;
+
+ agent('GET', url).end().then(
+ function(res) {
+ assert.equal(res.text, body);
+ done();
+ },
+
+ function(err) {
+ done(err);
+ }
+ );
+ });
+});
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 594d2c4

Please sign in to comment.