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
|
|
|
} else if (response.body) { |
55
|
|
|
// Handle other response types |
56
|
|
|
this.body = response.body; |
57
|
|
|
try { |
58
|
|
|
this.json = typeof response.body === 'string' ? JSON.parse(response.body) : response.body; |
59
|
|
|
} catch (e) { |
60
|
|
|
this.json = null; |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
this.body = ''; |
64
|
|
|
this.json = null; |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
this.body = ''; |
68
|
|
|
this.json = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Set intuit_tid |
72
|
|
|
this.intuit_tid = (response && response.headers && response.headers.intuit_tid) || ''; |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get Token |
77
|
|
|
* * |
78
|
|
|
* @returns {object} token |
79
|
|
|
*/ |
80
|
|
|
AuthResponse.prototype.getToken = function getToken() { |
81
|
|
|
return this.token.getToken(); |
82
|
|
|
}; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get Token |
86
|
|
|
* * |
87
|
|
|
* @returns {string} text |
88
|
|
|
*/ |
89
|
|
|
AuthResponse.prototype.text = function text() { |
90
|
|
|
return this.body; |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get Token |
95
|
|
|
* * |
96
|
|
|
* @returns {Number} statusCode |
97
|
|
|
*/ |
98
|
|
|
AuthResponse.prototype.status = function status() { |
99
|
|
|
return this.response.status; |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get response headers |
104
|
|
|
* * |
105
|
|
|
* @returns {Object} headers |
106
|
|
|
*/ |
107
|
|
|
AuthResponse.prototype.headers = function headers() { |
108
|
|
|
return this.response.headers; |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Is Response valid { response is valid ? } |
113
|
|
|
* * |
114
|
|
|
* @returns {*|boolean} |
115
|
|
|
*/ |
116
|
|
|
AuthResponse.prototype.valid = function valid() { |
117
|
|
|
return this.response && Number(this.response.status) >= 200 && Number(this.response.status) < 300; |
118
|
|
|
}; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get Json () { returns token as JSON } |
122
|
|
|
* * |
123
|
|
|
* @return {object} json |
124
|
|
|
*/ |
125
|
|
|
AuthResponse.prototype.getJson = function getJson() { |
126
|
|
|
if (!this.isJson()) { |
127
|
|
|
throw new Error('Response is not JSON'); |
128
|
|
|
} |
129
|
|
|
if (!this.json) { |
130
|
|
|
try { |
131
|
|
|
this.json = this.body ? JSON.parse(this.body) : null; |
132
|
|
|
} catch (e) { |
133
|
|
|
throw new Error(`Invalid JSON response: ${e.message}`); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return this.json; |
137
|
|
|
}; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get Intuit tid |
141
|
|
|
* * |
142
|
|
|
* @returns {string} intuit_tid |
143
|
|
|
*/ |
144
|
|
|
AuthResponse.prototype.getIntuitTid = function getIntuitTid() { |
145
|
|
|
return this.intuit_tid; |
146
|
|
|
}; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* isContentType |
150
|
|
|
* * |
151
|
|
|
* @returns {boolean} isContentType |
152
|
|
|
*/ |
153
|
|
|
AuthResponse.prototype.isContentType = function isContentType(contentType) { |
154
|
|
|
return this.getContentType().indexOf(contentType) > -1; |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* getContentType |
159
|
|
|
* * |
160
|
|
|
* @returns {string} getContentType |
161
|
|
|
*/ |
162
|
|
|
AuthResponse.prototype.getContentType = function getContentType() { |
163
|
|
|
return this.response.headers[AuthResponse._contentType] || ''; |
164
|
|
|
}; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* isJson |
168
|
|
|
* * |
169
|
|
|
* @returns {boolean} isJson |
170
|
|
|
*/ |
171
|
|
|
AuthResponse.prototype.isJson = function isJson() { |
172
|
|
|
return this.isContentType('application/json'); |
173
|
|
|
}; |
174
|
|
|
|
175
|
|
|
AuthResponse._contentType = 'content-type'; |
176
|
|
|
AuthResponse._jsonContentType = 'application/json'; |
177
|
|
|
AuthResponse._urlencodedContentType = 'application/x-www-form-urlencoded'; |
178
|
|
|
|
179
|
|
|
module.exports = AuthResponse; |
180
|
|
|
|