2016-07-15 19:34:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* Module dependencies.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-26 10:10:37 +00:00
|
|
|
|
export default class Requester {
|
|
|
|
|
|
2016-07-15 19:34:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* Make an ajax request.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} request
|
|
|
|
|
* @param {Function} success callback
|
|
|
|
|
* @param {Function} failure callback
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
|
2016-07-23 10:31:40 +00:00
|
|
|
|
fetch(request, resolve, reject) {
|
2016-09-08 19:42:00 +00:00
|
|
|
|
const {method, uri, headers, data} = request;
|
2016-07-23 10:31:40 +00:00
|
|
|
|
|
2016-06-26 10:10:37 +00:00
|
|
|
|
const success = (responseText) => {
|
|
|
|
|
resolve(
|
2016-07-23 10:31:40 +00:00
|
|
|
|
request,
|
2017-01-13 13:23:15 +00:00
|
|
|
|
{
|
|
|
|
|
status: 200,
|
|
|
|
|
statusText: 'OK',
|
|
|
|
|
responseText
|
|
|
|
|
}
|
2016-06-26 10:10:37 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fail = ({status, statusText, errorThrown}) => {
|
|
|
|
|
reject(
|
2016-07-23 10:31:40 +00:00
|
|
|
|
request,
|
2017-01-13 13:23:15 +00:00
|
|
|
|
{
|
|
|
|
|
status,
|
|
|
|
|
statusText,
|
|
|
|
|
errorThrown,
|
|
|
|
|
errors: `HTTP ${status} ${statusText?statusText:''}`
|
|
|
|
|
}
|
2016-06-26 10:10:37 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const xmlhttp = new XMLHttpRequest();
|
|
|
|
|
xmlhttp.onreadystatechange = () => {
|
2017-01-15 11:00:51 +00:00
|
|
|
|
if (xmlhttp.readyState === 4) { //XMLHttpRequest.DONE
|
2016-06-26 10:10:37 +00:00
|
|
|
|
if (xmlhttp.status === 200) {
|
|
|
|
|
success(xmlhttp.responseText);
|
|
|
|
|
} else {
|
|
|
|
|
fail({status: xmlhttp.status, statusText: xmlhttp.statusText});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
xmlhttp.open(method, uri, true);
|
|
|
|
|
if (headers) {
|
2017-01-15 11:00:51 +00:00
|
|
|
|
Object.keys(headers).forEach((header) => {
|
2016-06-26 10:10:37 +00:00
|
|
|
|
xmlhttp.setRequestHeader(header, headers[header]);
|
2017-01-15 11:00:51 +00:00
|
|
|
|
});
|
2016-06-26 10:10:37 +00:00
|
|
|
|
}
|
|
|
|
|
if (data) {
|
2016-07-09 22:58:03 +00:00
|
|
|
|
xmlhttp.send(data);
|
2016-06-26 10:10:37 +00:00
|
|
|
|
} else {
|
|
|
|
|
xmlhttp.send();
|
|
|
|
|
}
|
|
|
|
|
} catch (errorThrown) {
|
|
|
|
|
fail({errorThrown});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-13 13:23:15 +00:00
|
|
|
|
}
|
2017-06-22 19:17:18 +00:00
|
|
|
|
|
|
|
|
|
export const httpGetTransformer = {
|
|
|
|
|
uri({uri, headers, data}) {
|
|
|
|
|
if (!data) {
|
|
|
|
|
return uri;
|
|
|
|
|
}
|
|
|
|
|
let [uriWithoutAnchor, anchor] = [uri, ''];
|
|
|
|
|
const match = /^(.*)(#.*)$/.exec(uri);
|
|
|
|
|
if (match) {
|
|
|
|
|
[,uriWithoutAnchor, anchor] = /^(.*)(#.*)$/.exec(uri);
|
|
|
|
|
}
|
|
|
|
|
uriWithoutAnchor = Object.keys(data).reduce((gUri, d, index) => {
|
|
|
|
|
gUri += `${(index === 0 && gUri.indexOf('?') === -1)?'?':'&'}${d}=${data[d]}`;
|
|
|
|
|
return gUri;
|
|
|
|
|
}, uriWithoutAnchor);
|
|
|
|
|
return uriWithoutAnchor + anchor;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// export const httpPostTransformer = {
|
|
|
|
|
// headers({uri, headers, data}) {
|
|
|
|
|
// if (!data) {
|
|
|
|
|
// return headers;
|
|
|
|
|
// }
|
|
|
|
|
// const updatedHeaders = headers || {};
|
|
|
|
|
// if (!updatedHeaders['Content-Type']) {
|
|
|
|
|
// updatedHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
|
|
|
// }
|
|
|
|
|
// return updatedHeaders;
|
|
|
|
|
// }
|
|
|
|
|
// };
|