@translated/lara
Version:
Official Lara SDK for JavaScript and Node.js
88 lines (87 loc) • 3.01 kB
JavaScript
;
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, maybeGzip) {
// Default values when no content type or gzip flag is provided
let gzip = false;
let contentType = "csv/table-uni";
if (typeof gzipOrContentType === "boolean") {
// First overload: (id, csv, gzip)
gzip = gzipOrContentType;
}
else if (typeof gzipOrContentType === "string") {
// Second overload: (id, csv, contentType, gzip)
contentType = gzipOrContentType;
gzip = maybeGzip !== null && maybeGzip !== void 0 ? maybeGzip : false;
}
return await this.client.post(`/v2/glossaries/${id}/import`, {
compression: gzip ? "gzip" : undefined,
content_type: contentType
}, {
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 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;