mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-11-17 21:53:03 +05:30
Change functions order in request service
This commit is contained in:
parent
35d430e13c
commit
0ef3b00ee2
@ -1,33 +1,35 @@
|
||||
function convertQueryValue(value) {
|
||||
if (typeof value === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (value === true) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (value === false) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function buildQuery(data = {}) {
|
||||
return Object.keys(data)
|
||||
.map(
|
||||
(keyName) =>
|
||||
[keyName, convertQueryValue(data[keyName])]
|
||||
.map(encodeURIComponent)
|
||||
.join('=')
|
||||
)
|
||||
.join('&')
|
||||
;
|
||||
}
|
||||
|
||||
const middlewares = [];
|
||||
|
||||
export default {
|
||||
post(url, data) {
|
||||
return doFetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||
},
|
||||
body: buildQuery(data)
|
||||
});
|
||||
},
|
||||
|
||||
get(url, data) {
|
||||
if (typeof data === 'object') {
|
||||
const separator = url.indexOf('?') === -1 ? '?' : '&';
|
||||
url += separator + buildQuery(data);
|
||||
}
|
||||
|
||||
return doFetch(url);
|
||||
},
|
||||
|
||||
buildQuery,
|
||||
|
||||
addMiddleware(middleware) {
|
||||
if (!middlewares.find((mdware) => mdware === middleware)) {
|
||||
middlewares.push(middleware);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const checkStatus = (resp) => Promise[resp.status >= 200 && resp.status < 300 ? 'resolve' : 'reject'](resp);
|
||||
const toJSON = (resp) => resp.json();
|
||||
const rejectWithJSON = (resp) => toJSON(resp).then((resp) => {throw resp;});
|
||||
@ -66,31 +68,30 @@ function runMiddlewares(action, data, restart) {
|
||||
);
|
||||
}
|
||||
|
||||
export default {
|
||||
post(url, data) {
|
||||
return doFetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||
},
|
||||
body: buildQuery(data)
|
||||
});
|
||||
},
|
||||
|
||||
get(url, data) {
|
||||
if (typeof data === 'object') {
|
||||
const separator = url.indexOf('?') === -1 ? '?' : '&';
|
||||
url += separator + buildQuery(data);
|
||||
function convertQueryValue(value) {
|
||||
if (typeof value === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return doFetch(url);
|
||||
},
|
||||
|
||||
buildQuery,
|
||||
|
||||
addMiddleware(middleware) {
|
||||
if (!middlewares.find((mdware) => mdware === middleware)) {
|
||||
middlewares.push(middleware);
|
||||
if (value === true) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (value === false) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function buildQuery(data = {}) {
|
||||
return Object.keys(data)
|
||||
.map(
|
||||
(keyName) =>
|
||||
[keyName, convertQueryValue(data[keyName])]
|
||||
.map(encodeURIComponent)
|
||||
.join('=')
|
||||
)
|
||||
.join('&')
|
||||
;
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user