UNPKG

@atrysglobal/babel-ripper

Version:

Interface and strict typing toolkit for accessing the BABEL unified translation service.

135 lines (134 loc) 5.09 kB
import { BabelRipper } from '../babel-ripper'; export interface ITranslationPayload { [key: string]: string | BabelTargetInterface; } export interface IBabelService { loadTranslations(targetList: BabelTargetInterface[], options: BabelRequestOptions): Promise<BabelMessagingResult[]>; loadMessage(target: any, options: BabelRequestOptions): Promise<BabelMessagingResult>; loadTranslationsFromObject(dictionaryKeyPair: ITranslationPayload, options?: BabelRequestOptions): Promise<BabelMessagingResult[]>; } export interface BabelRequestOptions { locale: string; } export interface BabelMessagingResult { id: string; message: string; httpCode?: number; internalCode?: string; } export interface BabelTargetInterface { target: string; params?: any; } export interface BabelServiceConfigParams { defaultOkResponse?: boolean; defaultLocale?: string; apiKey: string; apiAddress?: string; clientTimeout?: number; } /** * Manages the organized retrieval of translations and pre-built * of error messages from atrys babel service. * * Babel Service is organized in two main sections: * * - Translations: Used to retrieve translations * for a given target. * - Interpreter: Used to retrieve pre-built error * messages for a given target. * * Also provides a method to retrieve translations * from a given structured object via Ripper tool. * */ export default class BabelProxyService implements IBabelService { private httpInstance; private serviceUrl; private defaultLocale; private recoveryMessagingTemplate; private recoveryInterpreterTemplate; constructor(options: BabelServiceConfigParams); private resolveCorePayload; /** * Core handler for Messaging features. * * @param {Method} method - HTTP verb to use. * @param {unknown} payload - Payload to send. * @param {BabelRequestOptions} options - Options to use. * * @returns {Promise<BabelMessagingResult>} - Promise with the result. */ private coreMessaging; /** * Core handler for Interpreter features. * * @param {Method} method - HTTP verb to use. * @param {unknown} payload - Payload to send. * @param {BabelRequestOptions} options - Options to use. * * @returns {Promise<BabelMessagingResult[]>} - Promise with the result. */ private coreInterpreter; /** * Manages the translation of multiple messages * through a reference list (i18n). * * Includes support for interpolation of parameters. * * @param {BabelTargetInterface[]} targetList - Translations reference list. * @param {BabelRequestOptions} options - On-the-fly configuration options. * @returns {BabelMessagingResult[]} - List of translated messages. */ loadTranslations(targetList: BabelTargetInterface[], options?: BabelRequestOptions): Promise<BabelMessagingResult[]>; /** * Message translation process tool through * reference dictionary (i18n). * * This tool will respond with a BabelMessagingResult * array. This mean that the translation result extracted * and/or processing have to be defined by the client. * * @param {ITranslationPayload} dictionaryKeyPair - Reference dictionary. * @param {BabelRequestOptions} options - On-the-fly configuration options. * @returns {BabelMessagingResult[]} - List of translated messages. */ loadTranslationsFromObject(dictionaryKeyPair: ITranslationPayload, options?: BabelRequestOptions): Promise<BabelMessagingResult[]>; /** * Message translation process tool through * reference dictionary (i18n). * * This tool will respond with a BabelRipper * object for an organized handling of translations. * * @example * * const dictionary = { * hello: 'messages.hello', * bye: 'messages.bye' * } * * const babel = new BabelProxyService(...); * const result = await babel.loadGuttedTranslations(dictionary); * * result.get(dictionary.hello); // Hola! * result.get(dictionary.bye); // Adiós! * * * @param {ITranslationPayload} dictionaryKeyPair - Reference dictionary. * @param {BabelRequestOptions} options - On-the-fly configuration options. * @returns {BabelRipper} - Organized translation object. */ loadGuttedTranslations(dictionaryKeyPair: ITranslationPayload, options?: BabelRequestOptions): Promise<BabelRipper>; /** * Get a pre-defined message (success or error) * translated through the babel service. * * Includes support for interpolation of parameters. * * @param {BabelTargetInterface} target - Object with message reference to be translated. * @param {BabelRequestOptions} options - On-the-fly configuration options. * @returns {BabelMessagingResult} - Translated message (or List of translated messages). */ loadMessage(target: BabelTargetInterface, options?: BabelRequestOptions): Promise<BabelMessagingResult>; }