All files / sdk utils.ts

90.62% Statements 29/32
80% Branches 20/25
100% Functions 2/2
90% Lines 27/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 61 62 63 64 656x 6x   6x 3827x 3827x 15141x 15141x   15141x       15141x 2x 2x           2x       3827x     6x     4x 4x 19x 1x 1x 1x   1x   1x       1x 1x     1x                   1x       4x    
import axios from "axios";
import fs from "fs";
 
export const convertReqParams = (properties: Record<string, any>) => {
  const clonedProperties = JSON.parse(JSON.stringify(properties));
  for (const propertyKey of Object.keys(clonedProperties)) {
    const object = clonedProperties[propertyKey];
    const isObject = typeof object === "object";
    const isFile =
      isObject &&
      object?.required?.includes("name") &&
      object?.required?.includes("content");
 
    if (isFile) {
      const newKey = `${propertyKey}_file_uri_path`;
      clonedProperties[newKey] = {
        type: "string",
        title: "Name",
        description: "Local absolute path to the file or http url to the file",
      };
 
      delete clonedProperties[propertyKey];
    }
  }
 
  return clonedProperties;
};
 
export const converReqParamForActionExecution = async (
  params: Record<string, any>
) => {
  const clonedParams = JSON.parse(JSON.stringify(params));
  for (const key of Object.keys(clonedParams)) {
    if (key.includes("file_uri_path")) {
      const initKey = key.replace("_file_uri_path", "");
      clonedParams[initKey] = {};
      const newKey = clonedParams[initKey];
 
      const valuePassedByClient = clonedParams[key];
      // if not http url
      if (
        valuePassedByClient.startsWith("http") ||
        valuePassedByClient.startsWith("https")
      ) {
        newKey.name = valuePassedByClient.split("/").pop();
        const response = await axios.get(valuePassedByClient, {
          responseType: "arraybuffer",
        });
        newKey.content = Buffer.from(response.data, "binary").toString(
          "base64"
        );
      } else E{
        const file = await fs.promises.readFile(valuePassedByClient, {
          encoding: null,
        });
        newKey.content = file.toString("base64");
        newKey.name = valuePassedByClient.split("/").pop();
      }
      delete clonedParams[key];
    }
  }
 
  return clonedParams;
};