generaltranslation 8.2.11 → 8.2.13
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 +15 -0
- package/dist/{IntlCache-k2qfrDqB.cjs → IntlCache-CAW8tKhd.cjs} +12 -12
- package/dist/IntlCache-CAW8tKhd.cjs.map +1 -0
- package/dist/{IntlCache-Ccg_cQPR.mjs → IntlCache-WZk0rKvj.mjs} +12 -12
- package/dist/IntlCache-WZk0rKvj.mjs.map +1 -0
- package/dist/LocaleConfig.d.ts +59 -0
- package/dist/LocaleConfig.js +225 -0
- package/dist/backwards-compatability/dataConversion.d.ts +23 -0
- package/dist/backwards-compatability/dataConversion.js +207 -0
- package/dist/backwards-compatability/oldHashJsxChildren.d.ts +23 -0
- package/dist/backwards-compatability/oldHashJsxChildren.js +79 -0
- package/dist/backwards-compatability/oldTypes.d.ts +21 -0
- package/dist/backwards-compatability/oldTypes.js +1 -0
- package/dist/backwards-compatability/typeChecking.d.ts +21 -0
- package/dist/backwards-compatability/typeChecking.js +61 -0
- package/dist/{base64-CUcEPEC5.cjs → base64-2fu94Klt.cjs} +3 -3
- package/dist/base64-2fu94Klt.cjs.map +1 -0
- package/dist/{base64-C1sogiix.mjs → base64-DH0STixb.mjs} +3 -3
- package/dist/base64-DH0STixb.mjs.map +1 -0
- package/dist/cache/IntlCache.d.ts +26 -0
- package/dist/cache/IntlCache.js +84 -0
- package/dist/cache/types.d.ts +32 -0
- package/dist/cache/types.js +1 -0
- package/dist/{core-3SOQ5ND6.cjs → core-7RP541eY.cjs} +18 -20
- package/dist/core-7RP541eY.cjs.map +1 -0
- package/dist/{core-6rY3oy32.d.cts → core-I9pWGafA.d.mts} +8 -8
- package/dist/{core-BYVWty_t.d.mts → core-TLJoDpJP.d.cts} +8 -8
- package/dist/{core-Bx0m6GOp.mjs → core-isLphYAZ.mjs} +18 -20
- package/dist/core-isLphYAZ.mjs.map +1 -0
- package/dist/core.cjs +1 -1
- package/dist/core.d.cts +1 -1
- package/dist/core.d.mts +1 -1
- package/dist/core.d.ts +128 -0
- package/dist/core.js +137 -0
- package/dist/core.mjs +1 -1
- package/dist/derive/condenseVars.d.ts +7 -0
- package/dist/derive/condenseVars.js +30 -0
- package/dist/derive/declareVar.d.ts +26 -0
- package/dist/derive/declareVar.js +38 -0
- package/dist/derive/decodeVars.d.ts +9 -0
- package/dist/derive/decodeVars.js +52 -0
- package/dist/derive/derive.d.ts +53 -0
- package/dist/derive/derive.js +55 -0
- package/dist/derive/extractVars.d.ts +9 -0
- package/dist/derive/extractVars.js +34 -0
- package/dist/derive/index.d.ts +7 -0
- package/dist/derive/index.js +7 -0
- package/dist/derive/indexVars.d.ts +6 -0
- package/dist/derive/indexVars.js +52 -0
- package/dist/derive/utils/constants.d.ts +2 -0
- package/dist/derive/utils/constants.js +2 -0
- package/dist/derive/utils/regex.d.ts +2 -0
- package/dist/derive/utils/regex.js +4 -0
- package/dist/derive/utils/sanitizeVar.d.ts +12 -0
- package/dist/derive/utils/sanitizeVar.js +38 -0
- package/dist/derive/utils/traverseHelpers.d.ts +4 -0
- package/dist/derive/utils/traverseHelpers.js +22 -0
- package/dist/derive/utils/traverseIcu.d.ts +20 -0
- package/dist/derive/utils/traverseIcu.js +49 -0
- package/dist/derive/utils/types.d.ts +23 -0
- package/dist/derive/utils/types.js +1 -0
- package/dist/errors/ApiError.d.ts +7 -0
- package/dist/errors/ApiError.js +33 -0
- package/dist/errors/formattingErrors.d.ts +1 -0
- package/dist/errors/formattingErrors.js +3 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +1 -0
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.d.ts +59 -0
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.js +147 -0
- package/dist/formatting/custom-formats/CutoffFormat/constants.d.ts +4 -0
- package/dist/formatting/custom-formats/CutoffFormat/constants.js +30 -0
- package/dist/formatting/custom-formats/CutoffFormat/types.d.ts +48 -0
- package/dist/formatting/custom-formats/CutoffFormat/types.js +2 -0
- package/dist/formatting/format.d.ts +1 -0
- package/dist/formatting/format.js +257 -0
- package/dist/id/hashSource.d.ts +25 -0
- package/dist/id/hashSource.js +98 -0
- package/dist/id/hashTemplate.d.ts +4 -0
- package/dist/id/hashTemplate.js +6 -0
- package/dist/id/types.d.ts +7 -0
- package/dist/id/types.js +1 -0
- package/dist/{id-COlX5v3V.cjs → id-CyiXsQrY.cjs} +9 -9
- package/dist/id-CyiXsQrY.cjs.map +1 -0
- package/dist/{id-BmOyfaug.mjs → id-DbD7K-HL.mjs} +9 -9
- package/dist/id-DbD7K-HL.mjs.map +1 -0
- package/dist/id.cjs +1 -1
- package/dist/id.d.cts +9 -9
- package/dist/id.d.mts +9 -9
- package/dist/id.d.ts +3 -0
- package/dist/id.js +3 -0
- package/dist/id.mjs +1 -1
- package/dist/index.cjs +140 -140
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +88 -88
- package/dist/index.d.mts +88 -88
- package/dist/index.d.ts +1019 -0
- package/dist/index.js +1664 -0
- package/dist/index.mjs +140 -140
- package/dist/index.mjs.map +1 -1
- package/dist/internal.cjs +41 -26
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +38 -23
- package/dist/internal.d.mts +38 -23
- package/dist/internal.d.ts +19 -0
- package/dist/internal.js +17 -0
- package/dist/internal.mjs +41 -26
- package/dist/internal.mjs.map +1 -1
- package/dist/locales/customLocaleMapping.d.ts +11 -0
- package/dist/locales/customLocaleMapping.js +23 -0
- package/dist/locales/determineLocale.d.ts +1 -0
- package/dist/locales/determineLocale.js +72 -0
- package/dist/locales/getLocaleDirection.d.ts +1 -0
- package/dist/locales/getLocaleDirection.js +89 -0
- package/dist/locales/getLocaleEmoji.d.ts +2 -0
- package/dist/locales/getLocaleEmoji.js +319 -0
- package/dist/locales/getLocaleName.d.ts +1 -0
- package/dist/locales/getLocaleName.js +45 -0
- package/dist/locales/getLocaleProperties.d.ts +32 -0
- package/dist/locales/getLocaleProperties.js +220 -0
- package/dist/locales/getPluralForm.d.ts +9 -0
- package/dist/locales/getPluralForm.js +63 -0
- package/dist/locales/getRegionProperties.d.ts +7 -0
- package/dist/locales/getRegionProperties.js +61 -0
- package/dist/locales/isSameDialect.d.ts +1 -0
- package/dist/locales/isSameDialect.js +41 -0
- package/dist/locales/isSameLanguage.d.ts +1 -0
- package/dist/locales/isSameLanguage.js +20 -0
- package/dist/locales/isSupersetLocale.d.ts +1 -0
- package/dist/locales/isSupersetLocale.js +22 -0
- package/dist/locales/isValidLocale.d.ts +1 -0
- package/dist/locales/isValidLocale.js +75 -0
- package/dist/locales/requiresTranslation.d.ts +1 -0
- package/dist/locales/requiresTranslation.js +32 -0
- package/dist/locales/resolveAliasLocale.d.ts +8 -0
- package/dist/locales/resolveAliasLocale.js +21 -0
- package/dist/locales/resolveCanonicalLocale.d.ts +8 -0
- package/dist/locales/resolveCanonicalLocale.js +13 -0
- package/dist/logging/errors.d.ts +10 -0
- package/dist/logging/errors.js +29 -0
- package/dist/logging/logger.d.ts +121 -0
- package/dist/logging/logger.js +259 -0
- package/dist/logging/warnings.d.ts +2 -0
- package/dist/logging/warnings.js +2 -0
- package/dist/projects/getProjectData.d.ts +1 -0
- package/dist/projects/getProjectData.js +87 -0
- package/dist/settings/plurals.d.ts +3 -0
- package/dist/settings/plurals.js +14 -0
- package/dist/settings/settings.d.ts +2 -0
- package/dist/settings/settings.js +2 -0
- package/dist/settings/settingsUrls.d.ts +3 -0
- package/dist/settings/settingsUrls.js +3 -0
- package/dist/translate/api.d.ts +1 -0
- package/dist/translate/api.js +1 -0
- package/dist/translate/awaitJobs.d.ts +19 -0
- package/dist/translate/awaitJobs.js +112 -0
- package/dist/translate/checkJobStatus.d.ts +8 -0
- package/dist/translate/checkJobStatus.js +55 -0
- package/dist/translate/createBranch.d.ts +10 -0
- package/dist/translate/createBranch.js +53 -0
- package/dist/translate/createTag.d.ts +19 -0
- package/dist/translate/createTag.js +67 -0
- package/dist/translate/downloadFileBatch.d.ts +1 -0
- package/dist/translate/downloadFileBatch.js +77 -0
- package/dist/translate/enqueueFiles.d.ts +8 -0
- package/dist/translate/enqueueFiles.js +96 -0
- package/dist/translate/getOrphanedFiles.d.ts +8 -0
- package/dist/translate/getOrphanedFiles.js +97 -0
- package/dist/translate/processFileMoves.d.ts +25 -0
- package/dist/translate/processFileMoves.js +90 -0
- package/dist/translate/publishFiles.d.ts +17 -0
- package/dist/translate/publishFiles.js +56 -0
- package/dist/translate/queryBranchData.d.ts +3 -0
- package/dist/translate/queryBranchData.js +53 -0
- package/dist/translate/queryFileData.d.ts +42 -0
- package/dist/translate/queryFileData.js +71 -0
- package/dist/translate/querySourceFile.d.ts +1 -0
- package/dist/translate/querySourceFile.js +67 -0
- package/dist/translate/setupProject.d.ts +11 -0
- package/dist/translate/setupProject.js +63 -0
- package/dist/translate/submitUserEditDiffs.d.ts +12 -0
- package/dist/translate/submitUserEditDiffs.js +68 -0
- package/dist/translate/translateMany.d.ts +1 -0
- package/dist/translate/translateMany.js +103 -0
- package/dist/translate/uploadSourceFiles.d.ts +1 -0
- package/dist/translate/uploadSourceFiles.js +87 -0
- package/dist/translate/uploadTranslations.d.ts +1 -0
- package/dist/translate/uploadTranslations.js +101 -0
- package/dist/translate/utils/apiRequest.d.ts +1 -0
- package/dist/translate/utils/apiRequest.js +130 -0
- package/dist/translate/utils/batch.d.ts +52 -0
- package/dist/translate/utils/batch.js +126 -0
- package/dist/translate/utils/fetchWithTimeout.d.ts +1 -0
- package/dist/translate/utils/fetchWithTimeout.js +93 -0
- package/dist/translate/utils/generateRequestHeaders.d.ts +2 -0
- package/dist/translate/utils/generateRequestHeaders.js +26 -0
- package/dist/translate/utils/handleFetchError.d.ts +1 -0
- package/dist/translate/utils/handleFetchError.js +12 -0
- package/dist/translate/utils/validateFileFormatTransform.d.ts +16 -0
- package/dist/translate/utils/validateFileFormatTransform.js +29 -0
- package/dist/translate/utils/validateResponse.d.ts +1 -0
- package/dist/translate/utils/validateResponse.js +72 -0
- package/dist/{types-Cy8q2XZX.d.mts → types-AHtYZIP-.d.mts} +63 -40
- package/dist/{types-NKhpChvT.d.cts → types-Bf8_Apq_.d.cts} +63 -40
- package/dist/types-dir/api/branch.d.ts +10 -0
- package/dist/types-dir/api/branch.js +1 -0
- package/dist/types-dir/api/checkFileTranslations.d.ts +37 -0
- package/dist/types-dir/api/checkFileTranslations.js +1 -0
- package/dist/types-dir/api/downloadFile.d.ts +3 -0
- package/dist/types-dir/api/downloadFile.js +1 -0
- package/dist/types-dir/api/downloadFileBatch.d.ts +35 -0
- package/dist/types-dir/api/downloadFileBatch.js +1 -0
- package/dist/types-dir/api/enqueueEntries.d.ts +21 -0
- package/dist/types-dir/api/enqueueEntries.js +1 -0
- package/dist/types-dir/api/enqueueFiles.d.ts +70 -0
- package/dist/types-dir/api/enqueueFiles.js +1 -0
- package/dist/types-dir/api/entry.d.ts +39 -0
- package/dist/types-dir/api/entry.js +1 -0
- package/dist/types-dir/api/fetchTranslations.d.ts +12 -0
- package/dist/types-dir/api/fetchTranslations.js +1 -0
- package/dist/types-dir/api/file.d.ts +57 -0
- package/dist/types-dir/api/file.js +1 -0
- package/dist/types-dir/api/json.d.ts +5 -0
- package/dist/types-dir/api/json.js +1 -0
- package/dist/types-dir/api/project.d.ts +7 -0
- package/dist/types-dir/api/project.js +1 -0
- package/dist/types-dir/api/translate.d.ts +34 -0
- package/dist/types-dir/api/translate.js +1 -0
- package/dist/types-dir/api/translateMany.d.ts +5 -0
- package/dist/types-dir/api/translateMany.js +1 -0
- package/dist/types-dir/api/translationStatus.d.ts +9 -0
- package/dist/types-dir/api/translationStatus.js +1 -0
- package/dist/types-dir/api/uploadFiles.d.ts +44 -0
- package/dist/types-dir/api/uploadFiles.js +1 -0
- package/dist/types-dir/jsx/content.d.ts +61 -0
- package/dist/types-dir/jsx/content.js +11 -0
- package/dist/types-dir/jsx/variables.d.ts +9 -0
- package/dist/types-dir/jsx/variables.js +1 -0
- package/dist/types-dir/transformations.d.ts +8 -0
- package/dist/types-dir/transformations.js +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +129 -0
- package/dist/types.js +2 -0
- package/dist/types.mjs.map +1 -1
- package/dist/utils/base64.d.ts +2 -0
- package/dist/utils/base64.js +28 -0
- package/dist/utils/isSupportedFileFormatTransform.d.ts +8 -0
- package/dist/utils/isSupportedFileFormatTransform.js +26 -0
- package/dist/utils/isVariable.d.ts +2 -0
- package/dist/utils/isVariable.js +22 -0
- package/dist/utils/minify.d.ts +2 -0
- package/dist/utils/minify.js +10 -0
- package/dist/utils/stableStringify.d.ts +1 -0
- package/dist/utils/stableStringify.js +40 -0
- package/package.json +5 -6
- package/dist/IntlCache-Ccg_cQPR.mjs.map +0 -1
- package/dist/IntlCache-k2qfrDqB.cjs.map +0 -1
- package/dist/base64-C1sogiix.mjs.map +0 -1
- package/dist/base64-CUcEPEC5.cjs.map +0 -1
- package/dist/core-3SOQ5ND6.cjs.map +0 -1
- package/dist/core-Bx0m6GOp.mjs.map +0 -1
- package/dist/id-BmOyfaug.mjs.map +0 -1
- package/dist/id-COlX5v3V.cjs.map +0 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
23
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
import { defaultRuntimeApiUrl } from '../settings/settingsUrls';
|
|
49
|
+
import apiRequest from './utils/apiRequest';
|
|
50
|
+
import { hashSource } from '../id';
|
|
51
|
+
export default function _translateMany(requests, globalMetadata, config, timeout) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
+
var isArray, hashOrder, requestsObject, entries, _i, entries_1, _a, key, request, normalized, source, metadata, hash, response;
|
|
54
|
+
var _b, _c;
|
|
55
|
+
return __generator(this, function (_d) {
|
|
56
|
+
switch (_d.label) {
|
|
57
|
+
case 0:
|
|
58
|
+
isArray = Array.isArray(requests);
|
|
59
|
+
hashOrder = isArray ? [] : undefined;
|
|
60
|
+
requestsObject = {};
|
|
61
|
+
entries = isArray
|
|
62
|
+
? requests.map(function (r) { return [undefined, r]; })
|
|
63
|
+
: Object.entries(requests);
|
|
64
|
+
for (_i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
|
|
65
|
+
_a = entries_1[_i], key = _a[0], request = _a[1];
|
|
66
|
+
normalized = typeof request === 'string' ? { source: request } : request;
|
|
67
|
+
source = normalized.source, metadata = normalized.metadata;
|
|
68
|
+
hash = (_b = key !== null && key !== void 0 ? key : metadata === null || metadata === void 0 ? void 0 : metadata.hash) !== null && _b !== void 0 ? _b : hashSource(__assign({ source: source, dataFormat: (_c = metadata === null || metadata === void 0 ? void 0 : metadata.dataFormat) !== null && _c !== void 0 ? _c : 'STRING' }, metadata));
|
|
69
|
+
hashOrder === null || hashOrder === void 0 ? void 0 : hashOrder.push(hash);
|
|
70
|
+
requestsObject[hash] = {
|
|
71
|
+
source: source,
|
|
72
|
+
metadata: metadata,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return [4 /*yield*/, apiRequest(__assign(__assign({}, config), { baseUrl: config.baseUrl || defaultRuntimeApiUrl }), "/v2/translate", {
|
|
76
|
+
body: {
|
|
77
|
+
requests: requestsObject,
|
|
78
|
+
targetLocale: globalMetadata.targetLocale,
|
|
79
|
+
sourceLocale: globalMetadata.sourceLocale,
|
|
80
|
+
metadata: globalMetadata,
|
|
81
|
+
},
|
|
82
|
+
timeout: timeout,
|
|
83
|
+
retryPolicy: 'none',
|
|
84
|
+
})];
|
|
85
|
+
case 1:
|
|
86
|
+
response = _d.sent();
|
|
87
|
+
// If input was an array, map the record response back to an array in input order
|
|
88
|
+
if (hashOrder) {
|
|
89
|
+
return [2 /*return*/, hashOrder.map(function (hash) {
|
|
90
|
+
var _a;
|
|
91
|
+
return (_a = response[hash]) !== null && _a !== void 0 ? _a : {
|
|
92
|
+
success: false,
|
|
93
|
+
error: 'No translation returned',
|
|
94
|
+
code: 500,
|
|
95
|
+
};
|
|
96
|
+
})];
|
|
97
|
+
}
|
|
98
|
+
// If input was a record, return the record response directly
|
|
99
|
+
return [2 /*return*/, response];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
import apiRequest from './utils/apiRequest';
|
|
38
|
+
import { encode } from '../utils/base64';
|
|
39
|
+
import { processBatches } from './utils/batch';
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
* Uploads source files to the General Translation API in batches.
|
|
43
|
+
* @param files - The files to upload.
|
|
44
|
+
* @param options - The options for the API call.
|
|
45
|
+
* @param config - The configuration for the API call.
|
|
46
|
+
* @returns Promise resolving to a BatchList with all uploaded files
|
|
47
|
+
*/
|
|
48
|
+
export default function _uploadSourceFiles(files, options, config) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
50
|
+
var _this = this;
|
|
51
|
+
return __generator(this, function (_a) {
|
|
52
|
+
return [2 /*return*/, processBatches(files, function (batch) { return __awaiter(_this, void 0, void 0, function () {
|
|
53
|
+
var body, result;
|
|
54
|
+
return __generator(this, function (_a) {
|
|
55
|
+
switch (_a.label) {
|
|
56
|
+
case 0:
|
|
57
|
+
body = {
|
|
58
|
+
data: batch.map(function (_a) {
|
|
59
|
+
var source = _a.source;
|
|
60
|
+
return ({
|
|
61
|
+
source: {
|
|
62
|
+
content: encode(source.content),
|
|
63
|
+
fileName: source.fileName,
|
|
64
|
+
fileFormat: source.fileFormat,
|
|
65
|
+
locale: source.locale,
|
|
66
|
+
dataFormat: source.dataFormat,
|
|
67
|
+
formatMetadata: source.formatMetadata,
|
|
68
|
+
fileId: source.fileId,
|
|
69
|
+
versionId: source.versionId,
|
|
70
|
+
branchId: source.branchId,
|
|
71
|
+
incomingBranchId: source.incomingBranchId,
|
|
72
|
+
checkedOutBranchId: source.checkedOutBranchId,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
}),
|
|
76
|
+
sourceLocale: options.sourceLocale,
|
|
77
|
+
};
|
|
78
|
+
return [4 /*yield*/, apiRequest(config, '/v2/project/files/upload-files', { body: body, timeout: options.timeout })];
|
|
79
|
+
case 1:
|
|
80
|
+
result = _a.sent();
|
|
81
|
+
return [2 /*return*/, result.uploadedFiles || []];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}); }, { batchSize: 100 })];
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
import apiRequest from './utils/apiRequest';
|
|
38
|
+
import { processBatches } from './utils/batch';
|
|
39
|
+
import { encode } from '../utils/base64';
|
|
40
|
+
import { validateFileFormatTransforms } from './utils/validateFileFormatTransform';
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
* Uploads multiple translations to the General Translation API in batches.
|
|
44
|
+
* @param files - Translations to upload with their source
|
|
45
|
+
* @param options - The options for the API call.
|
|
46
|
+
* @param config - The configuration for the API call.
|
|
47
|
+
* @returns Promise resolving to a BatchList with all uploaded files
|
|
48
|
+
*/
|
|
49
|
+
export default function _uploadTranslations(files, options, config) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
51
|
+
var _this = this;
|
|
52
|
+
return __generator(this, function (_a) {
|
|
53
|
+
validateFileFormatTransforms(files.map(function (_a) {
|
|
54
|
+
var source = _a.source;
|
|
55
|
+
return source;
|
|
56
|
+
}));
|
|
57
|
+
return [2 /*return*/, processBatches(files, function (batch) { return __awaiter(_this, void 0, void 0, function () {
|
|
58
|
+
var body, result;
|
|
59
|
+
return __generator(this, function (_a) {
|
|
60
|
+
switch (_a.label) {
|
|
61
|
+
case 0:
|
|
62
|
+
body = {
|
|
63
|
+
data: batch.map(function (_a) {
|
|
64
|
+
var source = _a.source, translations = _a.translations;
|
|
65
|
+
return ({
|
|
66
|
+
source: {
|
|
67
|
+
content: encode(source.content),
|
|
68
|
+
fileName: source.fileName,
|
|
69
|
+
fileFormat: source.fileFormat,
|
|
70
|
+
transformFormat: source.transformFormat,
|
|
71
|
+
locale: source.locale,
|
|
72
|
+
dataFormat: source.dataFormat,
|
|
73
|
+
formatMetadata: source.formatMetadata,
|
|
74
|
+
fileId: source.fileId,
|
|
75
|
+
versionId: source.versionId,
|
|
76
|
+
branchId: source.branchId,
|
|
77
|
+
},
|
|
78
|
+
translations: translations.map(function (t) { return ({
|
|
79
|
+
content: encode(t.content),
|
|
80
|
+
fileName: t.fileName,
|
|
81
|
+
fileFormat: t.fileFormat,
|
|
82
|
+
locale: t.locale,
|
|
83
|
+
dataFormat: t.dataFormat,
|
|
84
|
+
fileId: t.fileId,
|
|
85
|
+
versionId: t.versionId,
|
|
86
|
+
branchId: t.branchId,
|
|
87
|
+
}); }),
|
|
88
|
+
});
|
|
89
|
+
}),
|
|
90
|
+
sourceLocale: options.sourceLocale,
|
|
91
|
+
};
|
|
92
|
+
return [4 /*yield*/, apiRequest(config, '/v2/project/files/upload-translations', { body: body, timeout: options.timeout })];
|
|
93
|
+
case 1:
|
|
94
|
+
result = _a.sent();
|
|
95
|
+
return [2 /*return*/, result.uploadedFiles || []];
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}); }, { batchSize: 100 })];
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
import { defaultBaseUrl } from '../../settings/settingsUrls';
|
|
38
|
+
import { defaultTimeout } from '../../settings/settings';
|
|
39
|
+
import fetchWithTimeout from './fetchWithTimeout';
|
|
40
|
+
import validateResponse from './validateResponse';
|
|
41
|
+
import handleFetchError from './handleFetchError';
|
|
42
|
+
import generateRequestHeaders from './generateRequestHeaders';
|
|
43
|
+
var MAX_RETRIES = 3;
|
|
44
|
+
var INITIAL_DELAY_MS = 500;
|
|
45
|
+
function sleep(ms) {
|
|
46
|
+
return new Promise(function (resolve) { return setTimeout(resolve, ms); });
|
|
47
|
+
}
|
|
48
|
+
function getRetryDelay(policy, attempt) {
|
|
49
|
+
switch (policy) {
|
|
50
|
+
case 'linear':
|
|
51
|
+
return INITIAL_DELAY_MS * (attempt + 1);
|
|
52
|
+
case 'exponential':
|
|
53
|
+
return INITIAL_DELAY_MS * Math.pow(2, attempt);
|
|
54
|
+
default:
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @internal
|
|
60
|
+
*
|
|
61
|
+
* Makes an API request with automatic retry for 5XX errors.
|
|
62
|
+
*
|
|
63
|
+
* Encapsulates URL construction, fetch with timeout, error handling,
|
|
64
|
+
* response validation, and JSON parsing.
|
|
65
|
+
*
|
|
66
|
+
* @param config - The configuration for the API call.
|
|
67
|
+
* @param endpoint - The API endpoint path (e.g. '/v2/project/jobs/info')
|
|
68
|
+
* @param options - Optional request options.
|
|
69
|
+
* @returns The parsed JSON response.
|
|
70
|
+
*/
|
|
71
|
+
export default function apiRequest(config, endpoint, options) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
73
|
+
var timeout, url, method, retryPolicy, maxRetries, requestInit, attempt, response, error_1;
|
|
74
|
+
var _a, _b, _c;
|
|
75
|
+
return __generator(this, function (_d) {
|
|
76
|
+
switch (_d.label) {
|
|
77
|
+
case 0:
|
|
78
|
+
timeout = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : defaultTimeout;
|
|
79
|
+
url = "".concat(config.baseUrl || defaultBaseUrl).concat(endpoint);
|
|
80
|
+
method = (_b = options === null || options === void 0 ? void 0 : options.method) !== null && _b !== void 0 ? _b : 'POST';
|
|
81
|
+
retryPolicy = (_c = options === null || options === void 0 ? void 0 : options.retryPolicy) !== null && _c !== void 0 ? _c : 'exponential';
|
|
82
|
+
maxRetries = retryPolicy === 'none' ? 0 : MAX_RETRIES;
|
|
83
|
+
requestInit = {
|
|
84
|
+
method: method,
|
|
85
|
+
headers: generateRequestHeaders(config),
|
|
86
|
+
};
|
|
87
|
+
if ((options === null || options === void 0 ? void 0 : options.body) !== undefined) {
|
|
88
|
+
requestInit.body = JSON.stringify(options.body);
|
|
89
|
+
}
|
|
90
|
+
attempt = 0;
|
|
91
|
+
_d.label = 1;
|
|
92
|
+
case 1:
|
|
93
|
+
if (!(attempt <= maxRetries)) return [3 /*break*/, 13];
|
|
94
|
+
response = void 0;
|
|
95
|
+
_d.label = 2;
|
|
96
|
+
case 2:
|
|
97
|
+
_d.trys.push([2, 4, , 7]);
|
|
98
|
+
return [4 /*yield*/, fetchWithTimeout(url, requestInit, timeout)];
|
|
99
|
+
case 3:
|
|
100
|
+
response = _d.sent();
|
|
101
|
+
return [3 /*break*/, 7];
|
|
102
|
+
case 4:
|
|
103
|
+
error_1 = _d.sent();
|
|
104
|
+
if (!(attempt < maxRetries)) return [3 /*break*/, 6];
|
|
105
|
+
return [4 /*yield*/, sleep(getRetryDelay(retryPolicy, attempt))];
|
|
106
|
+
case 5:
|
|
107
|
+
_d.sent();
|
|
108
|
+
return [3 /*break*/, 12];
|
|
109
|
+
case 6:
|
|
110
|
+
handleFetchError(error_1, timeout);
|
|
111
|
+
return [3 /*break*/, 7];
|
|
112
|
+
case 7:
|
|
113
|
+
if (!(response.status >= 500 && attempt < maxRetries)) return [3 /*break*/, 9];
|
|
114
|
+
return [4 /*yield*/, sleep(getRetryDelay(retryPolicy, attempt))];
|
|
115
|
+
case 8:
|
|
116
|
+
_d.sent();
|
|
117
|
+
return [3 /*break*/, 12];
|
|
118
|
+
case 9: return [4 /*yield*/, validateResponse(response)];
|
|
119
|
+
case 10:
|
|
120
|
+
_d.sent();
|
|
121
|
+
return [4 /*yield*/, response.json()];
|
|
122
|
+
case 11: return [2 /*return*/, (_d.sent())];
|
|
123
|
+
case 12:
|
|
124
|
+
attempt++;
|
|
125
|
+
return [3 /*break*/, 1];
|
|
126
|
+
case 13: throw new Error('Max retries exceeded');
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits an array into batches of a specified size.
|
|
3
|
+
* @param items - The array to split into batches.
|
|
4
|
+
* @param batchSize - The maximum size of each batch.
|
|
5
|
+
* @returns An array of batches.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createBatches<T>(items: T[], batchSize: number): T[][];
|
|
8
|
+
/**
|
|
9
|
+
* Result of processing batches.
|
|
10
|
+
*/
|
|
11
|
+
export interface BatchList<T> {
|
|
12
|
+
/** The items successfully processed across all batches */
|
|
13
|
+
data: T[];
|
|
14
|
+
/** The total number of items processed */
|
|
15
|
+
count: number;
|
|
16
|
+
/** The number of batches processed */
|
|
17
|
+
batchCount: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Options for batch processing.
|
|
21
|
+
*/
|
|
22
|
+
export interface BatchProcessOptions {
|
|
23
|
+
/** Maximum number of items per batch (default: 100) */
|
|
24
|
+
batchSize?: number;
|
|
25
|
+
/** Whether to process batches in parallel (default: true) */
|
|
26
|
+
parallel?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Processes items in batches using a provided processor function.
|
|
30
|
+
*
|
|
31
|
+
* @param items - The items to process.
|
|
32
|
+
* @param processor - Async function that processes a single batch and returns items.
|
|
33
|
+
* @param options - Optional configuration for batch processing.
|
|
34
|
+
* @returns Promise that resolves to a BatchList containing all processed items.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const result = await processBatches(
|
|
39
|
+
* files,
|
|
40
|
+
* async (batch) => {
|
|
41
|
+
* const response = await uploadFiles(batch);
|
|
42
|
+
* return response.uploadedFiles;
|
|
43
|
+
* },
|
|
44
|
+
* { batchSize: 100 }
|
|
45
|
+
* );
|
|
46
|
+
*
|
|
47
|
+
* console.log(result.data); // All items
|
|
48
|
+
* console.log(result.count); // Total count
|
|
49
|
+
* console.log(result.batchCount); // Number of batches processed
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function processBatches<TInput, TOutput>(items: TInput[], processor: (batch: TInput[]) => Promise<TOutput[]>, options?: BatchProcessOptions): Promise<BatchList<TOutput>>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Splits an array into batches of a specified size.
|
|
39
|
+
* @param items - The array to split into batches.
|
|
40
|
+
* @param batchSize - The maximum size of each batch.
|
|
41
|
+
* @returns An array of batches.
|
|
42
|
+
*/
|
|
43
|
+
export function createBatches(items, batchSize) {
|
|
44
|
+
var batches = [];
|
|
45
|
+
for (var i = 0; i < items.length; i += batchSize) {
|
|
46
|
+
batches.push(items.slice(i, i + batchSize));
|
|
47
|
+
}
|
|
48
|
+
return batches;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Processes items in batches using a provided processor function.
|
|
52
|
+
*
|
|
53
|
+
* @param items - The items to process.
|
|
54
|
+
* @param processor - Async function that processes a single batch and returns items.
|
|
55
|
+
* @param options - Optional configuration for batch processing.
|
|
56
|
+
* @returns Promise that resolves to a BatchList containing all processed items.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const result = await processBatches(
|
|
61
|
+
* files,
|
|
62
|
+
* async (batch) => {
|
|
63
|
+
* const response = await uploadFiles(batch);
|
|
64
|
+
* return response.uploadedFiles;
|
|
65
|
+
* },
|
|
66
|
+
* { batchSize: 100 }
|
|
67
|
+
* );
|
|
68
|
+
*
|
|
69
|
+
* console.log(result.data); // All items
|
|
70
|
+
* console.log(result.count); // Total count
|
|
71
|
+
* console.log(result.batchCount); // Number of batches processed
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function processBatches(items_1, processor_1) {
|
|
75
|
+
return __awaiter(this, arguments, void 0, function (items, processor, options) {
|
|
76
|
+
var _a, batchSize, _b, parallel, batches, allItems, results, _i, results_1, result, _c, batches_1, batch, result;
|
|
77
|
+
if (options === void 0) { options = {}; }
|
|
78
|
+
return __generator(this, function (_d) {
|
|
79
|
+
switch (_d.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
_a = options.batchSize, batchSize = _a === void 0 ? 100 : _a, _b = options.parallel, parallel = _b === void 0 ? true : _b;
|
|
82
|
+
if (items.length === 0) {
|
|
83
|
+
return [2 /*return*/, {
|
|
84
|
+
data: [],
|
|
85
|
+
count: 0,
|
|
86
|
+
batchCount: 0,
|
|
87
|
+
}];
|
|
88
|
+
}
|
|
89
|
+
batches = createBatches(items, batchSize);
|
|
90
|
+
allItems = [];
|
|
91
|
+
if (!parallel) return [3 /*break*/, 2];
|
|
92
|
+
return [4 /*yield*/, Promise.all(batches.map(function (batch) { return processor(batch); }))];
|
|
93
|
+
case 1:
|
|
94
|
+
results = _d.sent();
|
|
95
|
+
for (_i = 0, results_1 = results; _i < results_1.length; _i++) {
|
|
96
|
+
result = results_1[_i];
|
|
97
|
+
if (result) {
|
|
98
|
+
allItems.push.apply(allItems, result);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return [3 /*break*/, 6];
|
|
102
|
+
case 2:
|
|
103
|
+
_c = 0, batches_1 = batches;
|
|
104
|
+
_d.label = 3;
|
|
105
|
+
case 3:
|
|
106
|
+
if (!(_c < batches_1.length)) return [3 /*break*/, 6];
|
|
107
|
+
batch = batches_1[_c];
|
|
108
|
+
return [4 /*yield*/, processor(batch)];
|
|
109
|
+
case 4:
|
|
110
|
+
result = _d.sent();
|
|
111
|
+
if (result) {
|
|
112
|
+
allItems.push.apply(allItems, result);
|
|
113
|
+
}
|
|
114
|
+
_d.label = 5;
|
|
115
|
+
case 5:
|
|
116
|
+
_c++;
|
|
117
|
+
return [3 /*break*/, 3];
|
|
118
|
+
case 6: return [2 /*return*/, {
|
|
119
|
+
data: allItems,
|
|
120
|
+
count: allItems.length,
|
|
121
|
+
batchCount: batches.length,
|
|
122
|
+
}];
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|