redux-resource-xhr
Version:
Action creators for Redux Resource using the xhr library
95 lines (85 loc) • 3.06 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
import xhr from 'xhr';
import qs from 'querystringify';
/* eslint no-param-reassign: 'off' */
// This is a small wrapper around `xhr`.
// It has two improvements:
// 1. Omit the `cb`, and a Promise will be returned instead
// 2. Pass a `qs` option for query string support
function buildUrl(uri, options) {
var qsStringify = options.qsStringify || qs.stringify;
if (options.qs) {
var stringified = qsStringify(options.qs, options.qsStringifyOptions);
if (stringified[0] !== '?') {
stringified = '?' + stringified;
}
uri += stringified;
}
return uri;
}
export default function request(uri, options, cb) {
var params = {};
// This handles the `xhr(options, cb)` syntax
if ((typeof uri === 'undefined' ? 'undefined' : _typeof(uri)) === 'object') {
params = uri;
} else if (typeof uri === 'string' && (typeof options === 'undefined' ? 'undefined' : _typeof(options)) === 'object') {
// This handles the `xhr(uri, options, cb)` syntax
params = options;
params.uri = uri;
} else {
// This handles the `xhr(uri, cb)` syntax
params.uri = uri;
}
// This adds support for the `qs` option
var urlString = params.uri ? params.uri : params.url;
params.uri = buildUrl(urlString, params);
if (params.url) {
delete params.url;
}
var callback = void 0;
if (typeof options === 'function') {
callback = options;
} else if (typeof cb === 'function') {
callback = cb;
}
// Return the `xhr` if a callback was passed. Otherwise, a Promise is returned
if (callback) {
return xhr(params, callback);
} else {
return new Promise(function (resolve, reject) {
xhr(params, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
}
// Also grabbed from xhr's source. This adds the convenience APIs;
// `xhr.get()`, for instance.
// Supported signatures:
//
// xhr[method](url, callback)
// xhr[method](url, options, callback)
// xhr[method](options, callback)
//
['get', 'put', 'post', 'patch', 'head', 'delete'].forEach(function (method) {
request[method === 'delete' ? 'del' : method] = function (uri, options, callback) {
var opts = void 0,
cb = void 0;
if ((typeof uri === 'undefined' ? 'undefined' : _typeof(uri)) === 'object') {
opts = uri;
cb = options;
} else if ((typeof options === 'undefined' ? 'undefined' : _typeof(options)) === 'object') {
opts = Object.assign({ uri: uri }, options);
cb = callback;
} else if (typeof uri === 'string' && (typeof options === 'undefined' ? 'undefined' : _typeof(options)) !== 'object') {
opts = { uri: uri };
cb = options;
}
opts.method = method.toUpperCase();
return request(opts, cb);
};
});