refactor: convert function expressions to declarations (#4926)
This commit is contained in:
committed by
GitHub
parent
c0236536dd
commit
141a2eba56
@ -3,15 +3,16 @@ import curry from 'lodash/curry';
|
||||
import flow from 'lodash/flow';
|
||||
import isString from 'lodash/isString';
|
||||
|
||||
const isAbortControllerSupported = () => {
|
||||
function isAbortControllerSupported() {
|
||||
if (typeof window !== 'undefined') {
|
||||
return !!window.AbortController;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
const timeout = 60;
|
||||
const fetchWithTimeout = (input, init) => {
|
||||
|
||||
function fetchWithTimeout(input, init) {
|
||||
if ((init && init.signal) || !isAbortControllerSupported()) {
|
||||
return fetch(input, init);
|
||||
}
|
||||
@ -28,42 +29,47 @@ const fetchWithTimeout = (input, init) => {
|
||||
}
|
||||
throw e;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
const decodeParams = paramsString =>
|
||||
List(paramsString.split('&'))
|
||||
function decodeParams(paramsString) {
|
||||
return List(paramsString.split('&'))
|
||||
.map(s => List(s.split('=')).map(decodeURIComponent))
|
||||
.update(Map);
|
||||
}
|
||||
|
||||
const fromURL = wholeURL => {
|
||||
function fromURL(wholeURL) {
|
||||
const [url, allParamsString] = wholeURL.split('?');
|
||||
return Map({ url, ...(allParamsString ? { params: decodeParams(allParamsString) } : {}) });
|
||||
};
|
||||
}
|
||||
|
||||
const fromFetchArguments = (wholeURL, options) => {
|
||||
function fromFetchArguments(wholeURL, options) {
|
||||
return fromURL(wholeURL).merge(
|
||||
(options ? fromJS(options) : Map()).remove('url').remove('params'),
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
const encodeParams = params =>
|
||||
params
|
||||
function encodeParams(params) {
|
||||
return params
|
||||
.entrySeq()
|
||||
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
|
||||
.join('&');
|
||||
}
|
||||
|
||||
const toURL = req =>
|
||||
`${req.get('url')}${req.get('params') ? `?${encodeParams(req.get('params'))}` : ''}`;
|
||||
function toURL(req) {
|
||||
return `${req.get('url')}${req.get('params') ? `?${encodeParams(req.get('params'))}` : ''}`;
|
||||
}
|
||||
|
||||
const toFetchArguments = req => [
|
||||
toURL(req),
|
||||
req
|
||||
.remove('url')
|
||||
.remove('params')
|
||||
.toJS(),
|
||||
];
|
||||
function toFetchArguments(req) {
|
||||
return [
|
||||
toURL(req),
|
||||
req
|
||||
.remove('url')
|
||||
.remove('params')
|
||||
.toJS(),
|
||||
];
|
||||
}
|
||||
|
||||
const maybeRequestArg = req => {
|
||||
function maybeRequestArg(req) {
|
||||
if (isString(req)) {
|
||||
return fromURL(req);
|
||||
}
|
||||
@ -71,9 +77,15 @@ const maybeRequestArg = req => {
|
||||
return fromJS(req);
|
||||
}
|
||||
return Map();
|
||||
};
|
||||
const ensureRequestArg = func => req => func(maybeRequestArg(req));
|
||||
const ensureRequestArg2 = func => (arg, req) => func(arg, maybeRequestArg(req));
|
||||
}
|
||||
|
||||
function ensureRequestArg(func) {
|
||||
return req => func(maybeRequestArg(req));
|
||||
}
|
||||
|
||||
function ensureRequestArg2(func) {
|
||||
return (arg, req) => func(arg, maybeRequestArg(req));
|
||||
}
|
||||
|
||||
// This actually performs the built request object
|
||||
const performRequest = ensureRequestArg(req => {
|
||||
@ -84,9 +96,14 @@ const performRequest = ensureRequestArg(req => {
|
||||
// Each of the following functions takes options and returns another
|
||||
// function that performs the requested action on a request.
|
||||
const getCurriedRequestProcessor = flow([ensureRequestArg2, curry]);
|
||||
const getPropSetFunction = path => getCurriedRequestProcessor((val, req) => req.setIn(path, val));
|
||||
const getPropMergeFunction = path =>
|
||||
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p = Map()) => p.merge(obj)));
|
||||
|
||||
function getPropSetFunction(path) {
|
||||
return getCurriedRequestProcessor((val, req) => req.setIn(path, val));
|
||||
}
|
||||
|
||||
function getPropMergeFunction(path) {
|
||||
return getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p = Map()) => p.merge(obj)));
|
||||
}
|
||||
|
||||
const withMethod = getPropSetFunction(['method']);
|
||||
const withBody = getPropSetFunction(['body']);
|
||||
|
Reference in New Issue
Block a user