This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathapi.js
129 lines (100 loc) · 2.83 KB
/
api.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import axios from 'axios'
import env from '../config/environment'
const getSplitValue = (key, raw, delim1, delim2) => {
const vars = raw.split(delim1)
for (let i = 0; i < vars.length; i += 1) {
const pair = vars[i].split(delim2)
if (pair.length === 2) {
const pairKey = pair[0].trim()
const pairValue = pair[1].trim()
if (pairKey === key && pairValue) {
return pairValue
}
}
}
return null
}
export const getQueryValue = (queryString, key) => getSplitValue(key, queryString.substring(1), '&', '=')
class Api {
constructor() {
this.proxy = axios.create({
baseURL: env ? env.ApiBaseURL() : '/api',
timeout: 30000,
withCredentials: true,
})
this.proxy.interceptors.response.use(this.handleResponseSuccess, this.handleResponseError)
}
handleResponseSuccess = (response) => {
// On a successful /status call, stash the CSRF token for later use.
if (response.config.url.endsWith(env.EndpointStatus())) {
this.csrfToken = response.headers['x-csrf-token']
}
return response
}
handleResponseError = (error) => {
console.warn(`API request failed: ${error.message}`)
return Promise.reject(error)
}
information() {
return this.proxy.get('/')
}
get(endpoint) {
return this.proxy.get(endpoint)
}
post(endpoint, params = {}, ignoreCSRF = false) {
const { csrfToken } = this
const headers = {}
if (!ignoreCSRF && !csrfToken) {
console.error('Attempting to make a POST without a CSRF Token set.', endpoint)
} else {
headers['X-CSRF-Token'] = csrfToken
}
return this.proxy.post(endpoint, params, { headers })
}
/** AUTH */
saml() {
return this.get(env.EndpointSaml())
}
samlSLO() {
return this.get(env.EndpointSamlSLO())
}
login = (username, password) => this.post(
env.EndpointBasicAuthentication(),
{ username, password },
true,
)
logout = () => this.post(env.EndpointLogout(), {}, true)
refresh = () => this.post(env.EndpointRefresh())
/** FORM */
save(payload) {
return this.post(env.EndpointSave(), payload)
}
status = () => this.get(env.EndpointStatus())
submit() {
return this.post(env.EndpointSubmit())
}
form = () => this.get(env.EndpointForm())
validate(payload) {
return this.post(env.EndpointValidate(), payload)
}
/** ATTACHMENTS */
listAttachments() {
return this.get(env.EndpointAttachment())
}
saveAttachment(formData) {
return this.post(env.EndpointAttachment(), formData)
}
updateAttachment(id, description) {
return this.post(env.EndpointAttachmentUpdate(id), {
description,
})
}
getAttachment(id) {
return this.get(env.EndpointAttachmentGet(id))
}
deleteAttachment(id) {
return this.post(env.EndpointAttachmentDelete(id))
}
}
const api = new Api()
export { api }