Merge pull request #21 from franze6/DID-2

Создание fetch wrapper
This commit is contained in:
Kirill Andrusyak
2022-06-21 11:47:58 +03:00
committed by GitHub
3 changed files with 118 additions and 3 deletions

19
package-lock.json generated
View File

@@ -12580,6 +12580,25 @@
"integrity": "sha512-bRuZp3C0itgLKHu/VNxi66DN/XVkQG7xtoBVWxpvC5FhAqbOCP21+nPhULjnzEqd7xBMybp6KwytdUpZKEgpIQ==",
"requires": {}
},
"mobx": {
"version": "6.6.0",
"resolved": "https://registry.npmjs.org/mobx/-/mobx-6.6.0.tgz",
"integrity": "sha512-MNTKevLH/6DShLZcmSL351+JgiJPO56A4GUpoiDQ3/yZ0mAtclNLdHK9q4BcQhibx8/JSDupfTpbX2NZPemlRg=="
},
"mobx-react": {
"version": "7.5.0",
"resolved": "https://registry.npmjs.org/mobx-react/-/mobx-react-7.5.0.tgz",
"integrity": "sha512-riHu0XZJA6f64L1iXZoAaDjVt6suYoy8I2HIfuz2tX3O4FFaAe4lVA2CoObttmUQTTFPM7j3Df6T4re0cHkghQ==",
"requires": {
"mobx-react-lite": "^3.4.0"
}
},
"mobx-react-lite": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/mobx-react-lite/-/mobx-react-lite-3.4.0.tgz",
"integrity": "sha512-bRuZp3C0itgLKHu/VNxi66DN/XVkQG7xtoBVWxpvC5FhAqbOCP21+nPhULjnzEqd7xBMybp6KwytdUpZKEgpIQ==",
"requires": {}
},
"moment": {
"version": "2.29.3",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.3.tgz",

View File

@@ -2,7 +2,7 @@ import React, { useState } from 'react';
import Sidebar from '../../partials/Sidebar';
import Header from '../../partials/Header';
function Verify () {
const Verify = (() => {
const [sidebarOpen, setSidebarOpen] = useState(false);
const [descriptionOpen, setDescriptionOpen] = useState(false);
@@ -165,6 +165,6 @@ function Verify () {
</div>
</div>
)
}
})
export default Verify;

View File

@@ -0,0 +1,96 @@
function prepareUrl(url) {
if (url.startsWith('http')) return url;
return `/api/${url}`;
}
function handleRequest(method, url, attempts, token, body) {
return new Promise((resolve, reject) => {
(function internalRequest() {
return request(method, url, token, body)
.then(resolve)
.catch(err => --attempts > 0 ? internalRequest() : reject(err))
})()
})
.then((res) => res.json())
.catch(() => [])
};
/**
* Request
* @param {string} url - endpoint url.
* @param {object} body - request body.
*/
function request(method, url, token, body) {
let controller = new AbortController;
let requestOptions = {};
if (method === 'GET' || method === 'DELETE'){
requestOptions = {
method: method,
headers: {
Authorization: `Token ${token}`,
},
signal: controller.signal,
}
};
if (method === 'POST' || method === 'PUT'){
requestOptions = {
method: method,
headers: {
Authorization: `Token ${token}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
signal: controller.signal,
}
};
if (!token) {
requestOptions.headers.Authorization = null;
}
setTimeout(() => controller.abort(), 3000);
return fetch(url, requestOptions);
};
function get(url, attempts = 1) {
return handleRequest('GET', url, attempts, null);
}
function getAuth(url, attempts = 1) {
return handleRequest('GET', url, attempts, registrationStore.tokenKey);
}
function del(url, attempts = 1) {
return handleRequest('DELETE', url, attempts, null);
}
function delAuth(url, attempts = 1) {
return handleRequest('DELETE', url, attempts, registrationStore.tokenKey);
}
function post(url, body, attempts = 1) {
return handleRequest('POST', url, attempts, null, body);
}
function postAuth(url, body, attempts = 1) {
return handleRequest('POST', url, attempts, registrationStore.tokenKey, body);
}
function put(url, body, attempts = 1) {
return handleRequest('PUT', url, attempts, null, body);
}
function putAuth(url, body, attempts = 1) {
return handleRequest('PUT', url, attempts, registrationStore.tokenKey, body);
}
export const fetchWrapper = {
get,
del,
post,
put,
getAuth,
delAuth,
postAuth,
putAuth,
baseUrl: ''
};