Skip to content

Instantly share code, notes, and snippets.

@chrisui
Created May 7, 2015 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisui/79eb65547f6b74905329 to your computer and use it in GitHub Desktop.
Save chrisui/79eb65547f6b74905329 to your computer and use it in GitHub Desktop.
Generic promise & data formatting wrapper around superagent
import superagent from 'superagent';
import Promise from 'promise';
/**
* Is this response object considered successful?
* @param {Object} res
* @returns {boolean}
*/
function isSuccess(res:Object) {
return res.ok && res.body.status && res.body.status.success && (!res.body.status.errors || !res.body.status.errors.length);
}
/**
* Create a new error from the res (this is assuming there is an error)
* @param {Object} res Response object (from superagent)
* @returns {Error}
*/
function makeError(res:Object) {
return new Error(
(res.body && res.body.responseStatus) ||
(res.body && res.body.status && res.body.status.message) ||
'Api error!'
);
}
/**
* Extended Request object to format the default request correctly and handle response
*/
class ApiRequest extends superagent.Request {
constructor(method:String, url:Object) {
url = this.parseUrl(url);
super(method, url);
this.accept('json');
}
end(cb:Function) {
let _super = superagent.Request.prototype.end;
let context = this;
return new Promise(function (accept, reject) {
_super.call(context, function (err, res) {
if (cb) {
cb(err, res);
}
if (!err && isSuccess(res)) {
accept(res.body.payload);
} else {
reject(err || makeError(res));
}
});
});
}
parseUrl(url:String) {
// If not beginning with 'https://' 'http://' '//' assume we just have a uri
// to complete with our root url
if (!/^(https?:)?\/\//.test(url)) {
url = __API__ + url;
}
return url;
}
}
/**
* Helper for making a GET request
* @param {String} url
* @param {Object} [data]
* @returns {ApiRequest}
*/
export function get(url:String, data:Object) {
let req = new ApiRequest('GET', url);
if (data) {
req.query(data);
}
return req;
}
/**
* Helper for making a HEAD request
* @param {String} url
* @param {Object} [data]
* @returns {ApiRequest}
*/
export function head(url:String, data:Object) {
let req = new ApiRequest('HEAD', url);
if (data) {
req.send(data);
}
return req;
}
/**
* Helper for making a DELETE request
* @param {String} url
* * @param {Object} [data]
* @returns {ApiRequest}
*/
export function del(url:String, data:Object) {
let req = new ApiRequest('DELETE', url);
if (data) {
req.send(data);
}
return req;
}
/**
* Helper for making a PATCH request
* @param {String} url
* @param {Object} [data]
* @returns {ApiRequest}
*/
export function patch(url:String, data:Object) {
let req = new ApiRequest('PATCH', url);
if (data) {
req.send(data);
}
return req;
}
/**
* Helper for making a POST request
* @param {String} url
* @param {Object} [data]
* @returns {ApiRequest}
*/
export function post(url:String, data:Object) {
let req = new ApiRequest('POST', url);
if (data) {
req.send(data);
}
return req;
}
/**
* Helper for making a PUT request
* @param {String} url
* @param {Object} [data]
* @returns {ApiRequest}
*/
export function put(url:String, data:Object) {
let req = new ApiRequest('PUT', url);
if (data) {
req.send(data);
}
return req;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment