Edits history of script submission #12116 for ' Upload and Translate a Document (deepl)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Deepl = {
      apiKey: string;
      baseUrl: string;
    };
    type Base64 = string;
    /**
     * Upload and Translate a Document
     * This call uploads a document and queues it for translation.
     */
    export async function main(
      auth: Deepl,
      body: {
        source_lang?:
          | "BG"
          | "CS"
          | "DA"
          | "DE"
          | "EL"
          | "EN"
          | "ES"
          | "ET"
          | "FI"
          | "FR"
          | "HU"
          | "ID"
          | "IT"
          | "JA"
          | "KO"
          | "LT"
          | "LV"
          | "NB"
          | "NL"
          | "PL"
          | "PT"
          | "RO"
          | "RU"
          | "SK"
          | "SL"
          | "SV"
          | "TR"
          | "UK"
          | "ZH";
        target_lang:
          | "BG"
          | "CS"
          | "DA"
          | "DE"
          | "EL"
          | "ES"
          | "ET"
          | "FI"
          | "FR"
          | "HU"
          | "ID"
          | "IT"
          | "JA"
          | "KO"
          | "LT"
          | "LV"
          | "NB"
          | "NL"
          | "PL"
          | "RO"
          | "RU"
          | "SK"
          | "SL"
          | "SV"
          | "TR"
          | "UK"
          | "ZH"
          | "EN-GB"
          | "EN-US"
          | "PT-BR"
          | "PT-PT"
          | "ZH-HANS";
        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;
        };
        filename?: string;
        output_format?: string;
        formality?: "default" | "more" | "less" | "prefer_more" | "prefer_less";
        glossary_id?: string;
      },
    ) {
      const url = new URL(`${auth.baseUrl}/v2/document`);
    
      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: "POST",
        headers: {
          Authorization: "DeepL-Auth-Key " + 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