Edits history of script submission #14534 for ' Restore folder (box)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Box = {
      token: string;
    };
    /**
     * Restore folder
     * Restores a folder that has been moved to the trash.
    
    An optional new parent ID can be provided to restore the folder to in case the
    original folder has been deleted.
    
    During this operation, part of the file tree will be locked, mainly
    the source folder and all of its descendants, as well as the destination
    folder.
    
    For the duration of the operation, no other move, copy, delete, or restore
    operation can performed on any of the locked folders.
     */
    export async function main(
      auth: Box,
      folder_id: string,
      fields: string | undefined,
      body: { name?: string; parent?: { id?: string } & {} },
    ) {
      const url = new URL(`https://api.box.com/2.0/folders/${folder_id}`);
      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