Skip to content

Commit 9a14fef

Browse files
author
Psilo
committed
several refactorings
1 parent 0f7837f commit 9a14fef

File tree

5 files changed

+242
-168
lines changed

5 files changed

+242
-168
lines changed

src/utils/axiosUtils.ts

Lines changed: 57 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,29 @@
11
import Vue from 'vue'
22
import store from '@/store'
3-
import { error } from './loggingUtils'
4-
import { getDeepProperty } from './objectUtils'
3+
import log from './loggingUtils'
4+
import objectUtils from '@/utils/objectUtils'
55

6-
function buildBaseUrl (server) {
7-
const config = getDeepProperty(server, store.getters['rest/config'].servers)
6+
function buildBaseUrl (server: string): string {
7+
const config = objectUtils.getDeepProperty(server, store.getters['rest/config'].servers)
88
return `${config.protocol}://${config.address}:${config.port}`
99
}
1010

11-
export async function handleErrors (wrapped) {
11+
export async function appendErrorCatcher (wrapped: Promise<any>): Promise<any> {
1212
return wrapped.catch(err => {
13-
const status = err ? err.status : err
13+
let status = err
14+
if (err && err.response) {
15+
status = err.response.status
16+
}
1417
let msg = err.message
1518
if (err != null && err.response != null && err.response.data != null && err.response.data.message != null) {
1619
msg = err.response.data.message
1720
}
18-
error(msg, 'communication', status)
21+
log.error(msg, 'communication', status)
22+
throw new Error('Internal Error.')
1923
})
2024
}
2125

22-
function setIndicator (workingIndicator, value) {
23-
if (workingIndicator !== null) {
24-
if (typeof workingIndicator === 'string') {
25-
store.dispatch(workingIndicator, value)
26-
} else {
27-
workingIndicator(value)
28-
}
29-
}
30-
}
31-
32-
function setResponse (dataSetter, response) {
33-
if (dataSetter != null) {
34-
if (typeof dataSetter === 'string') {
35-
store.dispatch(dataSetter, response)
36-
} else {
37-
dataSetter(response)
38-
}
39-
}
40-
}
41-
42-
function provideData (dataProvider) {
26+
function provideData (dataProvider: () => object | string): null | string | object {
4327
if (dataProvider !== null) {
4428
if (typeof dataProvider === 'string') {
4529
return store.getters[dataProvider]
@@ -50,61 +34,76 @@ function provideData (dataProvider) {
5034
return null
5135
}
5236

53-
async function internalRestCall (workingIndicator, responseSetter, restCallPromise) {
54-
setIndicator(workingIndicator, true)
55-
return handleErrors(restCallPromise.then(response => {
56-
setIndicator(workingIndicator, false)
37+
async function internalRestCall (restCallPromise: Promise<any>): Promise<any> {
38+
return appendErrorCatcher(restCallPromise.then(response => {
5739
// console.log(response)
58-
setResponse(responseSetter, response)
5940
return response
6041
}))
6142
}
6243

63-
async function internalGet (server, endpointPath) {
44+
function getAuthorizationHeader (): object {
45+
const token = store.getters['keycloak/token']
46+
if (token == null || token === undefined || token === '') {
47+
return {}
48+
}
49+
return { Authorization: 'Bearer ' + token }
50+
}
51+
52+
async function internalGet (server: string, endpointPath: string, isList: boolean): Promise<object | null> {
6453
// console.log(buildBaseUrl(server) + endpointPath)
6554
return Vue.axios
6655
.get(buildBaseUrl(server) + endpointPath, {
67-
headers: {
68-
}
56+
headers: Object.assign({}, getAuthorizationHeader())
6957
})
7058
.then(response => {
59+
if (response === undefined || response == null) {
60+
throw new Error('Response was null or undefined.')
61+
}
62+
const entries = response.data.entries
63+
if (isList && entries !== undefined && entries == null) {
64+
throw new Error(`Entries of response was null on call to ${server}${endpointPath}.`)
65+
}
7166
return response.data
7267
})
7368
}
7469

75-
async function internalDelete (server, endpointPath) {
70+
async function internalDelete (server: string, endpointPath: string): Promise<any> {
71+
// console.log(buildBaseUrl(server) + endpointPath)
7672
return Vue.axios
7773
.delete(buildBaseUrl(server) + endpointPath, {
7874
data: {
7975
},
80-
headers: {
81-
}
76+
headers: Object.assign({}, getAuthorizationHeader())
8277
})
8378
.then(response => {
8479
return response.data
8580
})
8681
}
8782

88-
async function internalPut (server, endpointPath, dataProvider) {
83+
async function internalPut (server: string, endpointPath: string, dataProvider): Promise<any> {
8984
// console.log(buildBaseUrl(server) + endpointPath)
9085
return Vue.axios
9186
.put(buildBaseUrl(server) + endpointPath, provideData(dataProvider), {
92-
headers: {
93-
}
87+
headers: Object.assign({}, getAuthorizationHeader())
9488
})
9589
.then(response => {
90+
if (response === undefined || response == null) {
91+
throw new Error('Response was null or undefined.')
92+
}
9693
return response.data
9794
})
9895
}
9996

100-
async function internalPost (server, endpointPath, dataProvider) {
97+
async function internalPost (server: string, endpointPath: string, dataProvider): Promise<any> {
10198
// console.log(buildBaseUrl(server) + endpointPath)
10299
return Vue.axios
103100
.post(buildBaseUrl(server) + endpointPath, provideData(dataProvider), {
104-
headers: {
105-
}
101+
headers: Object.assign({}, getAuthorizationHeader())
106102
})
107103
.then(response => {
104+
if (response === undefined || response == null) {
105+
throw new Error('Response was null or undefined.')
106+
}
108107
return response.data
109108
})
110109
}
@@ -113,72 +112,60 @@ async function internalPost (server, endpointPath, dataProvider) {
113112
* Send a GET retrieving the response from the server.
114113
* @param server name of the rest/config/servers property to use
115114
* @param endpointPath path to the correct endpoint-definition starting from rest/config/endpoint/
116-
* @param workingIndicator path to an indicator-action (true/false) or function(value) that will be called with value = (true/false) accordingly
117-
* @param responseSetter path to a vuex-action (object) or function(response) that will be called with the received response
118115
*/
119-
export async function getResponse (server, endpointPath, workingIndicator, responseSetter) {
120-
return internalRestCall(workingIndicator, responseSetter, internalGet(server, getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)))
116+
export async function getResponse (server: string, endpointPath: string): Promise<any> {
117+
return internalRestCall(internalGet(server, objectUtils.getDeepProperty(endpointPath, store.getters['rest/config'].endpoint), false))
121118
}
122119

123120
/**
124121
* Send a GET retrieving a data-object represented by an ID from the server.
125122
* @param server name of the rest/config/servers property to use
126123
* @param endpointPath path to the correct endpoint-definition starting from rest/config/endpoint/
127-
* @param workingIndicator path to an indicator-action (true/false) or function(value) that will be called with value = (true/false) accordingly
128-
* @param responseSetter path to a vuex-action (object) or function(response) that will be called with the received response
129124
* @param id the ID of the object to retrieve
130125
*/
131-
export async function getById (server, endpointPath, id, workingIndicator, responseSetter) {
132-
return internalRestCall(workingIndicator, responseSetter, internalGet(server, `${getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}/${id}`))
126+
export async function getById (server: string, endpointPath: string, id: string | number): Promise<any> {
127+
return internalRestCall(internalGet(server, `${objectUtils.getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}/${id}`, false))
133128
}
134129

135130
/**
136131
* Send a GET retrieving a list of data-objects from the server.
137132
* @param server name of the rest/config/servers property to use
138133
* @param endpointPath path to the correct endpoint-definition starting from rest/config/endpoint/
139-
* @param workingIndicator path to an indicator-action (true/false) or function(value) that will be called with value = (true/false) accordingly
140-
* @param responseSetter path to a vuex-action (object) or function(response) that will be called with the received response
141134
* @param size the size of a single page of the list
142135
* @param offset the number of pages to omit before returning the list
143136
* @param additionalQueryParams a string containing additional query parameters (like 'scanId=5&searchName=hallo' for example)
144137
*/
145-
export async function getList (server, endpointPath, size, offset, workingIndicator, responseSetter, additionalQueryParams) {
146-
return internalRestCall(workingIndicator, responseSetter, internalGet(server, `${getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}?size=${size}&offset=${offset}${additionalQueryParams != null ? '&' + additionalQueryParams : ''}`))
138+
export async function getList (server: string, endpointPath: string, size: number, offset: number, additionalQueryParams: string): Promise<any> {
139+
return internalRestCall(internalGet(server, `${objectUtils.getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}?size=${size}&offset=${offset}${additionalQueryParams != null ? '&' + additionalQueryParams : ''}`, true))
147140
}
148141

149142
/**
150143
* Sends a DEL request to the server for the object with the given ID.
151144
* @param server name of the rest/config/servers property to use
152145
* @param endpointPath path to the correct endpoint-definition starting from rest/config/endpoint/
153-
* @param workingIndicator path to an indicator-action (true/false) or function(value) that will be called with value = (true/false) accordingly
154-
* @param responseSetter path to a vuex-action (object) or function(response) that will be called with the received response
155146
* @param id the ID of the object to retrieve
156147
*/
157-
export async function del (server, endpointPath, id, workingIndicator, responseSetter) {
158-
return internalRestCall(workingIndicator, responseSetter, internalDelete(server, `${getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}/${id}`))
148+
export async function del (server: string, endpointPath: string, id: string | number): Promise<any> {
149+
return internalRestCall(internalDelete(server, `${objectUtils.getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}/${id}`))
159150
}
160151

161152
/**
162153
* Sends a PUT request to the server for the object with the given ID.
163154
* @param server name of the rest/config/servers property to use
164155
* @param endpointPath path to the correct endpoint-definition starting from rest/config/endpoint/
165-
* @param workingIndicator path to an indicator-action (true/false) or function(value) that will be called with value = (true/false) accordingly
166-
* @param dataProvider path to a vuex-getter or function that will be called in order to get the body for the call
167-
* @param responseSetter path to a vuex-action (object) or function(response) that will be called with the received response
168156
* @param id the ID of the object to retrieve
157+
* @param dataProvider path to a vuex-getter or function that will be called in order to get the body for the call
169158
*/
170-
export async function put (server, endpointPath, id, workingIndicator, dataProvider, responseSetter) {
171-
return internalRestCall(workingIndicator, responseSetter, internalPut(server, `${getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}/${id}`, dataProvider))
159+
export async function put (server: string, endpointPath: string, id: string | number, dataProvider: () => object): Promise<any> {
160+
return internalRestCall(internalPut(server, `${objectUtils.getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}/${id}`, dataProvider))
172161
}
173162

174163
/**
175164
* Sends a POST request to the server for the object with the given ID.
176165
* @param server name of the rest/config/servers property to use
177166
* @param endpointPath path to the correct endpoint-definition starting from rest/config/endpoint/
178-
* @param workingIndicator path to an indicator-action (true/false) or function(value) that will be called with value = (true/false) accordingly
179167
* @param dataProvider path to a vuex-getter or function that will be called in order to get the body for the call
180-
* @param responseSetter path to a vuex-action (object) or function(response) that will be called with the received response
181168
*/
182-
export async function post (server, endpointPath, workingIndicator, dataProvider, responseSetter) {
183-
return internalRestCall(workingIndicator, responseSetter, internalPost(server, `${getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}`, dataProvider))
169+
export async function post (server: string, endpointPath: string, dataProvider: () => object): Promise<any> {
170+
return internalRestCall(internalPost(server, `${objectUtils.getDeepProperty(endpointPath, store.getters['rest/config'].endpoint)}`, dataProvider))
184171
}

src/utils/jsUtils.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
export function toggleItem (item, array) {
2-
const index = array.indexOf(item)
3-
if (index === -1) {
4-
array.push(item)
5-
} else {
6-
array.splice(index, 1)
7-
}
8-
}
1+
export default {
2+
toggleItem: function (item, array) {
3+
const index = array.indexOf(item)
4+
if (index === -1) {
5+
array.push(item)
6+
} else {
7+
array.splice(index, 1)
8+
}
9+
},
910

10-
export function removeItem (item, array) {
11-
const index = array.indexOf(item)
12-
if (index !== -1) {
13-
array.splice(index, 1)
14-
}
15-
}
11+
removeItem: function (item, array) {
12+
const index = array.indexOf(item)
13+
if (index !== -1) {
14+
array.splice(index, 1)
15+
}
16+
},
1617

17-
export function containsItem (item, array) {
18-
return array.indexOf(item) !== -1
19-
}
18+
containsItem: function (item, array) {
19+
return array.indexOf(item) !== -1
20+
},
2021

21-
/**
22-
* Returns the value itself, or an empty string, if the value was null or undefined.
23-
* @param value the value to sanitize
24-
*/
25-
export function sanitize (value) {
26-
if (value === null || value === undefined) {
27-
return ''
22+
/**
23+
* Returns the value itself, or an empty string, if the value was null or undefined.
24+
* @param value the value to sanitize
25+
*/
26+
sanitize: function (value) {
27+
if (value === null || value === undefined) {
28+
return ''
29+
}
30+
return value
2831
}
29-
return value
3032
}

src/utils/loggingUtils.ts

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
import store from '@/store'
22

3-
/**
4-
* Logs a message to the snackbar.
5-
* @param message the message to log
6-
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
7-
* @param level the level to use ('error', 'warning')
8-
* @param status the status-code (integer) (may be omitted)
9-
*/
10-
export function log (message, group, level, status) {
11-
store.dispatch('gui/snackbar/snackbarEnqueue', {
12-
color: `${level}`,
13-
headingTKey: `message.${level}.heading`,
14-
descriptionTKey: `message.${level}.${group}`,
15-
status,
16-
message
17-
}, { root: true })
18-
}
3+
export default {
4+
/**
5+
* Logs a message to the snackbar.
6+
* @param message the message to log
7+
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
8+
* @param level the level to use ('error', 'warning')
9+
* @param status the status-code (integer) (may be omitted)
10+
*/
11+
log: function (message, group, level, status) {
12+
store.dispatch('gui/snackbar/snackbarEnqueue', {
13+
color: `${level}`,
14+
headingTKey: `message.${level}.heading`,
15+
descriptionTKey: `message.${level}.${group}`,
16+
status,
17+
message
18+
}, { root: true })
19+
},
1920

20-
/**
21-
* Logs a success message to the snackbar
22-
* @param message the message to log
23-
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
24-
* @param status the status-code (integer) (may be omitted)
25-
*/
26-
export function success (message, group, status?) {
27-
log(message, group, 'success', status)
28-
}
21+
/**
22+
* Logs a success message to the snackbar
23+
* @param message the message to log
24+
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
25+
* @param status the status-code (integer) (may be omitted)
26+
*/
27+
success: function (message, group, status?) {
28+
this.log(message, group, 'success', status)
29+
},
2930

30-
/**
31-
* Logs a warning to the snackbar
32-
* @param message the message to log
33-
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
34-
* @param status the status-code (integer) (may be omitted)
35-
*/
36-
export function warning (message, group, status?) {
37-
log(message, group, 'warning', status)
38-
}
31+
/**
32+
* Logs a warning to the snackbar
33+
* @param message the message to log
34+
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
35+
* @param status the status-code (integer) (may be omitted)
36+
*/
37+
warning: function (message, group, status?) {
38+
this.log(message, group, 'warning', status)
39+
},
3940

40-
/**
41-
* Logs an error to the snackbar
42-
* @param message the message to log
43-
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
44-
* @param status the status-code (integer) (may be omitted)
45-
*/
46-
export function error (message, group, status?) {
47-
log(message, group, 'error', status)
41+
/**
42+
* Logs an error to the snackbar
43+
* @param message the message to log
44+
* @param group the group to use ('internal', 'communication', ...) which is a path in the message-object in localization.
45+
* @param status the status-code (integer) (may be omitted)
46+
*/
47+
error: function (message, group, status?) {
48+
this.log(message, group, 'error', status)
49+
}
4850
}

0 commit comments

Comments
 (0)