UNPKG

arml

Version:

Enjoy unlimited and free translation with our API.

1 lines 11.4 kB
{"version":3,"sources":["../src/utils/apiClient.ts","../src/config.ts","../src/utils/handleError.ts","../src/translate/translate.tsx","../src/translate/index.ts"],"sourcesContent":["import axios from \"axios\";\r\nimport { TRANSLATE_API_URL } from \"@/config\";\r\nimport { handleError } from \"./handleError\";\r\n\r\nexport const apiClient = axios.create({\r\n baseURL: TRANSLATE_API_URL,\r\n headers: {\r\n \"Content-Type\": \"application/json\",\r\n },\r\n});\r\n\r\n/**\r\n * Sends a POST request to the translation API with retry logic.\r\n * @param url - API endpoint URL\r\n * @param body - Request body\r\n * @param params - Query parameters\r\n * @param retryCount - Number of retry attempts in case of failure\r\n * @returns Response data or null on failure\r\n */\r\nexport const sendPostRequest = async <T>(\r\n url: string,\r\n body: Record<string, unknown>,\r\n params: Record<string, string>\r\n): Promise<T | null> => {\r\n try {\r\n const response = await apiClient.post<T>(url, body, { params });\r\n if (response.status === 200) return response.data;\r\n } catch (error: any) {\r\n if (error.response) {\r\n handleError(error.response);\r\n } else {\r\n throw new Error(\"An unknown error occurred\"); \r\n }\r\n }\r\n return null;\r\n};\r\n","export const TRANSLATE_API_URL = \"https://api.translate.arml.trymagic.xyz/v1\"","/**\r\n * Handles error responses and throws an error with detailed message.\r\n * @param response - API response object\r\n */\r\nexport const handleError = (response: any) => {\r\n const { status, data } = response;\r\n \r\n // Handle known errors and throw an appropriate message\r\n if (status === 401) {\r\n throw new Error(`Unauthorized: ${data.detail?.msg || 'Invalid API key.'}`);\r\n }else if (status === 400) {\r\n throw new Error(`Bad Request: ${data.detail?.msg || 'You may have sent an incorrect or missing value.'}`);\r\n } else if (status === 403) {\r\n throw new Error('Forbidden: Access is denied.');\r\n } else if (status === 500) {\r\n throw new Error('Server error: Please try again later.');\r\n } else if (status === 404) {\r\n throw new Error('Not Found: The requested resource could not be found.');\r\n } else {\r\n throw new Error(`Unexpected error: ${data.detail?.msg || 'An unknown error occurred.'}`);\r\n }\r\n };","import {\r\n BatchTranslationResponse,\r\n DetectTranslationResponse,\r\n TranslationResponse,\r\n} from \"@/types\";\r\nimport { sendPostRequest } from \"@/utils/apiClient\";\r\n\r\n/**\r\n * Translates a single text.\r\n * @param apiKey - User's API key\r\n * @param text - Text to translate\r\n * @param target - Target language\r\n * @param source - Source language (default: auto)\r\n * @param retryCount - Number of retry attempts (default: 3)\r\n * @returns Translation response or null\r\n */\r\nexport const Translate = async (\r\n apiKey: string,\r\n text: string,\r\n target: string,\r\n source: string = \"auto\",\r\n retryCount: number = 3\r\n): Promise<TranslationResponse | null> => {\r\n const url = \"/translate\";\r\n const params = {\r\n api_key: apiKey,\r\n text,\r\n source,\r\n target,\r\n retry_count: retryCount.toString(),\r\n };\r\n\r\n return await sendPostRequest<TranslationResponse>(url, {}, params);\r\n};\r\n\r\n\r\n/**\r\n * Translates multiple texts in a batch.\r\n * @param apiKey - User's API key\r\n * @param texts - Array of texts to translate\r\n * @param target - Target language\r\n * @param source - Source language (default: auto)\r\n * @param retryCount - Number of retry attempts (default: 3)\r\n * @returns Batch translation response or null\r\n */\r\nexport const BatchTranslator = async (\r\n apiKey: string,\r\n texts: string[],\r\n target: string,\r\n source: string = \"auto\",\r\n retryCount: number = 3\r\n): Promise<BatchTranslationResponse | null> => {\r\n const url = \"/batch_translate\";\r\n const body = { texts, source, target, retry_count: retryCount.toString() };\r\n const params = { api_key: apiKey };\r\n\r\n return await sendPostRequest<BatchTranslationResponse>(url, body, params);\r\n};\r\n\r\n\r\n\r\n/**\r\n * Translates multiple texts in a batch.\r\n * @param apiKey - User's API key\r\n * @param text - text to translate\r\n * @returns Detect translation response or null\r\n */\r\nexport const DetectLang = async (\r\n apiKey: string,\r\n text: string,\r\n): Promise<DetectTranslationResponse | null> => {\r\n const url = \"/detect\";\r\n const params = {text: text, api_key: apiKey };\r\n\r\n return await sendPostRequest<DetectTranslationResponse>(url, {}, params);\r\n};\r\n","import {\r\n BatchTranslationResponse,\r\n DetectTranslationResponse,\r\n TranslateOptions,\r\n TranslationResponse,\r\n} from \"@/types\";\r\n\r\nimport { BatchTranslator, DetectLang, Translate } from \"./translate\";\r\n\r\n/**\r\n * The Translate class allows you to interact with the translation API.\r\n * You can translate individual text, batch translate multiple texts,\r\n * and detect the language of a given text.\r\n */\r\nclass Translator {\r\n private apiKey: string;\r\n\r\n /**\r\n * @param apiKey - User's API key required for accessing the translation service\r\n */\r\n constructor(apiKey: string) {\r\n if (!apiKey) {\r\n throw new Error(\"API key is required to create a Translate instance.\");\r\n }\r\n this.apiKey = apiKey;\r\n }\r\n\r\n /**\r\n * Translates a single piece of text to the target language.\r\n *\r\n * @param text - The text to translate\r\n * @param options - Object containing `source` and `target` languages\r\n * @returns A promise that resolves to the translated text or null in case of an error\r\n */\r\n async translate(\r\n text: string,\r\n { source = \"auto\", target = \"en\" }: TranslateOptions\r\n ): Promise<TranslationResponse | null> {\r\n if (!text || !target) {\r\n throw new Error(\"Both text and target language are required.\");\r\n }\r\n\r\n try {\r\n const data = await Translate(this.apiKey, text, target, source);\r\n return data;\r\n } catch (error) {\r\n console.error(\"Translation API error:\", error);\r\n return null;\r\n }\r\n }\r\n\r\n /**\r\n * Translates multiple texts in batch.\r\n *\r\n * @param texts - Array of texts to translate\r\n * @param options - Object containing `source` and `target` languages\r\n * @returns A promise that resolves to an array of translated texts or null in case of an error\r\n */\r\n async batchTranslate(\r\n texts: string[],\r\n { source = \"auto\", target = \"en\" }: TranslateOptions\r\n ): Promise<BatchTranslationResponse | null> {\r\n if (!texts || !target) {\r\n throw new Error(\"Both texts and target language are required.\");\r\n }\r\n\r\n try {\r\n const data = await BatchTranslator(this.apiKey, texts, target, source);\r\n return data;\r\n } catch (error) {\r\n console.error(\"Translation API error:\", error);\r\n return null;\r\n }\r\n }\r\n\r\n /**\r\n * Detects the language of a given text.\r\n *\r\n * @param text - The text whose language needs to be detected\r\n * @returns A promise that resolves to the detected language or null in case of an error\r\n */\r\n async detect(text: string): Promise<DetectTranslationResponse | null> {\r\n if (!text) {\r\n throw new Error(\"Text is required to detect language.\");\r\n }\r\n\r\n try {\r\n const data = await DetectLang(this.apiKey, text);\r\n return data;\r\n } catch (error) {\r\n console.error(\"Translation API error:\", error);\r\n return null;\r\n }\r\n }\r\n\r\n /**\r\n * Quick translation for a single piece of text.\r\n * This method does not require an instance of the class.\r\n *\r\n * @param text - The text to translate\r\n * @param apiKey - User's API key for the translation service\r\n * @param options - Object containing `source` and `target` languages\r\n * @returns The translated text or undefined if an error occurs\r\n */\r\n static async quick({\r\n text,\r\n apiKey,\r\n options,\r\n }: {\r\n text: string;\r\n apiKey: string;\r\n options: TranslateOptions;\r\n }): Promise<string | undefined> {\r\n try {\r\n const instance = new Translator(apiKey);\r\n const translation = await instance.translate(text, options);\r\n return translation?.translated_text;\r\n } catch (error) {\r\n console.error(\"Quick translation error:\", error);\r\n return undefined;\r\n }\r\n }\r\n}\r\n\r\nexport default Translator;\r\n"],"mappings":"AAAA,OAAOA,MAAW,QCAX,IAAMC,EAAoB,6CCI1B,IAAMC,EAAeC,GAAkB,CAC1C,GAAM,CAAE,OAAAC,EAAQ,KAAAC,CAAK,EAAIF,EAGzB,MAAIC,IAAW,IACP,IAAI,MAAM,iBAAiBC,EAAK,QAAQ,KAAO,kBAAkB,EAAE,EACjED,IAAW,IACb,IAAI,MAAM,gBAAgBC,EAAK,QAAQ,KAAO,kDAAkD,EAAE,EAC/FD,IAAW,IACd,IAAI,MAAM,8BAA8B,EACrCA,IAAW,IACd,IAAI,MAAM,uCAAuC,EAC9CA,IAAW,IACd,IAAI,MAAM,uDAAuD,EAEjE,IAAI,MAAM,qBAAqBC,EAAK,QAAQ,KAAO,4BAA4B,EAAE,CAE3F,EFjBK,IAAMC,EAAYC,EAAM,OAAO,CACpC,QAASC,EACT,QAAS,CACP,eAAgB,kBAClB,CACF,CAAC,EAUYC,EAAkB,MAC7BC,EACAC,EACAC,IACsB,CACtB,GAAI,CACF,IAAMC,EAAW,MAAMP,EAAU,KAAQI,EAAKC,EA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AAJ,EAAS,OAAQ,OAAAC,EAAS,IAAK,EACS,CAC1C,GAAI,CAACG,GAAS,CAACH,EACb,MAAM,IAAI,MAAM,8CAA8C,EAGhE,GAAI,CAEF,OADa,MAAMI,EAAgB,KAAK,OAAQD,EAAOH,EAAQD,CAAM,CAEvE,OAASG,EAAO,CACd,eAAQ,MAAM,yBAA0BA,CAAK,EACtC,IACT,CACF,CAQA,MAAM,OAAOJ,EAAyD,CACpE,GAAI,CAACA,EACH,MAAM,IAAI,MAAM,sCAAsC,EAGxD,GAAI,CAEF,OADa,MAAMO,EAAW,KAAK,OAAQP,CAAI,CAEjD,OAASI,EAAO,CACd,eAAQ,MAAM,yBAA0BA,CAAK,EACtC,IACT,CACF,CAWA,aAAa,MAAM,CACjB,KAAAJ,EACA,OAAAD,EACA,QAAAS,CACF,EAIgC,CAC9B,GAAI,CAGF,OADoB,MADH,IAAIV,EAAWC,CAAM,EACH,UAAUC,EAAMQ,CAAO,IACtC,eACtB,OAASJ,EAAO,CACd,QAAQ,MAAM,2BAA4BA,CAAK,EAC/C,MACF,CACF,CACF,EAEOK,EAAQZ","names":["axios","TRANSLATE_API_URL","handleError","response","status","data","apiClient","axios","TRANSLATE_API_URL","sendPostRequest","url","body","params","response","error","handleError","Translate","apiKey","text","target","source","retryCount","url","params","sendPostRequest","BatchTranslator","texts","body","DetectLang","Translator","_Translator","apiKey","text","source","target","Translate","error","texts","BatchTranslator","DetectLang","options","translate_default"]}