1
|
|
|
/** |
2
|
|
|
|
3
|
|
|
Copyright (c) 2018 Intuit |
4
|
|
|
# |
5
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
# you may not use this file except in compliance with the License. |
7
|
|
|
# You may obtain a copy of the License at |
8
|
|
|
# |
9
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
# |
11
|
|
|
# Unless required by applicable law or agreed to in writing, software |
12
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
# See the License for the specific language governing permissions and |
15
|
|
|
# limitations under the License. |
16
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @namespace AuthResponse |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
'use strict'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AuthResponse |
27
|
|
|
* @property {Token} token |
28
|
|
|
* @property {Response} response |
29
|
|
|
* @property {string} body |
30
|
|
|
* @property {object} json |
31
|
|
|
* @property {string} intuit_tid |
32
|
|
|
*/ |
33
|
|
|
function AuthResponse(params) { |
34
|
|
|
this.token = params.token || ''; |
35
|
|
|
this.response = params.response || ''; |
36
|
|
|
this.body = params.responseText || ''; |
37
|
|
|
this.json = null; |
38
|
|
|
this.intuit_tid = params.intuit_tid || ''; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Process Response |
43
|
|
|
* @param response |
44
|
|
|
*/ |
45
|
|
|
AuthResponse.prototype.processResponse = function processResponse(response) { |
46
|
|
|
this.response = response || ''; |
47
|
|
|
|
48
|
|
|
// Handle response data |
49
|
|
|
if (response) { |
50
|
|
|
if (response.data) { |
51
|
|
|
// Handle axios response |
52
|
|
|
this.body = typeof response.data === 'string' ? response.data : JSON.stringify(response.data); |
53
|
|
|
this.json = response.data; |
54
|
|
|
|
55
|
|
|
// Handle QuickBooks API error response |
56
|
|
|
if (response.data.Fault) { |
57
|
|
|
this.json = { |
58
|
|
|
Fault: response.data.Fault, |
59
|
|
|
time: response.data.time || new Date().toISOString(), |
60
|
|
|
}; |
61
|
|
|
} |
62
|
|
|
} else if (response.body) { |
63
|
|
|
// Handle other response types |
64
|
|
|
this.body = response.body; |
65
|
|
|
try { |
66
|
|
|
const parsedBody = typeof response.body === 'string' ? JSON.parse(response.body) : response.body; |
67
|
|
|
this.json = parsedBody; |
68
|
|
|
|
69
|
|
|
// Handle QuickBooks API error response |
70
|
|
|
if (parsedBody.Fault) { |
71
|
|
|
this.json = { |
72
|
|
|
Fault: parsedBody.Fault, |
73
|
|
|
time: parsedBody.time || new Date().toISOString(), |
74
|
|
|
}; |
75
|
|
|
} |
76
|
|
|
} catch (e) { |
77
|
|
|
this.json = null; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
this.body = ''; |
81
|
|
|
this.json = null; |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
this.body = ''; |
85
|
|
|
this.json = null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Set intuit_tid |
89
|
|
|
this.intuit_tid = (response && response.headers && response.headers.intuit_tid) || ''; |
90
|
|
|
}; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get Token |
94
|
|
|
* * |
95
|
|
|
* @returns {object} token |
96
|
|
|
*/ |
97
|
|
|
AuthResponse.prototype.getToken = function getToken() { |
98
|
|
|
return this.token.getToken(); |
99
|
|
|
}; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get Token |
103
|
|
|
* * |
104
|
|
|
* @returns {string} text |
105
|
|
|
*/ |
106
|
|
|
AuthResponse.prototype.text = function text() { |
107
|
|
|
return this.body; |
108
|
|
|
}; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get Token |
112
|
|
|
* * |
113
|
|
|
* @returns {Number} statusCode |
114
|
|
|
*/ |
115
|
|
|
AuthResponse.prototype.status = function status() { |
116
|
|
|
return this.response.status; |
117
|
|
|
}; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get response headers |
121
|
|
|
* * |
122
|
|
|
* @returns {Object} headers |
123
|
|
|
*/ |
124
|
|
|
AuthResponse.prototype.headers = function headers() { |
125
|
|
|
return this.response.headers; |
126
|
|
|
}; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Is Response valid { response is valid ? } |
130
|
|
|
* * |
131
|
|
|
* @returns {*|boolean} |
132
|
|
|
*/ |
133
|
|
|
AuthResponse.prototype.valid = function valid() { |
134
|
|
|
return this.response && Number(this.response.status) >= 200 && Number(this.response.status) < 300; |
135
|
|
|
}; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get Json () { returns response as JSON } |
139
|
|
|
* * |
140
|
|
|
* @return {object} json |
141
|
|
|
* @throws {Error} If response cannot be parsed as JSON |
142
|
|
|
*/ |
143
|
|
|
AuthResponse.prototype.getJson = function getJson() { |
144
|
|
|
// If we already have parsed JSON, return it |
145
|
|
|
if (this.json !== null) { |
146
|
|
|
return this.json; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Try to parse the body if we have one |
150
|
|
|
if (this.body) { |
151
|
|
|
try { |
152
|
|
|
this.json = typeof this.body === 'string' ? JSON.parse(this.body) : this.body; |
153
|
|
|
|
154
|
|
|
// Handle QuickBooks API error response |
155
|
|
|
if (this.json.Fault) { |
156
|
|
|
this.json = { |
157
|
|
|
Fault: this.json.Fault, |
158
|
|
|
time: this.json.time || new Date().toISOString(), |
159
|
|
|
}; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return this.json; |
163
|
|
|
} catch (e) { |
164
|
|
|
throw new Error(`Failed to parse response as JSON: ${e.message}`); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// If we have no body, return null |
169
|
|
|
return null; |
170
|
|
|
}; |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get Intuit tid |
174
|
|
|
* * |
175
|
|
|
* @returns {string} intuit_tid |
176
|
|
|
*/ |
177
|
|
|
AuthResponse.prototype.getIntuitTid = function getIntuitTid() { |
178
|
|
|
return this.intuit_tid; |
179
|
|
|
}; |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* isContentType |
183
|
|
|
* * |
184
|
|
|
* @returns {boolean} isContentType |
185
|
|
|
*/ |
186
|
|
|
AuthResponse.prototype.isContentType = function isContentType(contentType) { |
187
|
|
|
return this.getContentType().indexOf(contentType) > -1; |
188
|
|
|
}; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* getContentType |
192
|
|
|
* * |
193
|
|
|
* @returns {string} getContentType |
194
|
|
|
*/ |
195
|
|
|
AuthResponse.prototype.getContentType = function getContentType() { |
196
|
|
|
return this.response.headers[AuthResponse._contentType] || ''; |
197
|
|
|
}; |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* isJson |
201
|
|
|
* * |
202
|
|
|
* @returns {boolean} isJson |
203
|
|
|
*/ |
204
|
|
|
AuthResponse.prototype.isJson = function isJson() { |
205
|
|
|
return this.isContentType('application/json'); |
206
|
|
|
}; |
207
|
|
|
|
208
|
|
|
AuthResponse._contentType = 'content-type'; |
209
|
|
|
AuthResponse._jsonContentType = 'application/json'; |
210
|
|
|
AuthResponse._urlencodedContentType = 'application/x-www-form-urlencoded'; |
211
|
|
|
|
212
|
|
|
module.exports = AuthResponse; |
213
|
|
|
|