From 63eab704373c62453ad64189111532bf33fabac4 Mon Sep 17 00:00:00 2001 From: Jacob Wejendorp Date: Tue, 21 Jan 2014 00:17:58 +0100 Subject: [PATCH] Initial commit. --- .gitignore | 2 + History.md | 0 Makefile | 11 ++++ Readme.md | 41 +++++++++++++ bower.json | 24 ++++++++ component.json | 16 +++++ examples/RequestConfig.js | 0 index.js | 123 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 217 insertions(+) create mode 100644 .gitignore create mode 100644 History.md create mode 100644 Makefile create mode 100644 Readme.md create mode 100644 bower.json create mode 100644 component.json create mode 100644 examples/RequestConfig.js create mode 100644 index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48a2e24 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +components +build diff --git a/History.md b/History.md new file mode 100644 index 0000000..e69de29 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0f14dac --- /dev/null +++ b/Makefile @@ -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 diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..591f350 --- /dev/null +++ b/Readme.md @@ -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 + + 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. \ No newline at end of file diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..02bad47 --- /dev/null +++ b/bower.json @@ -0,0 +1,24 @@ +{ + "name": "angular-superagent", + "main": "index.js", + "version": "0.1.0", + "homepage": "https://github.com/wejendorp/angular-superagent", + "authors": [ + "Jacob Wejendorp " + ], + "description": "Angular wrapper for Superagent", + "keywords": [ + "angular", + "superagent", + "http", + "xhr" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/component.json b/component.json new file mode 100644 index 0000000..7fba8a8 --- /dev/null +++ b/component.json @@ -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" + ] +} \ No newline at end of file diff --git a/examples/RequestConfig.js b/examples/RequestConfig.js new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js new file mode 100644 index 0000000..fe1313e --- /dev/null +++ b/index.js @@ -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; + } + }; +});