* Make a request. * @param {string} method - the method for the request (GET, PUT, POST, DELETE) * @param {string} path - the path for the request * @param {*} [data] - the data to send to the server. For HTTP methods that don't have a body the data * will be sent a
(method, path, data, cb, raw)
| 154 | * @return {Promise} - the Promise for the http request |
| 155 | */ |
| 156 | _request(method, path, data, cb, raw) { |
| 157 | const url = this.__getURL(path); |
| 158 | |
| 159 | const AcceptHeader = (data || {}).AcceptHeader; |
| 160 | if (AcceptHeader) { |
| 161 | delete data.AcceptHeader; |
| 162 | } |
| 163 | const headers = this.__getRequestHeaders(raw, AcceptHeader); |
| 164 | |
| 165 | let queryParams = {}; |
| 166 | |
| 167 | const shouldUseDataAsParams = data && (typeof data === 'object') && methodHasNoBody(method); |
| 168 | if (shouldUseDataAsParams) { |
| 169 | queryParams = data; |
| 170 | data = undefined; |
| 171 | } |
| 172 | |
| 173 | const config = { |
| 174 | url: url, |
| 175 | method: method, |
| 176 | headers: headers, |
| 177 | params: queryParams, |
| 178 | data: data, |
| 179 | responseType: raw ? 'text' : 'json', |
| 180 | }; |
| 181 | |
| 182 | log(`${config.method} to ${config.url}`); |
| 183 | const requestPromise = axios(config).catch(callbackErrorOrThrow(cb, path)); |
| 184 | |
| 185 | if (cb) { |
| 186 | requestPromise.then((response) => { |
| 187 | if (response.data && Object.keys(response.data).length > 0) { |
| 188 | // When data has results |
| 189 | cb(null, response.data, response); |
| 190 | } else if (config.method !== 'GET' && Object.keys(response.data).length < 1) { |
| 191 | // True when successful submit a request and receive a empty object |
| 192 | cb(null, (response.status < 300), response); |
| 193 | } else { |
| 194 | cb(null, response.data, response); |
| 195 | } |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | return requestPromise; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Make a request to an endpoint the returns 204 when true and 404 when false |
no test coverage detected