@translated/lara
Version:
Official Lara SDK for JavaScript and Node.js
77 lines (76 loc) • 4.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Documents = exports.DocumentStatus = void 0;
const errors_1 = require("./errors");
const s3_1 = __importDefault(require("./net/s3"));
const to_snake_case_1 = __importDefault(require("./utils/to-snake-case"));
// biome-ignore format: keep comments aligned
var DocumentStatus;
(function (DocumentStatus) {
DocumentStatus["INITIALIZED"] = "initialized";
DocumentStatus["ANALYZING"] = "analyzing";
DocumentStatus["PAUSED"] = "paused";
DocumentStatus["READY"] = "ready";
DocumentStatus["TRANSLATING"] = "translating";
DocumentStatus["TRANSLATED"] = "translated";
DocumentStatus["ERROR"] = "error";
})(DocumentStatus || (exports.DocumentStatus = DocumentStatus = {}));
class Documents {
constructor(client) {
this.client = client;
this.s3Client = (0, s3_1.default)();
}
async upload(file, filename, source, target, options) {
const { url, fields } = await this.client.get(`/documents/upload-url`, { filename });
await this.s3Client.upload(url, fields, file);
const headers = (options === null || options === void 0 ? void 0 : options.noTrace) ? { "X-No-Trace": "true" } : {};
return this.client.post("/documents", {
source,
target,
s3key: fields.key,
adapt_to: options === null || options === void 0 ? void 0 : options.adaptTo,
glossaries: options === null || options === void 0 ? void 0 : options.glossaries,
style: options === null || options === void 0 ? void 0 : options.style,
password: options === null || options === void 0 ? void 0 : options.password,
extraction_params: (options === null || options === void 0 ? void 0 : options.extractionParams) ? (0, to_snake_case_1.default)(options.extractionParams) : undefined
}, undefined, headers);
}
async status(id) {
return await this.client.get(`/documents/${id}`);
}
async download(id, options) {
const { url } = await this.client.get(`/documents/${id}/download-url`, {
output_format: options === null || options === void 0 ? void 0 : options.outputFormat
});
return await this.s3Client.download(url);
}
async translate(file, filename, source, target, options) {
const uploadOptions = {
adaptTo: options === null || options === void 0 ? void 0 : options.adaptTo,
glossaries: options === null || options === void 0 ? void 0 : options.glossaries,
noTrace: options === null || options === void 0 ? void 0 : options.noTrace,
style: options === null || options === void 0 ? void 0 : options.style,
password: options === null || options === void 0 ? void 0 : options.password,
extractionParams: options === null || options === void 0 ? void 0 : options.extractionParams
};
const { id } = await this.upload(file, filename, source, target, uploadOptions);
const downloadOptions = (options === null || options === void 0 ? void 0 : options.outputFormat) ? { outputFormat: options.outputFormat } : undefined;
const pollingInterval = 2000;
const maxWaitTime = 1000 * 60 * 15; // 15 minutes
const start = Date.now();
while (Date.now() - start < maxWaitTime) {
await new Promise((resolve) => setTimeout(resolve, pollingInterval));
const { status, errorReason } = await this.status(id);
if (status === DocumentStatus.TRANSLATED)
return await this.download(id, downloadOptions);
if (status === DocumentStatus.ERROR) {
throw new errors_1.LaraApiError(500, "DocumentError", errorReason);
}
}
throw new errors_1.TimeoutError();
}
}
exports.Documents = Documents;