Issues (1)

src/response/AuthResponse.js (1 issue)

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
  this.body = (response && response.body) || (response && response.data) || '';
48
  this.json = this.body && this.isJson() ? this.body : null;
49
  this.intuit_tid = (response && response.headers && response.headers.intuit_tid) || '';
50
};
51
52
/**
53
 * Get Token
54
 * *
55
 * @returns {object} token
56
 */
57
AuthResponse.prototype.getToken = function getToken() {
58
  return this.token.getToken();
59
};
60
61
/**
62
 * Get Token
63
 * *
64
 * @returns {string} text
65
 */
66
AuthResponse.prototype.text = function text() {
67
  return this.body;
68
};
69
70
/**
71
 * Get Token
72
 * *
73
 * @returns {Number} statusCode
74
 */
75
AuthResponse.prototype.status = function status() {
76
  return this.response.status;
77
};
78
79
/**
80
 * Get response headers
81
 * *
82
 * @returns {Object} headers
83
 */
84
AuthResponse.prototype.headers = function headers() {
85
  return this.response.headers;
86
};
87
88
/**
89
 * Is Response valid { response is valid ? }
90
 * *
91
 * @returns {*|boolean}
92
 */
93
AuthResponse.prototype.valid = function valid() {
94
  return this.response && Number(this.response.status) >= 200 && Number(this.response.status) < 300;
95
};
96
97
/**
98
 * Get Json () { returns token as JSON }
99
 * *
100
 * @return {object} json
101
 */
102
AuthResponse.prototype.getJson = function getJson() {
103
  if (!this.isJson()) throw new Error('AuthResponse is not JSON');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
104
  if (!this.json) {
105
    this.json = this.body ? JSON.parse(this.body) : null;
106
  }
107
  return this.json;
108
};
109
110
/**
111
 * Get Intuit tid
112
 * *
113
 * @returns {string} intuit_tid
114
 */
115
AuthResponse.prototype.get_intuit_tid = function get_intuit_tid() {
116
  return this.intuit_tid;
117
};
118
119
/**
120
 * isContentType
121
 * *
122
 * @returns {boolean} isContentType
123
 */
124
AuthResponse.prototype.isContentType = function isContentType(contentType) {
125
  return this.getContentType().indexOf(contentType) > -1;
126
};
127
128
/**
129
 * getContentType
130
 * *
131
 * @returns {string} getContentType
132
 */
133
AuthResponse.prototype.getContentType = function getContentType() {
134
  return this.response.headers[AuthResponse._contentType] || '';
135
};
136
137
/**
138
 * isJson
139
 * *
140
 * @returns {boolean} isJson
141
 */
142
AuthResponse.prototype.isJson = function isJson() {
143
  return this.isContentType('application/json');
144
};
145
146
AuthResponse._contentType = 'content-type';
147
AuthResponse._jsonContentType = 'application/json';
148
AuthResponse._urlencodedContentType = 'application/x-www-form-urlencoded';
149
150
module.exports = AuthResponse;
151