Edits history of script submission #12109 for ' Download Translated Document (deepl)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Deepl = {
      apiKey: string;
      baseUrl: string;
    };
    /**
     * Download Translated Document
     * Once the status of the document translation process is `done`, the result can be downloaded.
    
    
    For privacy reasons the translated document is automatically removed from the server once it was downloaded and cannot be downloaded again.
     */
    export async function main(
      auth: Deepl,
      document_id: string,
      body: { document_key: string },
    ) {
      const url = new URL(
        `${auth.baseUrl}/v2/document/${document_id}/result`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "DeepL-Auth-Key " + auth.apiKey,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.text();
    }
    

    Submitted by hugo697 428 days ago