i18n-keyless-core 1.12.8 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.d.ts CHANGED
@@ -2,4 +2,5 @@ export declare const api: {
2
2
  fetchTranslation: (url: string, options: RequestInit) => Promise<any>;
3
3
  fetchTranslationsForOneLanguage: (url: string, options: RequestInit) => Promise<any>;
4
4
  fetchAllTranslationsForAllLanguages: (url: string, options: RequestInit) => Promise<any>;
5
+ postLastUsedTranslations: (url: string, options: RequestInit) => Promise<any>;
5
6
  };
package/dist/api.js CHANGED
@@ -20,4 +20,11 @@ export const api = {
20
20
  return { ok: false, error: err.message };
21
21
  });
22
22
  },
23
+ postLastUsedTranslations: async (url, options) => {
24
+ return fetch(url, options)
25
+ .then((res) => (res.status === 200 ? res.json() : { ok: false, error: res.statusText }))
26
+ .catch((err) => {
27
+ return { ok: false, error: err.message };
28
+ });
29
+ },
23
30
  };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export type { Lang, PrimaryLang, Translations, HandleTranslateFunction, GetAllTranslationsFunction, GetAllTranslationsForAllLanguagesFunction, LanguagesConfig, LastRefresh, UniqueId, I18nKeylessRequestBody, I18nKeylessResponse, I18nKeylessAllTranslationsResponse, FetchTranslationParams, TranslationOptions, } from "./types";
1
+ export type { Lang, PrimaryLang, Translations, LastUsedTranslation, HandleTranslateFunction, GetAllTranslationsFunction, SendTranslationsUsageFunction, GetAllTranslationsForAllLanguagesFunction, LanguagesConfig, LastRefresh, UniqueId, I18nKeylessRequestBody, I18nKeylessResponse, I18nKeylessAllTranslationsResponse, FetchTranslationParams, TranslationOptions, } from "./types";
2
2
  export { getTranslationCore, getAllTranslationsFromLanguage, queue } from "./service";
3
3
  export { api } from "./api";
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.12.8",
4
+ "version": "1.14.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
package/dist/service.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Lang, TranslationOptions, I18nKeylessResponse, FetchTranslationParams } from "./types";
1
+ import type { Lang, TranslationOptions, I18nKeylessResponse, FetchTranslationParams, LastUsedTranslation } from "./types";
2
2
  import MyPQueue from "./my-pqueue";
3
3
  export declare const queue: MyPQueue;
4
4
  /**
@@ -25,3 +25,19 @@ export declare function translateKey(key: string, store: FetchTranslationParams,
25
25
  * @returns Promise resolving to the translation response or void if failed
26
26
  */
27
27
  export declare function getAllTranslationsFromLanguage(targetLanguage: Lang, store: FetchTranslationParams): Promise<I18nKeylessResponse | void>;
28
+ /**
29
+ * Send the last used translations to i18n-keyless API
30
+ *
31
+ * This is used to clean up the translations database
32
+ * and to avoid paying for translations that are not used anymore
33
+ *
34
+ * It's called on lib initialization
35
+ * and everytime the language is set
36
+ * @param lastUsedTranslation - The last used translations
37
+ * @param store - The translation store
38
+ * @returns Promise resolving to the translation response or void if failed
39
+ */
40
+ export declare function sendTranslationsUsageToI18nKeyless(lastUsedTranslation: LastUsedTranslation, store: FetchTranslationParams): Promise<{
41
+ ok: boolean;
42
+ message: string;
43
+ } | void>;
package/dist/service.js CHANGED
@@ -161,3 +161,50 @@ export async function getAllTranslationsFromLanguage(targetLanguage, store) {
161
161
  console.error("i18n-keyless: fetch all translations error:", error);
162
162
  }
163
163
  }
