Edits history of script submission #12115 for ' Retrieve Supported Languages (deepl)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Deepl = {
      apiKey: string;
      baseUrl: string;
    };
    /**
     * Retrieve Supported Languages
     * Retrieve the list of languages that are currently supported for translation, either as source or target language, respectively.
    
    As of January 2024, Arabic (AR) is supported as a source and target language for text translation, 
    but it is not yet supported for document translation. Therefore, Arabic has not yet been added to 
    the `/languages` endpoint. We will add Arabic to the `/languages` endpoint after document translation 
    support is added.
     */
    export async function main(auth: Deepl, type: "source" | "target" | undefined) {
      const url = new URL(`${auth.baseUrl}/v2/languages`);
      for (const [k, v] of [["type", type]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "DeepL-Auth-Key " + auth.apiKey,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago