traverson
Version:
Hypermedia API/HATEOAS client for Node.js and the browser
128 lines (110 loc) • 3.6 kB
JavaScript
;
var superagent = require('superagent');
function Request() {}
Request.prototype.get = function(uri, options, callback) {
return mapRequest(superagent.get(uri), options)
.end(handleResponse(callback));
};
Request.prototype.post = function(uri, options, callback) {
return mapRequest(superagent.post(uri), options)
.end(handleResponse(callback));
};
Request.prototype.put = function(uri, options, callback) {
return mapRequest(superagent.put(uri), options)
.end(handleResponse(callback));
};
Request.prototype.patch = function(uri, options, callback) {
return mapRequest(superagent.patch(uri), options)
.end(handleResponse(callback));
};
Request.prototype.del = function(uri, options, callback) {
return mapRequest(superagent.del(uri), options)
.end(handleResponse(callback));
};
function mapRequest(superagentRequest, options) {
options = options || {};
mapQuery(superagentRequest, options);
mapHeaders(superagentRequest, options);
mapAuth(superagentRequest, options);
mapBody(superagentRequest, options);
mapForm(superagentRequest, options);
mapWithCredentials(superagentRequest, options);
return superagentRequest;
}
function mapQuery(superagentRequest, options) {
var qs = options.qs;
if (qs != null) {
superagentRequest = superagentRequest.query(qs);
}
}
function mapHeaders(superagentRequest, options) {
var headers = options.headers;
if (headers != null) {
superagentRequest = superagentRequest.set(headers);
}
}
function mapAuth(superagentRequest, options) {
var auth = options.auth;
if (auth != null) {
superagentRequest = superagentRequest.auth(
auth.user || auth.username,
auth.pass || auth.password
);
}
}
function mapBody(superagentRequest, options) {
if (options != null) {
var body = options.body;
if (body != null) {
superagentRequest = superagentRequest.send(body);
}
}
}
function mapForm(superagentRequest, options) {
if (options != null) {
var form = options.form;
if (form != null) {
// content-type header needs to be set before calling send AND it NEEDS
// to be all lower case otherwise superagent automatically sets
// application/json as content-type :-/
superagentRequest = superagentRequest.set('content-type',
'application/x-www-form-urlencoded');
superagentRequest = superagentRequest.send(form);
}
}
}
function mapWithCredentials(superagentRequest, options) {
if (options != null) {
var withCredentials = options.withCredentials;
if (withCredentials === true) {
// https://visionmedia.github.io/superagent/#cors
superagentRequest.withCredentials();
}
}
}
// map XHR response object properties to Node.js request lib's response object
// properties
function mapResponse(response) {
response.body = response.text;
response.statusCode = response.status;
return response;
}
function handleResponse(callback) {
return function(err, response) {
if (err) {
if (!response) {
// network error or timeout, no response
return callback(err);
} else {
// Since 1.0.0 superagent calls the callback with an error if the status
// code of the response is not in the 2xx range. In this cases, it also
// passes in the response. To align things with request, call the
// callback without the error but just with the response.
callback(null, mapResponse(response));
}
} else {
callback(null, mapResponse(response));
}
};
}
module.exports = new Request();