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