UNPKG

aptai

Version:

Node.js client for aptAI APIs

75 lines (66 loc) 1.78 kB
const { getConfig, apiDomain, defaultTimeout } = require("../../../config.js"); const axios = require("axios"); // Function to make the API request async function translateLatestAPI(options) { const apiUrl = `${apiDomain}/api/service/translate/inference/latest`; const { apiKey } = getConfig(); if (!apiKey) { throw new Error("API key must be initialized"); } // Default values for the parameters const defaults = { text: "", target_language: "en", timeout: defaultTimeout, }; // Merge the passed options with the defaults const data = { ...defaults, ...options }; const headers = { "Content-Type": "application/json", Authorization: `${apiKey}`, }; try { const response = await axios.post(apiUrl, data, { headers, timeout: data.timeout, }); return response; } catch (error) { if ( error && error.hasOwnProperty("response") && error.response != null && error.response.hasOwnProperty("data") ) { throw new Error(error.response.data.message); } else { throw new Error(error.message); } } } async function supportedLanguageAPI() { const apiUrl = `${apiDomain}/api/service/translate/inference/latest/language`; const { apiKey } = getConfig(); if (!apiKey) { throw new Error("API key must be initialized"); } try { const response = await axios.get(apiUrl); return response; } catch (error) { if ( error && error.hasOwnProperty("response") && error.response != null && error.response.hasOwnProperty("data") ) { throw new Error(error.response.data.message); } else { throw new Error(error.message); } } } module.exports = { translateLatestAPI, supportedLanguageAPI, };