All files / src/api TwitarrServer.ts

81.82% Statements 18/22
33.33% Branches 2/6
60% Functions 3/5
81.82% Lines 18/22

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  9x   9x 9x           9x                                                                 80x 80x 80x   75x   5x                       1x     2x   11x   2x   9x     1x   11x                                 1x   2x                
/** @hidden */
const URI = require('urijs');
 
import { TwitarrAuthConfig } from './TwitarrAuthConfig';
import { UUID } from '../internal/UUID';
 
/**
 * Represents a remote Twitarr server.
 * @module TwitarrServer
 */
export class TwitarrServer {
  /** A unique identifier for this server. */
  public id: string;
 
  /** An optional name associated with this server. */
  public name?: string;
 
  /** The base URL to the server. */
  public url: string;
 
  /** The authorization configuration associated with the server. */
  public auth: TwitarrAuthConfig;
 
  /**
   * Construct a new TwitarrServer object representing a remote server.
   * @example
   * <caption>provide a pre-existing [[TwitarrAuthConfig]] for auth</caption>
   * ```javascript
   * const server = new TwitarrServer('Test', 'https://myserver/', auth);
   * ```
   * @example
   * <caption>provide a username and password for auth</caption>
   * ```javascript
   * const server = new TwitarrServer('Test', 'https://myserver/', 'admin', 'admin');
   * ```
   * @constructor
   * @param name - A name for the server suitable for display.
   * @param url - The URL to the server.
   * @param auth - A [[TwitarrAuthConfig]], or the username to authorize as.
   * @param password - The password to authorize with if a username was
   *                   supplied to the `auth` parameter.
   */
  public constructor(name?: string, url?: string, auth?: TwitarrAuthConfig | string, password?: string) {
    this.id = UUID.generate();
    this.name = name;
    this.url = url;
    if (auth instanceof TwitarrAuthConfig) {
      this.auth = auth;
    } else {
      this.auth = new TwitarrAuthConfig(auth, password);
    }
  }
 
  /**
   * Given a relative URL fragment, construct a URL for that fragment on the server.
   * @param forFragment - The URL fragment to append to the server URL.
   * @parm withQuery - Query parameters to be appended to the URL.
   * @returns A complete URL.
   */
  public resolveURL(forFragment?: string, withQuery?: any) {
    if (!this.url) {
      return undefined;
    }
    if (forFragment === undefined) {
      return this.url;
    }
    let uri = URI(this.url);
    if (forFragment.indexOf('/') === 0 || forFragment.indexOf('http') === 0) {
      uri = URI(forFragment);
    } else {
      uri = uri.segment(forFragment);
    }
    if (withQuery !== undefined) {
      uri = uri.addQuery(withQuery);
    }
    return uri.toString();
  }
 
  /**
   * Create a new server object from this existing one.
   */
  public clone() {
    const auth = this.auth ? this.auth.clone() : undefined;
    const ret = new TwitarrServer(this.name, this.url, auth);
    return ret;
  }
 
  /**
   * Get the hostname portion of the URL associated with this server.
   */
  public get host() {
    if (!this.url) {
      return undefined;
    }
    return URI(this.url).hostname();
  }
 
  /** A string representation of this server suitable for display. */
  public toString() {
    return 'Twitarr at ' + (this.host || this.url);
  }
}