Edits history of script submission #22155 for ' Upload files (zoho)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Zoho = {
      token: string;
    };
    /**
     * Upload files
     *
     */
    export async function main(
      auth: Zoho,
      _type: string | undefined,
      body: { file: {}[] },
    ) {
      const url = new URL(`https://zohoapis.com/crm/v8/files`);
      for (const [k, v] of [["type", _type]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined) {
          formData.append(k, String(v));
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: "Zoho-oauthtoken " + auth.token,
        },
        body: formData,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago