UNPKG

@translated/lara

Version:

Official Lara SDK for JavaScript and Node.js

148 lines (147 loc) • 5.22 kB
"use strict"; 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; } /** @deprecated Use listPaginated() instead. */ async list() { return await this.client.get("/v2/memories"); } /** * List resources with optional name filtering and pagination. * Without options, returns the complete list of resources without pagination. * Providing search, page, or size enables pagination; omitted page and size default to 1 and 20. */ async listPaginated(options) { return await this.client.getCollection("/v2/memories", options); } async create(name, externalId) { return await this.client.post("/v2/memories", { name, external_id: externalId }); } async get(id) { try { return await this.client.get(`/v2/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(`/v2/memories/${id}`); } async update(id, name) { return await this.client.put(`/v2/memories/${id}`, { name }); } async connect(ids) { const memories = await this.client.post("/v2/memories/connect", { ids: Array.isArray(ids) ? ids : [ids] }); return (Array.isArray(ids) ? memories : memories[0]); } async getShares(id) { return await this.client.get(`/v2/memories/${id}/shares`); } async addAccountShare(id, name) { return await this.client.post(`/v2/memories/${id}/shares`, { name }); } async revokeAccountShare(id) { return await this.client.delete(`/v2/memories/${id}/shares`); } async renameAccountShare(id, name) { return await this.client.put(`/v2/memories/${id}/shares`, { name }); } async addGroupShare(id, groupId, name) { return await this.client.post(`/v2/memories/${id}/shares/groups/${groupId}`, { name }); } async revokeGroupShare(id, groupId) { return await this.client.delete(`/v2/memories/${id}/shares/groups/${groupId}`); } async renameGroupShare(id, groupId, name) { return await this.client.put(`/v2/memories/${id}/shares/groups/${groupId}`, { name }); } async importTmx(id, tmx, gzipOrCallbackUrl, maybeCallbackUrl) { let gzip = false; let callbackUrl; if (typeof gzipOrCallbackUrl === "boolean") { gzip = gzipOrCallbackUrl; callbackUrl = maybeCallbackUrl; } else if (typeof gzipOrCallbackUrl === "string") { callbackUrl = gzipOrCallbackUrl; } return await this.client.post(`/v2/memories/${id}/import`, { compression: gzip ? "gzip" : undefined, callback_url: callbackUrl }, { tmx }); } async exportAsync(id, callbackUrl, format) { return await this.client.get(`/v2/memories/${id}/export/async`, { callback_url: callbackUrl, format }); } 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("/v2/memories/content", body, undefined, headers); } else { return await this.client.put(`/v2/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("/v2/memories/content", undefined, body); } else { return await this.client.delete(`/v2/memories/${id}/content`, undefined, body); } } async getImportStatus(id) { return await this.client.get(`/v2/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;