Code coverage report for src/http-client-configuration.js

Statements: 100% (15 / 15)      Branches: 100% (2 / 2)      Functions: 100% (1 / 1)      Lines: 100% (15 / 15)      Ignored: none     

All files » src/ » http-client-configuration.js
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 991                                                               2 2                     1 1                             5 5                   1 1 1                           4       1 2 1     1    
import {RequestInit, Interceptor} from './interfaces';
 
/**
* A class for configuring HttpClients.
*/
export class HttpClientConfiguration {
  /**
  * The base URL to be prepended to each Request's url before sending.
  */
  baseUrl: string = '';
 
  /**
  * Default values to apply to init objects when creating Requests. Note that
  * defaults cannot be applied when Request objects are manually created because
  * Request provides its own defaults and discards the original init object.
  * See also https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
  */
  defaults: RequestInit = {};
 
  /**
  * Interceptors to be added to the HttpClient.
  */
  interceptors: Interceptor[] = [];
 
  /**
  * Sets the baseUrl.
  *
  * @param baseUrl The base URL.
  * @returns The chainable instance of this configuration object.
  * @chainable
  */
  withBaseUrl(baseUrl: string): HttpClientConfiguration {
    this.baseUrl = baseUrl;
    return this;
  }
 
  /**
  * Sets the defaults.
  *
  * @param defaults The defaults.
  * @returns The chainable instance of this configuration object.
  * @chainable
  */
  withDefaults(defaults: RequestInit): HttpClientConfiguration {
    this.defaults = defaults;
    return this;
  }
 
  /**
  * Adds an interceptor to be run on all requests or responses.
  *
  * @param interceptor An object with request, requestError,
  * response, or responseError methods. request and requestError act as
  * resolve and reject handlers for the Request before it is sent.
  * response and responseError act as resolve and reject handlers for
  * the Response after it has been received.
  * @returns The chainable instance of this configuration object.
  * @chainable
  */
  withInterceptor(interceptor: Interceptor): HttpClientConfiguration {
    this.interceptors.push(interceptor);
    return this;
  }
 
  /**
  * Applies a configuration that addresses common application needs, including
  * configuring same-origin credentials, and using rejectErrorResponses.
  * @returns The chainable instance of this configuration object.
  * @chainable
  */
  useStandardConfiguration(): HttpClientConfiguration {
    let standardConfig = { credentials: 'same-origin' };
    Object.assign(this.defaults, standardConfig, this.defaults);
    return this.rejectErrorResponses();
  }
 
  /**
  * Causes Responses whose status codes fall outside the range 200-299 to reject.
  * The fetch API only rejects on network errors or other conditions that prevent
  * the request from completing, meaning consumers must inspect Response.ok in the
  * Promise continuation to determine if the server responded with a success code.
  * This method adds a response interceptor that causes Responses with error codes
  * to be rejected, which is common behavior in HTTP client libraries.
  * @returns The chainable instance of this configuration object.
  * @chainable
  */
  rejectErrorResponses(): HttpClientConfiguration {
    return this.withInterceptor({ response: rejectOnError });
  }
}
 
function rejectOnError(response) {
  if (!response.ok) {
    throw response;
  }
 
  return response;
}