Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
wejendorp committed Jan 20, 2014
0 parents commit 63eab70
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
components
build
Empty file added History.md
Empty file.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

build: components index.js
@component build --dev

components: component.json
@component install --dev

clean:
rm -fr build components template.js

.PHONY: clean
41 changes: 41 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

# angular-superagent

Angular wrapper for Superagent

## Installation

Install with [component(1)](http://component.io):

$ component install wejendorp/angular-superagent

$ bower install angular-superagent

## API




## License

The MIT License (MIT)

Copyright (c) 2014 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
24 changes: 24 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "angular-superagent",
"main": "index.js",
"version": "0.1.0",
"homepage": "https://github.com/wejendorp/angular-superagent",
"authors": [
"Jacob Wejendorp <[email protected]>"
],
"description": "Angular wrapper for Superagent",
"keywords": [
"angular",
"superagent",
"http",
"xhr"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
16 changes: 16 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "angular-superagent",
"repo": "wejendorp/angular-superagent",
"description": "Angular wrapper for Superagent",
"version": "0.0.1",
"keywords": [],
"dependencies": {
"visionmedia/superagent": "*"
},
"development": {},
"license": "MIT",
"main": "index.js",
"scripts": [
"index.js"
]
}
Empty file added examples/RequestConfig.js
Empty file.
123 changes: 123 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

// Convenience service to wrap Superagent with defaults and promises
//
angular.module('services')
.provider('Request', function() {
var agent = window.superagent;

var options = {
baseUrl: '',
timeout: 10000,
headers: {},
promises: [],
transforms: []
};

return {
'$get' : function($q, $timeout) {
// Promisify this thing!
agent.Request.prototype._end = agent.Request.prototype.end;

agent.Request.prototype.promise = function() {
var deferred = $q.defer();

// Hack to make angular aware of this pending request
var p = $timeout(function(){
throw 'Request timed out after '+options.timeout+' ms';
}, options.timeout);

var cleanUp = function() {
$timeout.cancel(p); // angular no longer has pending req.
};
deferred.promise.finally(cleanUp);


this.end(function(err, res) {
if (err || !res.ok) {
if(err) {
/* jshint devel:true */
console.error(err);
}
return deferred.reject(res || {});
}
return deferred.resolve(res);
});


var chain = deferred.promise;
// Make this.reject / this.resolve possible since angular returns a new promise for each then
var methods = {
reject: $q.reject,
resolve: $q.when
};

// Wrap in helper to ignore non-returning handlers
var wrap = function(fn) {
return function(val) {
var res = fn(val);
if(typeof res === 'undefined') {
return val;
}
return res;
};
};

options.promises.forEach(function(fns) {
var success = fns[0] ? wrap(fns[0].bind(methods)) : null;
var error = fns[1] ? wrap(fns[1].bind(methods)) : null;
chain = chain.then(success, error);
});
return chain;
};

// agent.Request.prototype.end = function(fn) {
// this._end(function(err, res) {
// options.transforms.splice


// });
// };


// Configure request defaults
var methods = ['get', 'head', 'del', 'put', 'post', 'patch'];
methods.forEach(function(m) {
if(agent['_'+m]) return;
agent['_'+m] = agent[m];
agent[m] = function() {
arguments[0] = options.baseUrl + arguments[0]; // url
var method = agent['_'+m].apply({}, arguments);
if(options.credentials) method.withCredentials();
if(options.headers) method.set(options.headers);
return method;
};
});

return agent;
},
setHeaders : function(headers) {
options.headers = _.extend(options.headers, headers);
return this;
},
withCredentials : function() {
options.credentials = true;
return this;
},
baseUrl: function(url) {
options.baseUrl = url;
return this;
},
timeout: function(timeout) {
options.timeout = timeout;
return this;
},
// transform: function(fn) {
// options.transforms.push(fn);
// return this;
// },
then: function(success, error) {
options.promises.push([success, error]);
return this;
}
};
});

0 comments on commit 63eab70

Please sign in to comment.