All files / src/dao AbstractDAO.ts

77.78% Statements 7/9
100% Branches 4/4
75% Functions 3/4
77.78% Lines 7/9

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    8x     8x                             1x   58x             65x                 19x     19x            
import { IHasHTTP } from '../api/IHasHTTP';
import { ITwitarrHTTP } from '../api/ITwitarrHTTP';
import { TwitarrError } from '../api/TwitarrError';
import { TwitarrResult } from '../api/TwitarrResult';
 
export abstract class AbstractDAO {
  /**
   * The [[ITwitarrHTTP]] implementation to use internally when making requests.
   * @hidden
   */
  private httpImpl: ITwitarrHTTP;
 
  /**
   * Construct a DAO instance.
   *
   * @param impl - The HTTP implementation to use.  It is also legal to pass any object
   *               conforming to the [[IHasHTTP]] interface (like a [[Client]]).
   */
  public constructor(impl: ITwitarrHTTP | IHasHTTP) {
    if ((impl as IHasHTTP).http) {
      impl = (impl as IHasHTTP).http;
    }
    this.httpImpl = impl as ITwitarrHTTP;
  }
 
  /**
   * The HTTP implementation to use internally when making DAO requests.
   */
  public get http() {
    return this.httpImpl;
  }
 
  public set http(impl: ITwitarrHTTP) {
    this.httpImpl = impl;
  }
 
  protected handleErrors(result: TwitarrResult<any>) {
    if (result.isSuccess()) {
      const status = result.data && result.data.status ? result.data.status : 'ok';
      if (status === 'ok') {
        // console.debug('result was ok:', result);
        return result.data;
      }
    }
    throw new TwitarrError('Failed to parse result.', result.code, undefined, undefined, result.data);
  }
}