All files / src Client.ts

59.62% Statements 31/52
100% Branches 0/0
16.67% Functions 2/12
59.62% Lines 31/52

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 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    1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x           1x                 3x         1x   3x   3x 3x 3x 3x                                         4x       4x                                     1x 1x   1x           1x   1x                         1x                                                                                                                                                       1x      
import { IHasHTTP } from './api/IHasHTTP';
import { ITwitarrHTTP } from './api/ITwitarrHTTP';
import { TwitarrHTTPOptions } from './api/TwitarrHTTPOptions';
import { TwitarrError } from './api/TwitarrError';
import { TwitarrServer } from './api/TwitarrServer';
 
import { AlertDAO } from './dao/AlertDAO';
import { AutocompleteDAO } from './dao/AutocompleteDAO';
import { EventDAO } from './dao/EventDAO';
import { ForumDAO } from './dao/ForumDAO';
import { PhotoDAO } from './dao/PhotoDAO';
import { SeamailDAO } from './dao/SeamailDAO';
import { SearchDAO } from './dao/SearchDAO';
import { StreamDAO } from './dao/StreamDAO';
import { TextDAO } from './dao/TextDAO';
import { UserDAO } from './dao/UserDAO';
 
import { BrowserHTTP } from './rest/BrowserHTTP';
 
/**
 * The Twitarr client.  This is the primary interface to Twitarr servers.
 * @module Client
 */
export class Client implements IHasHTTP {
  /**
   * Given an TwitarrServer object, check that it can be connected to.
   *
   * @param server - the server to check
   * @param httpImpl - the [[ITwitarrHTTP]] implementation to use
   * @param timeout - how long to wait before giving up when making ReST calls
   */
  public static async checkServer(server: TwitarrServer, httpImpl?: ITwitarrHTTP, timeout?: number): Promise<boolean> {
    const opts = new TwitarrHTTPOptions(timeout, server.auth, server);
    if (!httpImpl) {
      if (!Client.defaultHttp) {
        throw new TwitarrError('No HTTP implementation is configured!');
      }
      httpImpl = Client.defaultHttp;
    }
    opts.headers.accept = 'application/json';
 
    const welcomeUrl = server.resolveURL('api/v2/text/welcome');
    console.debug('checkServer: checking URL: ' + welcomeUrl);
    await httpImpl.get(welcomeUrl, opts);
    return true;
  }
 
  /** The default TwitarrHTTP implementation to be used when making requests */
  private static defaultHttp: ITwitarrHTTP;
 
  /** the TwitarrHTTP implementation that will be used when making requests */
  public http: ITwitarrHTTP;
 
  /** The remote server to connect to */
  public server: TwitarrServer;
 
  /**
   * Construct a new Twitarr client.
   * @constructor
   * @param httpImpl - The TwitarrHTTP implementation to use. Normally
   *        this will automatically choose the best implementation
   *        based on the environment.
   */
  public constructor(httpImpl?: ITwitarrHTTP) {
    if (httpImpl) {
      Client.defaultHttp = httpImpl;
    } else {
      Client.defaultHttp = new BrowserHTTP();
    }
    this.http = Client.defaultHttp;
  }
 
  public async isLoggedIn(): Promise<boolean> {
    try {
      return this.user()
        .profile()
        .then(() => {
          return true;
        });
    } catch (err) {
      return false;
    }
  }
 
  /**
   * Connect to an Twitarr server and return a [[TwitarrServer]] for that connection.
   */
  public async connect(name: string, url: string, username: string, password: string, timeout?: number) {
    const self = this;
    const server = new TwitarrServer(name, url, username, password);
 
    await Client.checkServer(server, undefined, timeout);
 
    if (!self.http) {
      self.http = Client.defaultHttp;
    }
    if (!self.http.server) {
      self.http.server = server;
    }
    self.server = server;
 
    if (self.server.auth.key) {
      try {
        const profile = await this.user().profile();
        console.debug('found auth key:', self.server.auth.key, profile);
        return self;
      } catch (err) {
        console.error('auth key was invalid:', err);
      }
    }
 
    try {
      return await this.user().login();
    } catch (err) {
      if (err.code === 401) {
        throw new TwitarrError('username or password was invalid', err.code, err.options, err.errors, err.data);
      }
      throw new TwitarrError('unexpected error: ' + err.message, err.code, err.options, err.errors, err.data);
    }
  }
 
  /**
   * Get an alerts DAO.
   */
  public alerts() {
    return new AlertDAO(this);
  }
 
  /**
   * Get an autocomplete DAO.
   */
  public autocomplete() {
    return new AutocompleteDAO(this);
  }
 
  /**
   * Get an event DAO.
   */
  public events() {
    return new EventDAO(this);
  }
 
  /**
   * Get a forum DAO.
   */
  public forums() {
    return new ForumDAO(this);
  }
 
  /**
   * Get a photo DAO.
   */
  public photo() {
    return new PhotoDAO(this);
  }
 
  /**
   * Get a seamail DAO.
   */
  public seamail() {
    return new SeamailDAO(this);
  }
 
  /**
   * Get a search DAO.
   */
  public search() {
    return new SearchDAO(this);
  }
 
  /**
   * Get a stream DAO.
   */
  public stream() {
    return new StreamDAO(this);
  }
 
  /**
   * Get a text DAO.
   */
  public text() {
    return new TextDAO(this);
  }
 
  /**
   * Get a user DAO.
   */
  public user() {
    return new UserDAO(this);
  }
}