Skip to content

Instantly share code, notes, and snippets.

@anthonator
Created November 11, 2014 15:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonator/0dc0310a931398490fab to your computer and use it in GitHub Desktop.
Save anthonator/0dc0310a931398490fab to your computer and use it in GitHub Desktop.
Superagent wrapper with base URL support
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