8 Commits
DID-1 ... DID-2

Author SHA1 Message Date
Kirill Andrusyak
f89d59e7df Merge branch 'master' into DID-2 2022-06-21 11:47:49 +03:00
Kirill Andrusyak
c944644db0 Merge pull request #23 from franze6/DID-1
Did 1
2022-06-21 11:44:57 +03:00
Daria Golova
f209ee5f5f added token availability check 2022-06-16 16:05:48 +03:00
Daria Golova
9bef3d9311 fix authorization by token 2022-06-16 15:25:25 +03:00
Daria Golova
632b98a81a added authorization by token 2022-06-16 14:30:41 +03:00
Daria Golova
81f5ec2741 fix methods 2022-06-15 14:36:23 +03:00
Daria Golova
3fcda54070 fix wrapper 2022-06-15 14:21:39 +03:00
Daria Golova
49aff14a80 create fetch wrapper 2022-06-14 13:53:28 +03:00
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==", "integrity": "sha512-bRuZp3C0itgLKHu/VNxi66DN/XVkQG7xtoBVWxpvC5FhAqbOCP21+nPhULjnzEqd7xBMybp6KwytdUpZKEgpIQ==",
"requires": {} "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": { "moment": {
"version": "2.29.3", "version": "2.29.3",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.3.tgz", "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 Sidebar from '../../partials/Sidebar';
import Header from '../../partials/Header'; import Header from '../../partials/Header';
function Verify () { const Verify = (() => {
const [sidebarOpen, setSidebarOpen] = useState(false); const [sidebarOpen, setSidebarOpen] = useState(false);
const [descriptionOpen, setDescriptionOpen] = useState(false); const [descriptionOpen, setDescriptionOpen] = useState(false);
@@ -165,6 +165,6 @@ function Verify () {
</div> </div>
</div> </div>
) )
} })
export default Verify; 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: ''
};