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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | 72x 72x 72x 72x 72x 72x 72x 16x 72x 72x 54x 54x 54x 54x 6x 48x 48x 54x 42x 42x 42x 42x 42x 18x 18x 6x 6x 6x 6x 12x 12x 42x 54x 30x 24x 30x 30x 30x 30x 42x 42x 42x 114x 72x 1x 72x 72x 48x 72x 1x 1x 72x 72x 72x 72x 144x | import HttpStatusCode from './StatusCode'; import GenericError from '../error/GenericError'; /** * An object representing the complete request parameters used to create and * send the HTTP request. * @typedef {Object} HttpProxy~RequestParams * @property {string} method The HTTP method. * @property {string} url The original URL to which to make the request. * @property {string} transformedUrl The actual URL to which to make the * request, created by applying the URL transformer to the * original URL. * @property {Object<string, (boolean|number|string|Date)>} data The request * data, sent as query or body. * @property {HttpAgent~RequestOptions} options The high-level request options * provided by the HTTP agent. */ /** * An object that describes a failed HTTP request, providing * information about both the failure reported by the server and how the * request has been sent to the server. * @typedef {Object} HttpProxy~ErrorParams * @property {string} errorName An error name. * @property {number} status The HTTP response status code send by the * server. * @property {object} body The body of HTTP error response (detailed * information). * @property {Error} cause The low-level cause error. * @property {HttpProxy~RequestParams} params An object representing the * complete request parameters used to create and send the HTTP * request. */ /** * Middleware proxy between {@link HttpAgent} implementations and the * {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API Fetch API}, * providing a Promise-oriented API for sending requests. */ export default class HttpProxy { /** * Initializes the HTTP proxy. * * @param {UrlTransformer} transformer A transformer of URLs to which * requests are made. * @param {Window} window Helper for manipulating the global object `window` * regardless of the client/server-side environment. */ constructor(transformer, window) { /** * A transformer of URLs to which requests are made. * * @type {UrlTransformer} */ this._transformer = transformer; /** * Helper for manipulating the global object `window` regardless of the * client/server-side environment. * * @type {Window} */ this._window = window; /** * The default HTTP headers to include with the HTTP requests, unless * overridden. * * @type {Map<string, string>} */ this._defaultHeaders = new Map(); } /** * Executes a HTTP request to the specified URL using the specified HTTP * method, carrying the provided data. * * @param {string} method The HTTP method to use. * @param {string} url The URL to which the request should be made. * @param {Object<string, (boolean|number|string|Date)>} data The data to * be send to the server. The data will be included as query * parameters if the request method is `GET` or `HEAD`, and as * a request body for any other request method. * @param {HttpAgent~RequestOptions=} options Optional request options. * @return {Promise.<HttpAgent~Response>} A promise that resolves to the server * response. */ request(method, url, data, options) { const requestParams = this._composeRequestParams( method, url, data, options ); return new Promise((resolve, reject) => { let requestTimeoutId; Eif (options.timeout) { requestTimeoutId = setTimeout(() => { reject( new GenericError('The HTTP request timed out', { status: HttpStatusCode.TIMEOUT }) ); }, options.timeout); } const fetch = this._getFetchApi(); fetch( this._composeRequestUrl( url, !this._shouldRequestHaveBody(method) ? data : {} ), this._composeRequestInit(method, data, options) ) .then(response => { Eif (requestTimeoutId) { clearTimeout(requestTimeoutId); } const contentType = response.headers.get('content-type'); if (response.status === HttpStatusCode.NO_CONTENT) { return Promise.resolve([response, null]); } else Iif (contentType && contentType.includes('application/json')) { return response.json().then(body => [response, body]); } else { return response.text().then(body => [response, body]); } }) .then(([response, responseBody]) => this._processResponse(requestParams, response, responseBody) ) .then(resolve, reject); }).catch(fetchError => { throw this._processError(fetchError, requestParams); }); } /** * Sets the specified default HTTP header. The header will be sent with all * subsequent HTTP requests unless reconfigured using this method, * overridden by request options, or cleared by * {@link HttpProxy#clearDefaultHeaders} method. * * @param {string} header A header name. * @param {string} value A header value. */ setDefaultHeader(header, value) { this._defaultHeaders.set(header, value); } /** * Clears all defaults headers sent with all requests. */ clearDefaultHeaders() { this._defaultHeaders.clear(); } /** * Gets an object that describes a failed HTTP request, providing * information about both the failure reported by the server and how the * request has been sent to the server. * * @param {string} method The HTTP method used to make the request. * @param {string} url The URL to which the request has been made. * @param {Object<string, (boolean|number|string|Date)>} data The data sent * with the request. * @param {HttpAgent~RequestOptions} options Optional request options. * @param {number} status The HTTP response status code send by the server. * @param {object} body The body of HTTP error response (detailed * information). * @param {Error} cause The low-level cause error. * @return {HttpProxy~ErrorParams} An object containing both the details of * the error and the request that lead to it. */ getErrorParams(method, url, data, options, status, body, cause) { let params = this._composeRequestParams(method, url, data, options); Iif (typeof body === 'undefined') { body = {}; } let error = { status, body, cause }; switch (status) { case HttpStatusCode.TIMEOUT: error.errorName = 'Timeout'; break; case HttpStatusCode.BAD_REQUEST: error.errorName = 'Bad Request'; break; case HttpStatusCode.UNAUTHORIZED: error.errorName = 'Unauthorized'; break; case HttpStatusCode.FORBIDDEN: error.errorName = 'Forbidden'; break; case HttpStatusCode.NOT_FOUND: error.errorName = 'Not Found'; break; case HttpStatusCode.SERVER_ERROR: error.errorName = 'Internal Server Error'; break; default: error.errorName = 'Unknown'; break; } return Object.assign(error, params); } /** * Returns `true` if cookies have to be processed manually by setting * `Cookie` HTTP header on requests and parsing the `Set-Cookie` HTTP * response header. * * The result of this method depends on the current application * environment, the client-side usually handles cookie processing * automatically, leading this method returning `false`. * * At the client-side, the method tests whether the client has cookies * enabled (which results in cookies being automatically processed by the * browser), and returns `true` or `false` accordingly. * * @return {boolean} `true` if cookies are not processed automatically by * the environment and have to be handled manually by parsing * response headers and setting request headers, otherwise `false`. */ haveToSetCookiesManually() { return !this._window.isCookieEnabled(); } /** * Processes the response received from the server. * * @param {HttpProxy~RequestParams} requestParams The original request's * parameters. * @param {Response} response The Fetch API's `Response` object representing * the server's response. * @param {*} responseBody The server's response body. * @return {HttpAgent~Response} The server's response along with all related * metadata. */ _processResponse(requestParams, response, responseBody) { if (response.ok) { return { status: response.status, body: responseBody, params: requestParams, headers: this._headersToPlainObject(response.headers), cached: false }; } else { throw new GenericError('The request failed', { status: response.status, body: responseBody }); } } /** * Converts the provided Fetch API's `Headers` object to a plain object. * * @param {Headers} headers The headers to convert. * @return {Object.<string, string>} Converted headers. */ _headersToPlainObject(headers) { let plainHeaders = {}; Eif (headers.entries) { for (let [key, value] of headers.entries()) { plainHeaders[key] = value; } } else if (headers.getAll) { /** * @todo This branch should be removed with node-fetch release * 2.0.0. */ headers.forEach((_, headerName) => { const headerValue = headers.getAll(headerName).join(', '); plainHeaders[headerName] = headerValue; }); } else { /** * @todo If Microsoft Edge supported headers.entries(), we'd remove * this branch. */ headers.forEach((headerValue, headerName) => { plainHeaders[headerName] = headerValue; }); } return plainHeaders; } /** * Processes the provided Fetch API or internal error and creates an error * to expose to the calling API. * * @param {Error} fetchError The internal error to process. * @param {HttpProxy~RequestParams} requestParams An object representing the * complete request parameters used to create and send the HTTP * request. * @return {GenericError} The error to provide to the calling API. */ _processError(fetchError, requestParams) { const errorParams = fetchError instanceof GenericError ? fetchError.getParams() : {}; return this._createError( fetchError, requestParams, errorParams.status || HttpStatusCode.SERVER_ERROR, errorParams.body || null ); } /** * Creates an error that represents a failed HTTP request. * * @param {Error} cause The error's message. * @param {HttpProxy~RequestParams} requestParams An object representing the * complete request parameters used to create and send the HTTP * request. * @param {number} status Server's response HTTP status code. * @param {*} responseBody The body of the server's response, if any. * @return {GenericError} The error representing a failed HTTP request. */ _createError(cause, requestParams, status, responseBody = null) { return new GenericError( cause.message, this.getErrorParams( requestParams.method, requestParams.url, requestParams.data, requestParams.options, status, responseBody, cause ) ); } /** * Returns {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch window.fetch} * compatible API to use, depending on the method being used at the server * (polyfill) or client (native/polyfill) side. * * @return {function((string|Request), RequestInit=): Promise.<Response>} An * implementation of the Fetch API to use. */ _getFetchApi() { const fetch = 'node-fetch'; return this._window.isClient() ? this._window.getWindow().fetch : require(fetch); } /** * Composes an object representing the HTTP request parameters from the * provided arguments. * * @param {string} method The HTTP method to use. * @param {string} url The URL to which the request should be sent. * @param {Object<string, (boolean|number|string|Date)>} data The data to * send with the request. * @param {HttpAgent~RequestOptions} options Optional request options. * @return {HttpProxy~RequestParams} An object * representing the complete request parameters used to create and * send the HTTP request. */ _composeRequestParams(method, url, data, options) { return { method, url, transformedUrl: this._transformer.transform(url), data, options }; } /** * Composes an init object, which can be used as a second argument of * `window.fetch` method. * * @param {string} method The HTTP method to use. * @param {Object.<string, (boolean|number|string|Date)>} data The data to * be send with a request. * @param {HttpAgent~RequestOptions} options Options provided by the HTTP * agent. * @return {RequestInit} A `RequestInit` object of the Fetch API. */ _composeRequestInit(method, data, options) { if (!options.headers['Content-Type']) { options.headers['Content-Type'] = this._getContentType(data); } let requestInit = { method: method.toUpperCase(), headers: options.headers, credentials: options.withCredentials ? 'include' : 'same-origin', redirect: 'follow' }; if (this._shouldRequestHaveBody(method)) { requestInit.body = JSON.stringify(data); } return requestInit; } /** * Gets a `Content-Type` header value by the data. * * @param {*} data The data to be send with a request. * @return {string} A `Content-Type` header value. */ _getContentType(data) { switch (typeof data) { case 'object': return 'application/json'; case 'string': return 'text/plain'; default: return ''; } } /** * Transforms the provided URL using the current URL transformer and adds * the provided data to the URL's query string. * * @param {string} url The URL to prepare for use with the fetch API. * @param {Object<string, (boolean|number|string|Date)>} data The data to be * attached to the query string. * @return {string} The transformed URL with the provided data attached to * its query string. */ _composeRequestUrl(url, data) { const transformedUrl = this._transformer.transform(url); const queryString = Object.keys(data) .map(key => [key, data[key]].map(encodeURIComponent).join('=')) .join('&'); const delimeter = queryString ? transformedUrl.includes('?') ? '&' : '?' : ''; return `${transformedUrl}${delimeter}${queryString}`; } /** * Checks if a request should have a body (`GET` and `HEAD` requests don't * have a body). * * @param {string} method The HTTP method. * @return {boolean} `true` if a request has a body, otherwise `false`. */ _shouldRequestHaveBody(method) { return ['get', 'head'].indexOf(method.toLowerCase()) === -1; } } |