All files / client/methods getthumbs.js

80% Statements 12/15
100% Branches 5/5
75% Functions 3/4
80% Lines 12/15
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            19x           1x       1x 1x 1x 1x   1x 1x   1x                               1x 1x 1x      
/* @flow */
 
import invariant from "invariant";
import type { MethodApi, thumbTypes, thumbSizes, thumbB64 } from "../types";
import createParser from "../../utils/thumbs";
 
export default ({ client }: MethodApi) => (
  fileids: Array<number>,
  receiveThumb: thumbB64 => void,
  thumbType: thumbTypes = "auto",
  size: thumbSizes = "32x32"
): Promise<Array<thumbB64>> => {
  invariant(
    typeof fileids === "object" && "length" in fileids && fileids.length,
    "`fileids` is required, must be array of numbers."
  );
  invariant(["auto", "png", "jpg"].indexOf(thumbType) !== 1, 'thumbType must be one of: "auto", "png", "jpg".');
  invariant(["32x32", "120x120"].indexOf(size) !== 1, 'size must be one of: "32x32", "120x120".');
  invariant(receiveThumb, "`receiveThumb` is required.");
  invariant(typeof receiveThumb === "function", "`receiveThumb` must be a function.");
 
  let thumbs = [];
  const parser = createParser();
 
  return client
    .api("getthumbs", {
      responseType: "text",
      params: {
        fileids: fileids.join(","),
        type: thumbType,
        size: size,
        crop: 1
      },
      onProgress: (progress: any) => {
        const progressThumbs = parser(progress.currentTarget.responseText);
        thumbs = thumbs.concat(progressThumbs);
        progressThumbs.forEach(receiveThumb);
      }
    })
    .then(response => {
      const responseThumbs = parser(response);
      responseThumbs.forEach(receiveThumb);
      return thumbs.concat(responseThumbs);
    });
};