All files thumbnail.ts

90.91% Statements 40/44
88.89% Branches 32/36
100% Functions 8/8
90.48% Lines 38/42

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 981x 1x       1x     1x                       11x 11x     1x 11x 11x 11x     1x 11x 7x   4x     11x     1x 11x 7x   4x   11x     1x 2x 2x     1x 10x 10x     1x 11x       11x       11x   11x       11x 11x       11x                       11x   11x 11x   1x  
import { API } from "./api";
import { removeUndefinedFromObj } from "./utils";
import { Watermark } from "./watermark";
import { Blob } from "./blob";
import { Webhooks } from "./webhooks";
import { Job } from "./job";
import { Executable } from "./Executable";
 
export class ThumbnailJob implements Executable {
  apiKey: string;
  successUrl?: string;
  failureUrl?: string;
  inputUrl?: string;
  inputBlob?: Blob;
  outputUrl?: string;
  outputBlob?: Blob;
  thumbWidth?: number;
  thumbWatermark?: Watermark;
 
  constructor(apiKey: string) {
    this.thumbWidth = 720;
    this.apiKey = apiKey;
  }
 
  webhooks(webhooks: Webhooks): ThumbnailJob {
    this.successUrl = webhooks.successUrl;
    this.failureUrl = webhooks.failureUrl;
    return this;
  }
 
  from(source: string | Blob): ThumbnailJob {
    if (typeof source === "string") {
      this.inputUrl = source;
    } else {
      this.inputBlob = source;
    }
 
    return this;
  }
 
  to(destination: string | Blob): ThumbnailJob {
    if (typeof destination === "string") {
      this.outputUrl = destination;
    } else {
      this.outputBlob = destination;
    }
    return this;
  }
 
  watermark(watermark: Watermark): ThumbnailJob {
    this.thumbWatermark = watermark;
    return this;
  }
 
  width(width: number): ThumbnailJob {
    this.thumbWidth = width;
    return this;
  }
 
  async execute() {
    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");
    }
 
    const body: any = {
      apiKey: this.apiKey,
      inputCreds: this.inputBlob?.toApiCredentials(),
      outputCreds: this.outputBlob?.toApiCredentials(),
      successURL: this.successUrl,
      failureURL: this.failureUrl,
      inputURL: this.inputUrl || this.inputBlob.toApiUrl(),
      outputURL: this.outputUrl || this.outputBlob.toApiUrl(),
      width: `${this.thumbWidth}`,
      watermark: this.thumbWatermark?.toJSON(),
    };
 
    const resp = await API.createJob("thumbnail", removeUndefinedFromObj(body));
 
    const job = new Job(resp.data.id);
    return job;
  }
}