-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
113 lines (106 loc) · 3.39 KB
/
api.js
File metadata and controls
113 lines (106 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const json = {
token: ''
}
let connection = new Connection('test')
const urlAuth = connection.getAuthServiceConnection()
const urlCore = connection.getStorageServiceConnection()
/**
* Factory for api
* @param url
* @param method (GET or POST)
* @param endpoint
* @param message (message for errors)
* @param headers
*/
//super class, on the basis of which the same
//objects with different values are created
function makeRequest(url, method, endpoint, message, headers) {
return async function (data = null) {
try {
console.log(JSON.stringify(data))
//add headers
const header = headers !== undefined ? headers : {};
//add header with token
header['Authorization'] = getCookie('token');
//assign method, data to the request
const response = await fetch(url + endpoint, {
method: method,
body: data === null ? undefined : data,
headers: header
});
//get response from the server
return await response.json()
} catch (e) {
console.error(e.message)
document.getElementById("status").innerText = "Error in " + message
}
}
}
//factory class for requests
const API = {
//a request is created depending on the method, endpoint, headers
sendData: makeRequest(urlCore, 'POST', "/data/", "sending data"),
getData: makeRequest(urlCore, 'GET', "/category/", "getting categories"),
auth: makeRequest(urlAuth, 'POST', "/sign_in/", "authorization", {'Content-Type': 'application/json'}),
register: makeRequest(urlAuth,'POST', "/sign_up/", "registration", {'Content-Type': 'application/json'})
}
/**
* method for downloading files
* @param url (endpoint with category id)
*/
async function downloadCategory(url) {
//add headers with token
const options = {
headers: {
Authorization: getCookie('token')
}
};
try {
//get a response from the server
let response = await fetch(url, options);
//get filename from header 'content-disposition'
let filename = response.headers.get('content-disposition').split('filename=')[1].split(';')[0];
//get file from response and download it
let blob = await response.blob()
let link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename
link.click();
link.remove();
} catch (e) {
console.error(e.message)
}
}
/**
* Method for set cookie
* @param json (json with token)
*/
function setCookie(json) {
let date = new Date(Date.now() + 86400e3);
date = date.toUTCString();
document.cookie = `token=${json.token}; expires=` + date;
}
/**
* Method for getting cookie
* @param name (the parameter we want to get from the cookie)
* @returns {string} (the parameter we want to get from the cookie)
*/
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts.pop().split(';').shift()
} else {
return 'undefined';
}
}
/**
* Method to check token in cookie
* and if there is no token, then the user is redirected to the
* authorization page
*/
function checkCookie() {
if (getCookie('token') === 'undefined') {
window.location.href = "auth.html"
}
}