164
+ /**
165
+ * Send the last used translations to i18n-keyless API
166
+ *
167
+ * This is used to clean up the translations database
168
+ * and to avoid paying for translations that are not used anymore
169
+ *
170
+ * It's called on lib initialization
171
+ * and everytime the language is set
172
+ * @param lastUsedTranslation - The last used translations
173
+ * @param store - The translation store
174
+ * @returns Promise resolving to the translation response or void if failed
175
+ */
176
+ export async function sendTranslationsUsageToI18nKeyless(lastUsedTranslation, store) {
177
+ const config = store.config;
178
+ if (!config.API_KEY) {
179
+ console.error("i18n-keyless: No config found");
180
+ return;
181
+ }
182
+ if (Object.keys(lastUsedTranslation).length === 0) {
183
+ return;
184
+ }
185
+ try {
186
+ const response = config.sendTranslationsUsage
187
+ ? await config.sendTranslationsUsage(lastUsedTranslation)
188
+ : await api
189
+ .postLastUsedTranslations(`${config.API_URL || "https://api.i18n-keyless.com"}/translate/last-used-translations`, {
190
+ method: "POST",
191
+ headers: {
192
+ "Content-Type": "application/json",
193
+ Authorization: `Bearer ${config.API_KEY}`,
194
+ Version: packageJson.version,
195
+ },
196
+ body: JSON.stringify({
197
+ primaryLanguage: config.languages.primary,
198
+ lastUsedTranslation,
199
+ }),
200
+ })
201
+ .then((res) => res);
202
+ if (response.message) {
203
+ console.warn("i18n-keyless: ", response.message);
204
+ }
205
+ return response;
206
+ }
207
+ catch (error) {
208
+ console.error("i18n-keyless: send last used translation error:", error);
209
+ }
210
+ }
package/dist/types.d.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  export type PrimaryLang = "fr" | "en";
2
2
  export type Lang = "fr" | "en" | "nl" | "it" | "de" | "es" | "pl" | "pt" | "ro" | "sv" | "tr" | "ja" | "cn" | "ru" | "ko" | "ar";
3
3
  export type Translations = Record<string, string>;
4
+ /**
5
+ * The last used translation for a key
6
+ * Useful to clean up the translations database and to avoid paying for translations that are not used anymore
7
+ * Record<string, YYYY-MM-DD>;
8
+ */
9
+ export type LastUsedTranslation = Record<string, string>;
4
10
  export type HandleTranslateFunction = (key: string) => Promise<{
5
11
  ok: boolean;
6
12
  message: string;
@@ -10,6 +16,10 @@ export type HandleTranslateFunction = (key: string) => Promise<{
10
16
  }>;
11
17
  export type GetAllTranslationsFunction = () => Promise<I18nKeylessResponse>;
12
18
  export type GetAllTranslationsForAllLanguagesFunction = () => Promise<I18nKeylessAllTranslationsResponse>;
19
+ export type SendTranslationsUsageFunction = (lastUsedTranslation: LastUsedTranslation) => Promise<{
20
+ ok: boolean;
21
+ message: string;
22
+ }>;
13
23
  export type LastRefresh = string | null;
14
24
  export type UniqueId = string | null;
15
25
  export type LanguagesConfig = {
@@ -59,6 +69,10 @@ export interface I18nKeylessRequestBody {
59
69
  languages: LanguagesConfig["supported"];
60
70
  primaryLanguage: LanguagesConfig["primary"];
61
71
  }
72
+ export interface I18nKeylessTranslationsUsageRequestBody {
73
+ primaryLanguage: LanguagesConfig["primary"];
74
+ lastUsedTranslation: LastUsedTranslation;
75
+ }
62
76
  export interface I18nKeylessResponse {
63
77
  ok: boolean;
64
78
  data: {
@@ -92,6 +106,7 @@ export type FetchTranslationParams = {
92
106
  handleTranslate?: HandleTranslateFunction;
93
107
  getAllTranslations?: GetAllTranslationsFunction;
94
108
  getAllTranslationsForAllLanguages?: GetAllTranslationsForAllLanguagesFunction;
109
+ sendTranslationsUsage?: SendTranslationsUsageFunction;
95
110
  };
96
111
  translations: Translations;
97
112
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.12.8",
4
+ "version": "1.14.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",