Merge pull request #96 from dderbentsov/UC-29
[WIP] Добавил обертку для запросов
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { fetchWrapper } from "../../shared/fetchWrapper.js";
|
||||
import * as moment from "moment/moment";
|
||||
import CalendarSchedule from "./components/CalendarSchedule.vue";
|
||||
import CalendarSidebar from "./components/CalendarSidebar.vue";
|
||||
@@ -68,14 +69,14 @@ export default {
|
||||
this.membersData = res.results;
|
||||
},
|
||||
fetchEventsData() {
|
||||
fetch("http://45.84.227.122:8080/registry/event/")
|
||||
.then((res) => res.json())
|
||||
fetchWrapper
|
||||
.get("registry/event/")
|
||||
.then((res) => this.saveEventsData(res));
|
||||
fetch("http://45.84.227.122:8080/general/employee/")
|
||||
.then((res) => res.json())
|
||||
fetchWrapper
|
||||
.get("general/employee/")
|
||||
.then((res) => this.saveEmployeesData(res));
|
||||
fetch("http://45.84.227.122:8080/general/person/")
|
||||
.then((res) => res.json())
|
||||
fetchWrapper
|
||||
.get("general/person/")
|
||||
.then((res) => this.saveMembersData(res));
|
||||
},
|
||||
changeWidth(value) {
|
||||
|
||||
@@ -70,7 +70,7 @@ export default {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(this.user),
|
||||
};
|
||||
fetch("http://45.84.227.122:8080/auth/jwt/create", requestOptions)
|
||||
fetch("http://45.84.227.122:8080/auth/jwt/create/", requestOptions)
|
||||
.then((result) => result.json())
|
||||
.then((token) => {
|
||||
localStorage.setItem("tokenAccess", token.access);
|
||||
|
||||
78
src/shared/fetchWrapper.js
Normal file
78
src/shared/fetchWrapper.js
Normal file
@@ -0,0 +1,78 @@
|
||||
function prepareUrl(url) {
|
||||
if (url.startsWith("http")) return url;
|
||||
return `http://45.84.227.122:8080/${url}`;
|
||||
}
|
||||
|
||||
function handleRequest(method, url, headers, attempts, body) {
|
||||
return new Promise((resolve, reject) => {
|
||||
(function internalRequest() {
|
||||
return request(method, url, headers, body)
|
||||
.then(resolve)
|
||||
.catch((err) => (--attempts > 0 ? internalRequest() : reject(err)));
|
||||
})();
|
||||
})
|
||||
.then((res) => {
|
||||
return res.json();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return err;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Request
|
||||
* @param {string} method - method name
|
||||
* @param {string} url - endpoint url.
|
||||
* @param {object} body - request body.
|
||||
* @param {object} headers - request headers
|
||||
*/
|
||||
function request(method, url, headers = {}, body) {
|
||||
let token = localStorage.getItem("tokenAccess");
|
||||
let requestOptions = {};
|
||||
if (method === "GET" || method === "DELETE") {
|
||||
requestOptions = {
|
||||
method: method,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
...headers,
|
||||
},
|
||||
};
|
||||
}
|
||||
if (method === "POST" || method === "PUT") {
|
||||
requestOptions = {
|
||||
method: method,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
...headers,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
}
|
||||
return fetch(prepareUrl(url), requestOptions);
|
||||
}
|
||||
|
||||
function get(url, headers, attempts = 3) {
|
||||
return handleRequest("GET", url, headers, attempts, null);
|
||||
}
|
||||
|
||||
function del(url, headers, attempts = 3) {
|
||||
return handleRequest("DELETE", url, headers, attempts, null);
|
||||
}
|
||||
|
||||
function post(url, headers, body, attempts = 3) {
|
||||
return handleRequest("POST", url, headers, attempts, null, body);
|
||||
}
|
||||
|
||||
function put(url, headers, body, attempts = 3) {
|
||||
return handleRequest("PUT", url, headers, attempts, null, body);
|
||||
}
|
||||
|
||||
export const fetchWrapper = {
|
||||
get,
|
||||
del,
|
||||
post,
|
||||
put,
|
||||
};
|
||||
Reference in New Issue
Block a user