Created
November 11, 2014 15:41
-
-
Save anthonator/0dc0310a931398490fab to your computer and use it in GitHub Desktop.
Superagent wrapper with base URL support
This file contains hidden or 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
var request = require("superagent"); | |
/** | |
* Superagent wrapper for easily making AJAX requests. Provides a layer of | |
* convenience by adding a base URL to each request. The base URL is derived | |
* from the ```API_URL``` environment variable. | |
*/ | |
module.exports = { | |
/** | |
* DELETE relative `path` with optional callback `fn(res)`. | |
* | |
* @method del | |
* @param {String} path | |
* @param {Function} fn | |
* @return {Request} | |
* @public | |
*/ | |
del: function(path, fn) { | |
return ( | |
request | |
.del(process.env.API_URL + path, fn) | |
); | |
}, | |
/** | |
* GET relative `path` with optional `data` and callback `fn(res)`. | |
* | |
* @method get | |
* @param {String} path | |
* @param {Mixed} data | |
* @param {Function} fn | |
* @return {Request} | |
* @public | |
*/ | |
get: function(path, data, fn) { | |
return ( | |
request | |
.get(process.env.API_URL + path, data, fn) | |
); | |
}, | |
/** | |
* PATCH relative `path` with optional `data` and callback `fn(res)`. | |
* | |
* @method patch | |
* @param {String} path | |
* @param {Mixed} data | |
* @param {Function} fn | |
* @return {Request} | |
* @public | |
*/ | |
patch: function(path, data, fn) { | |
return ( | |
request | |
.patch(process.env.API_URL + path, data, fn) | |
); | |
}, | |
/** | |
* POST relative `path` with optional `data` and callback `fn(res)`. | |
* | |
* @method post | |
* @param {String} path | |
* @param {Mixed} data | |
* @param {Function} fn | |
* @return {Request} | |
* @public | |
*/ | |
post: function(path, data, fn) { | |
return ( | |
request | |
.post(process.env.API_URL + path, data, fn) | |
); | |
}, | |
/** | |
* PUT relative `path` with optional `data` and callback `fn(res)`. | |
* | |
* @method put | |
* @param {String} path | |
* @param {Mixed} data | |
* @param {Function} fn | |
* @return {Request} | |
* @public | |
*/ | |
put: function(path, data, fn) { | |
return ( | |
request | |
.put(process.env.API_URL + path, data, fn) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment