i18n-keyless-node 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/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { init, getAllTranslationsForAllLanguages, awaitForTranslation } from "./service";
1
+ export { init, getAllTranslationsForAllLanguages, getSupportedLanguages, awaitForTranslation } from "./service";
2
2
  export type { Translations, I18nKeylessNodeConfig, I18nKeylessNodeStore, TranslationOptions, I18nKeylessRequestBody, I18nKeylessAllTranslationsResponse, } from "./types";
3
3
  export { type Lang, type PrimaryLang, type I18nKeylessResponse, queue } from "i18n-keyless-core";
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { init, getAllTranslationsForAllLanguages, awaitForTranslation } from "./service";
1
+ export { init, getAllTranslationsForAllLanguages, getSupportedLanguages, awaitForTranslation } from "./service";
2
2
  export { queue } from "i18n-keyless-core";
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-node",
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",
@@ -15,7 +15,7 @@
15
15
  "postpublish": "rm -rf ./dist && rm *.tgz"
16
16
  },
17
17
  "dependencies": {
18
- "i18n-keyless-core": "1.12.8"
18
+ "i18n-keyless-core": "1.14.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@eslint/js": "^9.9.0",
package/dist/service.d.ts CHANGED
@@ -6,6 +6,22 @@ import { I18nKeylessNodeConfig } from "types";
6
6
  * @returns Promise resolving to the translation response or void if failed
7
7
  */
8
8
  export declare function getAllTranslationsForAllLanguages(): Promise<I18nKeylessAllTranslationsResponse | void>;
9
+ /**
10
+ * Send the last used translations to i18n-keyless API
11
+ *
12
+ * This is used to clean up the translations database
13
+ * and to avoid paying for translations that are not used anymore
14
+ *
15
+ * It's called on lib initialization
16
+ * and everytime the language is set
17
+ * @param lastUsedTranslation - The last used translations
18
+ * @param store - The translation store
19
+ * @returns Promise resolving to the translation response or void if failed
20
+ */
21
+ export declare function sendTranslationsUsageToI18nKeyless(): Promise<{
22
+ ok: boolean;
23
+ message: string;
24
+ } | void>;
9
25
  export declare function init(newConfig: I18nKeylessNodeConfig): Promise<I18nKeylessNodeConfig>;
10
26
  /**
11
27
  * Core logic for fetching/retrieving a translation asynchronously.
@@ -34,4 +50,5 @@ declare function awaitForTranslationFn(key: string, currentLanguage: Lang, optio
34
50
  * @throws Re-throws any internal error if the promise rejection is not handled by the caller.
35
51
  */
36
52
  export declare const awaitForTranslation: typeof awaitForTranslationFn;
53
+ export declare function getSupportedLanguages(): Lang[];
37
54
  export {};
package/dist/service.js CHANGED
@@ -19,6 +19,7 @@ const store = {
19
19
  ko: {},
20
20
  ar: {},
21
21
  },
22
+ lastUsedTranslation: {},
22
23
  uniqueId: "",
23
24
  lastRefresh: "",
24
25
  config: {
@@ -71,6 +72,54 @@ export async function getAllTranslationsForAllLanguages() {
71
72
  console.error("i18n-keyless: fetch all translations error:", error);
72
73
  }
73
74
  }
75
+ /**
76
+ * Send the last used translations to i18n-keyless API
77
+ *
78
+ * This is used to clean up the translations database
79
+ * and to avoid paying for translations that are not used anymore
80
+ *
81
+ * It's called on lib initialization
82
+ * and everytime the language is set
83
+ * @param lastUsedTranslation - The last used translations
84
+ * @param store - The translation store
85
+ * @returns Promise resolving to the translation response or void if failed
86
+ */
87
+ export async function sendTranslationsUsageToI18nKeyless() {
88
+ const config = store.config;
89
+ if (!config.API_KEY) {
90
+ console.error("i18n-keyless: No config found");
91
+ return;
92
+ }
93
+ const lastUsedTranslation = store.lastUsedTranslation;
94
+ if (Object.keys(lastUsedTranslation).length === 0) {
95
+ return;
96
+ }
97
+ try {
98
+ const response = config.sendTranslationsUsage
99
+ ? await config.sendTranslationsUsage(lastUsedTranslation)
100
+ : await api
101
+ .postLastUsedTranslations(`${config.API_URL || "https://api.i18n-keyless.com"}/translate/last-used-translations`, {
102
+ method: "POST",
103
+ headers: {
104
+ "Content-Type": "application/json",
105
+ Authorization: `Bearer ${config.API_KEY}`,
106
+ Version: packageJson.version,
107
+ },
108
+ body: JSON.stringify({
109
+ primaryLanguage: config.languages.primary,
110
+ lastUsedTranslation,
111
+ }),
112
+ })
113
+ .then((res) => res);
114
+ if (response.message) {
115
+ console.warn("i18n-keyless: ", response.message);
116
+ }
117
+ return response;
118
+ }
119
+ catch (error) {
120
+ console.error("i18n-keyless: send last used translation error:", error);
121
+ }
122
+ }
74
123
  queue.on("empty", () => {
75
124
  // when each word is translated, fetch the translations for the current language
76
125
  getAllTranslationsForAllLanguages().then((res) => {
@@ -251,3 +300,6 @@ export const awaitForTranslation = new Proxy(awaitForTranslationFn, // Target th
251
300
  return promise;
252
301
  },
253
302
  });
303
+ export function getSupportedLanguages() {
304
+ return store.config.languages.supported;
305
+ }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { HandleTranslateFunction, Lang, PrimaryLang } from "i18n-keyless-core";
1
+ import { HandleTranslateFunction, Lang, LastUsedTranslation, PrimaryLang, SendTranslationsUsageFunction } from "i18n-keyless-core";
2
2
  export type Translations = Record<string, string>;
3
3
  export interface I18nKeylessNodeConfig {
4
4
  /**
@@ -64,6 +64,15 @@ export interface I18nKeylessNodeConfig {
64
64
  * - not use this `getAllTranslationsForAllLanguages` function nor API_KEY key, and provide your own API_URL
65
65
  */
66
66
  getAllTranslationsForAllLanguages?: () => Promise<I18nKeylessAllTranslationsResponse>;
67
+ /**
68
+ * if this function exists, it will be called instead of the API call
69
+ * if this function doesn't exist, the default behavior is to call the API, with the API_KEY
70
+ * therefore you need either to
71
+ * - use this `sendTranslationsUsage` function to handle the translation with your own API
72
+ * - not use this `sendTranslationsUsage` function, and use the built in API call with the API_KEY filled
73
+ * - not use this `sendTranslationsUsage` function nor API_KEY key, and provide your own API_URL
74
+ */
75
+ sendTranslationsUsage?: SendTranslationsUsageFunction;
67
76
  }
68
77
  export interface I18nKeylessNodeStore {
69
78
  /**
@@ -78,6 +87,10 @@ export interface I18nKeylessNodeStore {
78
87
  * the translations fetched from i18n-keyless' API
79
88
  */
80
89
  translations: Record<Lang, Translations>;
90
+ /**
91
+ * the last used translations
92
+ */
93
+ lastUsedTranslation: LastUsedTranslation;
81
94
  /**
82
95
  * i18n-keyless' config
83
96
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-node",
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",
@@ -15,7 +15,7 @@
15
15
  "postpublish": "rm -rf ./dist && rm *.tgz"
16
16
  },
17
17
  "dependencies": {
18
- "i18n-keyless-core": "1.12.8"
18
+ "i18n-keyless-core": "1.14.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@eslint/js": "^9.9.0",