Skip to content

Instantly share code, notes, and snippets.

@anthonator

anthonator/client.js

Created Nov 11, 2014
Embed
What would you like to do?
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