UNPKG

@gooddata/gooddata-js

Version:
317 lines (316 loc) • 11.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // (C) 2007-2020 GoodData Corporation var util_1 = require("./util"); var DEFAULT_PALETTE = [ { r: 0x2b, g: 0x6b, b: 0xae }, { r: 0x69, g: 0xaa, b: 0x51 }, { r: 0xee, g: 0xb1, b: 0x4c }, { r: 0xd5, g: 0x3c, b: 0x38 }, { r: 0x89, g: 0x4d, b: 0x94 }, { r: 0x73, g: 0x73, b: 0x73 }, { r: 0x44, g: 0xa9, b: 0xbe }, { r: 0x96, g: 0xbd, b: 0x5f }, { r: 0xfd, g: 0x93, b: 0x69 }, { r: 0xe1, g: 0x5d, b: 0x86 }, { r: 0x7c, g: 0x6f, b: 0xad }, { r: 0xa5, g: 0xa5, b: 0xa5 }, { r: 0x7a, g: 0xa6, b: 0xd5 }, { r: 0x82, g: 0xd0, b: 0x8d }, { r: 0xff, g: 0xd2, b: 0x89 }, { r: 0xf1, g: 0x84, b: 0x80 }, { r: 0xbf, g: 0x90, b: 0xc6 }, { r: 0xbf, g: 0xbf, b: 0xbf }, ]; var isProjectCreated = function (project) { // TODO var projectState = project.content.state; return projectState === "ENABLED" || projectState === "DELETED"; }; // Parses string values to boolean, number and string exports.parseSettingItemValue = function (value) { if (value === "true") { return true; } if (value === "false") { return false; } var nr = Number(value); if (nr.toString() === value) { return nr; } return value; }; /** * Functions for working with projects * * @class project * @module project */ var ProjectModule = /** @class */ (function () { function ProjectModule(xhr) { this.xhr = xhr; } /** * Get current project id * * @method getCurrentProjectId * @return {String} current project identifier */ ProjectModule.prototype.getCurrentProjectId = function () { return this.xhr .get("/gdc/app/account/bootstrap") .then(function (r) { return r.getData(); }) .then(this.getCurrentProjectIdInBootstrap); }; /** * Return current project id in bootstrap * @method getCurrentProjectIdInBootstrap * @param bootstrapData - data was got from bootstrap resource */ ProjectModule.prototype.getCurrentProjectIdInBootstrap = function (bootstrapData) { var currentProject = bootstrapData.bootstrapResource.current.project; // handle situation in which current project is missing (e.g. new user) if (!currentProject) { return null; } return bootstrapData.bootstrapResource.current.project.links.self.split("/").pop(); }; /** * Fetches projects available for the user represented by the given profileId * * @method getProjects * @param {String} profileId - User profile identifier * @return {Array} An Array of projects */ ProjectModule.prototype.getProjects = function (profileId) { return util_1.getAllPagesByOffsetLimit(this.xhr, "/gdc/account/profile/" + profileId + "/projects", "projects").then(function (result) { return result.map(function (p) { return p.project; }); }); }; /** * Fetches all datasets for the given project * * @method getDatasets * @param {String} projectId - GD project identifier * @return {Array} An array of objects containing datasets metadata */ ProjectModule.prototype.getDatasets = function (projectId) { return this.xhr .get("/gdc/md/" + projectId + "/query/datasets") .then(function (r) { return r.getData(); }) .then(util_1.getIn("query.entries")); }; /** * Fetches a chart color palette for a project represented by the given * projectId parameter. * * @method getColorPalette * @param {String} projectId - A project identifier * @return {Array} An array of objects with r, g, b fields representing a project's * color palette */ ProjectModule.prototype.getColorPalette = function (projectId) { return this.xhr .get("/gdc/projects/" + projectId + "/styleSettings") .then(function (r) { return r.getData(); }) .then(function (result) { return result.styleSettings.chartPalette.map(function (c) { return { r: c.fill.r, g: c.fill.g, b: c.fill.b, }; }); }, function (err) { if (err.status === 200) { return DEFAULT_PALETTE; } throw new Error(err.statusText); }); }; /** * Fetches a chart color palette for a project represented by the given * projectId parameter. * * @method getColorPaletteWithGuids * @param {String} projectId - A project identifier * @return {Array} An array of objects representing a project's * color palette with color guid or undefined */ ProjectModule.prototype.getColorPaletteWithGuids = function (projectId) { return this.xhr .get("/gdc/projects/" + projectId + "/styleSettings") .then(function (apiResponse) { return apiResponse.getData(); }) .then(function (result) { if (result && result.styleSettings) { return result.styleSettings.chartPalette; } else { return undefined; } }); }; /** * Sets given colors as a color palette for a given project. * * @method setColorPalette * @param {String} projectId - GD project identifier * @param {Array} colors - An array of colors that we want to use within the project. * Each color should be an object with r, g, b fields. // TODO really object? */ ProjectModule.prototype.setColorPalette = function (projectId, colors) { return this.xhr.put("/gdc/projects/" + projectId + "/styleSettings", { body: { styleSettings: { chartPalette: colors.map(function (fill, idx) { return { fill: fill, guid: "guid" + idx }; }), }, }, }); }; /** * Gets current timezone and its offset. Example output: * * { * id: 'Europe/Prague', * displayName: 'Central European Time', * currentOffsetMs: 3600000 * } * * @method getTimezone * @param {String} projectId - GD project identifier */ ProjectModule.prototype.getTimezone = function (projectId) { var bootstrapUrl = "/gdc/app/account/bootstrap?projectId=" + projectId; return this.xhr .get(bootstrapUrl) .then(function (r) { return r.getData(); }) .then(function (result) { return result.bootstrapResource.current.timezone; }); }; ProjectModule.prototype.setTimezone = function (projectId, timezone) { var timezoneServiceUrl = "/gdc/md/" + projectId + "/service/timezone"; var data = { service: { timezone: timezone }, }; return this.xhr .post(timezoneServiceUrl, { body: data, }) .then(function (r) { return r.getData(); }); }; /** * Create project * Note: returns a promise which is resolved when the project creation is finished * * @experimental * @method createProject * @param {String} title * @param {String} authorizationToken * @param {Object} options for project creation (summary, projectTemplate, ...) * @return {Object} created project object */ ProjectModule.prototype.createProject = function (title, authorizationToken, options) { var _this = this; if (options === void 0) { options = {}; } var summary = options.summary, projectTemplate = options.projectTemplate, _a = options.driver, driver = _a === void 0 ? "Pg" : _a, _b = options.environment, environment = _b === void 0 ? "TESTING" : _b, _c = options.guidedNavigation, guidedNavigation = _c === void 0 ? 1 : _c; return this.xhr .post("/gdc/projects", { body: JSON.stringify({ project: { content: { guidedNavigation: guidedNavigation, driver: driver, authorizationToken: authorizationToken, environment: environment, }, meta: { title: title, summary: summary, projectTemplate: projectTemplate, }, }, }), }) .then(function (r) { return r.getData(); }) .then(function (project) { return util_1.handlePolling(_this.xhr.get.bind(_this.xhr), project.uri, function (response) { // TODO project response return isProjectCreated(response.project); }, options); }); }; /** * Delete project * * @method deleteProject * @param {String} projectId */ ProjectModule.prototype.deleteProject = function (projectId) { return this.xhr.del("/gdc/projects/" + projectId); }; /** * Gets aggregated feature flags for given project and current user * * @method getFeatureFlags * @param {String} projectId - A project identifier * @return {IFeatureFlags} Hash table of feature flags and theirs values where feature flag is as key */ ProjectModule.prototype.getFeatureFlags = function (projectId) { return this.xhr .get("/gdc/app/projects/" + projectId + "/featureFlags") .then(function (apiResponse) { return apiResponse.getData(); }) .then(function (result) { if (result && result.featureFlags) { return result.featureFlags; } return {}; }); }; /** * Gets project config including project specific feature flags * * @param {String} projectId - A project identifier * @return {IProjectConfigSettingItem[]} An array of project config setting items */ ProjectModule.prototype.getConfig = function (projectId) { return this.xhr .get("/gdc/app/projects/" + projectId + "/config") .then(function (apiResponse) { var projectConfig = apiResponse.getData(); return projectConfig; }) .then(function (result) { if (result && result.settings && result.settings.items) { return result.settings.items; } return []; }); }; /** * Gets project specific feature flags * * @param {String} projectId - A project identifier * @param {String} source - optional filter settingItems with specific source * @return {IFeatureFlags} Hash table of feature flags and theirs values where feature flag is as key */ ProjectModule.prototype.getProjectFeatureFlags = function (projectId, source) { return this.getConfig(projectId).then(function (settingItems) { var filteredSettingItems = source ? settingItems.filter(function (settingItem) { return settingItem.settingItem.source === source; }) : settingItems; var featureFlags = {}; filteredSettingItems.forEach(function (settingItem) { featureFlags[settingItem.settingItem.key] = exports.parseSettingItemValue(settingItem.settingItem.value); }); return featureFlags; }); }; return ProjectModule; }()); exports.ProjectModule = ProjectModule;