All files api.ts

54.29% Statements 19/35
27.78% Branches 5/18
75% Functions 3/4
50% Lines 15/30

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 611x   1x   1x                   103x 103x 42x             1x 1x 42x       42x 42x   41x       41x     1x                                       1x  
import axios from "axios";
 
const BASE_API_PATH = "https://api.mediamachine.io";
 
const SERVICES_TO_PATH = {
  thumbnail: "/thumbnail",
  gif_summary: "/summary/gif",
  mp4_summary: "/summary/mp4",
  transcode: "/transcode",
};
 
export type JobStatus = "notStarted" | "queued" | "errored" | "done";
 
function includes(arr: unknown[], elem: unknown) {
  for (const item of arr) {
    if (item === elem) {
      return true;
    }
  }
 
  return false;
}
 
export class API {
  static async createJob(jobType: string, body: unknown) {
    Iif (!includes(Object.keys(SERVICES_TO_PATH), jobType)) {
      return; //throw an error
    }
 
    const uri = `${BASE_API_PATH}${SERVICES_TO_PATH[jobType]}`;
    const res = await axios.post(uri, body);
 
    Iif (res.status !== 201 && res.status !== 200) {
      throw new Error(`Got ${res.status} for body: ${JSON.stringify(body)}`);
    }
 
    return res;
  }
 
  static async jobStatus(reqId: string): Promise<JobStatus> {
    const uri = `${BASE_API_PATH}/job/status?reqId=${reqId}`;
    const res = await axios.get(uri);
 
    if (res.status === 404) {
      return "notStarted";
    }
 
    if (res.status === 200) {
      if (res.data.status === "errored") {
        return "errored";
      } else if (res.data.status === "done") {
        return "done";
      } else if (res.data.status === "queued") {
        return "queued";
      }
    }
 
    return "notStarted";
  }
}