i18n-keyless-core 1.9.5 → 1.10.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
@@ -1,4 +1,5 @@
1
1
  export declare const api: {
2
- fetchTranslations: (url: string, options: RequestInit) => Promise<any>;
3
2
  fetchTranslation: (url: string, options: RequestInit) => Promise<any>;
3
+ fetchTranslationsForOneLanguage: (url: string, options: RequestInit) => Promise<any>;
4
+ fetchAllTranslationsForAllLanguages: (url: string, options: RequestInit) => Promise<any>;
4
5
  };
package/dist/api.js CHANGED
@@ -1,12 +1,19 @@
1
1
  export const api = {
2
- fetchTranslations: async (url, options) => {
2
+ fetchTranslation: async (url, options) => {
3
3
  return fetch(url, options)
4
4
  .then((res) => res.json())
5
5
  .catch((err) => {
6
6
  return { ok: false, error: err.message };
7
7
  });
8
8
  },
9
- fetchTranslation: async (url, options) => {
9
+ fetchTranslationsForOneLanguage: async (url, options) => {
10
+ return fetch(url, options)
11
+ .then((res) => res.json())
12
+ .catch((err) => {
13
+ return { ok: false, error: err.message };
14
+ });
15
+ },
16
+ fetchAllTranslationsForAllLanguages: async (url, options) => {
10
17
  return fetch(url, options)
11
18
  .then((res) => res.json())
12
19
  .catch((err) => {
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export type { I18nConfig, Lang, PrimaryLang, Translations, TranslationStore, TranslationStoreState, I18nKeylessRequestBody, I18nKeylessResponse, TranslationOptions, } from "./types";
2
- export { getTranslationCore, fetchAllTranslations, validateLanguage, queue } from "./service";
2
+ export { getTranslationCore, getAllTranslationsFromLanguage, getAllTranslationsForAllLanguages, validateLanguage, queue, } from "./service";
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { getTranslationCore, fetchAllTranslations, validateLanguage, queue } from "./service";
1
+ export { getTranslationCore, getAllTranslationsFromLanguage, getAllTranslationsForAllLanguages, validateLanguage, queue, } from "./service";
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.9.5",
4
+ "version": "1.10.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -10,7 +10,8 @@
10
10
  "dist"
11
11
  ],
12
12
  "scripts": {
13
- "prepublishOnly": "rm -rf ./dist && tsc --project tsconfig.json && npm pack"
13
+ "prepublishOnly": "rm -rf ./dist && tsc --project tsconfig.json && npm pack",
14
+ "test": "echo 'no test for core'"
14
15
  },
15
16
  "devDependencies": {
16
17
  "@types/node": "^22.5.5",
package/dist/service.d.ts CHANGED
@@ -32,4 +32,11 @@ export declare function translateKey(key: string, store: TranslationStore, optio
32
32
  * @param store - The translation store
33
33
  * @returns Promise resolving to the translation response or void if failed
34
34
  */
35
- export declare function fetchAllTranslations(targetLanguage: Lang, store: TranslationStore): Promise<I18nKeylessResponse | void>;
35
+ export declare function getAllTranslationsFromLanguage(targetLanguage: Lang, store: TranslationStore): Promise<I18nKeylessResponse | void>;
36
+ /**
37
+ * Fetches all translations for a target language
38
+ * @param targetLanguage - The language code to fetch translations for
39
+ * @param store - The translation store
40
+ * @returns Promise resolving to the translation response or void if failed
41
+ */
42
+ export declare function getAllTranslationsForAllLanguages(store: Omit<TranslationStore, "currentLanguage" | "setLanguage">): Promise<I18nKeylessResponse | void>;
package/dist/service.js CHANGED
@@ -140,7 +140,7 @@ export function translateKey(key, store, options) {
140
140
  * @param store - The translation store
141
141
  * @returns Promise resolving to the translation response or void if failed
142
142
  */
143
- export async function fetchAllTranslations(targetLanguage, store) {
143
+ export async function getAllTranslationsFromLanguage(targetLanguage, store) {
144
144
  const config = store.config;
145
145
  const lastRefresh = store.lastRefresh;
146
146
  const uniqueId = store.uniqueId;
@@ -155,7 +155,50 @@ export async function fetchAllTranslations(targetLanguage, store) {
155
155
  const response = config.getAllTranslations
156
156
  ? await config.getAllTranslations()
157
157
  : await api
158
- .fetchTranslations(`${config.API_URL || "https://api.i18n-keyless.com"}/translate/${targetLanguage}?last_refresh=${lastRefresh}`, {
158
+ .fetchTranslationsForOneLanguage(`${config.API_URL || "https://api.i18n-keyless.com"}/translate/${targetLanguage}?last_refresh=${lastRefresh}`, {
159
+ method: "GET",
160
+ headers: {
161
+ "Content-Type": "application/json",
162
+ Authorization: `Bearer ${config.API_KEY}`,
163
+ Version: packageJson.version,
164
+ unique_id: uniqueId || "",
165
+ },
166
+ })
167
+ .then((res) => res);
168
+ if (!response.ok) {
169
+ throw new Error(response.error);
170
+ }
171
+ if (response.message) {
172
+ console.warn("i18n-keyless: ", response.message);
173
+ }
174
+ return response;
175
+ }
176
+ catch (error) {
177
+ console.error("i18n-keyless: fetch all translations error:", error);
178
+ }
179
+ }
180
+ /**
181
+ * Fetches all translations for a target language
182
+ * @param targetLanguage - The language code to fetch translations for
183
+ * @param store - The translation store
184
+ * @returns Promise resolving to the translation response or void if failed
185
+ */
186
+ export async function getAllTranslationsForAllLanguages(store) {
187
+ const config = store.config;
188
+ const lastRefresh = store.lastRefresh;
189
+ const uniqueId = store.uniqueId;
190
+ if (!config) {
191
+ console.error("i18n-keyless: No config found");
192
+ return;
193
+ }
194
+ // if (config.languages.primary === targetLanguage) {
195
+ // return;
196
+ // }
197
+ try {
198
+ const response = config.getAllTranslations
199
+ ? await config.getAllTranslations()
200
+ : await api
201
+ .fetchAllTranslationsForAllLanguages(`${config.API_URL || "https://api.i18n-keyless.com"}/translate/?last_refresh=${lastRefresh}`, {
159
202
  method: "GET",
160
203
  headers: {
161
204
  "Content-Type": "application/json",
package/dist/types.d.ts CHANGED
@@ -95,6 +95,15 @@ export interface I18nConfig {
95
95
  * - not use this `getAllTranslations` function nor API_KEY key, and provide your own API_URL
96
96
  */
97
97
  getAllTranslations?: () => Promise<I18nKeylessResponse>;
98
+ /**
99
+ * if this function exists, it will be called instead of the API call
100
+ * if this function doesn't exist, the default behavior is to call the API, with the API_KEY
101
+ * therefore you need either to
102
+ * - use this `getAllTranslationsForAllLanguages` function to handle the translation with your own API
103
+ * - not use this `getAllTranslationsForAllLanguages` function, and use the built in API call with the API_KEY filled
104
+ * - not use this `getAllTranslationsForAllLanguages` function nor API_KEY key, and provide your own API_URL
105
+ */
106
+ getAllTranslationsForAllLanguages?: () => Promise<I18nKeylessAllTranslationsResponse>;
98
107
  /**
99
108
  * the storage to use for the translations
100
109
  *
@@ -194,4 +203,14 @@ export interface I18nKeylessResponse {
194
203
  error: string;
195
204
  message: string;
196
205
  }
206
+ export interface I18nKeylessAllTranslationsResponse {
207
+ ok: boolean;
208
+ data: {
209
+ translations: Record<Lang, Translations>;
210
+ uniqueId: string | null;
211
+ lastRefresh: string | null;
212
+ };
213
+ error: string;
214
+ message: string;
215
+ }
197
216
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "i18n-keyless-core",
3
3
  "private": false,
4
- "version": "1.9.5",
4
+ "version": "1.10.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -10,7 +10,8 @@
10
10
  "dist"
11
11
  ],
12
12
  "scripts": {
13
- "prepublishOnly": "rm -rf ./dist && tsc --project tsconfig.json && npm pack"
13
+ "prepublishOnly": "rm -rf ./dist && tsc --project tsconfig.json && npm pack",
14
+ "test": "echo 'no test for core'"
14
15
  },
15
16
  "devDependencies": {
16
17
  "@types/node": "^22.5.5",