All files / src/response AuthResponse.js

100% Statements 60/60
89.13% Branches 41/46
100% Functions 12/12
100% Lines 60/60

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                                                                  58x 58x 58x 58x 58x             1x 55x     55x   54x   54x   19x     19x 2x             17x   35x   30x 30x 30x     20x 2x             18x     10x     5x 5x     1x 1x 1x                 1x 8x               1x 1x               1x 2x               1x 2x               1x 13x                 1x   5x 1x       4x 3x 3x     2x 1x             2x   1x         1x               1x 16x               1x 3x               1x 5x               1x 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
      if (response.data.Fault) {
        this.json = {
          Fault: response.data.Fault,
          time: response.data.time || new Date().toISOString(),
          intuit_tid: this.intuit_tid,
        };
      } else {
        // Store the raw response data
        this.json = response.data;
      }
    } 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
        if (parsedBody.Fault) {
          this.json = {
            Fault: parsedBody.Fault,
            time: parsedBody.time || new Date().toISOString(),
            intuit_tid: this.intuit_tid,
          };
        } else {
          // Store the raw response data
          this.json = parsedBody;
        }
      } catch (e) {
        this.json = null;
      }
    } else {
      this.body = '';
      this.json = null;
    }
  } 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
  if (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
      if (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;