@localess/cli 0.0.1-dev.20260217120300 → 0.0.1-dev.20260220080241
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.js +45 -10
- package/dist/index.mjs +45 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -73,6 +73,33 @@ var TTLCache = class {
|
|
|
73
73
|
|
|
74
74
|
// src/client.ts
|
|
75
75
|
var LOG_GROUP = `${FG_BLUE}[Localess:Client]${RESET}`;
|
|
76
|
+
async function fetchWithRetry(url, options, retryCount = 3, retryDelay = 500, debug) {
|
|
77
|
+
let attempt = 0;
|
|
78
|
+
let lastError;
|
|
79
|
+
while (attempt <= retryCount) {
|
|
80
|
+
try {
|
|
81
|
+
const response = await fetch(url, options);
|
|
82
|
+
if (!response.ok && response.status >= 500) {
|
|
83
|
+
if (debug) {
|
|
84
|
+
console.log(LOG_GROUP, `fetchWithRetry: HTTP ${response.status} on attempt ${attempt + 1}`);
|
|
85
|
+
}
|
|
86
|
+
lastError = new Error(`HTTP ${response.status}`);
|
|
87
|
+
} else {
|
|
88
|
+
return response;
|
|
89
|
+
}
|
|
90
|
+
} catch (err) {
|
|
91
|
+
if (debug) {
|
|
92
|
+
console.log(LOG_GROUP, `fetchWithRetry: network error on attempt ${attempt + 1}`, err);
|
|
93
|
+
}
|
|
94
|
+
lastError = err;
|
|
95
|
+
}
|
|
96
|
+
attempt++;
|
|
97
|
+
if (attempt <= retryCount) {
|
|
98
|
+
await new Promise((res) => setTimeout(res, retryDelay));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
throw lastError;
|
|
102
|
+
}
|
|
76
103
|
function localessClient(options) {
|
|
77
104
|
if (options.debug) {
|
|
78
105
|
console.log(LOG_GROUP, "Client Options : ", options);
|
|
@@ -103,7 +130,7 @@ function localessClient(options) {
|
|
|
103
130
|
return cache.get(url);
|
|
104
131
|
}
|
|
105
132
|
try {
|
|
106
|
-
const response = await
|
|
133
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
107
134
|
if (options.debug) {
|
|
108
135
|
console.log(LOG_GROUP, "getSpace status : ", response.status);
|
|
109
136
|
}
|
|
@@ -142,7 +169,7 @@ function localessClient(options) {
|
|
|
142
169
|
return cache.get(url);
|
|
143
170
|
}
|
|
144
171
|
try {
|
|
145
|
-
const response = await
|
|
172
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
146
173
|
if (options.debug) {
|
|
147
174
|
console.log(LOG_GROUP, "getLinks status : ", response.status);
|
|
148
175
|
}
|
|
@@ -180,7 +207,7 @@ function localessClient(options) {
|
|
|
180
207
|
return cache.get(url);
|
|
181
208
|
}
|
|
182
209
|
try {
|
|
183
|
-
const response = await
|
|
210
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
184
211
|
if (options.debug) {
|
|
185
212
|
console.log(LOG_GROUP, "getContentBySlug status : ", response.status);
|
|
186
213
|
}
|
|
@@ -218,7 +245,7 @@ function localessClient(options) {
|
|
|
218
245
|
return cache.get(url);
|
|
219
246
|
}
|
|
220
247
|
try {
|
|
221
|
-
const response = await
|
|
248
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
222
249
|
if (options.debug) {
|
|
223
250
|
console.log(LOG_GROUP, "getContentById status : ", response.status);
|
|
224
251
|
}
|
|
@@ -230,11 +257,19 @@ function localessClient(options) {
|
|
|
230
257
|
return {};
|
|
231
258
|
}
|
|
232
259
|
},
|
|
233
|
-
async getTranslations(locale) {
|
|
260
|
+
async getTranslations(locale, params) {
|
|
234
261
|
if (options.debug) {
|
|
235
262
|
console.log(LOG_GROUP, "getTranslations() locale : ", locale);
|
|
263
|
+
console.log(LOG_GROUP, "getTranslations() params : ", JSON.stringify(params));
|
|
264
|
+
}
|
|
265
|
+
let version = "";
|
|
266
|
+
if (options?.version && options.version == "draft") {
|
|
267
|
+
version = `&version=${options.version}`;
|
|
268
|
+
}
|
|
269
|
+
if (params?.version && params.version == "draft") {
|
|
270
|
+
version = `&version=${params.version}`;
|
|
236
271
|
}
|
|
237
|
-
let url = `${options.origin}/api/v1/spaces/${options.spaceId}/translations/${locale}?token=${options.token}`;
|
|
272
|
+
let url = `${options.origin}/api/v1/spaces/${options.spaceId}/translations/${locale}?token=${options.token}${version}`;
|
|
238
273
|
if (options.debug) {
|
|
239
274
|
console.log(LOG_GROUP, "getTranslations fetch url : ", url);
|
|
240
275
|
}
|
|
@@ -245,7 +280,7 @@ function localessClient(options) {
|
|
|
245
280
|
return cache.get(url);
|
|
246
281
|
}
|
|
247
282
|
try {
|
|
248
|
-
const response = await
|
|
283
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
249
284
|
if (options.debug) {
|
|
250
285
|
console.log(LOG_GROUP, "getTranslations status : ", response.status);
|
|
251
286
|
}
|
|
@@ -272,14 +307,14 @@ function localessClient(options) {
|
|
|
272
307
|
values
|
|
273
308
|
};
|
|
274
309
|
try {
|
|
275
|
-
const response = await
|
|
310
|
+
const response = await fetchWithRetry(url, {
|
|
276
311
|
method: "POST",
|
|
277
312
|
headers: {
|
|
278
313
|
"X-API-KEY": options.token,
|
|
279
314
|
...fetchOptions.headers
|
|
280
315
|
},
|
|
281
316
|
body: JSON.stringify(body)
|
|
282
|
-
});
|
|
317
|
+
}, options.retryCount, options.retryDelay, options.debug);
|
|
283
318
|
if (options.debug) {
|
|
284
319
|
console.log(LOG_GROUP, "updateTranslations status : ", response.status);
|
|
285
320
|
}
|
|
@@ -303,7 +338,7 @@ function localessClient(options) {
|
|
|
303
338
|
return cache.get(url);
|
|
304
339
|
}
|
|
305
340
|
try {
|
|
306
|
-
const response = await
|
|
341
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
307
342
|
if (options.debug) {
|
|
308
343
|
console.log(LOG_GROUP, "getOpenApi status : ", response.status);
|
|
309
344
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -50,6 +50,33 @@ var TTLCache = class {
|
|
|
50
50
|
|
|
51
51
|
// src/client.ts
|
|
52
52
|
var LOG_GROUP = `${FG_BLUE}[Localess:Client]${RESET}`;
|
|
53
|
+
async function fetchWithRetry(url, options, retryCount = 3, retryDelay = 500, debug) {
|
|
54
|
+
let attempt = 0;
|
|
55
|
+
let lastError;
|
|
56
|
+
while (attempt <= retryCount) {
|
|
57
|
+
try {
|
|
58
|
+
const response = await fetch(url, options);
|
|
59
|
+
if (!response.ok && response.status >= 500) {
|
|
60
|
+
if (debug) {
|
|
61
|
+
console.log(LOG_GROUP, `fetchWithRetry: HTTP ${response.status} on attempt ${attempt + 1}`);
|
|
62
|
+
}
|
|
63
|
+
lastError = new Error(`HTTP ${response.status}`);
|
|
64
|
+
} else {
|
|
65
|
+
return response;
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (debug) {
|
|
69
|
+
console.log(LOG_GROUP, `fetchWithRetry: network error on attempt ${attempt + 1}`, err);
|
|
70
|
+
}
|
|
71
|
+
lastError = err;
|
|
72
|
+
}
|
|
73
|
+
attempt++;
|
|
74
|
+
if (attempt <= retryCount) {
|
|
75
|
+
await new Promise((res) => setTimeout(res, retryDelay));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
throw lastError;
|
|
79
|
+
}
|
|
53
80
|
function localessClient(options) {
|
|
54
81
|
if (options.debug) {
|
|
55
82
|
console.log(LOG_GROUP, "Client Options : ", options);
|
|
@@ -80,7 +107,7 @@ function localessClient(options) {
|
|
|
80
107
|
return cache.get(url);
|
|
81
108
|
}
|
|
82
109
|
try {
|
|
83
|
-
const response = await
|
|
110
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
84
111
|
if (options.debug) {
|
|
85
112
|
console.log(LOG_GROUP, "getSpace status : ", response.status);
|
|
86
113
|
}
|
|
@@ -119,7 +146,7 @@ function localessClient(options) {
|
|
|
119
146
|
return cache.get(url);
|
|
120
147
|
}
|
|
121
148
|
try {
|
|
122
|
-
const response = await
|
|
149
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
123
150
|
if (options.debug) {
|
|
124
151
|
console.log(LOG_GROUP, "getLinks status : ", response.status);
|
|
125
152
|
}
|
|
@@ -157,7 +184,7 @@ function localessClient(options) {
|
|
|
157
184
|
return cache.get(url);
|
|
158
185
|
}
|
|
159
186
|
try {
|
|
160
|
-
const response = await
|
|
187
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
161
188
|
if (options.debug) {
|
|
162
189
|
console.log(LOG_GROUP, "getContentBySlug status : ", response.status);
|
|
163
190
|
}
|
|
@@ -195,7 +222,7 @@ function localessClient(options) {
|
|
|
195
222
|
return cache.get(url);
|
|
196
223
|
}
|
|
197
224
|
try {
|
|
198
|
-
const response = await
|
|
225
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
199
226
|
if (options.debug) {
|
|
200
227
|
console.log(LOG_GROUP, "getContentById status : ", response.status);
|
|
201
228
|
}
|
|
@@ -207,11 +234,19 @@ function localessClient(options) {
|
|
|
207
234
|
return {};
|
|
208
235
|
}
|
|
209
236
|
},
|
|
210
|
-
async getTranslations(locale) {
|
|
237
|
+
async getTranslations(locale, params) {
|
|
211
238
|
if (options.debug) {
|
|
212
239
|
console.log(LOG_GROUP, "getTranslations() locale : ", locale);
|
|
240
|
+
console.log(LOG_GROUP, "getTranslations() params : ", JSON.stringify(params));
|
|
241
|
+
}
|
|
242
|
+
let version = "";
|
|
243
|
+
if (options?.version && options.version == "draft") {
|
|
244
|
+
version = `&version=${options.version}`;
|
|
245
|
+
}
|
|
246
|
+
if (params?.version && params.version == "draft") {
|
|
247
|
+
version = `&version=${params.version}`;
|
|
213
248
|
}
|
|
214
|
-
let url = `${options.origin}/api/v1/spaces/${options.spaceId}/translations/${locale}?token=${options.token}`;
|
|
249
|
+
let url = `${options.origin}/api/v1/spaces/${options.spaceId}/translations/${locale}?token=${options.token}${version}`;
|
|
215
250
|
if (options.debug) {
|
|
216
251
|
console.log(LOG_GROUP, "getTranslations fetch url : ", url);
|
|
217
252
|
}
|
|
@@ -222,7 +257,7 @@ function localessClient(options) {
|
|
|
222
257
|
return cache.get(url);
|
|
223
258
|
}
|
|
224
259
|
try {
|
|
225
|
-
const response = await
|
|
260
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
226
261
|
if (options.debug) {
|
|
227
262
|
console.log(LOG_GROUP, "getTranslations status : ", response.status);
|
|
228
263
|
}
|
|
@@ -249,14 +284,14 @@ function localessClient(options) {
|
|
|
249
284
|
values
|
|
250
285
|
};
|
|
251
286
|
try {
|
|
252
|
-
const response = await
|
|
287
|
+
const response = await fetchWithRetry(url, {
|
|
253
288
|
method: "POST",
|
|
254
289
|
headers: {
|
|
255
290
|
"X-API-KEY": options.token,
|
|
256
291
|
...fetchOptions.headers
|
|
257
292
|
},
|
|
258
293
|
body: JSON.stringify(body)
|
|
259
|
-
});
|
|
294
|
+
}, options.retryCount, options.retryDelay, options.debug);
|
|
260
295
|
if (options.debug) {
|
|
261
296
|
console.log(LOG_GROUP, "updateTranslations status : ", response.status);
|
|
262
297
|
}
|
|
@@ -280,7 +315,7 @@ function localessClient(options) {
|
|
|
280
315
|
return cache.get(url);
|
|
281
316
|
}
|
|
282
317
|
try {
|
|
283
|
-
const response = await
|
|
318
|
+
const response = await fetchWithRetry(url, fetchOptions, options.retryCount, options.retryDelay, options.debug);
|
|
284
319
|
if (options.debug) {
|
|
285
320
|
console.log(LOG_GROUP, "getOpenApi status : ", response.status);
|
|
286
321
|
}
|