All files / src/api TwitarrResult.ts

84.62% Statements 11/13
94.74% Branches 18/19
75% Functions 3/4
84.62% Lines 11/13

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 648x           8x     57x                                                               57x 57x 57x   57x 57x 57x     1x                   79x      
import { TwitarrError } from './TwitarrError';
 
/**
 * An [[ITwitarrHTTP]] query result.
 * @module TwitarrResult
 */
export class TwitarrResult<T> {
  /** Create a new success result. */
  public static ok(response: any, message?: string, code?: number, type?: string) {
    return new TwitarrResult(response, message, code || 200, type);
  }
 
  /** Create a new "No Content" result. */
  public static noContent() {
    // Use a null string for the data
    return new TwitarrResult(null, 'No Content', 204);
  }
 
  /** The data, if any. */
  public data: T;
 
  /** The request type, if any. */
  public type: string;
 
  /** The status message associated with this result. */
  public message: string;
 
  /** The response code associated with this result. */
  public code: number;
 
  /** The error associated with this response if any. */
  public error: TwitarrError;
 
  /**
   * Construct a new result.
   * @param data The payload of the response.
   * @param message The status message associated with the result.
   * @param code The response code of the response.
   * @param type The request type of the response.
   */
  public constructor(data: T, message?: string, code?: number, type?: string) {
    this.message = message;
    this.code = code;
    this.type = type;
 
    this.data = data;
    const errors = this.data && (this.data as any).errors ? (this.data as any).errors : undefined;
    const status = this.data && (this.data as any).status ? (this.data as any).status : undefined;
 
    if (!message && this.data && (this.data as any).message) {
      this.message = (this.data as any).message;
    }
 
    if (!this.isSuccess() || errors || status === 'error') {
      this.error = new TwitarrError(message, code, errors, undefined, data);
    }
  }
 
  /** Whether this result is considered successful. */
  public isSuccess() {
    return this.code === 200 || this.code === 202 || this.code === 204;
  }
}