forked from wejendorp/angular-superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 63eab70
Showing
8 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
components | ||
build |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
}); |