router fix

This commit is contained in:
kandrusyak
2022-06-22 14:53:31 +03:00
parent 83e5e39d3c
commit d617de75b5
3 changed files with 33 additions and 25 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
# dependencies # dependencies
/node_modules /node_modules
.idea

View File

@@ -1,7 +1,7 @@
import React from 'react' import React from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
import {Buffer} from "buffer"; import {Buffer} from "buffer";
import { BrowserRouter as Router } from 'react-router-dom' import { HashRouter as Router } from 'react-router-dom'
import App from './App' import App from './App'
globalThis.Buffer = Buffer globalThis.Buffer = Buffer

View File

@@ -1,12 +1,14 @@
import {registrationStore} from '../store/store';
function prepareUrl(url) { function prepareUrl(url) {
if (url.startsWith('http')) return url; if (url.startsWith('http')) return url;
return `/api/${url}`; return `http://3.125.47.101/api/${url}`;
} }
function handleRequest(method, url, attempts, token, body) { function handleRequest(method, url, headers, attempts, token, body) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
(function internalRequest() { (function internalRequest() {
return request(method, url, token, body) return request(method, url, headers, token, body)
.then(resolve) .then(resolve)
.catch(err => --attempts > 0 ? internalRequest() : reject(err)) .catch(err => --attempts > 0 ? internalRequest() : reject(err))
})() })()
@@ -17,17 +19,21 @@ function handleRequest(method, url, attempts, token, body) {
/** /**
* Request * Request
* @param {string} method - method name
* @param {string} url - endpoint url. * @param {string} url - endpoint url.
* @param {string} token - auth token
* @param {object} body - request body. * @param {object} body - request body.
* @param {object} headers - request headers
*/ */
function request(method, url, token, body) { function request(method, url, headers = {}, token, body) {
let controller = new AbortController; let controller = new AbortController;
let requestOptions = {}; let requestOptions = {};
if (method === 'GET' || method === 'DELETE'){ if (method === 'GET' || method === 'DELETE'){
requestOptions = { requestOptions = {
method: method, method: method,
headers: { headers: {
Authorization: `Token ${token}`, authorization: `IDNTTY ${token}`,
...headers
}, },
signal: controller.signal, signal: controller.signal,
} }
@@ -36,9 +42,10 @@ function request(method, url, token, body) {
requestOptions = { requestOptions = {
method: method, method: method,
headers: { headers: {
Authorization: `Token ${token}`, authorization: `IDNTTY ${token}`,
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...headers
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
signal: controller.signal, signal: controller.signal,
@@ -48,39 +55,39 @@ function request(method, url, token, body) {
requestOptions.headers.Authorization = null; requestOptions.headers.Authorization = null;
} }
setTimeout(() => controller.abort(), 3000); setTimeout(() => controller.abort(), 3000);
return fetch(url, requestOptions); return fetch(prepareUrl(url), requestOptions);
}; };
function get(url, attempts = 1) { function get(url, headers, attempts = 1) {
return handleRequest('GET', url, attempts, null); return handleRequest('GET', url, headers, attempts, null);
} }
function getAuth(url, attempts = 1) { function getAuth(url, headers, attempts = 1) {
return handleRequest('GET', url, attempts, registrationStore.tokenKey); return handleRequest('GET', url, headers, attempts, registrationStore.tokenKey);
} }
function del(url, attempts = 1) { function del(url, headers, attempts = 1) {
return handleRequest('DELETE', url, attempts, null); return handleRequest('DELETE', url, headers, attempts, null);
} }
function delAuth(url, attempts = 1) { function delAuth(url, headers, attempts = 1) {
return handleRequest('DELETE', url, attempts, registrationStore.tokenKey); return handleRequest('DELETE', url, headers, attempts, registrationStore.tokenKey);
} }
function post(url, body, attempts = 1) { function post(url, headers, body, attempts = 1) {
return handleRequest('POST', url, attempts, null, body); return handleRequest('POST', url, headers, attempts, null, body);
} }
function postAuth(url, body, attempts = 1) { function postAuth(url, headers, body, attempts = 1) {
return handleRequest('POST', url, attempts, registrationStore.tokenKey, body); return handleRequest('POST', url, headers, attempts, registrationStore.tokenKey, body);
} }
function put(url, body, attempts = 1) { function put(url, headers, body, attempts = 1) {
return handleRequest('PUT', url, attempts, null, body); return handleRequest('PUT', url, headers, attempts, null, body);
} }
function putAuth(url, body, attempts = 1) { function putAuth(url, headers, body, attempts = 1) {
return handleRequest('PUT', url, attempts, registrationStore.tokenKey, body); return handleRequest('PUT', url, headers, attempts, registrationStore.tokenKey, body);
} }
export const fetchWrapper = { export const fetchWrapper = {