generaltranslation 9.4.2 → 9.5.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/CHANGELOG.md +34 -0
- package/README.md +2 -1
- package/dist/{ApiError-BcjcnTAr.cjs → ApiError-BCjEo_CM.cjs} +1 -1
- package/dist/{ApiError-BcjcnTAr.cjs.map → ApiError-BCjEo_CM.cjs.map} +1 -1
- package/dist/{ApiError-CUKcz3YS.mjs → ApiError-DxWKM0Xt.mjs} +1 -1
- package/dist/{ApiError-CUKcz3YS.mjs.map → ApiError-DxWKM0Xt.mjs.map} +1 -1
- package/dist/adapter/createGtApi.d.ts +196 -27
- package/dist/adapter/createGtApi.js +561 -117
- package/dist/adapter/modelProvider.d.ts +4 -0
- package/dist/adapter/modelProvider.js +6 -0
- package/dist/derive-Cv5_y8Tf.mjs +656 -0
- package/dist/derive-Cv5_y8Tf.mjs.map +1 -0
- package/dist/{derive-DBCCXTmL.d.cts → derive-J2NQLqZm.d.cts} +1 -1
- package/dist/derive-t_E3ftJN.cjs +733 -0
- package/dist/derive-t_E3ftJN.cjs.map +1 -0
- package/dist/diagnostics-CqAHg_Vc.d.cts +34 -0
- package/dist/diagnostics-DKA2SkZB.mjs +45 -0
- package/dist/diagnostics-DKA2SkZB.mjs.map +1 -0
- package/dist/{unwrapApiResult-BzGfPw75.mjs → diagnostics-DKtiPUvo.cjs} +12 -31
- package/dist/diagnostics-DKtiPUvo.cjs.map +1 -0
- package/dist/diagnostics-DYtVjOm5.d.mts +34 -0
- package/dist/diagnostics.cjs +4 -0
- package/dist/diagnostics.d.cts +2 -0
- package/dist/diagnostics.d.mts +2 -0
- package/dist/diagnostics.d.ts +1 -0
- package/dist/diagnostics.js +1 -0
- package/dist/diagnostics.mjs +2 -0
- package/dist/errors.cjs +1 -1
- package/dist/errors.mjs +1 -1
- package/dist/file-B37WkCqM.cjs.map +1 -1
- package/dist/file-Dga6cyM8.mjs.map +1 -1
- package/dist/{id-X2zyx5Jk.mjs → id-B2wA7-S-.mjs} +19 -3
- package/dist/id-B2wA7-S-.mjs.map +1 -0
- package/dist/{id-pdr3R1xs.cjs → id-DJ_59q21.cjs} +25 -3
- package/dist/id-DJ_59q21.cjs.map +1 -0
- package/dist/id.cjs +1 -1
- package/dist/id.d.cts +1 -1
- package/dist/id.d.mts +1 -1
- package/dist/id.mjs +1 -1
- package/dist/index.cjs +94 -753
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -33
- package/dist/index.d.mts +13 -32
- package/dist/index.d.ts +20 -16
- package/dist/index.js +110 -98
- package/dist/index.mjs +88 -746
- package/dist/index.mjs.map +1 -1
- package/dist/internal.cjs +16 -186
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +200 -62
- package/dist/internal.d.mts +199 -61
- package/dist/internal.d.ts +3 -1
- package/dist/internal.js +2 -1
- package/dist/internal.mjs +6 -178
- package/dist/internal.mjs.map +1 -1
- package/dist/logging/errors.d.ts +0 -1
- package/dist/logging/errors.js +0 -6
- package/dist/logging/logger.js +6 -4
- package/dist/openapi.json +2344 -845
- package/dist/runtime.cjs +580 -2
- package/dist/runtime.cjs.map +1 -0
- package/dist/runtime.d.cts +12 -6
- package/dist/runtime.d.mts +12 -6
- package/dist/runtime.d.ts +10 -4
- package/dist/runtime.js +10 -65
- package/dist/runtime.mjs +579 -1
- package/dist/runtime.mjs.map +1 -0
- package/dist/runtimeTranslate-CVw143pQ.mjs +506 -0
- package/dist/runtimeTranslate-CVw143pQ.mjs.map +1 -0
- package/dist/runtimeTranslate-DJioRjb6.cjs +601 -0
- package/dist/runtimeTranslate-DJioRjb6.cjs.map +1 -0
- package/dist/translate/awaitJobs.d.ts +2 -2
- package/dist/translate/awaitJobs.js +1 -156
- package/dist/translate/checkJobStatus.d.ts +2 -1
- package/dist/translate/checkJobStatus.js +1 -55
- package/dist/translate/createBranch.d.ts +2 -6
- package/dist/translate/createBranch.js +1 -53
- package/dist/translate/enqueueFiles.js +1 -95
- package/dist/translate/getProjectInfo.d.ts +2 -6
- package/dist/translate/getProjectInfo.js +1 -54
- package/dist/translate/processFileMoves.d.ts +3 -13
- package/dist/translate/processFileMoves.js +1 -90
- package/dist/translate/publishFiles.d.ts +3 -15
- package/dist/translate/publishFiles.js +1 -56
- package/dist/translate/queryBranchData.js +1 -53
- package/dist/translate/queryFileData.d.ts +3 -41
- package/dist/translate/queryFileData.js +1 -71
- package/dist/translate/runtimeTranslate.d.ts +6 -0
- package/dist/translate/runtimeTranslate.js +297 -0
- package/dist/translate/setupProject.d.ts +2 -6
- package/dist/translate/setupProject.js +1 -63
- package/dist/translate/submitUserEditDiffs.js +1 -68
- package/dist/translate/utils/fetchWithTimeout.js +19 -12
- package/dist/{types-bCr8pV1Z.d.cts → types-Si4UstLL.d.mts} +41 -165
- package/dist/types-dir/__tests__/runtimeTranslate.type-test.js +18 -0
- package/dist/types-dir/api/branch.d.ts +3 -9
- package/dist/types-dir/api/checkFileTranslations.d.ts +2 -22
- package/dist/types-dir/api/downloadFileBatch.d.ts +2 -7
- package/dist/types-dir/api/entry.d.ts +3 -8
- package/dist/types-dir/api/file.d.ts +2 -1
- package/dist/types-dir/api/json.d.ts +1 -4
- package/dist/types-dir/api/project.d.ts +2 -7
- package/dist/types-dir/api/uploadAssets.d.ts +3 -10
- package/dist/types-dir/api/uploadAssets.js +0 -4
- package/dist/types-dir/config.d.ts +1 -0
- package/dist/{types-Dp1drjxq.d.mts → types-yByDbKnV.d.cts} +41 -165
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +26 -7
- package/package.json +15 -2
- package/dist/derive-2CDqwrvS.cjs +0 -303
- package/dist/derive-2CDqwrvS.cjs.map +0 -1
- package/dist/derive-CTzR56T_.mjs +0 -233
- package/dist/derive-CTzR56T_.mjs.map +0 -1
- package/dist/id-X2zyx5Jk.mjs.map +0 -1
- package/dist/id-pdr3R1xs.cjs.map +0 -1
- package/dist/isVariable-8-Wy5FnX.mjs +0 -20
- package/dist/isVariable-8-Wy5FnX.mjs.map +0 -1
- package/dist/isVariable-I3nllgeI.cjs +0 -25
- package/dist/isVariable-I3nllgeI.cjs.map +0 -1
- package/dist/projects/getProjectData.js +0 -87
- package/dist/runtime-Bcp4F2ME.mjs +0 -974
- package/dist/runtime-Bcp4F2ME.mjs.map +0 -1
- package/dist/runtime-C25lwh9m.cjs +0 -1027
- package/dist/runtime-C25lwh9m.cjs.map +0 -1
- package/dist/translate/api.d.ts +0 -4
- package/dist/translate/api.js +0 -4
- package/dist/translate/createTag.d.ts +0 -19
- package/dist/translate/createTag.js +0 -67
- package/dist/translate/downloadFileBatch.d.ts +0 -1
- package/dist/translate/downloadFileBatch.js +0 -80
- package/dist/translate/getOrphanedFiles.d.ts +0 -8
- package/dist/translate/getOrphanedFiles.js +0 -97
- package/dist/translate/querySourceFile.d.ts +0 -1
- package/dist/translate/querySourceFile.js +0 -67
- package/dist/translate/translateMany.d.ts +0 -1
- package/dist/translate/translateMany.js +0 -124
- package/dist/translate/uploadFonts.d.ts +0 -1
- package/dist/translate/uploadFonts.js +0 -74
- package/dist/translate/uploadSourceFiles.d.ts +0 -1
- package/dist/translate/uploadSourceFiles.js +0 -90
- package/dist/translate/uploadTranslations.d.ts +0 -1
- package/dist/translate/uploadTranslations.js +0 -106
- package/dist/translate/utils/apiRequest.d.ts +0 -1
- package/dist/translate/utils/apiRequest.js +0 -167
- package/dist/translate/utils/generateRequestHeaders.d.ts +0 -2
- package/dist/translate/utils/generateRequestHeaders.js +0 -21
- package/dist/translate/utils/handleFetchError.d.ts +0 -1
- package/dist/translate/utils/handleFetchError.js +0 -12
- package/dist/unwrapApiResult-BzGfPw75.mjs.map +0 -1
- package/dist/unwrapApiResult-Dv6Mfw_K.cjs +0 -134
- package/dist/unwrapApiResult-Dv6Mfw_K.cjs.map +0 -1
- /package/dist/{projects/getProjectData.d.ts → types-dir/__tests__/runtimeTranslate.type-test.d.ts} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -1,688 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const require_file = require("./file-B37WkCqM.cjs");
|
|
2
|
+
const require_runtimeTranslate = require("./runtimeTranslate-DJioRjb6.cjs");
|
|
3
|
+
const require_derive = require("./derive-t_E3ftJN.cjs");
|
|
4
|
+
const require_runtime = require("./runtime.cjs");
|
|
6
5
|
let _generaltranslation_format = require("@generaltranslation/format");
|
|
7
|
-
let _generaltranslation_api = require("@generaltranslation/api");
|
|
8
|
-
//#region src/translate/utils/handleFetchError.ts
|
|
9
|
-
function handleFetchError(error, timeout) {
|
|
10
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
11
|
-
const errorMessage = require_runtime.translationTimeoutError(timeout);
|
|
12
|
-
require_runtime.fetchLogger.error(errorMessage);
|
|
13
|
-
throw new Error(errorMessage);
|
|
14
|
-
}
|
|
15
|
-
const errorMessage = require_runtime.translationRequestFailedError(error instanceof Error ? error.message : String(error));
|
|
16
|
-
require_runtime.fetchLogger.error(errorMessage);
|
|
17
|
-
throw error;
|
|
18
|
-
}
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/translate/utils/generateRequestHeaders.ts
|
|
21
|
-
function generateRequestHeaders(config, excludeContentType = false) {
|
|
22
|
-
const authHeaders = {
|
|
23
|
-
...!excludeContentType && { "Content-Type": "application/json" },
|
|
24
|
-
"gt-project-id": config.projectId
|
|
25
|
-
};
|
|
26
|
-
if (config.apiKey) authHeaders["Authorization"] = `Bearer ${config.apiKey}`;
|
|
27
|
-
authHeaders["gt-api-version"] = _generaltranslation_api.API_VERSION;
|
|
28
|
-
return authHeaders;
|
|
29
|
-
}
|
|
30
|
-
//#endregion
|
|
31
|
-
//#region src/translate/utils/apiRequest.ts
|
|
32
|
-
const MAX_RETRIES = 3;
|
|
33
|
-
const INITIAL_DELAY_MS = 500;
|
|
34
|
-
const RATE_LIMIT_RETRY_DELAY_MS = 6e4;
|
|
35
|
-
const MS_PER_SECOND = 1e3;
|
|
36
|
-
function sleep(ms) {
|
|
37
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
38
|
-
}
|
|
39
|
-
function getRetryDelay(policy, attempt) {
|
|
40
|
-
switch (policy) {
|
|
41
|
-
case "linear": return INITIAL_DELAY_MS * (attempt + 1);
|
|
42
|
-
case "exponential": return INITIAL_DELAY_MS * 2 ** attempt;
|
|
43
|
-
default: return 0;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function parseDelayMs(value) {
|
|
47
|
-
if (!value) return;
|
|
48
|
-
const seconds = Number(value.split(",")[0].split(";")[0].trim());
|
|
49
|
-
if (!Number.isFinite(seconds) || seconds < 0) return;
|
|
50
|
-
return seconds * MS_PER_SECOND;
|
|
51
|
-
}
|
|
52
|
-
function parseRetryAfter(value) {
|
|
53
|
-
const delayMs = parseDelayMs(value);
|
|
54
|
-
if (delayMs !== void 0) return delayMs;
|
|
55
|
-
if (!value) return;
|
|
56
|
-
const retryDate = Date.parse(value);
|
|
57
|
-
if (Number.isNaN(retryDate)) return;
|
|
58
|
-
return Math.max(retryDate - Date.now(), 0);
|
|
59
|
-
}
|
|
60
|
-
function getRateLimitRetryDelay(response) {
|
|
61
|
-
return parseRetryAfter(response.headers.get("Retry-After")) ?? parseDelayMs(response.headers.get("RateLimit-Reset")) ?? RATE_LIMIT_RETRY_DELAY_MS;
|
|
62
|
-
}
|
|
63
|
-
function getResponseRetryDelay(response, policy, attempt) {
|
|
64
|
-
if (response.status === 429) return getRateLimitRetryDelay(response);
|
|
65
|
-
return getRetryDelay(policy, attempt);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* @internal
|
|
69
|
-
*
|
|
70
|
-
* Makes an API request with automatic retry for 429 and 5XX errors.
|
|
71
|
-
*
|
|
72
|
-
* Encapsulates URL construction, fetch with timeout, error handling,
|
|
73
|
-
* response validation, and JSON parsing.
|
|
74
|
-
*
|
|
75
|
-
* @param config - The configuration for the API call.
|
|
76
|
-
* @param endpoint - The API endpoint path (e.g. '/v2/project/jobs/info')
|
|
77
|
-
* @param options - Optional request options.
|
|
78
|
-
* @returns The parsed JSON response.
|
|
79
|
-
*/
|
|
80
|
-
async function apiRequest(config, endpoint, options) {
|
|
81
|
-
const timeout = options?.timeout ?? 6e4;
|
|
82
|
-
const retryPolicy = options?.retryPolicy ?? "exponential";
|
|
83
|
-
const url = `${config.baseUrl || "https://api.gtx.dev"}${endpoint}`;
|
|
84
|
-
const method = options?.method ?? "POST";
|
|
85
|
-
const maxRetries = retryPolicy === "none" ? 0 : MAX_RETRIES;
|
|
86
|
-
const requestInit = {
|
|
87
|
-
method,
|
|
88
|
-
headers: generateRequestHeaders(config)
|
|
89
|
-
};
|
|
90
|
-
if (options?.body !== void 0) requestInit.body = JSON.stringify(options.body);
|
|
91
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
92
|
-
let response;
|
|
93
|
-
try {
|
|
94
|
-
response = await require_runtime.fetchWithTimeout(url, requestInit, timeout);
|
|
95
|
-
} catch (error) {
|
|
96
|
-
if (attempt < maxRetries) {
|
|
97
|
-
await sleep(getRetryDelay(retryPolicy, attempt));
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
handleFetchError(error, timeout);
|
|
101
|
-
}
|
|
102
|
-
if ((response.status === 429 || response.status >= 500) && attempt < maxRetries) {
|
|
103
|
-
await sleep(getResponseRetryDelay(response, retryPolicy, attempt));
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
await require_runtime.validateResponse(response);
|
|
107
|
-
return await response.json();
|
|
108
|
-
}
|
|
109
|
-
throw new Error("Max retries exceeded");
|
|
110
|
-
}
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region src/translate/setupProject.ts
|
|
113
|
-
/**
|
|
114
|
-
* @internal
|
|
115
|
-
* Enqueues files for project setup the General Translation API.
|
|
116
|
-
* @param files - References of files to translate (file content already uploaded)
|
|
117
|
-
* @param config - The configuration for the API call.
|
|
118
|
-
* @param timeoutMS - The timeout in milliseconds
|
|
119
|
-
* @returns The result of the API call.
|
|
120
|
-
*/
|
|
121
|
-
async function _setupProject(files, config, options) {
|
|
122
|
-
return apiRequest(config, "/v2/project/setup/generate", {
|
|
123
|
-
body: {
|
|
124
|
-
files: files.map((f) => ({
|
|
125
|
-
branchId: f.branchId,
|
|
126
|
-
fileId: f.fileId,
|
|
127
|
-
versionId: f.versionId
|
|
128
|
-
})),
|
|
129
|
-
locales: options?.locales,
|
|
130
|
-
force: options?.force
|
|
131
|
-
},
|
|
132
|
-
timeout: options?.timeoutMs
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
//#endregion
|
|
136
|
-
//#region src/translate/utils/batch.ts
|
|
137
|
-
/**
|
|
138
|
-
* Splits an array into batches of a specified size.
|
|
139
|
-
* @param items - The array to split into batches.
|
|
140
|
-
* @param batchSize - The maximum size of each batch.
|
|
141
|
-
* @returns An array of batches.
|
|
142
|
-
*/
|
|
143
|
-
function createBatches(items, batchSize) {
|
|
144
|
-
const batches = [];
|
|
145
|
-
for (let i = 0; i < items.length; i += batchSize) batches.push(items.slice(i, i + batchSize));
|
|
146
|
-
return batches;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Processes items in batches using a provided processor function.
|
|
150
|
-
*
|
|
151
|
-
* @param items - The items to process.
|
|
152
|
-
* @param processor - Async function that processes a single batch and returns items.
|
|
153
|
-
* @param options - Optional configuration for batch processing.
|
|
154
|
-
* @returns Promise that resolves to a BatchList containing all processed items.
|
|
155
|
-
*
|
|
156
|
-
* @example
|
|
157
|
-
* ```typescript
|
|
158
|
-
* const result = await processBatches(
|
|
159
|
-
* files,
|
|
160
|
-
* async (batch) => {
|
|
161
|
-
* const response = await uploadFiles(batch);
|
|
162
|
-
* return response.uploadedFiles;
|
|
163
|
-
* },
|
|
164
|
-
* { batchSize: 100 }
|
|
165
|
-
* );
|
|
166
|
-
*
|
|
167
|
-
* console.log(result.data); // All items
|
|
168
|
-
* console.log(result.count); // Total count
|
|
169
|
-
* console.log(result.batchCount); // Number of batches processed
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
172
|
-
async function processBatches(items, processor, options = {}) {
|
|
173
|
-
const { batchSize = 100, parallel = true } = options;
|
|
174
|
-
if (items.length === 0) return {
|
|
175
|
-
data: [],
|
|
176
|
-
count: 0,
|
|
177
|
-
batchCount: 0
|
|
178
|
-
};
|
|
179
|
-
const batches = createBatches(items, batchSize);
|
|
180
|
-
const allItems = [];
|
|
181
|
-
if (parallel) {
|
|
182
|
-
const results = await Promise.all(batches.map((batch) => processor(batch)));
|
|
183
|
-
for (const result of results) if (result) allItems.push(...result);
|
|
184
|
-
} else for (const batch of batches) {
|
|
185
|
-
const result = await processor(batch);
|
|
186
|
-
if (result) allItems.push(...result);
|
|
187
|
-
}
|
|
188
|
-
return {
|
|
189
|
-
data: allItems,
|
|
190
|
-
count: allItems.length,
|
|
191
|
-
batchCount: batches.length
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
//#endregion
|
|
195
|
-
//#region src/translate/utils/validateFileFormatTransform.ts
|
|
196
|
-
/**
|
|
197
|
-
* Returns a user-facing validation error when a requested file format transform
|
|
198
|
-
* is missing source format context or is not currently supported.
|
|
199
|
-
*/
|
|
200
|
-
function getFileFormatTransformError(file) {
|
|
201
|
-
if (!file.transformFormat) return void 0;
|
|
202
|
-
const fileLabel = file.fileName ?? file.fileId ?? "unknown file";
|
|
203
|
-
if (!file.fileFormat) return `fileFormat is required when transformFormat is provided for ${fileLabel}`;
|
|
204
|
-
if (!require_derive.isSupportedFileFormatTransform(file.fileFormat, file.transformFormat)) return `Unsupported file format transform: ${file.fileFormat} -> ${file.transformFormat}`;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Validates file format transforms before sending upload/enqueue requests.
|
|
208
|
-
*/
|
|
209
|
-
function validateFileFormatTransforms(files) {
|
|
210
|
-
for (const file of files) {
|
|
211
|
-
const error = getFileFormatTransformError(file);
|
|
212
|
-
if (error) throw new Error(error);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
//#endregion
|
|
216
|
-
//#region src/translate/enqueueFiles.ts
|
|
217
|
-
/**
|
|
218
|
-
* @internal
|
|
219
|
-
* Enqueues files for translation in the General Translation API.
|
|
220
|
-
* @param files - References of files to translate (file content already uploaded)
|
|
221
|
-
* @param options - The options for the API call.
|
|
222
|
-
* @param config - The configuration for the API call.
|
|
223
|
-
* @returns The result of the API call.
|
|
224
|
-
*/
|
|
225
|
-
async function _enqueueFiles(files, options, config) {
|
|
226
|
-
validateFileFormatTransforms(files);
|
|
227
|
-
const result = await processBatches(files, async (batch) => {
|
|
228
|
-
const apiResult = await apiRequest(config, "/v2/project/translations/enqueue", {
|
|
229
|
-
body: {
|
|
230
|
-
files: batch.map((f) => ({
|
|
231
|
-
branchId: f.branchId,
|
|
232
|
-
fileId: f.fileId,
|
|
233
|
-
versionId: f.versionId,
|
|
234
|
-
fileName: f.fileName,
|
|
235
|
-
transformFormat: f.transformFormat
|
|
236
|
-
})),
|
|
237
|
-
targetLocales: options.targetLocales,
|
|
238
|
-
sourceLocale: options.sourceLocale,
|
|
239
|
-
modelProvider: options.modelProvider,
|
|
240
|
-
force: options.force
|
|
241
|
-
},
|
|
242
|
-
timeout: options.timeout
|
|
243
|
-
});
|
|
244
|
-
return Array.from(Object.entries(apiResult.jobData));
|
|
245
|
-
}, { batchSize: 100 });
|
|
246
|
-
return {
|
|
247
|
-
jobData: Object.fromEntries(result.data.map(([jobId, jobData]) => [jobId, jobData])),
|
|
248
|
-
locales: options.targetLocales,
|
|
249
|
-
message: `Successfully enqueued ${result.count} file translation jobs in ${result.batchCount} batch(es)`
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
//#endregion
|
|
253
|
-
//#region src/translate/createTag.ts
|
|
254
|
-
/**
|
|
255
|
-
* @internal
|
|
256
|
-
* Creates or upserts a file tag in the General Translation API.
|
|
257
|
-
* @param options - The tag creation options.
|
|
258
|
-
* @param config - The configuration for the API call.
|
|
259
|
-
* @returns The created or updated tag.
|
|
260
|
-
*/
|
|
261
|
-
async function _createTag(options, config) {
|
|
262
|
-
return await apiRequest(config, "/v2/project/tags/create", { body: {
|
|
263
|
-
tagId: options.tagId,
|
|
264
|
-
files: options.files,
|
|
265
|
-
...options.message && { message: options.message }
|
|
266
|
-
} });
|
|
267
|
-
}
|
|
268
|
-
//#endregion
|
|
269
|
-
//#region src/translate/downloadFileBatch.ts
|
|
270
|
-
/**
|
|
271
|
-
* @internal
|
|
272
|
-
* Downloads multiple translation files in batches.
|
|
273
|
-
* @param files - Array of files to download
|
|
274
|
-
* @param options - The options for the API call.
|
|
275
|
-
* @param config - The configuration for the request.
|
|
276
|
-
* @returns Promise resolving to a BatchList with all downloaded files
|
|
277
|
-
*/
|
|
278
|
-
async function _downloadFileBatch(requests, options, config) {
|
|
279
|
-
return processBatches(requests, async (batch) => {
|
|
280
|
-
return (await apiRequest(config, "/v2/project/files/download", {
|
|
281
|
-
body: batch,
|
|
282
|
-
timeout: options.timeout
|
|
283
|
-
})).files.map((file) => ({
|
|
284
|
-
...file,
|
|
285
|
-
data: require_file.isBinaryFileFormat(file.fileFormat) ? file.data : require_derive.decode(file.data)
|
|
286
|
-
}));
|
|
287
|
-
}, { batchSize: 100 });
|
|
288
|
-
}
|
|
289
|
-
//#endregion
|
|
290
|
-
//#region src/translate/submitUserEditDiffs.ts
|
|
291
|
-
/**
|
|
292
|
-
* @internal
|
|
293
|
-
* Submits user edit diffs so the service can learn/persist user-intended rules.
|
|
294
|
-
*/
|
|
295
|
-
async function _submitUserEditDiffs(payload, config, options = {}) {
|
|
296
|
-
await processBatches(payload.diffs, async (batch) => {
|
|
297
|
-
await apiRequest(config, "/v2/project/files/diffs", {
|
|
298
|
-
body: { diffs: batch },
|
|
299
|
-
timeout: options.timeout
|
|
300
|
-
});
|
|
301
|
-
return [{ success: true }];
|
|
302
|
-
}, { batchSize: 100 });
|
|
303
|
-
return { success: true };
|
|
304
|
-
}
|
|
305
|
-
//#endregion
|
|
306
|
-
//#region src/translate/uploadSourceFiles.ts
|
|
307
|
-
/**
|
|
308
|
-
* @internal
|
|
309
|
-
* Uploads source files to the General Translation API in batches.
|
|
310
|
-
* @param files - The files to upload.
|
|
311
|
-
* @param options - The options for the API call.
|
|
312
|
-
* @param config - The configuration for the API call.
|
|
313
|
-
* @returns Promise resolving to a BatchList with all uploaded files
|
|
314
|
-
*/
|
|
315
|
-
async function _uploadSourceFiles(files, options, config) {
|
|
316
|
-
return processBatches(files, async (batch) => {
|
|
317
|
-
return (await apiRequest(config, "/v2/project/files/upload-files", {
|
|
318
|
-
body: {
|
|
319
|
-
data: batch.map(({ source }) => ({ source: {
|
|
320
|
-
content: require_file.isBinaryFileFormat(source.fileFormat) ? source.content : require_derive.encode(source.content),
|
|
321
|
-
fileName: source.fileName,
|
|
322
|
-
fileFormat: source.fileFormat,
|
|
323
|
-
locale: source.locale,
|
|
324
|
-
dataFormat: source.dataFormat,
|
|
325
|
-
formatMetadata: source.formatMetadata,
|
|
326
|
-
fileId: source.fileId,
|
|
327
|
-
versionId: source.versionId,
|
|
328
|
-
branchId: source.branchId,
|
|
329
|
-
incomingBranchId: source.incomingBranchId,
|
|
330
|
-
checkedOutBranchId: source.checkedOutBranchId
|
|
331
|
-
} })),
|
|
332
|
-
sourceLocale: options.sourceLocale
|
|
333
|
-
},
|
|
334
|
-
timeout: options.timeout
|
|
335
|
-
})).uploadedFiles || [];
|
|
336
|
-
}, { batchSize: 100 });
|
|
337
|
-
}
|
|
338
|
-
//#endregion
|
|
339
|
-
//#region src/translate/uploadFonts.ts
|
|
340
|
-
const FONT_UPLOAD_BATCH_SIZE = 50;
|
|
341
|
-
/**
|
|
342
|
-
* @internal
|
|
343
|
-
* Uploads project fonts to the General Translation API in batches. Idempotent
|
|
344
|
-
* on the server (re-running only stores genuinely new fonts). `content` is
|
|
345
|
-
* already base64-encoded by the caller (fonts are binary).
|
|
346
|
-
*/
|
|
347
|
-
async function _uploadFonts(fonts, options, config) {
|
|
348
|
-
return processBatches(fonts, async (batch) => {
|
|
349
|
-
return (await apiRequest(config, "/v2/project/assets", {
|
|
350
|
-
body: { assets: batch.map((font) => ({
|
|
351
|
-
assetType: font.assetType,
|
|
352
|
-
content: font.content,
|
|
353
|
-
fileName: font.fileName
|
|
354
|
-
})) },
|
|
355
|
-
timeout: options.timeout
|
|
356
|
-
})).assets || [];
|
|
357
|
-
}, { batchSize: FONT_UPLOAD_BATCH_SIZE });
|
|
358
|
-
}
|
|
359
|
-
//#endregion
|
|
360
|
-
//#region src/translate/uploadTranslations.ts
|
|
361
|
-
/**
|
|
362
|
-
* @internal
|
|
363
|
-
* Uploads multiple translations to the General Translation API in batches.
|
|
364
|
-
* @param files - Translations to upload with their source
|
|
365
|
-
* @param options - The options for the API call.
|
|
366
|
-
* @param config - The configuration for the API call.
|
|
367
|
-
* @returns Promise resolving to a BatchList with all uploaded files
|
|
368
|
-
*/
|
|
369
|
-
async function _uploadTranslations(files, options, config) {
|
|
370
|
-
validateFileFormatTransforms(files.map(({ source }) => source));
|
|
371
|
-
return processBatches(files, async (batch) => {
|
|
372
|
-
return (await apiRequest(config, "/v2/project/files/upload-translations", {
|
|
373
|
-
body: {
|
|
374
|
-
data: batch.map(({ source, translations }) => ({
|
|
375
|
-
source: {
|
|
376
|
-
content: require_file.isBinaryFileFormat(source.fileFormat) ? source.content : require_derive.encode(source.content),
|
|
377
|
-
fileName: source.fileName,
|
|
378
|
-
fileFormat: source.fileFormat,
|
|
379
|
-
transformFormat: source.transformFormat,
|
|
380
|
-
locale: source.locale,
|
|
381
|
-
dataFormat: source.dataFormat,
|
|
382
|
-
formatMetadata: source.formatMetadata,
|
|
383
|
-
fileId: source.fileId,
|
|
384
|
-
versionId: source.versionId,
|
|
385
|
-
branchId: source.branchId
|
|
386
|
-
},
|
|
387
|
-
translations: translations.map((t) => ({
|
|
388
|
-
content: require_file.isBinaryFileFormat(t.fileFormat) ? t.content : require_derive.encode(t.content),
|
|
389
|
-
fileName: t.fileName,
|
|
390
|
-
fileFormat: t.fileFormat,
|
|
391
|
-
locale: t.locale,
|
|
392
|
-
dataFormat: t.dataFormat,
|
|
393
|
-
fileId: t.fileId,
|
|
394
|
-
versionId: t.versionId,
|
|
395
|
-
branchId: t.branchId
|
|
396
|
-
}))
|
|
397
|
-
})),
|
|
398
|
-
sourceLocale: options.sourceLocale
|
|
399
|
-
},
|
|
400
|
-
timeout: options.timeout
|
|
401
|
-
})).uploadedFiles || [];
|
|
402
|
-
}, { batchSize: 100 });
|
|
403
|
-
}
|
|
404
|
-
//#endregion
|
|
405
|
-
//#region src/translate/querySourceFile.ts
|
|
406
|
-
/**
|
|
407
|
-
* @internal
|
|
408
|
-
* Gets the source file and translation information for a given file ID and version ID.
|
|
409
|
-
* @param query - The file ID and version ID to get the source file and translation information for
|
|
410
|
-
* @param options - The options for the API call.
|
|
411
|
-
* @param config - The configuration for the request.
|
|
412
|
-
* @returns The source file and translation information for the given file ID and version ID
|
|
413
|
-
*/
|
|
414
|
-
async function _querySourceFile(query, options, config) {
|
|
415
|
-
const branchId = query.branchId;
|
|
416
|
-
const versionId = query.versionId;
|
|
417
|
-
const fileId = query.fileId;
|
|
418
|
-
const searchParams = new URLSearchParams();
|
|
419
|
-
if (branchId) searchParams.set("branchId", branchId);
|
|
420
|
-
if (versionId) searchParams.set("versionId", versionId);
|
|
421
|
-
return apiRequest(config, `/v2/project/translations/files/status/${encodeURIComponent(fileId)}?${searchParams.toString()}`, {
|
|
422
|
-
method: "GET",
|
|
423
|
-
timeout: options.timeout
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
//#endregion
|
|
427
|
-
//#region src/projects/getProjectData.ts
|
|
428
|
-
/**
|
|
429
|
-
* @internal
|
|
430
|
-
* Gets the project data for a given project ID.
|
|
431
|
-
* @param projectId - The project ID to get the project data for
|
|
432
|
-
* @param options - The options for the API call.
|
|
433
|
-
* @param config - The configuration for the request.
|
|
434
|
-
* @returns The project data for the given project ID.
|
|
435
|
-
*/
|
|
436
|
-
async function _getProjectData(projectId, options, config) {
|
|
437
|
-
const { baseUrl } = config;
|
|
438
|
-
const timeout = options.timeout ? options.timeout : require_unwrapApiResult.defaultTimeout;
|
|
439
|
-
const url = `${baseUrl || "https://api.gtx.dev"}/v2/project/info/${encodeURIComponent(projectId)}`;
|
|
440
|
-
let response;
|
|
441
|
-
try {
|
|
442
|
-
response = await require_runtime.fetchWithTimeout(url, {
|
|
443
|
-
method: "GET",
|
|
444
|
-
headers: generateRequestHeaders(config)
|
|
445
|
-
}, timeout);
|
|
446
|
-
} catch (error) {
|
|
447
|
-
handleFetchError(error, timeout);
|
|
448
|
-
}
|
|
449
|
-
await require_runtime.validateResponse(response);
|
|
450
|
-
return await response.json();
|
|
451
|
-
}
|
|
452
|
-
//#endregion
|
|
453
|
-
//#region src/translate/checkJobStatus.ts
|
|
454
|
-
/**
|
|
455
|
-
* @internal
|
|
456
|
-
* Queries job statuses for a project.
|
|
457
|
-
* @param jobIds - Job IDs.
|
|
458
|
-
* @param config - The configuration for the API call.
|
|
459
|
-
* @param timeoutMs - The timeout in milliseconds.
|
|
460
|
-
* @returns The result of the API call.
|
|
461
|
-
*/
|
|
462
|
-
async function _checkJobStatus(jobIds, config, timeoutMs) {
|
|
463
|
-
return apiRequest(config, "/v2/project/jobs/info", {
|
|
464
|
-
body: { jobIds },
|
|
465
|
-
timeout: timeoutMs
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
//#endregion
|
|
469
|
-
//#region src/translate/awaitJobs.ts
|
|
470
|
-
/**
|
|
471
|
-
* @internal
|
|
472
|
-
* Polls job statuses until all jobs are finished or the timeout is reached.
|
|
473
|
-
* @param jobIds - Job IDs to poll.
|
|
474
|
-
* @param options - Polling configuration.
|
|
475
|
-
* @param config - API credentials and configuration.
|
|
476
|
-
* @returns The final status of all jobs.
|
|
477
|
-
*/
|
|
478
|
-
async function _awaitJobIds(jobIds, options, config) {
|
|
479
|
-
const pollingInterval = (options?.pollingIntervalSeconds ?? 5) * 1e3;
|
|
480
|
-
const timeout = options?.timeoutSeconds !== void 0 ? options.timeoutSeconds * 1e3 : 600 * 1e3;
|
|
481
|
-
if (jobIds.length === 0) return {
|
|
482
|
-
complete: true,
|
|
483
|
-
jobs: []
|
|
484
|
-
};
|
|
485
|
-
const deadline = Date.now() + timeout;
|
|
486
|
-
const finalStatuses = new Map(jobIds.map((id) => [id, {
|
|
487
|
-
jobId: id,
|
|
488
|
-
status: "unknown"
|
|
489
|
-
}]));
|
|
490
|
-
const pendingJobIds = new Set(jobIds);
|
|
491
|
-
while (pendingJobIds.size > 0) {
|
|
492
|
-
const remainingTime = deadline - Date.now();
|
|
493
|
-
if (remainingTime <= 0) break;
|
|
494
|
-
let statuses;
|
|
495
|
-
try {
|
|
496
|
-
statuses = await _checkJobStatus(Array.from(pendingJobIds), config, Math.min(remainingTime, require_unwrapApiResult.defaultTimeout));
|
|
497
|
-
} catch (error) {
|
|
498
|
-
if (Date.now() >= deadline) break;
|
|
499
|
-
throw error;
|
|
500
|
-
}
|
|
501
|
-
if (Date.now() >= deadline) break;
|
|
502
|
-
const returnedJobIds = new Set(statuses.map(({ jobId }) => jobId));
|
|
503
|
-
for (const job of statuses) if (job.status === "completed" || job.status === "failed" || job.status === "unknown") {
|
|
504
|
-
finalStatuses.set(job.jobId, {
|
|
505
|
-
jobId: job.jobId,
|
|
506
|
-
status: job.status,
|
|
507
|
-
...job.error ? { error: job.error } : {}
|
|
508
|
-
});
|
|
509
|
-
pendingJobIds.delete(job.jobId);
|
|
510
|
-
} else finalStatuses.set(job.jobId, {
|
|
511
|
-
jobId: job.jobId,
|
|
512
|
-
status: job.status
|
|
513
|
-
});
|
|
514
|
-
for (const jobId of Array.from(pendingJobIds)) if (!returnedJobIds.has(jobId)) {
|
|
515
|
-
finalStatuses.set(jobId, {
|
|
516
|
-
jobId,
|
|
517
|
-
status: "unknown"
|
|
518
|
-
});
|
|
519
|
-
pendingJobIds.delete(jobId);
|
|
520
|
-
}
|
|
521
|
-
if (pendingJobIds.size === 0) break;
|
|
522
|
-
const remainingSleepTime = deadline - Date.now();
|
|
523
|
-
if (remainingSleepTime <= 0) break;
|
|
524
|
-
await new Promise((resolve) => setTimeout(resolve, Math.min(pollingInterval, remainingSleepTime)));
|
|
525
|
-
}
|
|
526
|
-
return {
|
|
527
|
-
complete: pendingJobIds.size === 0,
|
|
528
|
-
jobs: Array.from(finalStatuses.values())
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
//#endregion
|
|
532
|
-
//#region src/translate/queryFileData.ts
|
|
533
|
-
/**
|
|
534
|
-
* @internal
|
|
535
|
-
* Queries data about one or more source or translation files.
|
|
536
|
-
* @param data - Object mapping source or translation file information
|
|
537
|
-
* @param options - The options for the API call.
|
|
538
|
-
* @param config - The configuration for the API call.
|
|
539
|
-
* @returns The file data.
|
|
540
|
-
*/
|
|
541
|
-
async function _queryFileData(data, options = {}, config) {
|
|
542
|
-
return apiRequest(config, "/v2/project/files/info", {
|
|
543
|
-
body: {
|
|
544
|
-
sourceFiles: data.sourceFiles?.map((item) => ({
|
|
545
|
-
fileId: item.fileId,
|
|
546
|
-
versionId: item.versionId,
|
|
547
|
-
branchId: item.branchId
|
|
548
|
-
})),
|
|
549
|
-
translatedFiles: data.translatedFiles?.map((item) => ({
|
|
550
|
-
fileId: item.fileId,
|
|
551
|
-
versionId: item.versionId,
|
|
552
|
-
branchId: item.branchId,
|
|
553
|
-
locale: item.locale
|
|
554
|
-
}))
|
|
555
|
-
},
|
|
556
|
-
timeout: options.timeout
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
//#endregion
|
|
560
|
-
//#region src/translate/getProjectInfo.ts
|
|
561
|
-
/**
|
|
562
|
-
* @internal
|
|
563
|
-
* Fetches project info (name, locales, review settings) for a project.
|
|
564
|
-
* @param options - The options for the API call.
|
|
565
|
-
* @param config - The configuration for the API call.
|
|
566
|
-
* @returns The project info.
|
|
567
|
-
*/
|
|
568
|
-
async function _getProjectInfo(options, config) {
|
|
569
|
-
return apiRequest(config, `/v2/project/info/${config.projectId}`, {
|
|
570
|
-
method: "GET",
|
|
571
|
-
timeout: options.timeout
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
//#endregion
|
|
575
|
-
//#region src/translate/queryBranchData.ts
|
|
576
|
-
/**
|
|
577
|
-
* @internal
|
|
578
|
-
* Queries branch information from the API.
|
|
579
|
-
* @param query - Object mapping the current branch and incoming branches
|
|
580
|
-
* @param config - The configuration for the API call.
|
|
581
|
-
* @returns The branch information.
|
|
582
|
-
*/
|
|
583
|
-
async function _queryBranchData(query, config) {
|
|
584
|
-
return apiRequest(config, "/v2/project/branches/info", { body: query });
|
|
585
|
-
}
|
|
586
|
-
//#endregion
|
|
587
|
-
//#region src/translate/createBranch.ts
|
|
588
|
-
/**
|
|
589
|
-
* @internal
|
|
590
|
-
* Creates a new branch in the API.
|
|
591
|
-
* @param query - Object mapping the branch name and default branch flag
|
|
592
|
-
* @param config - The configuration for the API call.
|
|
593
|
-
* @returns The created branch information.
|
|
594
|
-
*/
|
|
595
|
-
async function _createBranch(query, config) {
|
|
596
|
-
return apiRequest(config, "/v2/project/branches/create", { body: query });
|
|
597
|
-
}
|
|
598
|
-
//#endregion
|
|
599
|
-
//#region src/translate/processFileMoves.ts
|
|
600
|
-
/**
|
|
601
|
-
* @internal
|
|
602
|
-
* Processes file moves by cloning source files and translations with new fileIds.
|
|
603
|
-
* Called when the CLI detects that files have been moved/renamed.
|
|
604
|
-
* @param moves - Array of move mappings (old fileId to new fileId)
|
|
605
|
-
* @param options - Options including branchId and timeout
|
|
606
|
-
* @param config - The configuration for the API call.
|
|
607
|
-
* @returns Promise resolving to the move results
|
|
608
|
-
*/
|
|
609
|
-
async function _processFileMoves(moves, options, config) {
|
|
610
|
-
if (moves.length === 0) return {
|
|
611
|
-
results: [],
|
|
612
|
-
summary: {
|
|
613
|
-
total: 0,
|
|
614
|
-
succeeded: 0,
|
|
615
|
-
failed: 0
|
|
616
|
-
}
|
|
617
|
-
};
|
|
618
|
-
const batchResult = await processBatches(moves, async (batch) => {
|
|
619
|
-
return (await apiRequest(config, "/v2/project/files/moves", {
|
|
620
|
-
body: {
|
|
621
|
-
branchId: options.branchId,
|
|
622
|
-
moves: batch
|
|
623
|
-
},
|
|
624
|
-
timeout: options.timeout
|
|
625
|
-
})).results;
|
|
626
|
-
}, { batchSize: 100 });
|
|
627
|
-
const succeeded = batchResult.data.filter((r) => r.success).length;
|
|
628
|
-
const failed = batchResult.data.filter((r) => !r.success).length;
|
|
629
|
-
return {
|
|
630
|
-
results: batchResult.data,
|
|
631
|
-
summary: {
|
|
632
|
-
total: moves.length,
|
|
633
|
-
succeeded,
|
|
634
|
-
failed
|
|
635
|
-
}
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
//#endregion
|
|
639
|
-
//#region src/translate/getOrphanedFiles.ts
|
|
640
|
-
/**
|
|
641
|
-
* @internal
|
|
642
|
-
* Gets orphaned files for a branch - files that exist on the branch
|
|
643
|
-
* but whose fileIds are not in the provided list.
|
|
644
|
-
* Used for move detection.
|
|
645
|
-
* @param branchId - The branch to check for orphaned files
|
|
646
|
-
* @param fileIds - List of current file IDs (files that are NOT orphaned)
|
|
647
|
-
* @param options - The options for the API call
|
|
648
|
-
* @param config - The configuration for the API call
|
|
649
|
-
* @returns The orphaned files
|
|
650
|
-
*/
|
|
651
|
-
async function _getOrphanedFiles(branchId, fileIds, options = {}, config) {
|
|
652
|
-
const makeRequest = (batchFileIds) => apiRequest(config, "/v2/project/files/orphaned", {
|
|
653
|
-
body: {
|
|
654
|
-
branchId,
|
|
655
|
-
fileIds: batchFileIds
|
|
656
|
-
},
|
|
657
|
-
timeout: options.timeout
|
|
658
|
-
});
|
|
659
|
-
if (fileIds.length === 0) return makeRequest([]);
|
|
660
|
-
const batches = createBatches(fileIds, 100);
|
|
661
|
-
const batchResults = await Promise.all(batches.map((batch) => makeRequest(batch)));
|
|
662
|
-
if (batchResults.length === 1) return batchResults[0];
|
|
663
|
-
const orphanedFileMap = /* @__PURE__ */ new Map();
|
|
664
|
-
for (const orphan of batchResults[0].orphanedFiles) orphanedFileMap.set(orphan.fileId, orphan);
|
|
665
|
-
for (let i = 1; i < batchResults.length; i++) {
|
|
666
|
-
const batchOrphanIds = new Set(batchResults[i].orphanedFiles.map((f) => f.fileId));
|
|
667
|
-
Array.from(orphanedFileMap.keys()).forEach((fileId) => {
|
|
668
|
-
if (!batchOrphanIds.has(fileId)) orphanedFileMap.delete(fileId);
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
return { orphanedFiles: Array.from(orphanedFileMap.values()) };
|
|
672
|
-
}
|
|
673
|
-
//#endregion
|
|
674
|
-
//#region src/translate/publishFiles.ts
|
|
675
|
-
/**
|
|
676
|
-
* @internal
|
|
677
|
-
* Publishes or unpublishes files on the CDN.
|
|
678
|
-
* @param files - Array of file entries with publish flags
|
|
679
|
-
* @param config - The configuration for the API call.
|
|
680
|
-
* @returns The result of the API call.
|
|
681
|
-
*/
|
|
682
|
-
async function _publishFiles(files, config) {
|
|
683
|
-
return await apiRequest(config, "/v2/project/files/publish", { body: { files } });
|
|
684
|
-
}
|
|
685
|
-
//#endregion
|
|
686
6
|
//#region src/index.ts
|
|
687
7
|
/**
|
|
688
8
|
* GT is the core driver for the General Translation library.
|
|
@@ -704,6 +24,23 @@ async function _publishFiles(files, config) {
|
|
|
704
24
|
* });
|
|
705
25
|
*/
|
|
706
26
|
var GT = class extends require_runtime.GTRuntime {
|
|
27
|
+
setConfig(params) {
|
|
28
|
+
try {
|
|
29
|
+
super.setConfig(params);
|
|
30
|
+
} finally {
|
|
31
|
+
this._apiAdapter = void 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
_getApiAdapter() {
|
|
35
|
+
this._apiAdapter ??= require_derive.createGtApiAdapter({
|
|
36
|
+
apiKey: this.apiKey || this.devApiKey,
|
|
37
|
+
userTokenProvider: this.userTokenProvider,
|
|
38
|
+
baseUrl: this.baseUrl || "https://api.gtx.dev",
|
|
39
|
+
projectId: this.projectId,
|
|
40
|
+
customMapping: this.customMapping
|
|
41
|
+
});
|
|
42
|
+
return this._apiAdapter;
|
|
43
|
+
}
|
|
707
44
|
/**
|
|
708
45
|
* Queries branch information from the API.
|
|
709
46
|
*
|
|
@@ -711,8 +48,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
711
48
|
* @returns {Promise<BranchDataResult>} The branch information.
|
|
712
49
|
*/
|
|
713
50
|
async queryBranchData(query) {
|
|
714
|
-
|
|
715
|
-
return await
|
|
51
|
+
require_runtimeTranslate.validateAuth("queryBranchData", this._getTranslationConfig());
|
|
52
|
+
return await this._getApiAdapter().queryBranchData(query);
|
|
716
53
|
}
|
|
717
54
|
/**
|
|
718
55
|
* Creates a new branch in the API. If the branch already exists, it will be returned.
|
|
@@ -721,8 +58,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
721
58
|
* @returns {Promise<CreateBranchResult>} The created branch information.
|
|
722
59
|
*/
|
|
723
60
|
async createBranch(query) {
|
|
724
|
-
|
|
725
|
-
return await
|
|
61
|
+
require_runtimeTranslate.validateAuth("createBranch", this._getTranslationConfig());
|
|
62
|
+
return await this._getApiAdapter().createBranch(query);
|
|
726
63
|
}
|
|
727
64
|
/**
|
|
728
65
|
* Processes file moves by cloning source files and translations with new fileIds.
|
|
@@ -738,8 +75,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
738
75
|
* ], { branchId: 'main' });
|
|
739
76
|
*/
|
|
740
77
|
async processFileMoves(moves, options = {}) {
|
|
741
|
-
|
|
742
|
-
return await
|
|
78
|
+
require_runtimeTranslate.validateAuth("processFileMoves", this._getTranslationConfig());
|
|
79
|
+
return await this._getApiAdapter().processFileMoves(moves, options);
|
|
743
80
|
}
|
|
744
81
|
/**
|
|
745
82
|
* Gets orphaned files for a branch - files that exist on the branch
|
|
@@ -755,8 +92,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
755
92
|
* const result = await gt.getOrphanedFiles('branch-id', ['file-1', 'file-2']);
|
|
756
93
|
*/
|
|
757
94
|
async getOrphanedFiles(branchId, fileIds, options = {}) {
|
|
758
|
-
|
|
759
|
-
return await
|
|
95
|
+
require_runtimeTranslate.validateAuth("getOrphanedFiles", this._getTranslationConfig());
|
|
96
|
+
return await this._getApiAdapter().getOrphanedFiles(branchId, fileIds, options);
|
|
760
97
|
}
|
|
761
98
|
/**
|
|
762
99
|
* Enqueues project setup job using the specified file references
|
|
@@ -771,12 +108,12 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
771
108
|
* @returns {Promise<SetupProjectResult>} Object containing the jobId and status
|
|
772
109
|
*/
|
|
773
110
|
async setupProject(files, options) {
|
|
774
|
-
|
|
111
|
+
require_runtimeTranslate.validateAuth("setupProject", this._getTranslationConfig());
|
|
775
112
|
options = {
|
|
776
113
|
...options,
|
|
777
114
|
locales: options?.locales?.map((locale) => this.resolveServiceLocale(locale))
|
|
778
115
|
};
|
|
779
|
-
return await
|
|
116
|
+
return await this._getApiAdapter().setupProject(files, options);
|
|
780
117
|
}
|
|
781
118
|
/**
|
|
782
119
|
* Checks the current status of one or more project jobs by their unique identifiers.
|
|
@@ -795,8 +132,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
795
132
|
* ], 10000);
|
|
796
133
|
*/
|
|
797
134
|
async checkJobStatus(jobIds, timeoutMs) {
|
|
798
|
-
|
|
799
|
-
return await
|
|
135
|
+
require_runtimeTranslate.validateAuth("checkJobStatus", this._getTranslationConfig());
|
|
136
|
+
return await this._getApiAdapter().checkJobStatus(jobIds, timeoutMs);
|
|
800
137
|
}
|
|
801
138
|
/**
|
|
802
139
|
* Polls job statuses until all jobs are finished or the timeout is reached.
|
|
@@ -806,8 +143,9 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
806
143
|
* @returns {Promise<AwaitJobsResult>} The final status of all jobs and whether they all completed.
|
|
807
144
|
*/
|
|
808
145
|
async awaitJobs(jobs, options) {
|
|
809
|
-
|
|
810
|
-
|
|
146
|
+
require_runtimeTranslate.validateAuth("awaitJobs", this._getTranslationConfig());
|
|
147
|
+
const jobIds = Array.isArray(jobs) ? jobs : Object.keys(jobs.jobData);
|
|
148
|
+
return await this._getApiAdapter().awaitJobs(jobIds, options);
|
|
811
149
|
}
|
|
812
150
|
/**
|
|
813
151
|
* Enqueues translation jobs for previously uploaded source files.
|
|
@@ -823,20 +161,20 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
823
161
|
* @returns {Promise<EnqueueFilesResult>} Result containing job IDs, queue status, and processing information.
|
|
824
162
|
*/
|
|
825
163
|
async enqueueFiles(files, options) {
|
|
826
|
-
|
|
164
|
+
require_runtimeTranslate.validateAuth("enqueueFiles", this._getTranslationConfig());
|
|
827
165
|
let mergedOptions = {
|
|
828
166
|
...options,
|
|
829
167
|
sourceLocale: options.sourceLocale ?? this.sourceLocale,
|
|
830
168
|
targetLocales: options.targetLocales ?? [this.targetLocale]
|
|
831
169
|
};
|
|
832
170
|
if (!mergedOptions.sourceLocale) {
|
|
833
|
-
const error =
|
|
834
|
-
|
|
171
|
+
const error = require_runtimeTranslate.noSourceLocaleProvidedError("enqueueFiles");
|
|
172
|
+
require_runtimeTranslate.gtInstanceLogger.error(error);
|
|
835
173
|
throw new Error(error);
|
|
836
174
|
}
|
|
837
175
|
if (!mergedOptions.targetLocales || mergedOptions.targetLocales.length === 0) {
|
|
838
|
-
const error =
|
|
839
|
-
|
|
176
|
+
const error = require_runtimeTranslate.noTargetLocaleProvidedError("enqueueFiles");
|
|
177
|
+
require_runtimeTranslate.gtInstanceLogger.error(error);
|
|
840
178
|
throw new Error(error);
|
|
841
179
|
}
|
|
842
180
|
const targetLocaleIdentities = [...mergedOptions.targetLocales];
|
|
@@ -846,7 +184,7 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
846
184
|
targetLocales: mergedOptions.targetLocales.map((locale) => this.resolveServiceLocale(locale))
|
|
847
185
|
};
|
|
848
186
|
return {
|
|
849
|
-
...await
|
|
187
|
+
...await this._getApiAdapter().enqueueFiles(files, mergedOptions),
|
|
850
188
|
locales: targetLocaleIdentities
|
|
851
189
|
};
|
|
852
190
|
}
|
|
@@ -858,8 +196,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
858
196
|
* @returns {Promise<CreateTagResult>} The created or updated tag.
|
|
859
197
|
*/
|
|
860
198
|
async createTag(options) {
|
|
861
|
-
|
|
862
|
-
return await
|
|
199
|
+
require_runtimeTranslate.validateAuth("createTag", this._getTranslationConfig());
|
|
200
|
+
return await this._getApiAdapter().createTag(options);
|
|
863
201
|
}
|
|
864
202
|
/**
|
|
865
203
|
* Publishes or unpublishes files on the CDN.
|
|
@@ -868,8 +206,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
868
206
|
* @returns {Promise<PublishFilesResult>} Result containing per-file success/failure
|
|
869
207
|
*/
|
|
870
208
|
async publishFiles(files) {
|
|
871
|
-
|
|
872
|
-
return { results: (await
|
|
209
|
+
require_runtimeTranslate.validateAuth("publishFiles", this._getTranslationConfig());
|
|
210
|
+
return { results: (await this._getApiAdapter().publishFiles(files)).results.map((item) => ({
|
|
873
211
|
...item,
|
|
874
212
|
...item.locale && { locale: this.resolveServiceResponseLocale(item.locale) }
|
|
875
213
|
})) };
|
|
@@ -881,14 +219,15 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
881
219
|
* @returns {Promise<void>} Resolves when submission succeeds.
|
|
882
220
|
*/
|
|
883
221
|
async submitUserEditDiffs(payload) {
|
|
884
|
-
|
|
885
|
-
|
|
222
|
+
require_runtimeTranslate.validateAuth("submitUserEditDiffs", this._getTranslationConfig());
|
|
223
|
+
const normalized = {
|
|
886
224
|
...payload,
|
|
887
225
|
diffs: (payload.diffs || []).map((d) => ({
|
|
888
226
|
...d,
|
|
889
227
|
locale: this.resolveServiceLocale(d.locale)
|
|
890
228
|
}))
|
|
891
|
-
}
|
|
229
|
+
};
|
|
230
|
+
await this._getApiAdapter().submitUserEditDiffs(normalized);
|
|
892
231
|
}
|
|
893
232
|
/**
|
|
894
233
|
* Queries data about one or more source or translation files.
|
|
@@ -917,8 +256,8 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
917
256
|
* @returns The project info.
|
|
918
257
|
*/
|
|
919
258
|
async getProjectInfo(options = {}) {
|
|
920
|
-
|
|
921
|
-
const result = await
|
|
259
|
+
require_runtimeTranslate.validateAuth("getProjectInfo", this._getTranslationConfig());
|
|
260
|
+
const result = await this._getApiAdapter().getProjectInfo(void 0, options.timeout);
|
|
922
261
|
return {
|
|
923
262
|
...result,
|
|
924
263
|
defaultLocale: this.resolveServiceResponseLocale(result.defaultLocale),
|
|
@@ -926,12 +265,12 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
926
265
|
};
|
|
927
266
|
}
|
|
928
267
|
async queryFileData(data, options = {}) {
|
|
929
|
-
|
|
268
|
+
require_runtimeTranslate.validateAuth("queryFileData", this._getTranslationConfig());
|
|
930
269
|
data.translatedFiles = data.translatedFiles?.map((item) => ({
|
|
931
270
|
...item,
|
|
932
271
|
locale: this.resolveServiceLocale(item.locale)
|
|
933
272
|
}));
|
|
934
|
-
const result = await
|
|
273
|
+
const result = await this._getApiAdapter().queryFileData(data, options.timeout);
|
|
935
274
|
result.translatedFiles = result.translatedFiles?.map((item) => ({
|
|
936
275
|
...item,
|
|
937
276
|
...item.locale && { locale: this.resolveServiceResponseLocale(item.locale) }
|
|
@@ -958,8 +297,11 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
958
297
|
*
|
|
959
298
|
*/
|
|
960
299
|
async querySourceFile(data, options = {}) {
|
|
961
|
-
|
|
962
|
-
const result = await
|
|
300
|
+
require_runtimeTranslate.validateAuth("querySourceFile", this._getTranslationConfig());
|
|
301
|
+
const result = await this._getApiAdapter().querySourceFile({ fileId: data.fileId }, {
|
|
302
|
+
branchId: data.branchId,
|
|
303
|
+
versionId: data.versionId
|
|
304
|
+
}, options.timeout);
|
|
963
305
|
result.translations = result.translations.map((item) => ({
|
|
964
306
|
...item,
|
|
965
307
|
...item.locale && { locale: this.resolveServiceResponseLocale(item.locale) }
|
|
@@ -971,6 +313,9 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
971
313
|
/**
|
|
972
314
|
* Get project data for a given project ID.
|
|
973
315
|
*
|
|
316
|
+
* @deprecated Use `getProjectInfo()` on a `GT` instance configured with the
|
|
317
|
+
* target `projectId`; it reads the same resource and also returns
|
|
318
|
+
* `autoApprove`. This method will be removed in the next major version.
|
|
974
319
|
* @param {string} projectId - The ID of the project to get the data for.
|
|
975
320
|
* @returns {Promise<ProjectData>} The project data.
|
|
976
321
|
*
|
|
@@ -981,11 +326,13 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
981
326
|
*
|
|
982
327
|
*/
|
|
983
328
|
async getProjectData(projectId, options = {}) {
|
|
984
|
-
|
|
985
|
-
const
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
329
|
+
require_runtimeTranslate.validateAuth("getProjectData", this._getTranslationConfig());
|
|
330
|
+
const { autoApprove: _autoApprove, ...project } = await this._getApiAdapter().getProjectInfo(projectId, options.timeout);
|
|
331
|
+
return {
|
|
332
|
+
...project,
|
|
333
|
+
defaultLocale: this.resolveServiceResponseLocale(project.defaultLocale),
|
|
334
|
+
currentLocales: project.currentLocales.map((item) => this.resolveServiceResponseLocale(item))
|
|
335
|
+
};
|
|
989
336
|
}
|
|
990
337
|
/**
|
|
991
338
|
* Downloads a single file.
|
|
@@ -1009,14 +356,11 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1009
356
|
* });
|
|
1010
357
|
*/
|
|
1011
358
|
async downloadFile(file, options = {}) {
|
|
1012
|
-
|
|
1013
|
-
return (await
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
versionId: file.versionId,
|
|
1018
|
-
useLatestAvailableVersion: file.useLatestAvailableVersion
|
|
1019
|
-
}], options, this._getTranslationConfig())).data?.[0]?.data ?? "";
|
|
359
|
+
require_runtimeTranslate.validateAuth("downloadTranslatedFile", this._getTranslationConfig());
|
|
360
|
+
return (await this._getApiAdapter().downloadFileBatch([{
|
|
361
|
+
...file,
|
|
362
|
+
locale: file.locale ? this.resolveServiceLocale(file.locale) : void 0
|
|
363
|
+
}], options)).files[0]?.data ?? "";
|
|
1020
364
|
}
|
|
1021
365
|
/**
|
|
1022
366
|
* Downloads multiple files in a batch.
|
|
@@ -1035,7 +379,7 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1035
379
|
* });
|
|
1036
380
|
*/
|
|
1037
381
|
async downloadFileBatch(requests, options = {}) {
|
|
1038
|
-
|
|
382
|
+
require_runtimeTranslate.validateAuth("downloadFileBatch", this._getTranslationConfig());
|
|
1039
383
|
const requestedFiles = /* @__PURE__ */ new Map();
|
|
1040
384
|
requests = requests.map((request) => {
|
|
1041
385
|
const locale = request.locale ? this.resolveServiceLocale(request.locale) : void 0;
|
|
@@ -1050,11 +394,11 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1050
394
|
locale
|
|
1051
395
|
};
|
|
1052
396
|
});
|
|
1053
|
-
const result = await
|
|
397
|
+
const result = await this._getApiAdapter().downloadFileBatch(requests, options);
|
|
1054
398
|
return {
|
|
1055
|
-
files: result.
|
|
399
|
+
files: result.files.map((file) => {
|
|
1056
400
|
if (!file.locale) return file;
|
|
1057
|
-
const serviceLocale = this.
|
|
401
|
+
const serviceLocale = this.resolveServiceLocale(file.locale);
|
|
1058
402
|
const matchingRequests = (requestedFiles.get(JSON.stringify([file.fileId, serviceLocale])) ?? []).filter((request) => (!request.branchId || request.branchId === file.branchId) && (!request.versionId || request.versionId === file.versionId));
|
|
1059
403
|
return {
|
|
1060
404
|
...file,
|
|
@@ -1077,7 +421,7 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1077
421
|
* @returns {Promise<UploadFilesResponse>} Upload result containing file IDs, version information, and upload status.
|
|
1078
422
|
*/
|
|
1079
423
|
async uploadSourceFiles(files, options) {
|
|
1080
|
-
|
|
424
|
+
require_runtimeTranslate.validateAuth("uploadSourceFiles", this._getTranslationConfig());
|
|
1081
425
|
const mergedOptions = {
|
|
1082
426
|
...options,
|
|
1083
427
|
sourceLocale: this.resolveServiceLocale(options.sourceLocale ?? this.sourceLocale ?? "en")
|
|
@@ -1089,11 +433,11 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1089
433
|
locale: this.resolveServiceLocale(f.source.locale)
|
|
1090
434
|
}
|
|
1091
435
|
}));
|
|
1092
|
-
const
|
|
436
|
+
const { uploadedFiles } = await this._getApiAdapter().uploadSourceFiles(files, mergedOptions);
|
|
1093
437
|
return {
|
|
1094
|
-
uploadedFiles
|
|
1095
|
-
count:
|
|
1096
|
-
message: `Successfully uploaded ${
|
|
438
|
+
uploadedFiles,
|
|
439
|
+
count: uploadedFiles.length,
|
|
440
|
+
message: `Successfully uploaded ${uploadedFiles.length} files in ${Math.ceil(files.length / 100)} batch(es)`
|
|
1097
441
|
};
|
|
1098
442
|
}
|
|
1099
443
|
/**
|
|
@@ -1102,15 +446,11 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1102
446
|
* re-running only stores new fonts.
|
|
1103
447
|
* @param {AssetUpload[]} fonts - Fonts to upload (`content` base64-encoded).
|
|
1104
448
|
* @param {UploadAssetsOptions} options - Optional settings (e.g. timeout).
|
|
1105
|
-
* @returns {Promise<UploadAssetsResponse>} The stored
|
|
449
|
+
* @returns {Promise<UploadAssetsResponse>} The stored assets.
|
|
1106
450
|
*/
|
|
1107
451
|
async uploadFonts(fonts, options = {}) {
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
return {
|
|
1111
|
-
assets: result.data,
|
|
1112
|
-
count: result.count
|
|
1113
|
-
};
|
|
452
|
+
require_runtimeTranslate.validateAuth("uploadFonts", this._getTranslationConfig());
|
|
453
|
+
return await this._getApiAdapter().uploadFonts(fonts, options);
|
|
1114
454
|
}
|
|
1115
455
|
/**
|
|
1116
456
|
* Uploads translation files that correspond to previously uploaded source files.
|
|
@@ -1127,18 +467,18 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1127
467
|
* @returns {Promise<UploadFilesResponse>} Upload result containing translation IDs, status, and processing information.
|
|
1128
468
|
*/
|
|
1129
469
|
async uploadTranslations(files, options) {
|
|
1130
|
-
|
|
470
|
+
require_runtimeTranslate.validateAuth("uploadTranslations", this._getTranslationConfig());
|
|
1131
471
|
const mergedOptions = {
|
|
1132
472
|
...options,
|
|
1133
473
|
sourceLocale: options.sourceLocale ?? this.sourceLocale
|
|
1134
474
|
};
|
|
1135
475
|
if (!mergedOptions.sourceLocale) {
|
|
1136
|
-
const error =
|
|
1137
|
-
|
|
476
|
+
const error = require_runtimeTranslate.noSourceLocaleProvidedError("uploadTranslations");
|
|
477
|
+
require_runtimeTranslate.gtInstanceLogger.error(error);
|
|
1138
478
|
throw new Error(error);
|
|
1139
479
|
}
|
|
1140
480
|
mergedOptions.sourceLocale = this.resolveServiceLocale(mergedOptions.sourceLocale);
|
|
1141
|
-
const
|
|
481
|
+
const targetFiles = files.map((f) => ({
|
|
1142
482
|
...f,
|
|
1143
483
|
source: {
|
|
1144
484
|
...f.source,
|
|
@@ -1148,11 +488,12 @@ var GT = class extends require_runtime.GTRuntime {
|
|
|
1148
488
|
...t,
|
|
1149
489
|
locale: this.resolveServiceLocale(t.locale)
|
|
1150
490
|
}))
|
|
1151
|
-
}))
|
|
491
|
+
}));
|
|
492
|
+
const { uploadedFiles } = await this._getApiAdapter().uploadTranslations(targetFiles, mergedOptions);
|
|
1152
493
|
return {
|
|
1153
|
-
uploadedFiles
|
|
1154
|
-
count:
|
|
1155
|
-
message: `Successfully uploaded ${
|
|
494
|
+
uploadedFiles,
|
|
495
|
+
count: uploadedFiles.length,
|
|
496
|
+
message: `Successfully uploaded ${uploadedFiles.length} files in ${Math.ceil(files.length / 100)} batch(es)`
|
|
1156
497
|
};
|
|
1157
498
|
}
|
|
1158
499
|
};
|