11import Vue from 'vue'
22import 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}
0 commit comments