Edits history of script submission #18758 for ' Download a file (replicate)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Replicate = {
      token: string;
    };
    /**
     * Download a file
     * Download a file by providing the file owner, access expiry, and a valid signature.
    
    Example cURL request:
    
    ```console
    curl -X GET "https://api.replicate.com/v1/files/cneqzikepnug6xezperrr4z55o/download?expiry=1708515345&owner=mattt&signature=zuoghqlrcnw8YHywkpaXQlHsVhWen%2FDZ4aal76dLiOo%3D"
    ```
    
     */
    export async function main(
      auth: Replicate,
      file_id: string,
      owner: string | undefined,
      expiry: string | undefined,
      signature: string | undefined,
    ) {
      const url = new URL(`https://api.replicate.com/v1/files/${file_id}/download`);
      for (const [k, v] of [
        ["owner", owner],
        ["expiry", expiry],
        ["signature", signature],
      ]) {
        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.text();
    }
    

    Submitted by hugo697 235 days ago