Edits history of script submission #12070 for ' Upload a logo for the organization (clerk)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Clerk = {
      apiKey: string;
    };
    type Base64 = string;
    /**
     * Upload a logo for the organization
     * Set or replace an organization's logo, by uploading an image file.
    This endpoint uses the `multipart/form-data` request content type and accepts a file of image type.
    The file size cannot exceed 10MB.
    Only the following file content types are supported: `image/jpeg`, `image/png`, `image/gif`, `image/webp`, `image/x-icon`, `image/vnd.microsoft.icon`.
     */
    export async function main(
      auth: Clerk,
      organization_id: string,
      body: {
        uploader_user_id?: string;
        file: {
          base64: Base64;
          type:
            | "image/png"
            | "image/jpeg"
            | "image/gif"
            | "application/pdf"
            | "appication/json"
            | "text/csv"
            | "text/plain"
            | "audio/mpeg"
            | "audio/wav"
            | "video/mp4";
          name: string;
        };
      },
    ) {
      const url = new URL(
        `https://api.clerk.com/v1/organizations/${organization_id}/logo`,
      );
    
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined && v !== "") {
          if (["file"].includes(k)) {
            const { base64, type, name } = v as {
              base64: Base64;
              type: string;
              name: string;
            };
            formData.append(
              k,
              new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
                type,
              }),
              name,
            );
          } else {
            formData.append(k, String(v));
          }
        }
      }
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          Authorization: "Bearer " + auth.apiKey,
        },
        body: formData,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago