UNPKG

@translated/lara

Version:

Official Lara SDK for JavaScript and Node.js

99 lines (98 loc) 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Glossaries = void 0; const errors_1 = require("./errors"); class Glossaries { constructor(client) { this.client = client; this.pollingInterval = 2000; } async list() { return await this.client.get("/v2/glossaries"); } async create(name) { return await this.client.post("/v2/glossaries", { name }); } async get(id) { try { return await this.client.get(`/v2/glossaries/${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/glossaries/${id}`); } async update(id, name) { return await this.client.put(`/v2/glossaries/${id}`, { name }); } async importCsv(id, csv, gzipOrContentType, maybeGzipOrCallbackUrl, maybeCallbackUrl) { // Default values when no content type or gzip flag is provided let gzip = false; let contentType = "csv/table-uni"; let callbackUrl; if (typeof gzipOrContentType === "boolean") { // First overload: (id, csv, gzip, callbackUrl) gzip = gzipOrContentType; callbackUrl = typeof maybeGzipOrCallbackUrl === "string" ? maybeGzipOrCallbackUrl : undefined; } else if (typeof gzipOrContentType === "string") { // Second overload: (id, csv, contentType, gzip, callbackUrl) contentType = gzipOrContentType; gzip = typeof maybeGzipOrCallbackUrl === "boolean" ? maybeGzipOrCallbackUrl : false; callbackUrl = maybeCallbackUrl; } return await this.client.post(`/v2/glossaries/${id}/import`, { compression: gzip ? "gzip" : undefined, content_type: contentType, callback_url: callbackUrl }, { csv }); } async getImportStatus(id) { return await this.client.get(`/v2/glossaries/imports/${id}`); } async waitForImport(gImport, updateCallback, maxWaitTime) { const start = Date.now(); while (gImport.progress < 1.0) { if (maxWaitTime && Date.now() - start > maxWaitTime) throw new errors_1.TimeoutError(); await new Promise((resolve) => setTimeout(resolve, this.pollingInterval)); gImport = await this.getImportStatus(gImport.id); if (updateCallback) updateCallback(gImport); } return gImport; } async counts(id) { return await this.client.get(`/v2/glossaries/${id}/counts`); } async export(id, contentType, source) { return await this.client.get(`/v2/glossaries/${id}/export`, { content_type: contentType, source }); } async exportAsync(id, callbackUrl, contentType, source) { return await this.client.get(`/v2/glossaries/${id}/export/async`, { callback_url: callbackUrl, content_type: contentType, source }); } async addOrReplaceEntry(id, terms, guid) { return await this.client.put(`/v2/glossaries/${id}/content`, { terms, guid }); } async deleteEntry(id, term, guid) { return await this.client.delete(`/v2/glossaries/${id}/content`, undefined, { term, guid }); } } exports.Glossaries = Glossaries;