Edits history of script submission #12247 for ' Upload attachments to a doc (grist)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Grist = {
      apiKey: string;
      host: string;
    };
    /**
     * Upload attachments to a doc
     *
     */
    export async function main(
      auth: Grist,
      docId: string,
      body: { upload?: string[] },
    ) {
      const url = new URL(`https://${auth.host}/api/docs/${docId}/attachments`);
    
      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: "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