Edits history of script submission #12160 for ' Render images of file nodes (figma)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Figma = {
      token: string;
    };
    /**
     * Render images of file nodes
     * Renders images from a file.
     */
    export async function main(
      auth: Figma,
      file_key: string,
      ids: string | undefined,
      version: string | undefined,
      scale: string | undefined,
      format: "jpg" | "png" | "svg" | "pdf" | undefined,
      svg_outline_text: string | undefined,
      svg_include_id: string | undefined,
      svg_include_node_id: string | undefined,
      svg_simplify_stroke: string | undefined,
      contents_only: string | undefined,
      use_absolute_bounds: string | undefined,
    ) {
      const url = new URL(`https://api.figma.com/v1/images/${file_key}`);
      for (const [k, v] of [
        ["ids", ids],
        ["version", version],
        ["scale", scale],
        ["format", format],
        ["svg_outline_text", svg_outline_text],
        ["svg_include_id", svg_include_id],
        ["svg_include_node_id", svg_include_node_id],
        ["svg_simplify_stroke", svg_simplify_stroke],
        ["contents_only", contents_only],
        ["use_absolute_bounds", use_absolute_bounds],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago