Edits history of script submission #14340 for ' Create folder (box)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Box = {
      token: string;
    };
    /**
     * Create folder
     * Creates a new empty folder within the specified parent folder.
     */
    export async function main(
      auth: Box,
      fields: string | undefined,
      body: {
        name: string;
        parent: { id: string };
        folder_upload_email?: { access?: "open" | "collaborators" } & {};
        sync_state?: "synced" | "not_synced" | "partially_synced";
      },
    ) {
      const url = new URL(`https://api.box.com/2.0/folders`);
      for (const [k, v] of [["fields", fields]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago