diff --git a/package-lock.json b/package-lock.json index c19a16b..5de8cdc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/src/pages/digitalId/Verify.jsx b/src/pages/digitalId/Verify.jsx index a6a4730..570cc64 100644 --- a/src/pages/digitalId/Verify.jsx +++ b/src/pages/digitalId/Verify.jsx @@ -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 () { ) -} +}) -export default Verify; +export default Verify; \ No newline at end of file diff --git a/src/shared/fetchWrapper.js b/src/shared/fetchWrapper.js new file mode 100644 index 0000000..a44f72b --- /dev/null +++ b/src/shared/fetchWrapper.js @@ -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: '' +}; \ No newline at end of file