import CustomCache from './CustomCache'; import FetchOptions from './FetchOptions'; import { LangCodeOrLocale } from './TranslationRecords'; interface ParticularConfig extends Record { /** * Your Particular.Cloud API key. * Please use your read-access token. * Using a write-access token should be avoided, as it allows you to modify your texts on Particular.Cloud. * Only use a write-access token if you are developing a custom plugin for Particular.Cloud. * Make sure not to leak a write-access token to your client applications or via your public repository. * You don't need a token if you are not fetching from Particular.Cloud. */ token?: string; /** * Which language should be selected? * Instead of passing a locale to each t() function call, you can also pass acceptLanguage and defaultLanguage config options. * acceptLanguage follows the HTTP header Accept-Language specification, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language. * The language will be selected based on matching between Accept-Language header content, your defaultLanguage, and your active and default project languages on Particular.Cloud. * If you just want to pass a default languageCode or locale, then just keep this config paramater empty and use defaultLanguage instead. */ acceptLanguage?: string; /** * What should be the default/fallback language? * Can be of type languageCode (en, de, ...) or locale (en-Us, en-GB, de-DE, ...). * If of type languageCode, your project default language for that languageCode will be selected. * If of type locale, the exact active language will be selected. * In case locale does not have an exact match, the default langauge for the lagnCode will be selected. */ defaultLanguage?: LangCodeOrLocale; /** * onChangeDefaultLanguage callback will be triggered if the defaultLanugage is changed * This can be used to update a reactive variable (e.g. data binding in frontend frameworks) */ onChangeDefaultLanguage?: (defaultLanguage?: LangCodeOrLocale) => void; /** * onChangeLanguage callback will be triggered if the currently used locale/language changed * this can be used to update a reactive variable (e.g. data binding in frontend frameworks) */ onChangeLanguage?: (langCodeOrLocale: LangCodeOrLocale) => void; /** * onChangeConfig callback will be triggered if the config is changed * this can be used to update a reactive variable (e.g. data binding in frontend frameworks) */ onChangeConfig?: (config: ParticularConfig) => void; /** * What should happen on "{missedKey}" template syntax after population? * ignore: for we ignore open template syntax after populating values * warn: for console.warn * throw: for throw Error * default is warn */ onLeftOverTemplateSyntax?: 'ignore' | 'warn' | 'throw'; /** * What should happen to array values? * random: for production experience * pickFirst: for testing * default is pickRandom */ onPickStringFromArray?: 'pickRandom' | 'pickFirst'; /** * What should happen if a requested key is not specified? * generatePlaceholder: will return a mock value for testing * warn: for console.warn * throw: for throw Error * default is warn */ onKeyNotFound?: 'generatePlaceholder' | 'warn' | 'throw'; /** * What should happen if a request throws (e.g. token access denied) * Configuration errors will always be thrown. * This option only handles runtime request errors * warn: for console.warn * throw: for throw Error */ onError?: 'warn' | 'throw'; /** * enableWebsocket set to true will register a websocket to listen to publish events * on publish on Particular.Cloud, client will be notified of changes and refetch keys * default is false */ enableWebsocket?: boolean; /** * onPublish callback will be triggered if the webhook receives an onPublish event * this can be used to re-trigger translation calls to fetch latest values from cache */ onPublish?: () => void; /** * When fetching from Particular.Cloud, should we filter active languages? * true: only active languages will be considered/returned. * false: all languages in the project will be considered/returned. * For a good developer experience, try: activeLanguagesOnly: process.env.NODE_ENV === 'production' */ activeLanguagesOnly?: boolean; /** * What should happen if a memory-cached or custom-cached key expires? * never: keyExpiresIn will be ignored. Only locally cached keys are used. * fetchOnExpired: will fetch the key from Particular.Cloud and update the cache * both values can work together with enableWebsocket */ updateStrategy?: 'never' | 'fetchOnExpired'; /** * When (miliseconds) should the in-memory cached key values expire? * (for customCache, you need to define your own expire time) * no effect if you are not fetching from Particular.Cloud. * default is one day */ keyExpiresIn?: number; /** * You can pass a custom cache implementation to the ParticularConfig. * This makes sense in lambda or serverless environemnts. * This package uses an in-memory cache to manage the localized texts. * This might not be effective in serverless environments. * Implementing a custom (e.g. redis cache) as a layer between this package and Particular.Cloud * might increaes performance by quite a lot. * You don't need a customCache if you are not fetching from Particular.Cloud. */ customCache?: CustomCache; /** * For debugging / package development only */ _useLocalhost?: boolean; /** * For debugging / package development only * Hey you! If you want to use this, please contact us! * We are thinking of making it Request => Promise * to match the fetch signature. Let us know what you need! */ _fetch?: (url: string, options: FetchOptions) => Promise; } export default ParticularConfig;