UNPKG

qlik-saas-api

Version:

Interact with Qlik Sense SaaS REST API

1,082 lines (1,058 loc) 226 kB
import { QlikFormData, QlikSaaSClient } from 'qlik-rest-api'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __classPrivateFieldGet(receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); } function __classPrivateFieldSet(receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; class URLBuild { constructor(url) { this.params = []; this.url = url; } addParam(name, value) { if (value) this.params.push(`${name}=${encodeURIComponent(value)}`); } getParams() { return this.params.join("&"); } getUrl() { if (this.params.length == 0) return this.url; this.url += `?${this.getParams()}`; return this.url; } } var _Media_id, _Media_saasClient; class Media { constructor(saasClient, id, details) { var _a; _Media_id.set(this, void 0); _Media_saasClient.set(this, void 0); if (!id) throw new Error(`app.get: "id" parameter is required`); __classPrivateFieldSet(this, _Media_id, id, "f"); __classPrivateFieldSet(this, _Media_saasClient, saasClient, "f"); this.details = details !== null && details !== void 0 ? details : {}; this.shortLink = (_a = this.details.link.replace("/api/v1/", "")) !== null && _a !== void 0 ? _a : ""; } content() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _Media_saasClient, "f") .Get(this.shortLink, "", "arraybuffer") .then((res) => res.data); }); } remove() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _Media_saasClient, "f") .Delete(this.shortLink) .then((res) => res.status); }); } update(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.content) throw new Error(`appMedia.update: "content" parameter is required`); return yield __classPrivateFieldGet(this, _Media_saasClient, "f") .Put(this.shortLink, arg.content, "application/octet-stream") .then((res) => res.status); }); } } _Media_id = new WeakMap(), _Media_saasClient = new WeakMap(); var _AppEvaluation_id, _AppEvaluation_saasClient; class AppEvaluation { constructor(saasClient, id, details) { var _a; _AppEvaluation_id.set(this, void 0); _AppEvaluation_saasClient.set(this, void 0); if (!id) throw new Error(`evaluations.get: "id" parameter is required`); __classPrivateFieldSet(this, _AppEvaluation_id, id, "f"); __classPrivateFieldSet(this, _AppEvaluation_saasClient, saasClient, "f"); this.details = details !== null && details !== void 0 ? details : {}; if (details && details.ID && !details.id) this.details.id = (_a = this.details.ID) !== null && _a !== void 0 ? _a : ""; } init() { return __awaiter(this, void 0, void 0, function* () { if (!this.details || Object.keys(this.details).length == 0) { this.details = yield __classPrivateFieldGet(this, _AppEvaluation_saasClient, "f") .Get(`/app/evaluations/${__classPrivateFieldGet(this, _AppEvaluation_id, "f")}`) .then((res) => res.data); } }); } /** * Download a detailed XML log of a specific evaluation */ download() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _AppEvaluation_saasClient, "f") .Get(`apps/evaluations/${__classPrivateFieldGet(this, _AppEvaluation_id, "f")}/actions/download`) .then((res) => res.data); }); } /** * Compare the base evaluation against the provided evaluation id * * Returns the comparison result AND downloads the log, in XML format */ compare(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.comparisonId) throw new Error(`evaluation.compare: "comparisonId" parameter is required`); const [result, log] = yield Promise.all([ __classPrivateFieldGet(this, _AppEvaluation_saasClient, "f") .Get(`apps/evaluations/${__classPrivateFieldGet(this, _AppEvaluation_id, "f")}/actions/compare/${arg.comparisonId}?all=true&format=json`) .then((res) => res.data), __classPrivateFieldGet(this, _AppEvaluation_saasClient, "f") .Get(`apps/evaluations/${__classPrivateFieldGet(this, _AppEvaluation_id, "f")}/actions/compare/${arg.comparisonId}/actions/download`) .then((res) => res.data), ]); return { result, log }; }); } } _AppEvaluation_id = new WeakMap(), _AppEvaluation_saasClient = new WeakMap(); /** * Convert custom search statements to JS statement string. * Custom search statements format/examples: * - id eq 123-123-123-13 * - name eq "some name" and description eq "something else" * * @param filter the actual filter string * @param baseObjPath the path of the origin object. This path will be included in the final JS statement * @returns string based JS statement */ function parseFilter(filter, baseObjPath // data: {} ) { // if (!filter) throw new Error(""); // split the conditions on "and" and "or" // - ignore cases: AND = and = And etc. // - can be preceded or followed by any number of spaces const r = new RegExp(/(?:\s*and\s* | \s*or\s* )+/, "ig"); // use the above regex to get list of all conditions const conditions = filter.split(r); // @ts-ignore: Object is possibly 'null'. const r1 = r.test(filter) ? filter.match(r).map((a) => a.toLowerCase()) : []; // prepare the conditions - separate the property, operation and value const parsedConditions = conditions.map((condition) => { const f = filter.split(" "); return { property: f[0], operation: f[1], value: f.slice(2).join(" "), }; }); // the final JS filter statement // const jsFilter = constructFilter(parsedConditions, r1, baseObjPath); return constructFilter(parsedConditions, r1, baseObjPath); // return Function(`return arguments[0].filter(a => ${jsFilter})`)(data); } // convert the parsed filter condition to a JS statement function constructFilter(conditions, operations, baseObjPath) { const c = { // equal eq: (p, v) => `${p} == ${v}`, // not equal ne: (p, v) => `${p} != ${v}`, // starts with sw: (p, v) => `${p}.startsWith(${v})`, // ends with ew: (p, v) => `${p}.endsWith(${v})`, // substring of so: (p, v) => `${p}.includes(${v})`, // grater than gt: (p, v) => `${p} > ${v}`, // greater than or equal ge: (p, v) => `${p} >= ${v}`, // less than lt: (p, v) => `${p} < ${v}`, // less than or equal le: (p, v) => `${p} <= ${v}`, }; const op = { and: "&&", or: "||", " and ": "&&", " or ": "||", " and": "&&", " or": "||", "and ": "&&", "or ": "||", }; return conditions .map((c1, i) => { var _a; if (!c[c1.operation]) throw new Error(`"${c1.operation}" is not a valid filter operation.`); if (isNaN(Number(c1.value)) && (!c1.value.startsWith(`"`) || !c1.value.endsWith(`"`)) && (!c1.value.startsWith(`'`) || !c1.value.endsWith(`'`))) throw new Error(`Error while parsing filter value. The value is an instance of a string but its not surrounded by double/single-quotes. Provided value was: ${c1.value}`); return `${baseObjPath}.${c[c1.operation](c1.property, c1.value)} ${(_a = op[operations[i]]) !== null && _a !== void 0 ? _a : ""} `; }) .join(""); } var _AppEvaluations_saasClient; class AppEvaluations { constructor(saasClient, appId) { _AppEvaluations_saasClient.set(this, void 0); __classPrivateFieldSet(this, _AppEvaluations_saasClient, saasClient, "f"); this.appId = appId; } getAll() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _AppEvaluations_saasClient, "f") .Get(`/apps/${this.appId}/evaluations?all=true&limit=50`) .then((res) => res.data) .then((data) => data.map((t) => { var _a; return new AppEvaluation(__classPrivateFieldGet(this, _AppEvaluations_saasClient, "f"), (_a = t.id) !== null && _a !== void 0 ? _a : t.ID, t); })); }); } getFilter(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`evaluations.getFilter: "filter" parameter is required`); return yield this.getAll().then((entities) => { const anonFunction = Function("entities", `return entities.filter(f => ${parseFilter(arg.filter, "f.details")})`); return anonFunction(entities); }); }); } create() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _AppEvaluations_saasClient, "f") .Post(`/apps/${this.appId}/evaluations`, {}) .then((res) => { var _a; return new AppEvaluation(__classPrivateFieldGet(this, _AppEvaluations_saasClient, "f"), (_a = res.data.id) !== null && _a !== void 0 ? _a : res.data.ID, res.data); }); }); } } _AppEvaluations_saasClient = new WeakMap(); var _Reload_id, _Reload_saasClient; class Reload { constructor(saasClient, id, details) { _Reload_id.set(this, void 0); _Reload_saasClient.set(this, void 0); if (!id) throw new Error(`app.get: "id" parameter is required`); this.details = details !== null && details !== void 0 ? details : {}; __classPrivateFieldSet(this, _Reload_id, id, "f"); __classPrivateFieldSet(this, _Reload_saasClient, saasClient, "f"); } init() { return __awaiter(this, void 0, void 0, function* () { if (!this.details || Object.keys(this.details).length == 0) { this.details = yield __classPrivateFieldGet(this, _Reload_saasClient, "f") .Get(`reloads/${__classPrivateFieldGet(this, _Reload_id, "f")}`) .then((res) => res.data); } }); } cancel(arg) { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _Reload_saasClient, "f") .Post(`reloads/${__classPrivateFieldGet(this, _Reload_id, "f")}/actions/cancel`, {}) .then((res) => res.status); }); } } _Reload_id = new WeakMap(), _Reload_saasClient = new WeakMap(); var _Reloads_saasClient; class Reloads { constructor(saasClient) { _Reloads_saasClient.set(this, void 0); __classPrivateFieldSet(this, _Reloads_saasClient, saasClient, "f"); } get(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.id) throw new Error(`reloads.get: "id" parameter is required`); const reload = new Reload(__classPrivateFieldGet(this, _Reloads_saasClient, "f"), arg.id); yield reload.init(); return reload; }); } getAll(arg) { return __awaiter(this, void 0, void 0, function* () { const urlBuild = new URLBuild(`reloads`); urlBuild.addParam("limit", "50"); urlBuild.addParam("log", `${arg === null || arg === void 0 ? void 0 : arg.log}`); const url = urlBuild.getUrl(); return yield __classPrivateFieldGet(this, _Reloads_saasClient, "f") .Get(url) .then((res) => res.data) .then((data) => data.map((t) => new Reload(__classPrivateFieldGet(this, _Reloads_saasClient, "f"), t.id, t))); }); } getFilter(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`reloads.getFilter: "filter" parameter is required`); const urlBuild = new URLBuild(`reloads`); urlBuild.addParam("limit", 50); urlBuild.addParam("filter", arg.filter); urlBuild.addParam("log", arg.log); const url = urlBuild.getUrl(); return yield __classPrivateFieldGet(this, _Reloads_saasClient, "f") .Get(url) .then((res) => res.data) .then((data) => data.map((t) => new Reload(__classPrivateFieldGet(this, _Reloads_saasClient, "f"), t.id, t))); }); } start(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.appId) throw new Error(`reloads.start: "appId" parameter is required`); return yield __classPrivateFieldGet(this, _Reloads_saasClient, "f") .Post(`reloads`, { appId: arg.appId, partial: arg.partial }) .then((res) => new Reload(__classPrivateFieldGet(this, _Reloads_saasClient, "f"), res.data.id, res.data)); }); } } _Reloads_saasClient = new WeakMap(); var _ReloadTask_id, _ReloadTask_saasClient; class ReloadTask { constructor(saasClient, id, details) { _ReloadTask_id.set(this, void 0); _ReloadTask_saasClient.set(this, void 0); if (!id) throw new Error(`app.get: "id" parameter is required`); this.details = details !== null && details !== void 0 ? details : {}; __classPrivateFieldSet(this, _ReloadTask_id, id, "f"); __classPrivateFieldSet(this, _ReloadTask_saasClient, saasClient, "f"); } init(arg) { return __awaiter(this, void 0, void 0, function* () { if (!this.details || Object.keys(this.details).length == 0 || (arg === null || arg === void 0 ? void 0 : arg.force) == true) { this.details = yield __classPrivateFieldGet(this, _ReloadTask_saasClient, "f") .Get(`reload-tasks/${__classPrivateFieldGet(this, _ReloadTask_id, "f")}`) .then((res) => res.data); } }); } remove() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _ReloadTask_saasClient, "f") .Delete(`reload-tasks/${__classPrivateFieldGet(this, _ReloadTask_id, "f")}`) .then((res) => res.status); }); } update(arg) { return __awaiter(this, void 0, void 0, function* () { arg["appId"] = this.details.appId; if (!arg.partial) arg.partial = this.details.partial; if (!arg.autoReload) arg.autoReload = this.details.autoReload; if (!arg.autoReloadPartial) arg.autoReloadPartial = this.details.autoReloadPartial; if (!arg.startDateTime) arg.startDateTime = this.details.startDateTime; if (!arg.endDateTime) arg.endDateTime = this.details.endDateTime || ""; if (!arg.timeZone) arg.timeZone = this.details.timeZone || "Europe/London"; if (!arg.recurrence) arg.recurrence = this.details.recurrence; // if (!arg.state) arg.state = this.details.state; let updateStatus = 0; return yield __classPrivateFieldGet(this, _ReloadTask_saasClient, "f") .Put(`reload-tasks/${__classPrivateFieldGet(this, _ReloadTask_id, "f")}`, arg) .then((res) => { updateStatus = res.status; return this.init({ force: true }); }) .then(() => updateStatus); }); } } _ReloadTask_id = new WeakMap(), _ReloadTask_saasClient = new WeakMap(); var _ReloadTasks_saasClient; class ReloadTasks { constructor(saasClient) { _ReloadTasks_saasClient.set(this, void 0); __classPrivateFieldSet(this, _ReloadTasks_saasClient, saasClient, "f"); } get(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.id) throw new Error(`reloadTasks.get: "id" parameter is required`); const rt = new ReloadTask(__classPrivateFieldGet(this, _ReloadTasks_saasClient, "f"), arg.id); yield rt.init(); return rt; }); } getAll() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _ReloadTasks_saasClient, "f") .Get(`reload-tasks?limit=50`) .then((res) => res.data) .then((data) => data.map((t) => new ReloadTask(__classPrivateFieldGet(this, _ReloadTasks_saasClient, "f"), t.id, t))); }); } getFilter(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`reloadTasks.getFilter: "filter" parameter is required`); return yield this.getAll().then((entities) => { const anonFunction = Function("entities", `return entities.filter(f => ${parseFilter(arg.filter, "f.details")})`); return anonFunction(entities); }); }); } removeFilter(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`reloadTasks.removeFilter: "filter" parameter is required`); return yield this.getFilter(arg).then((entities) => Promise.all(entities.map((entity) => entity.remove().then((s) => ({ id: entity.details.id, status: s }))))); }); } create(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.appId) throw new Error(`reloadTasks.create: "appId" parameter is required`); if (!arg.recurrence) throw new Error(`reloadTasks.create: "recurrence" parameter is required`); if (!arg.startDateTime) throw new Error(`reloadTasks.create: "startDateTime" parameter is required`); if (!arg.partial) arg["partial"] = false; if (!arg.autoReload) arg["autoReload"] = false; if (!arg.autoReloadPartial) arg["autoReloadPartial"] = false; if (!arg.endDateTime) arg["endDateTime"] = ""; if (!arg.timeZone) arg["timeZone"] = "Europe/London"; arg["state"] = "Enabled"; arg["type"] = "scheduled_reload"; return yield __classPrivateFieldGet(this, _ReloadTasks_saasClient, "f") .Post(`reload-tasks`, arg) .then((res) => new ReloadTask(__classPrivateFieldGet(this, _ReloadTasks_saasClient, "f"), res.data.id, res.data)); }); } } _ReloadTasks_saasClient = new WeakMap(); var _AppActions_id, _AppActions_saasClient; class AppActions { constructor(saasClient, id) { _AppActions_id.set(this, void 0); _AppActions_saasClient.set(this, void 0); __classPrivateFieldSet(this, _AppActions_id, id, "f"); __classPrivateFieldSet(this, _AppActions_saasClient, saasClient, "f"); this.reloadTasks = new ReloadTasks(__classPrivateFieldGet(this, _AppActions_saasClient, "f")); this.appReload = new Reloads(__classPrivateFieldGet(this, _AppActions_saasClient, "f")); } /** * Create scheduled reload task for the app */ createReloadTask(arg) { return __awaiter(this, void 0, void 0, function* () { yield this.reloadTasks.create(Object.assign({ appId: __classPrivateFieldGet(this, _AppActions_id, "f") }, arg)); return true; }); } /** * Get list of all scheduled reloads for the app */ getReloadTasks() { return __awaiter(this, void 0, void 0, function* () { return yield this.reloadTasks .getAll() .then((allTasks) => allTasks.filter((r) => r.details.appId == __classPrivateFieldGet(this, _AppActions_id, "f"))); }); } /** * Reloads the app */ reload(arg) { return __awaiter(this, void 0, void 0, function* () { if (arg && !arg.partial) throw new Error(`app.reload: argument object is passed but "partial" property is missing`); return yield this.appReload.start({ appId: __classPrivateFieldGet(this, _AppActions_id, "f"), partial: (arg === null || arg === void 0 ? void 0 : arg.partial) || false, }); }); } changeObjectOwner(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.objectId) throw new Error(`app.changeObjectOwner: "objectId" parameter is required`); if (!arg.ownerId) throw new Error(`app.changeObjectOwner: "ownerId" parameter is required`); return __classPrivateFieldGet(this, _AppActions_saasClient, "f").Post(`apps/${__classPrivateFieldGet(this, _AppActions_id, "f")}/objects/${arg.objectId}/actions/change-owner`, { ownerId: arg.ownerId }); }); } } _AppActions_id = new WeakMap(), _AppActions_saasClient = new WeakMap(); var _AppScript_id, _AppScript_saasClient; class AppScript { constructor(saasClient, versionId, appId, details) { _AppScript_id.set(this, void 0); _AppScript_saasClient.set(this, void 0); if (!versionId) throw new Error(`"versionId" parameter is required`); this.details = details !== null && details !== void 0 ? details : {}; __classPrivateFieldSet(this, _AppScript_id, versionId, "f"); this.appId = appId; __classPrivateFieldSet(this, _AppScript_saasClient, saasClient, "f"); } init(arg) { return __awaiter(this, void 0, void 0, function* () { if (!this.details || (arg === null || arg === void 0 ? void 0 : arg.force) == true) { this.details = yield __classPrivateFieldGet(this, _AppScript_saasClient, "f") .Get(`apps/${this.appId}/scripts?filter=ScriptId eq "${__classPrivateFieldGet(this, _AppScript_id, "f")}"`) .then((res) => (Object.assign(Object.assign({}, res.data), { script: "" }))); } }); } getScriptContent() { return __awaiter(this, void 0, void 0, function* () { this.details.script = yield __classPrivateFieldGet(this, _AppScript_saasClient, "f") .Get(`apps/${this.appId}/scripts/${__classPrivateFieldGet(this, _AppScript_id, "f")}`) .then((res) => res.data["script"]); return this.details.script; }); } remove() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _AppScript_saasClient, "f") .Delete(`apps/${this.appId}/scripts/${__classPrivateFieldGet(this, _AppScript_id, "f")}`) .then((res) => res.status); }); } /** * Not very clear from the documentation what can be done here * * The UI allows only the name of the version (`versionMessage` ) to be updated. * And because of this, this method only updates the name as well */ update(arg) { return __awaiter(this, void 0, void 0, function* () { const updateStatus = yield __classPrivateFieldGet(this, _AppScript_saasClient, "f") .Patch(`apps/${this.appId}/scripts/${__classPrivateFieldGet(this, _AppScript_id, "f")}`, [ { op: "replace", path: "/versionMessage", value: arg.name, }, ]) .then((res) => res.status); yield this.init({ force: true }); return updateStatus; }); } } _AppScript_id = new WeakMap(), _AppScript_saasClient = new WeakMap(); var _AppReportFilter_id, _AppReportFilter_saasClient; class AppReportFilter { constructor(saasClient, reportFilterId, appId, details) { _AppReportFilter_id.set(this, void 0); _AppReportFilter_saasClient.set(this, void 0); if (!appId) throw new Error(`"appId" parameter is required`); this.details = details !== null && details !== void 0 ? details : {}; __classPrivateFieldSet(this, _AppReportFilter_id, reportFilterId, "f"); this.appId = appId; __classPrivateFieldSet(this, _AppReportFilter_saasClient, saasClient, "f"); } init(arg) { return __awaiter(this, void 0, void 0, function* () { if (!this.details || (arg === null || arg === void 0 ? void 0 : arg.force) == true) { this.details = yield __classPrivateFieldGet(this, _AppReportFilter_saasClient, "f") .Get(`apps/${this.appId}/report-filters/${__classPrivateFieldGet(this, _AppReportFilter_id, "f")}`) .then((res) => (Object.assign(Object.assign({}, res.data), { script: "" }))); } }); } remove() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _AppReportFilter_saasClient, "f") .Delete(`apps/${this.appId}/report-filters/${__classPrivateFieldGet(this, _AppReportFilter_id, "f")}`) .then((res) => res.status); }); } } _AppReportFilter_id = new WeakMap(), _AppReportFilter_saasClient = new WeakMap(); var _AppReportFilters_saasClient; class AppReportFilters { constructor(saasClient, appId) { _AppReportFilters_saasClient.set(this, void 0); __classPrivateFieldSet(this, _AppReportFilters_saasClient, saasClient, "f"); this.appId = appId; } getAll() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _AppReportFilters_saasClient, "f") .Get(`/apps/${this.appId}/report-filters?limit=50&filterTypes=REP`) .then((res) => res.data) .then((data) => data.map((t) => new AppReportFilter(__classPrivateFieldGet(this, _AppReportFilters_saasClient, "f"), t.id, this.appId, t))); }); } getFilter(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`reportFilters.getFilter: "filter" parameter is required`); return yield this.getAll().then((entities) => { const anonFunction = Function("entities", `return entities.filter(f => ${parseFilter(arg.filter, "f.details")})`); return anonFunction(entities); }); }); } create(arg) { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _AppReportFilters_saasClient, "f") .Post(`/apps/${this.appId}/report-filters`, arg) .then((res) => new AppReportFilter(__classPrivateFieldGet(this, _AppReportFilters_saasClient, "f"), res.data.id, this.appId, res.data)); }); } count(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg || !arg.filterType) throw new Error(`reportFilters.count: "filterType" parameter is required`); if (arg.filterType != "REP" && arg.filterType != "SUB") throw new Error(`reportFilters.count: unknown "filterType" value. Allowed are "REP" or "SUB"`); return yield __classPrivateFieldGet(this, _AppReportFilters_saasClient, "f") .Get(`/apps/${this.appId}/report-filters/actions/count?filterTypes=${arg.filterType}`) .then((res) => { // seems that the API returns empty object when there are no filters // because of this we have to enforce and return 0 // when "total" is not present in the response data if (res.data && !res.data.total) return 0; return res.data.total; }); }); } } _AppReportFilters_saasClient = new WeakMap(); var _App_id, _App_saasClient; class App { constructor(saasClient, id, details) { var _a, _b, _c; _App_id.set(this, void 0); _App_saasClient.set(this, void 0); this.swapResourceIdAndId = false; if (!id) throw new Error(`app.get: "id" parameter is required`); if (((_c = (_b = (_a = saasClient.configFull.options) === null || _a === void 0 ? void 0 : _a.saas) === null || _b === void 0 ? void 0 : _b.apps) === null || _c === void 0 ? void 0 : _c.swapResourceIdAndId) == true) this.swapResourceIdAndId = true; this.details = details !== null && details !== void 0 ? details : {}; __classPrivateFieldSet(this, _App_id, id, "f"); __classPrivateFieldSet(this, _App_saasClient, saasClient, "f"); this.evaluations = new AppEvaluations(__classPrivateFieldGet(this, _App_saasClient, "f"), __classPrivateFieldGet(this, _App_id, "f")); this.reportFilters = new AppReportFilters(__classPrivateFieldGet(this, _App_saasClient, "f"), __classPrivateFieldGet(this, _App_id, "f")); this._actions = new AppActions(__classPrivateFieldGet(this, _App_saasClient, "f"), __classPrivateFieldGet(this, _App_id, "f")); // if we have to swap id and resourceId properties if (this.swapResourceIdAndId == true && Object.keys(this.details).length > 0) [this.details["id"], this.details["resourceId"]] = [ this.details["resourceId"], this.details["id"], ]; } init(arg) { return __awaiter(this, void 0, void 0, function* () { if (!this.details || Object.keys(this.details).length == 0 || (arg === null || arg === void 0 ? void 0 : arg.force) == true) { this.details = yield __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`items?resourceType=app&resourceId=${__classPrivateFieldGet(this, _App_id, "f")}`) .then((res) => { if (res.data.length == 0) throw new Error(`apps.get: app with id ${__classPrivateFieldGet(this, _App_id, "f")} was not found`); // if we have to swap id and resourceId properties if (this.swapResourceIdAndId == true) [res.data[0]["id"], res.data[0]["resourceId"]] = [ res.data[0]["resourceId"], res.data[0]["id"], ]; return res.data[0]; }); } }); } copy(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.name) throw new Error(`app.copy: "name" parameter is required`); return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Post(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/copy`, arg) .then((res) => new App(__classPrivateFieldGet(this, _App_saasClient, "f"), res.data.resourceId, res.data)); }); } dataLineage() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/data/lineage`) .then((res) => res.data); }); } metaData() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/data/metadata`) .then((res) => res.data); }); } export(arg) { return __awaiter(this, void 0, void 0, function* () { const urlBuild = new URLBuild(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/export`); urlBuild.addParam("NoData", arg === null || arg === void 0 ? void 0 : arg.noData); const tempContentLocation = yield __classPrivateFieldGet(this, _App_saasClient, "f").Post(urlBuild.getUrl(), {}, "application/json", "json", false, true); const appContent = yield __classPrivateFieldGet(this, _App_saasClient, "f") .Get(tempContentLocation.data.location.replace("/api/v1/", ""), "", "arraybuffer") .then((a) => a.data); return { file: appContent, id: `${__classPrivateFieldGet(this, _App_id, "f")}`, name: `${this.details.name}.qvf`, }; }); } /** * IMPORTANT! This method to is to publish apps to **MANAGED** spaces only. * For shared spaces please use `addToSpace` method */ publish(arg) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { let data = {}; data = { spaceId: arg.spaceId, attributes: { name: (_a = arg.appName) !== null && _a !== void 0 ? _a : this.details.name, description: (_b = arg.description) !== null && _b !== void 0 ? _b : "", }, moveApp: false, data: "source", }; if (arg.data) data["data"] = arg.data; if (arg.description) data.attributes["description"] = arg.description; if (arg.moveApp) data.moveApp = arg.moveApp; if (arg.originAppId) data.originalAppId = arg.originAppId; if (arg.originAppId && !arg.moveApp) throw new Error(`apps.publish: If app is moved, originAppId needs to be provided.`); return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Post(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/publish`, data) .then((res) => __classPrivateFieldGet(this, _App_saasClient, "f").Get(`items?resourceType=app&resourceId=${res.data.attributes.id}`)) .then((items) => new App(__classPrivateFieldGet(this, _App_saasClient, "f"), items.data[0].resourceId, items.data[0])); }); } /** * IMPORTANT! This method to is to re-publish apps to **MANAGED** spaces only. * For shared spaces please use `addToSpace` method */ rePublish(arg) { var _a; return __awaiter(this, void 0, void 0, function* () { let data = {}; data = { targetId: arg.targetId, data: "source", attributes: { name: arg.appName, description: (_a = arg.description) !== null && _a !== void 0 ? _a : "", }, checkOriginAppId: true, }; if (arg.data) data["data"] = arg.data; if (arg.description) data.attributes["description"] = arg.description; if (arg.checkOriginAppId) data["checkOriginAppId"] = arg.checkOriginAppId; return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Put(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/publish`, data) .then((res) => __classPrivateFieldGet(this, _App_saasClient, "f").Get(`items?resourceType=app&resourceId=${res.data.attributes.id}`)) .then((items) => new App(__classPrivateFieldGet(this, _App_saasClient, "f"), items.data[0].resourceId, items.data[0])); }); } // REVIEW: the name? addToSpace(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.spaceId) throw new Error(`app.addToSpace: "spaceId" parameter is required`); return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Put(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/space`, { spaceId: arg.spaceId }) .then((res) => __classPrivateFieldGet(this, _App_saasClient, "f").Get(`items?resourceType=app&resourceId=${res.data.attributes.id}`)) .then((res) => { this.details = res.data[0]; return res.status; }); }); } // REVIEW: the name? removeFromSpace() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Delete(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/space`) .then((res) => __classPrivateFieldGet(this, _App_saasClient, "f").Get(`items?resourceType=app&resourceId=${__classPrivateFieldGet(this, _App_id, "f")}`)) .then((res) => { this.details = res.data[0]; return res.status; }); }); } remove() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Delete(`apps/${__classPrivateFieldGet(this, _App_id, "f")}`) .then((res) => res.status); }); } update(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.name) throw new Error(`app.update: "name" parameter is required`); let updateStatus = 0; return __classPrivateFieldGet(this, _App_saasClient, "f") .Put(`apps/${__classPrivateFieldGet(this, _App_id, "f")}`, { attributes: arg, }) .then((res) => { updateStatus = res.status; return this.init({ force: true }); }) .then(() => updateStatus); }); } thumbnail() { return __awaiter(this, void 0, void 0, function* () { return __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/media/thumbnail`) .then((res) => res.data) .catch((e) => { if (e.message.indexOf("404") > -1) throw new Error(`app.thumbnail: thumbnail file is not found`); throw new Error(e); }); }); } mediaFiles() { return __awaiter(this, void 0, void 0, function* () { return __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/media/list`) .then((res) => { return res.data.map((m) => new Media(__classPrivateFieldGet(this, _App_saasClient, "f"), m.id, m)); }); }); } addMedia(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.content) throw new Error(`app.addMedia: "content" parameter is required`); if (!arg.fileName) throw new Error(`app.addMedia: "fileName" parameter is required`); return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Put(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/media/files/${arg.fileName}`, arg.content, "application/octet-stream") .then((res) => new Media(__classPrivateFieldGet(this, _App_saasClient, "f"), res.data.id, res.data)); }); } /** // * List of all script versions // * // * To reduce the number of API calls the actual script content is initially left empty // * Call `getScriptContent()` for each version. // * // * Rate limit: Tier 1 (600 requests per minute) // */ scriptVersions() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/scripts`) .then((res) => res.data) .then((data) => { return data.scripts.map((t) => new AppScript(__classPrivateFieldGet(this, _App_saasClient, "f"), t.scriptId, __classPrivateFieldGet(this, _App_id, "f"), Object.assign(Object.assign({}, t), { script: "" }))); }); }); } /** * Get all details (including the script) for a specific script version * * Rate limit: Tier 1 (600 requests per minute) */ scriptVersion(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.versionId) throw new Error(`app.scriptVersions: "versionId" parameter is required`); const scriptVersion = new AppScript(__classPrivateFieldGet(this, _App_saasClient, "f"), arg.versionId, __classPrivateFieldGet(this, _App_id, "f")); yield scriptVersion.init(); yield scriptVersion.getScriptContent(); return scriptVersion; }); } /** * Set the app script and create new script version * * Rate limit: Tier 2 (60 requests per minute) */ setScript(arg) { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _App_saasClient, "f") .Post(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/scripts`, arg) .then((res) => res.status); }); } /** // * List of reload logs (actual log is not included) // */ reloadLogs() { return __awaiter(this, void 0, void 0, function* () { return __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/reloads/logs`) .then((res) => res.data); }); } /** * Returns the reload log content for the specified reloadId */ reloadLogContent(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.reloadId) throw new Error(`app.reloadLogContent: "reloadId" parameter is required`); return __classPrivateFieldGet(this, _App_saasClient, "f") .Get(`apps/${__classPrivateFieldGet(this, _App_id, "f")}/reloads/logs/${arg.reloadId}`) .then((res) => res.data); }); } } _App_id = new WeakMap(), _App_saasClient = new WeakMap(); var _Apps_saasClient; class Apps { constructor(saasClient) { _Apps_saasClient.set(this, void 0); __classPrivateFieldSet(this, _Apps_saasClient, saasClient, "f"); } get(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.id) throw new Error(`apps.get: "id" parameter is required`); const app = new App(__classPrivateFieldGet(this, _Apps_saasClient, "f"), arg.id); yield app.init(); return app; }); } getEvaluation(arg) { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _Apps_saasClient, "f") .Get(`apps/evaluations/${arg.id}`) .then((res) => res.data) .then((data) => { var _a; return new AppEvaluation(__classPrivateFieldGet(this, _Apps_saasClient, "f"), (_a = (data.id || data.ID)) !== null && _a !== void 0 ? _a : "", data); }); }); } getAll() { return __awaiter(this, void 0, void 0, function* () { return yield __classPrivateFieldGet(this, _Apps_saasClient, "f") .Get(`items?resourceType=app&limit=50`) .then((res) => res.data) .then((data) => { return data.map((t) => new App(__classPrivateFieldGet(this, _Apps_saasClient, "f"), t.resourceId, t)); }); }); } getFilter(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`apps.getFilter: "filter" parameter is required`); return yield this.getAll().then((entities) => { const anonFunction = Function("entities", `return entities.filter(f => ${parseFilter(arg.filter, "f.details")})`); return anonFunction(entities); }); }); } getFilterNative(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`apps.getFilter: "filter" parameter is required`); return yield __classPrivateFieldGet(this, _Apps_saasClient, "f") .Get(`items?resourceType=app,qvapp,qlikview&query=${arg.filter}`) .then((res) => res.data) .then((data) => { return data.map((t) => new App(__classPrivateFieldGet(this, _Apps_saasClient, "f"), t.id, t)); }); }); } removeFilterNative(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`apps.removeFilter: "filter" parameter is required`); const apps = yield this.getFilter({ filter: arg.filter }); return Promise.all(apps.map((app) => app.remove().then((s) => ({ id: app.details.id, status: s })))); }); } removeFilter(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.filter) throw new Error(`apps.removeFilter: "filter" parameter is required`); return yield this.getFilter(arg).then((entities) => Promise.all(entities.map((entity) => entity .remove() .then((s) => ({ id: entity.details.resourceId, status: s }))))); }); } import(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.file) throw new Error(`apps.import: "file" parameter is required`); const urlBuild = new URLBuild(`apps/import`); urlBuild.addParam("name", arg.name); urlBuild.addParam("spaceId", arg.spaceId); urlBuild.addParam("mode", arg.mode); urlBuild.addParam("mode", arg.appId); urlBuild.addParam("fallbackName", arg.fallbackName); return yield __classPrivateFieldGet(this, _Apps_saasClient, "f") .Post(urlBuild.getUrl(), arg.file, "application/octet-stream") .then((res) => __classPrivateFieldGet(this, _Apps_saasClient, "f").Get(`/items?resourceType=app&resourceId=${res.data.attributes.id}`)) .then((res) => new App(__classPrivateFieldGet(this, _Apps_saasClient, "f"), res.data[0].resourceId, res.data[0])); }); } create(arg) { return __awaiter(this, void 0, void 0, function* () { if (!arg.name) throw new Error(`apps.create: "name" parameter is required`);