@translated/lara
Version:
Official Lara SDK for JavaScript and Node.js
102 lines (101 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Memories = void 0;
const errors_1 = require("./errors");
class Memories {
constructor(client) {
this.client = client;
this.pollingInterval = 2000;
}
async list() {
return await this.client.get("/memories");
}
async create(name, externalId) {
return await this.client.post("/memories", {
name,
external_id: externalId
});
}
async get(id) {
try {
return await this.client.get(`/memories/${id}`);
}
catch (e) {
if (e instanceof errors_1.LaraApiError && e.statusCode === 404) {
return null;
}
throw e;
}
}
async delete(id) {
return await this.client.delete(`/memories/${id}`);
}
async update(id, name) {
return await this.client.put(`/memories/${id}`, { name });
}
async connect(ids) {
const memories = await this.client.post("/memories/connect", {
ids: Array.isArray(ids) ? ids : [ids]
});
return (Array.isArray(ids) ? memories : memories[0]);
}
async importTmx(id, tmx, gzip = false) {
return await this.client.post(`/memories/${id}/import`, {
compression: gzip ? "gzip" : undefined
}, {
tmx
});
}
async addTranslation(id, source, target, sentence, translation, tuid, sentenceBefore, sentenceAfter, headers) {
const body = {
source,
target,
sentence,
translation,
tuid,
sentence_before: sentenceBefore,
sentence_after: sentenceAfter
};
if (Array.isArray(id)) {
body.ids = id;
return await this.client.put("/memories/content", body, undefined, headers);
}
else {
return await this.client.put(`/memories/${id}/content`, body, undefined, headers);
}
}
async deleteTranslation(id, source, target, sentence, translation, tuid, sentenceBefore, sentenceAfter) {
const body = {
source,
target,
sentence,
translation,
tuid,
sentence_before: sentenceBefore,
sentence_after: sentenceAfter
};
if (Array.isArray(id)) {
body.ids = id;
return await this.client.delete("/memories/content", body);
}
else {
return await this.client.delete(`/memories/${id}/content`, body);
}
}
async getImportStatus(id) {
return await this.client.get(`/memories/imports/${id}`);
}
async waitForImport(mImport, updateCallback, maxWaitTime) {
const start = Date.now();
while (mImport.progress < 1.0) {
if (maxWaitTime && Date.now() - start > maxWaitTime)
throw new errors_1.TimeoutError();
await new Promise((resolve) => setTimeout(resolve, this.pollingInterval));
mImport = await this.getImportStatus(mImport.id);
if (updateCallback)
updateCallback(mImport);
}
return mImport;
}
}
exports.Memories = Memories;