All files / src/api TwitarrHTTPOptions.ts

96.55% Statements 28/29
100% Branches 0/0
100% Functions 9/9
96.55% Lines 28/29

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 1209x         9x     9x     9x           9x       1x   1x       1x               1x       4x             112x     112x                             1x     3x     3x                   1x 1x                   32x   32x               7x 7x 7x       2x   1x 1x     1x 1x   2x      
import { TwitarrAuthConfig } from './TwitarrAuthConfig';
import { TwitarrServer } from './TwitarrServer';
import { IHash } from '../internal/IHash';
 
/** @hidden */
const DEFAULT_TIMEOUT = 10000;
 
/** @hidden */
const TIMEOUT_PROP = Symbol.for('timeout');
 
/** @hidden */
const AUTH_PROP = Symbol.for('auth');
 
/**
 * Options to be used when making HTTP ReST calls.
 * @module TwitarrHTTPOptions
 */
export class TwitarrHTTPOptions {
  /** How long to wait for ReST calls to time out. */
  public get timeout(): number {
    if (this[TIMEOUT_PROP]) {
      return this[TIMEOUT_PROP];
    }
    return DEFAULT_TIMEOUT;
  }
 
  public set timeout(t: number) {
    this[TIMEOUT_PROP] = t;
  }
 
  /** The authentication config that should be used when no auth is associated with the [[TwitarrServer]]. */
  public get auth(): TwitarrAuthConfig {
    if (!this[AUTH_PROP]) {
      this[AUTH_PROP] = new TwitarrAuthConfig();
    }
    return this[AUTH_PROP];
  }
 
  public set auth(a: TwitarrAuthConfig) {
    this[AUTH_PROP] = a;
  }
 
  /** The server to use if no server is set on the HTTP implementation. */
  public server: TwitarrServer;
 
  /** HTTP headers to be passed to the request. */
  public headers: IHash<string> = {};
 
  /** HTTP parameters to be passed on the URL. */
  public parameters: IHash<string> = {};
 
  /** HTTP data to be passed when POSTing */
  public data: any;
 
  private [TIMEOUT_PROP]: number;
 
  private [AUTH_PROP]: TwitarrAuthConfig;
 
  /**
   * Construct a new TwitarrHTTPOptions object.
   * @constructor
   */
  public constructor(timeout?: number, auth?: TwitarrAuthConfig, server?: TwitarrServer) {
    if (timeout !== undefined) {
      this.timeout = timeout;
    }
    if (auth !== undefined) {
      this.auth = auth;
    }
    if (server !== undefined) {
      this.server = server;
    }
  }
 
  /**
   * Add a header.  Returns the TwitarrHTTPOptions object so it can be chained.
   * @param key - the header
   * @param value - the header value
   */
  public withHeader(header: string, value: string): TwitarrHTTPOptions {
    this.headers[header] = value;
    return this;
  }
 
  /**
   * Add a URL parameter. Returns the TwitarrHTTPOptions object so it can be chained.
   * @param key - the parameter's key
   * @param value - the parameter's value
   */
  public withParameter(key: string, value?: any): TwitarrHTTPOptions {
    if (value !== undefined) {
      this.parameters[key] = '' + value;
    }
    return this;
  }
 
  /**
   * Set the data to be passed when POSTing.
   * @param data - the data to POST
   */
  public withData(data: any): TwitarrHTTPOptions {
    this.data = {};
    Object.assign(this.data, data);
    return this;
  }
 
  public toJSON(): object {
    const ret = Object.assign({}, this);
    if (this[TIMEOUT_PROP]) {
      ret.timeout = this.timeout;
      delete ret[TIMEOUT_PROP];
    }
    if (this[AUTH_PROP]) {
      ret.auth = this.auth;
      delete ret[AUTH_PROP];
    }
    return ret;
  }
}