All files summary.ts

87.27% Statements 48/55
87.5% Branches 35/40
90% Functions 9/10
86.79% Lines 46/53

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 1221x 1x   1x             1x                           21x 21x     1x 21x 21x 21x     1x 21x 13x   8x     21x     1x 21x 15x   6x   21x     1x 4x 4x     1x 21x 21x     1x 19x 19x     1x         1x 21x 21x 10x     21x       21x       21x   21x       21x 21x       21x       21x                         21x   20x 20x   1x  
import { API } from "./api";
import { removeUndefinedFromObj } from "./utils";
import { Blob } from "./blob";
import { Job } from "./job";
import { Webhooks } from "./webhooks";
import { Executable } from "./Executable";
import { Watermark } from "./watermark";
 
export type SummaryType = "mp4" | "gif";
 
export class SummaryJob implements Executable {
  apiKey: string;
  successUrl?: string;
  failureUrl?: string;
  inputUrl?: string;
  inputBlob?: Blob;
  outputUrl?: string;
  outputBlob?: Blob;
  summaryWidth?: number;
  summaryWatermark?: Watermark;
  summaryType: SummaryType;
  summaryRemoveAudio?: boolean;
 
  constructor(apikey: string) {
    this.summaryWidth = 720;
    this.apiKey = apikey;
  }
 
  webhooks(webhooks: Webhooks): SummaryJob {
    this.successUrl = webhooks.successUrl;
    this.failureUrl = webhooks.failureUrl;
    return this;
  }
 
  from(source: string | Blob): SummaryJob {
    if (typeof source === "string") {
      this.inputUrl = source;
    } else {
      this.inputBlob = source;
    }
 
    return this;
  }
 
  to(destination: string | Blob): SummaryJob {
    if (typeof destination === "string") {
      this.outputUrl = destination;
    } else {
      this.outputBlob = destination;
    }
    return this;
  }
 
  watermark(watermark: Watermark): SummaryJob {
    this.summaryWatermark = watermark;
    return this;
  }
 
  type(type: SummaryType): SummaryJob {
    this.summaryType = type;
    return this;
  }
 
  width(width: number): SummaryJob {
    this.summaryWidth = width;
    return this;
  }
 
  removeAudio(value: boolean): SummaryJob {
    this.summaryRemoveAudio = value;
    return this;
  }
 
  async execute() {
    let jobType = "gif_summary";
    if (this.summaryType === "mp4") {
      jobType = "mp4_summary";
    }
 
    Iif (this.apiKey === null) {
      throw new Error("Missing apiKey");
    }
 
    Iif (this.apiKey.trim() == "") {
      throw new Error("Missing apiKey");
    }
 
    const emptyInputUrl = !this.inputUrl || this.inputUrl.trim() === "";
 
    Iif (!this.inputBlob && emptyInputUrl) {
      throw new Error("Missing inputBlob or inputUrl");
    }
 
    const emptyOutputUrl = !this.outputUrl || this.outputUrl.trim() == "";
    Iif (!this.outputBlob && emptyOutputUrl) {
      throw new Error("Missing outputBlob or outputUrl");
    }
 
    Iif (!this.summaryType) {
      throw new Error("Missing summaryType");
    }
 
    const body = {
      apiKey: this.apiKey,
      successURL: this.successUrl,
      failureURL: this.failureUrl,
      inputCreds: this.inputBlob?.toApiCredentials(),
      outputCreds: this.outputBlob?.toApiCredentials(),
      inputURL: this.inputUrl || this.inputBlob.toApiUrl(),
      outputURL: this.outputUrl || this.outputBlob.toApiUrl(),
      width: `${this.summaryWidth}`,
      watermark: this.summaryWatermark?.toJSON(),
      removeAudio: this.summaryRemoveAudio,
    };
 
    const resp = await API.createJob(jobType, removeUndefinedFromObj(body));
 
    const job = new Job(resp.data.id);
    return job;
  }
}