@@ -3,6 +3,13 @@ const querystring = require('querystring')
3
3
const consts = require ( '../utils/consts' )
4
4
const jwtDecode = require ( 'jwt-decode' )
5
5
6
+ /**
7
+ * Have a look at ../utils/consts.js
8
+ */
9
+ const LB_ADDR = process . env . LB_ADDR || consts . LB_ADDR
10
+ const AXIOS_TIMEOUT = process . env . AXIOS_TIMEOUT || consts . AXIOS_TIMEOUT_DEFAULT
11
+ const ENDPOINT_BACKEND_VALIDATE = process . env . ENDPOINT_BACKEND_VALIDATE || consts . ENDPOINT_BACKEND_VALIDATE
12
+
6
13
const decode = token => {
7
14
return token ? jwtDecode ( token ) : null
8
15
}
@@ -34,8 +41,61 @@ const handleTokenExp = exp => {
34
41
return out
35
42
}
36
43
44
+ const createRequestConfig = ( verb , url , requestConfig ) => {
45
+ const {
46
+ payload = null ,
47
+ ...remainder
48
+ } = requestConfig
49
+ const method = verb . toUpperCase ( )
50
+ const baseURL = LB_ADDR
51
+ const timeout = AXIOS_TIMEOUT
52
+ let requestConfigObj = {
53
+ ...remainder ,
54
+ method,
55
+ baseURL,
56
+ url,
57
+ timeout
58
+ }
59
+ // Maybe stringify data, when
60
+ // - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
61
+ // - method is POST
62
+ if ( payload !== null ) {
63
+ requestConfigObj . data = querystring . stringify ( payload )
64
+ }
65
+ // console.log(`createRequestConfig ${method} ${baseURL}${url}, requestConfig`, {...requestConfigObj})
66
+ return requestConfigObj
67
+ }
68
+
69
+ function axiosRequestCatcher ( e ) {
70
+ const status = e . response . status
71
+ const statusText = e . response . statusText || e . message
72
+ const {
73
+ url,
74
+ headers,
75
+ method,
76
+ params,
77
+ baseURL
78
+ } = e . response . config
79
+ const recv = {
80
+ status,
81
+ statusText,
82
+ data : e . response . data || { } ,
83
+ config : {
84
+ url,
85
+ headers,
86
+ method,
87
+ params,
88
+ baseURL
89
+ }
90
+ }
91
+ console . log ( `axiosRequestCatcher caught exception ${ e . message } , serving:` , {
92
+ ...recv
93
+ } )
94
+ return recv
95
+ }
96
+
37
97
/**
38
- * Make an async off-the-band POST request.
98
+ * Make an async off-the-band request.
39
99
*
40
100
* Notice that LB_ADDR can be superseeded to your own backend
41
101
* instead of mocking (static) endpoint.
@@ -48,25 +108,12 @@ const handleTokenExp = exp => {
48
108
*/
49
109
const createRequest = async ( method , url , requestConfig ) => {
50
110
// #TODO #refactorCreateRequestBackIntoKoa Make this only return a request configuration object, and factor axios out from this.
51
- const baseURL = process . env . LB_ADDR || consts . LB_ADDR
52
- const verb = method . toUpperCase ( )
53
- console . log ( `createRequest ${ verb } ${ baseURL } ${ url } , requestConfig` , requestConfig )
54
- const {
55
- payload = null ,
56
- ...restOfRequestConfig
57
- } = requestConfig
58
- let requestConfigObj = {
59
- timeout : consts . AXIOS_DEFAULT_TIMEOUT ,
60
- baseURL,
61
- method : verb ,
62
- url,
63
- ...restOfRequestConfig
64
- }
65
- if ( payload !== null ) {
66
- requestConfigObj . data = querystring . stringify ( payload )
67
- }
68
-
69
- const recv = await axios . request ( requestConfigObj )
111
+ const requestConfigObj = createRequestConfig ( method , url , requestConfig )
112
+ // let axiosRequestStatusCode = 0 // #axiosRequestDEBUG
113
+ // let axiosRequestLog = `${requestConfigObj.method} ${requestConfigObj.baseURL}${requestConfigObj.url}` // #axiosRequestDEBUG
114
+ // console.log(`createRequest for ${axiosRequestLog} BEGIN`, {...requestConfigObj}) // #axiosRequestDEBUG
115
+ const recv = await axios . request ( requestConfigObj ) . catch ( e => axiosRequestCatcher ( e ) )
116
+ // axiosRequestStatusCode = recv.status || 0 // #axiosRequestDEBUG
70
117
const data = Object . assign ( { } , recv . data )
71
118
72
119
return Promise . resolve ( data )
@@ -83,8 +130,6 @@ const getUserData = async (token) => {
83
130
userinfo : userinfo . join ( ',' )
84
131
}
85
132
86
- const ENDPOINT_BACKEND_VALIDATE = process . env . ENDPOINT_BACKEND_VALIDATE || consts . ENDPOINT_BACKEND_VALIDATE
87
-
88
133
/**
89
134
* Would create a request like this;
90
135
*
0 commit comments