@vizzly/api-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +1 -0
  2. package/dist/errors/DashboardQuotaReached.d.ts +3 -0
  3. package/dist/errors/DashboardQuotaReached.js +10 -0
  4. package/dist/errors/FailedToCreateDashboard.d.ts +3 -0
  5. package/dist/errors/FailedToCreateDashboard.js +10 -0
  6. package/dist/errors/FailedToFetchDashboardAccessTokens.d.ts +3 -0
  7. package/dist/errors/FailedToFetchDashboardAccessTokens.js +10 -0
  8. package/dist/errors/FailedToFetchDashboards.d.ts +3 -0
  9. package/dist/errors/FailedToFetchDashboards.js +10 -0
  10. package/dist/errors/FailedToFetchGlobalLibraries.d.ts +3 -0
  11. package/dist/errors/FailedToFetchGlobalLibraries.js +10 -0
  12. package/dist/errors/FailedToFetchGlobalLibraryAccessTokens.d.ts +3 -0
  13. package/dist/errors/FailedToFetchGlobalLibraryAccessTokens.js +10 -0
  14. package/dist/errors/FailedToResolveDataSets.d.ts +3 -0
  15. package/dist/errors/FailedToResolveDataSets.js +10 -0
  16. package/dist/errors/FailedToSetupProject.d.ts +3 -0
  17. package/dist/errors/FailedToSetupProject.js +10 -0
  18. package/dist/errors/MissingAuthParameter.d.ts +4 -0
  19. package/dist/errors/MissingAuthParameter.js +9 -0
  20. package/dist/errors/MissingRequiredVizzlyQueryEngineApi.d.ts +2 -0
  21. package/dist/errors/MissingRequiredVizzlyQueryEngineApi.js +6 -0
  22. package/dist/errors/SQLPreviewError.d.ts +4 -0
  23. package/dist/errors/SQLPreviewError.js +11 -0
  24. package/dist/errors/ValidationError.d.ts +3 -0
  25. package/dist/errors/ValidationError.js +10 -0
  26. package/dist/errors/index.d.ts +10 -0
  27. package/dist/errors/index.js +26 -0
  28. package/dist/index.d.ts +8 -0
  29. package/dist/index.js +37 -0
  30. package/dist/interfaces/iSerialize.d.ts +10 -0
  31. package/dist/interfaces/iSerialize.js +1 -0
  32. package/dist/models/Api.d.ts +9 -0
  33. package/dist/models/Api.js +47 -0
  34. package/dist/models/Authentication.d.ts +16 -0
  35. package/dist/models/Authentication.js +98 -0
  36. package/dist/models/Dashboard.d.ts +24 -0
  37. package/dist/models/Dashboard.js +156 -0
  38. package/dist/models/DashboardRepository.d.ts +41 -0
  39. package/dist/models/DashboardRepository.js +92 -0
  40. package/dist/models/GlobalLibraryRepository.d.ts +35 -0
  41. package/dist/models/GlobalLibraryRepository.js +106 -0
  42. package/dist/models/ImplementationStrategy.d.ts +9 -0
  43. package/dist/models/ImplementationStrategy.js +18 -0
  44. package/dist/models/Permission.d.ts +26 -0
  45. package/dist/models/Permission.js +22 -0
  46. package/dist/models/Vizzly.d.ts +17 -0
  47. package/dist/models/Vizzly.js +39 -0
  48. package/dist/models/VizzlyApi.d.ts +77 -0
  49. package/dist/models/VizzlyApi.js +423 -0
  50. package/dist/models/VizzlyAppApi.d.ts +54 -0
  51. package/dist/models/VizzlyAppApi.js +348 -0
  52. package/dist/models/VizzlyQueryEngineApi.d.ts +50 -0
  53. package/dist/models/VizzlyQueryEngineApi.js +311 -0
  54. package/dist/types/dashboard.d.ts +23 -0
  55. package/dist/types/dashboard.js +2 -0
  56. package/dist/types.d.ts +405 -0
  57. package/dist/types.js +17 -0
  58. package/package.json +37 -0
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Dashboard = void 0;
13
+ const lodash_1 = require("lodash");
14
+ const GlobalLibraryRepository_1 = require("./GlobalLibraryRepository");
15
+ class Dashboard {
16
+ constructor(vizzly) {
17
+ this.vizzly = vizzly;
18
+ }
19
+ create(params) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const dashboardRepository = this.vizzly.getDashboardRepository();
22
+ return yield dashboardRepository.createDashboard(params);
23
+ });
24
+ }
25
+ update(params) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ var _a, _b, _c;
28
+ const dashboardRepository = this.vizzly.getDashboardRepository();
29
+ const globalLibraryRepository = this.vizzly.getGlobalLibraryRepository();
30
+ const { permissions: dashboardPermissions } = yield dashboardRepository.fetchDashboardAccessTokens();
31
+ const { permissions: globalLibraryPermissions } = yield globalLibraryRepository.fetchGlobalLibraryAccessTokens();
32
+ const { found: globalLibraries } = yield globalLibraryRepository.fetchGlobalLibraries();
33
+ const { found: dashboards } = yield dashboardRepository.fetchDashboards();
34
+ const dashboardToUpdate = dashboards.find((dashboard) => dashboard.id === params.dashboardId);
35
+ if (!dashboardToUpdate)
36
+ throw new Error('Cannot find dashboard to update.');
37
+ const { parentGlobalLibraries, childGlobalLibraries } = GlobalLibraryRepository_1.GlobalLibraryRepository.splitParentAndChildGlobalLibraries(globalLibraries);
38
+ // Get the token to update the dashboard.
39
+ const updateDashboardToken = dashboardPermissions.find((permission) => permission.scope === 'read_write' && permission.dashboardId === params.dashboardId);
40
+ if (!updateDashboardToken)
41
+ throw new Error(`Failed to find update token for dashboard.`);
42
+ // Send the update request.
43
+ const dashboardUpdatedResponse = yield dashboardRepository.updateDashboard(Object.assign(Object.assign({}, params), { dashboardSessionToken: updateDashboardToken === null || updateDashboardToken === void 0 ? void 0 : updateDashboardToken.token }));
44
+ if (dashboardUpdatedResponse.status != 200)
45
+ throw new Error('Failed to update dashboard.');
46
+ // Build the views to store in the libraries
47
+ let libraryViewsToSave = [];
48
+ if ((_a = params.definition) === null || _a === void 0 ? void 0 : _a.componentLibrary) {
49
+ libraryViewsToSave = (0, lodash_1.cloneDeep)(params.definition.componentLibrary || []).filter((c) => {
50
+ const isParentDashboard = dashboardToUpdate.parent_dashboard_id === null;
51
+ if (isParentDashboard) {
52
+ // All content of the library are for the parent library, if the parent dashboard
53
+ // is being edited.
54
+ return true;
55
+ }
56
+ else {
57
+ return !c.attributes.protectedByOrganisation;
58
+ }
59
+ });
60
+ }
61
+ const librariesToUpdate = dashboardToUpdate.parent_dashboard_id === null ? parentGlobalLibraries : childGlobalLibraries;
62
+ // If it's not a parent dashboard, and there are views or custom fields to save, but there are no libraries
63
+ // then create one first.
64
+ const isParent = dashboardToUpdate.parent_dashboard_id === null;
65
+ const hasThingsToSaveInLibrary = libraryViewsToSave.length > 0 || !!((_b = params.definition) === null || _b === void 0 ? void 0 : _b.customFields);
66
+ const parentGlobalLibrary = globalLibraries.find((gL) => gL.parent_global_library_id === null);
67
+ if (!isParent && parentGlobalLibrary && hasThingsToSaveInLibrary && librariesToUpdate.length === 0) {
68
+ const createdChildGlobalLibrary = yield globalLibraryRepository.createGlobalLibrary({
69
+ library: {
70
+ views: libraryViewsToSave,
71
+ // No concept of being protected, so only option is to save them all
72
+ customFields: ((_c = params.definition) === null || _c === void 0 ? void 0 : _c.customFields) || {},
73
+ },
74
+ parentGlobalLibraryId: parentGlobalLibrary.id,
75
+ permissions: [{ scope: 'read_write' }],
76
+ });
77
+ if (createdChildGlobalLibrary.status != 200)
78
+ throw new Error(`Failed to create first child library for user. ${createdChildGlobalLibrary.status}`);
79
+ }
80
+ const updatePromises = librariesToUpdate.map((gL) => __awaiter(this, void 0, void 0, function* () {
81
+ var _d;
82
+ const writePermission = globalLibraryPermissions.find((permission) => permission.globalLibraryId === gL.id && permission.scope === 'read_write');
83
+ if (!writePermission)
84
+ console.log('No write permission');
85
+ if (writePermission) {
86
+ yield globalLibraryRepository.updateGlobalLibrary({
87
+ globalLibrarySessionToken: writePermission.token,
88
+ library: {
89
+ views: libraryViewsToSave,
90
+ // No concept of being protected, so only option is to save them all
91
+ customFields: ((_d = params.definition) === null || _d === void 0 ? void 0 : _d.customFields) || {},
92
+ },
93
+ });
94
+ }
95
+ }));
96
+ yield Promise.all(updatePromises);
97
+ });
98
+ }
99
+ getDashboards() {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ const dashboardRepo = this.vizzly.getDashboardRepository();
102
+ const libraryRepo = this.vizzly.getGlobalLibraryRepository();
103
+ const { found: dashboards } = yield dashboardRepo.fetchDashboards();
104
+ const { found: globalLibraries } = yield libraryRepo.fetchGlobalLibraries();
105
+ return Dashboard.mergeDashboardsAndGlobalLibraries(dashboards, globalLibraries);
106
+ });
107
+ }
108
+ static mergeDashboardsAndGlobalLibraries(dashboards, globalLibraries) {
109
+ return (0, lodash_1.cloneDeep)(dashboards).map((dashboard) => {
110
+ // Views attached directly to a library on the parent dashboard.
111
+ let dashboardComponentLibrary = (0, lodash_1.cloneDeep)(dashboard.definition.componentLibrary);
112
+ let parentDashboardComponentLibrary = dashboard.parent_dashboard_id === null
113
+ ? []
114
+ : dashboards.find((d) => d.id === dashboard.parent_dashboard_id).definition.componentLibrary;
115
+ let { parentGlobalLibraries, childGlobalLibraries } = GlobalLibraryRepository_1.GlobalLibraryRepository.splitParentAndChildGlobalLibraries(globalLibraries);
116
+ // Only include the child libraries if the dashboard is not a parent.
117
+ childGlobalLibraries = dashboard.parent_dashboard_id === null ? [] : childGlobalLibraries;
118
+ // If parent dashboard, all libraries are for the parent.
119
+ // parentGlobalLibraries = dashboard.parent_dashboard_id === null ? [...parentGlobalLibraries, ...childGlobalLibraries] : [];
120
+ // Add the views from the various sources together. The order does matter -
121
+ // first elements are kept if a duplicate view is found.
122
+ let newLibrary = (0, lodash_1.uniqBy)([
123
+ ...Dashboard.markViewsAsProtected(parentGlobalLibraries.flatMap((gL) => gL.library.views || [])),
124
+ ...childGlobalLibraries.flatMap((gL) => gL.library.views || []),
125
+ ...Dashboard.markViewsAsProtected(parentDashboardComponentLibrary),
126
+ ...(dashboard.parent_dashboard_id === null
127
+ ? Dashboard.markViewsAsProtected(dashboardComponentLibrary)
128
+ : dashboardComponentLibrary || []),
129
+ ], (view) => view.attributes.viewId);
130
+ dashboard.definition.componentLibrary = newLibrary;
131
+ dashboard.definition.customFields = Dashboard.mergeCustomFields(dashboard, dashboards, parentGlobalLibraries, childGlobalLibraries);
132
+ return dashboard;
133
+ });
134
+ }
135
+ static mergeCustomFields(dashboard, dashboards, parentGlobalLibrariesForDashboard, childGlobalLibrariesForDashboard) {
136
+ var _a;
137
+ const maybeParentDashboardCustomFields = dashboard.parent_dashboard_id === null
138
+ ? {}
139
+ : ((_a = dashboards.find((d) => d.id === dashboard.parent_dashboard_id)) === null || _a === void 0 ? void 0 : _a.definition.customFields) || {};
140
+ const doMerge = (...toBeMerged) => {
141
+ return toBeMerged.reduce((customFields, acc) => {
142
+ Object.keys(customFields || {}).forEach((dataSetId) => {
143
+ if (!acc[dataSetId])
144
+ acc[dataSetId] = [];
145
+ acc[dataSetId] = (0, lodash_1.uniqBy)([...acc[dataSetId], ...((customFields === null || customFields === void 0 ? void 0 : customFields[dataSetId]) || [])], (cF) => cF.id);
146
+ });
147
+ return acc;
148
+ }, {});
149
+ };
150
+ return doMerge((0, lodash_1.cloneDeep)(dashboard.definition.customFields) || {}, (0, lodash_1.cloneDeep)(maybeParentDashboardCustomFields) || {}, ...(0, lodash_1.cloneDeep)(childGlobalLibrariesForDashboard).map((p) => p.library.customFields || {}), ...(0, lodash_1.cloneDeep)(parentGlobalLibrariesForDashboard).map((p) => p.library.customFields || {}));
151
+ }
152
+ static markViewsAsProtected(componentLibrary) {
153
+ return componentLibrary.map((view) => (Object.assign(Object.assign({}, view), { attributes: Object.assign(Object.assign({}, view.attributes), { protectedByOrganisation: true }) })));
154
+ }
155
+ }
156
+ exports.Dashboard = Dashboard;
@@ -0,0 +1,41 @@
1
+ import { CreateDashboardParams, Dashboard, Response, UpdateDashboardParams } from '../types';
2
+ import { Vizzly } from './Vizzly';
3
+ type CreateDashboardResponseBody = {
4
+ dashboard: Dashboard;
5
+ permissions: Array<{
6
+ dashboard_id: string;
7
+ scope: 'read' | 'read_write';
8
+ token: string;
9
+ user_reference: string;
10
+ }>;
11
+ };
12
+ type FetchDashboardsResponseBody = {
13
+ found: Dashboard[];
14
+ not_found: any[];
15
+ };
16
+ export declare class DashboardRepository {
17
+ private vizzly;
18
+ constructor(vizzly: Vizzly);
19
+ createDashboard(createParams: CreateDashboardParams): Promise<{
20
+ dashboard: Dashboard;
21
+ permissions: ({
22
+ userReference: string;
23
+ token: string;
24
+ scope: "read" | "read_write";
25
+ } & {
26
+ dashboardId: string;
27
+ })[];
28
+ }>;
29
+ updateDashboard(params: UpdateDashboardParams): Promise<Response<CreateDashboardResponseBody>>;
30
+ fetchDashboards(): Promise<FetchDashboardsResponseBody>;
31
+ fetchDashboardAccessTokens(): Promise<{
32
+ permissions: ({
33
+ userReference: string;
34
+ token: string;
35
+ scope: "read" | "read_write";
36
+ } & {
37
+ dashboardId: string;
38
+ })[];
39
+ }>;
40
+ }
41
+ export {};
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.DashboardRepository = void 0;
13
+ const DashboardQuotaReached_1 = require("../errors/DashboardQuotaReached");
14
+ const FailedToCreateDashboard_1 = require("../errors/FailedToCreateDashboard");
15
+ const FailedToFetchDashboardAccessTokens_1 = require("../errors/FailedToFetchDashboardAccessTokens");
16
+ const FailedToFetchDashboards_1 = require("../errors/FailedToFetchDashboards");
17
+ const Permission_1 = require("./Permission");
18
+ class DashboardRepository {
19
+ constructor(vizzly) {
20
+ this.vizzly = vizzly;
21
+ }
22
+ createDashboard(createParams) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ let builtDashboard;
25
+ if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) {
26
+ const queryEngineAPI = this.vizzly.getVizzlyQueryEngineAPI();
27
+ builtDashboard = yield queryEngineAPI.execute(queryEngineAPI.buildCreateDashboardRequest(createParams));
28
+ }
29
+ else {
30
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
31
+ builtDashboard = yield vizzlyApi.execute(vizzlyApi.buildCreateDashboardRequest(createParams));
32
+ }
33
+ if (builtDashboard.status === 200) {
34
+ return {
35
+ dashboard: builtDashboard.body.dashboard,
36
+ permissions: builtDashboard.body.permissions.map(Permission_1.Permission.formatDashboardPermission),
37
+ };
38
+ }
39
+ if (builtDashboard.status === 422) {
40
+ throw new DashboardQuotaReached_1.DashboardQuotaReached(`Dashboard quota reached; ${JSON.stringify(builtDashboard.body)}`);
41
+ }
42
+ if (builtDashboard.status != 200) {
43
+ throw new FailedToCreateDashboard_1.FailedToCreateDashboard(`Failed to create the dashboard from POST /api/v2/dashboard. Got ${builtDashboard.status}`);
44
+ }
45
+ throw new FailedToCreateDashboard_1.FailedToCreateDashboard(`Unknown failure. Got status ${builtDashboard.status}`);
46
+ });
47
+ }
48
+ updateDashboard(params) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ let updatedDashboard;
51
+ if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) {
52
+ const queryEngineAPI = this.vizzly.getVizzlyQueryEngineAPI();
53
+ updatedDashboard = yield queryEngineAPI.execute(queryEngineAPI.buildUpdateDashboardRequest(params));
54
+ }
55
+ else {
56
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
57
+ updatedDashboard = yield vizzlyApi.execute(vizzlyApi.buildUpdateDashboardRequest(params));
58
+ }
59
+ return updatedDashboard;
60
+ });
61
+ }
62
+ fetchDashboards() {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const { permissions } = yield this.fetchDashboardAccessTokens();
65
+ let dashboardResponse;
66
+ if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) {
67
+ const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI();
68
+ dashboardResponse = yield vizzlyQueryEngineApi.execute(vizzlyQueryEngineApi.buildFetchDashboardsRequest(permissions.map((p) => p.token)));
69
+ }
70
+ else {
71
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
72
+ dashboardResponse = yield vizzlyApi.execute(vizzlyApi.buildFetchDashboardsRequest(permissions.map((p) => p.token)));
73
+ }
74
+ if (dashboardResponse.status === 200)
75
+ return dashboardResponse.body;
76
+ throw new FailedToFetchDashboards_1.FailedToFetchDashboards(`Failed to fetch dashboards. Got status code ${dashboardResponse.status}.`);
77
+ });
78
+ }
79
+ fetchDashboardAccessTokens() {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
82
+ const accessTokens = yield vizzlyApi.execute(vizzlyApi.buildFetchDashboardAccessTokensRequest());
83
+ if (accessTokens.status === 200) {
84
+ return {
85
+ permissions: accessTokens.body.permissions.map(Permission_1.Permission.formatDashboardPermission),
86
+ };
87
+ }
88
+ throw new FailedToFetchDashboardAccessTokens_1.FailedToFetchDashboardAccessTokens('Failed to fetch dashboard access tokens.');
89
+ });
90
+ }
91
+ }
92
+ exports.DashboardRepository = DashboardRepository;
@@ -0,0 +1,35 @@
1
+ import { GlobalLibrary, UpdateGlobalLibraryParams, nVizzlyApi } from '../types';
2
+ import { Vizzly } from './Vizzly';
3
+ export declare class GlobalLibraryRepository {
4
+ private vizzly;
5
+ constructor(vizzly: Vizzly);
6
+ createGlobalLibrary(createParams: nVizzlyApi.CreateGlobalLibraryParams): Promise<import("../types").Response<{
7
+ global_library: GlobalLibrary;
8
+ permissions: import("../types").GlobalLibraryPermission[];
9
+ }>>;
10
+ fetchGlobalLibraries(): Promise<{
11
+ found: GlobalLibrary[];
12
+ }>;
13
+ fetchGlobalLibraryAccessTokens(): Promise<{
14
+ permissions: ({
15
+ userReference: string;
16
+ token: string;
17
+ scope: "read" | "read_write";
18
+ } & {
19
+ globalLibraryId: string;
20
+ })[];
21
+ }>;
22
+ updateGlobalLibrary(params: UpdateGlobalLibraryParams): Promise<import("../types").Response<unknown>>;
23
+ getChildGlobalLibrariesByParentGlobalLibraryId(parentGlobalLibraryId: string): Promise<GlobalLibrary[]>;
24
+ static splitParentAndChildGlobalLibraries(globalLibraries: GlobalLibrary[]): {
25
+ childGlobalLibraries: GlobalLibrary[];
26
+ parentGlobalLibraries: GlobalLibrary[];
27
+ };
28
+ /**
29
+ * Takes a list of global libraries, and returns a mapping of parent global
30
+ * library to a list of it's children
31
+ */
32
+ static mapParentToChildGlobalLibraries(globalLibraries: GlobalLibrary[]): {
33
+ [parentGlobalLibraryId: string]: GlobalLibrary[];
34
+ };
35
+ }
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.GlobalLibraryRepository = void 0;
13
+ const lodash_1 = require("lodash");
14
+ const Permission_1 = require("./Permission");
15
+ const FailedToFetchGlobalLibraryAccessTokens_1 = require("../errors/FailedToFetchGlobalLibraryAccessTokens");
16
+ const FailedToFetchGlobalLibraries_1 = require("../errors/FailedToFetchGlobalLibraries");
17
+ class GlobalLibraryRepository {
18
+ constructor(vizzly) {
19
+ this.vizzly = vizzly;
20
+ }
21
+ createGlobalLibrary(createParams) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
24
+ const result = yield vizzlyApi.createGlobalLibrary(createParams);
25
+ return result;
26
+ });
27
+ }
28
+ fetchGlobalLibraries() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
31
+ const accessTokens = yield this.fetchGlobalLibraryAccessTokens();
32
+ const globalLibraries = yield vizzlyApi.execute(vizzlyApi.buildFetchGlobalLibrariesRequest({
33
+ globalLibraryAccessTokens: accessTokens.permissions.map((p) => p.token),
34
+ }));
35
+ if (globalLibraries.status === 200)
36
+ return globalLibraries.body;
37
+ throw new FailedToFetchGlobalLibraries_1.FailedToFetchGlobalLibraries(`Failed to fetch global libraries. Got status ${globalLibraries.status}`);
38
+ });
39
+ }
40
+ // async updateGlobalLibrary(params: {}) {
41
+ // const vizzlyApi = this.vizzly.getVizzlyAPI();
42
+ // const accessTokens = await vizzlyApi.execute<{
43
+ // permissions: Array<{ global_library_id: string; token: string; user_reference: string, scope: 'read' | 'read_write' }>;
44
+ // }>(vizzlyApi.());
45
+ // return accessTokens;
46
+ // }
47
+ fetchGlobalLibraryAccessTokens() {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
50
+ const accessTokens = yield vizzlyApi.execute(vizzlyApi.buildFetchGlobalLibraryAccessTokensRequest());
51
+ if (accessTokens.status === 200) {
52
+ return {
53
+ permissions: accessTokens.body.permissions.map(Permission_1.Permission.formatGlobalLibraryPermission),
54
+ };
55
+ }
56
+ throw new FailedToFetchGlobalLibraryAccessTokens_1.FailedToFetchGlobalLibraryAccessTokens('Failed to load access tokens for the global library.');
57
+ });
58
+ }
59
+ updateGlobalLibrary(params) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const vizzlyApi = this.vizzly.getVizzlyAPI();
62
+ return yield vizzlyApi.updateGlobalLibrary(params);
63
+ });
64
+ }
65
+ getChildGlobalLibrariesByParentGlobalLibraryId(parentGlobalLibraryId) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const { found: globalLibraries } = yield this.fetchGlobalLibraries();
68
+ return GlobalLibraryRepository.mapParentToChildGlobalLibraries(globalLibraries)[parentGlobalLibraryId] || [];
69
+ });
70
+ }
71
+ static splitParentAndChildGlobalLibraries(globalLibraries) {
72
+ let childGlobalLibraries = [];
73
+ let parentGlobalLibraries = [];
74
+ globalLibraries.forEach((gL) => {
75
+ if (gL.parent_global_library_id === null) {
76
+ parentGlobalLibraries = [...parentGlobalLibraries, gL];
77
+ }
78
+ else {
79
+ childGlobalLibraries = [...childGlobalLibraries, gL];
80
+ }
81
+ });
82
+ return { childGlobalLibraries, parentGlobalLibraries };
83
+ }
84
+ /**
85
+ * Takes a list of global libraries, and returns a mapping of parent global
86
+ * library to a list of it's children
87
+ */
88
+ static mapParentToChildGlobalLibraries(globalLibraries) {
89
+ let mapping = {};
90
+ (0, lodash_1.cloneDeep)(globalLibraries).forEach((globalLibrary) => {
91
+ if (globalLibrary.parent_global_library_id !== null) {
92
+ // Child library
93
+ // Ensure the parent has been setup
94
+ if (!mapping[globalLibrary.parent_global_library_id])
95
+ mapping[globalLibrary.parent_global_library_id] = [];
96
+ // Add the child to the parent's list of children
97
+ mapping[globalLibrary.parent_global_library_id] = [
98
+ ...mapping[globalLibrary.parent_global_library_id],
99
+ globalLibrary,
100
+ ];
101
+ }
102
+ });
103
+ return mapping;
104
+ }
105
+ }
106
+ exports.GlobalLibraryRepository = GlobalLibraryRepository;
@@ -0,0 +1,9 @@
1
+ export declare abstract class ImplementationStrategy {
2
+ abstract shouldPersistViaQueryEngine(): boolean;
3
+ }
4
+ export declare class PersistViaQueryEngine extends ImplementationStrategy {
5
+ shouldPersistViaQueryEngine(): boolean;
6
+ }
7
+ export declare class PersistDirectToVizzlyApi extends ImplementationStrategy {
8
+ shouldPersistViaQueryEngine(): boolean;
9
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PersistDirectToVizzlyApi = exports.PersistViaQueryEngine = exports.ImplementationStrategy = void 0;
4
+ class ImplementationStrategy {
5
+ }
6
+ exports.ImplementationStrategy = ImplementationStrategy;
7
+ class PersistViaQueryEngine extends ImplementationStrategy {
8
+ shouldPersistViaQueryEngine() {
9
+ return true;
10
+ }
11
+ }
12
+ exports.PersistViaQueryEngine = PersistViaQueryEngine;
13
+ class PersistDirectToVizzlyApi extends ImplementationStrategy {
14
+ shouldPersistViaQueryEngine() {
15
+ return false;
16
+ }
17
+ }
18
+ exports.PersistDirectToVizzlyApi = PersistDirectToVizzlyApi;
@@ -0,0 +1,26 @@
1
+ type BasePermission = {
2
+ userReference: string;
3
+ token: string;
4
+ scope: 'read' | 'read_write';
5
+ };
6
+ type DashboardPermission = BasePermission & {
7
+ dashboardId: string;
8
+ };
9
+ type GlobalLibraryPermission = BasePermission & {
10
+ globalLibraryId: string;
11
+ };
12
+ export declare class Permission {
13
+ static formatDashboardPermission(permission: {
14
+ dashboard_id: string;
15
+ user_reference: string;
16
+ token: string;
17
+ scope: 'read' | 'read_write';
18
+ }): DashboardPermission;
19
+ static formatGlobalLibraryPermission(permission: {
20
+ global_library_id: string;
21
+ user_reference: string;
22
+ token: string;
23
+ scope: 'read' | 'read_write';
24
+ }): GlobalLibraryPermission;
25
+ }
26
+ export {};
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Permission = void 0;
4
+ class Permission {
5
+ static formatDashboardPermission(permission) {
6
+ return {
7
+ userReference: permission.user_reference,
8
+ scope: permission.scope,
9
+ token: permission.token,
10
+ dashboardId: permission.dashboard_id,
11
+ };
12
+ }
13
+ static formatGlobalLibraryPermission(permission) {
14
+ return {
15
+ userReference: permission.user_reference,
16
+ scope: permission.scope,
17
+ token: permission.token,
18
+ globalLibraryId: permission.global_library_id,
19
+ };
20
+ }
21
+ }
22
+ exports.Permission = Permission;
@@ -0,0 +1,17 @@
1
+ import { APIs, PartialBy } from '../types';
2
+ import { ImplementationStrategy, PersistDirectToVizzlyApi } from './ImplementationStrategy';
3
+ import { DashboardRepository } from './DashboardRepository';
4
+ import { GlobalLibraryRepository } from './GlobalLibraryRepository';
5
+ import { Dashboard } from './Dashboard';
6
+ export declare class Vizzly {
7
+ private APIs;
8
+ implementationStrategy: ImplementationStrategy;
9
+ constructor(APIs: PartialBy<APIs, 'vizzlyQueryEngineApi'>, implementationStrategy: PersistDirectToVizzlyApi);
10
+ getAPIs(): APIs | PartialBy<APIs, 'vizzlyQueryEngineApi'>;
11
+ getVizzlyAppApi(): import("./VizzlyAppApi").VizzlyAppApi;
12
+ getVizzlyQueryEngineAPI(): import("./VizzlyQueryEngineApi").VizzlyQueryEngineApi;
13
+ getVizzlyAPI(): import("./VizzlyApi").VizzlyApi;
14
+ getDashboardRepository(): DashboardRepository;
15
+ getGlobalLibraryRepository(): GlobalLibraryRepository;
16
+ getDashboard(): Dashboard;
17
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Vizzly = void 0;
4
+ const MissingRequiredVizzlyQueryEngineApi_1 = require("../errors/MissingRequiredVizzlyQueryEngineApi");
5
+ const DashboardRepository_1 = require("./DashboardRepository");
6
+ const GlobalLibraryRepository_1 = require("./GlobalLibraryRepository");
7
+ const Dashboard_1 = require("./Dashboard");
8
+ class Vizzly {
9
+ constructor(APIs, implementationStrategy) {
10
+ this.APIs = APIs;
11
+ this.implementationStrategy = implementationStrategy;
12
+ }
13
+ getAPIs() {
14
+ if (this.implementationStrategy.shouldPersistViaQueryEngine() && !this.APIs.vizzlyQueryEngineApi) {
15
+ throw new MissingRequiredVizzlyQueryEngineApi_1.MissingRequiredVizzlyQueryEngineApi('Strategy is to persist via a query engine. However no query engine API was provided.');
16
+ }
17
+ return this.APIs;
18
+ }
19
+ getVizzlyAppApi() {
20
+ return this.getAPIs().vizzlyAppApi;
21
+ }
22
+ getVizzlyQueryEngineAPI() {
23
+ return this.getAPIs().vizzlyQueryEngineApi;
24
+ }
25
+ getVizzlyAPI() {
26
+ return this.getAPIs().vizzlyApi;
27
+ }
28
+ getDashboardRepository() {
29
+ return new DashboardRepository_1.DashboardRepository(this);
30
+ }
31
+ getGlobalLibraryRepository() {
32
+ return new GlobalLibraryRepository_1.GlobalLibraryRepository(this);
33
+ }
34
+ // TODO; what should this be called!?
35
+ getDashboard() {
36
+ return new Dashboard_1.Dashboard(this);
37
+ }
38
+ }
39
+ exports.Vizzly = Vizzly;
@@ -0,0 +1,77 @@
1
+ import { Api } from './Api';
2
+ import { Authentication } from './Authentication';
3
+ import { CreateDashboardParams, GlobalLibrary, GlobalLibraryPermission, Request, UpdateDashboardParams, UpdateGlobalLibraryParams } from '../types';
4
+ import { nVizzlyApi } from '../types';
5
+ export declare class VizzlyApi extends Api {
6
+ constructor(auth: Authentication, host?: string);
7
+ buildFetchDashboardAccessTokensRequest(): Request<{
8
+ projectAdminOverrideToken: string | undefined;
9
+ dashboardAccessToken: string;
10
+ }>;
11
+ buildFetchGlobalLibrariesRequest(params: {
12
+ globalLibraryAccessTokens: string[];
13
+ }): Request<{
14
+ globalLibraryAccessTokens: string[];
15
+ }>;
16
+ buildFetchGlobalLibraryAccessTokensRequest(): Request<{
17
+ projectAdminOverrideToken: string | undefined;
18
+ dashboardAccessToken: string;
19
+ }>;
20
+ buildCreateDashboardRequest(params: CreateDashboardParams): Request<CreateDashboardParams & {
21
+ dashboardAccessToken: string;
22
+ }>;
23
+ buildFetchDashboardsRequest(dashboardSessionAccessTokens: string[]): Request<{}>;
24
+ createParentDashboard(params: nVizzlyApi.CreateParentDashboardParams): Promise<import("../types").Response<unknown>>;
25
+ buildCreateParentDashboardRequest(params: nVizzlyApi.CreateParentDashboardParams): Request<{}>;
26
+ createParentGlobalLibrary(params: nVizzlyApi.CreateParentGlobalLibraryParams): Promise<import("../types").Response<unknown>>;
27
+ buildCreateParentGlobalLibraryRequest(params: nVizzlyApi.CreateParentGlobalLibraryParams): Request<{}>;
28
+ createGlobalLibrary(params: nVizzlyApi.CreateGlobalLibraryParams): Promise<import("../types").Response<{
29
+ global_library: GlobalLibrary;
30
+ permissions: Array<GlobalLibraryPermission>;
31
+ }>>;
32
+ buildCreateGlobalLibraryRequest(params: nVizzlyApi.CreateGlobalLibraryParams): Request<{}>;
33
+ getProjects(): Promise<import("../types").Response<unknown>>;
34
+ buildGetProjectsRequest(): Request<{}>;
35
+ createProjectApiKey(params: nVizzlyApi.CreateProjectApiKeyParams): Promise<import("../types").Response<unknown>>;
36
+ buildCreateProjectApiKeyRequest(params: nVizzlyApi.CreateProjectApiKeyParams): Request<{}>;
37
+ createQueryEngineUser(params: nVizzlyApi.CreateQueryEngineUserParams): Promise<import("../types").Response<unknown>>;
38
+ buildCreateQueryEngineUserRequest(params: nVizzlyApi.CreateQueryEngineUserParams): Request<{}>;
39
+ buildCreateSelfHostedDynamicQueryEngineProjectRequest(): Request<{}>;
40
+ buildCreateSelfHostedInBrowserProjectRequest(): Request<{}>;
41
+ buildCreateSelfHostedProjectRequest(): Request<{}>;
42
+ createManagedQueryEngineProject(): Promise<import("../types").Response<unknown>>;
43
+ buildCreateManagedQueryEngineProjectRequest(): Request<{}>;
44
+ createVizzlyConfigVersion(params: nVizzlyApi.CreateVizzlyConfigVersionParams): Promise<import("../types").Response<unknown>>;
45
+ buildCreateVizzlyConfigVersionRequest(params: nVizzlyApi.CreateVizzlyConfigVersionParams): Request<{}>;
46
+ duplicateParentDashboard(params: nVizzlyApi.DuplicateParentDashboardParams): Promise<import("../types").Response<unknown>>;
47
+ buildDuplicateParentDashboardRequest(params: nVizzlyApi.DuplicateParentDashboardParams): Request<{}>;
48
+ getConnection(params: nVizzlyApi.GetConnectionParams): Promise<import("../types").Response<unknown>>;
49
+ buildGetConnectionRequest(params: nVizzlyApi.GetConnectionParams): Request<{}>;
50
+ getManagedQueryEngineKeyPair(params: nVizzlyApi.GetManagedQueryEngineKeyPairParams): Promise<import("../types").Response<unknown>>;
51
+ buildGetManagedQueryEngineKeyPairRequest(params: nVizzlyApi.GetManagedQueryEngineKeyPairParams): Request<{}>;
52
+ getProject(params: nVizzlyApi.GetProjectParams): Promise<import("../types").Response<unknown>>;
53
+ buildGetProjectRequest(params: nVizzlyApi.GetProjectParams): Request<{}>;
54
+ getProjectAccessToken(params: nVizzlyApi.GetProjectAccessTokenParams): Promise<import("../types").Response<unknown>>;
55
+ buildGetProjectAccessTokenRequest(params: nVizzlyApi.GetProjectAccessTokenParams): Request<{}>;
56
+ getQueryEngineUser(params: nVizzlyApi.GetQueryEngineUserParams): Promise<import("../types").Response<unknown>>;
57
+ buildGetQueryEngineUserRequest(params: nVizzlyApi.GetQueryEngineUserParams): Request<{}>;
58
+ getTeamMembers(params: nVizzlyApi.GetTeamMembers): Promise<import("../types").Response<unknown>>;
59
+ buildGetTeamMembersRequest(params: nVizzlyApi.GetTeamMembers): Request<{}>;
60
+ getVizzlyConfig(params: nVizzlyApi.GetVizzlyConfigParams): Promise<import("../types").Response<unknown>>;
61
+ buildGetVizzlyConfigRequest(params: nVizzlyApi.GetVizzlyConfigParams): Request<{}>;
62
+ saveConnection(params: nVizzlyApi.SaveConnectionParams): Promise<import("../types").Response<unknown>>;
63
+ buildSaveConnectionRequest(params: nVizzlyApi.SaveConnectionParams): Request<{}>;
64
+ buildSaveEncryptedProjectEncryptionSecretRequest(params: nVizzlyApi.SaveEncryptedProjectEncryptionSecretParams): Request<{}>;
65
+ saveScheduledReport(params: nVizzlyApi.SaveScheduledReportParams): Promise<import("../types").Response<unknown>>;
66
+ buildSaveScheduledReportRequest(params: nVizzlyApi.SaveScheduledReportParams): Request<{}>;
67
+ updateDashboard(params: UpdateDashboardParams): Promise<import("../types").Response<unknown>>;
68
+ buildUpdateDashboardRequest(params: UpdateDashboardParams): Request<{}>;
69
+ updateParentGlobalLibrary(params: nVizzlyApi.UpdateParentGlobalLibraryParams): Promise<import("../types").Response<unknown>>;
70
+ buildUpdateParentGlobalLibraryRequest(params: nVizzlyApi.UpdateParentGlobalLibraryParams): Request<{}>;
71
+ updateGlobalLibrary(params: UpdateGlobalLibraryParams): Promise<import("../types").Response<unknown>>;
72
+ buildUpdateGlobalLibraryRequest(params: UpdateGlobalLibraryParams): Request<{}>;
73
+ updateProjectApiKey(params: nVizzlyApi.UpdateProjectApiKeyParams): Promise<import("../types").Response<unknown>>;
74
+ buildUpdateProjectApiKeyRequest(params: nVizzlyApi.UpdateProjectApiKeyParams): Request<{}>;
75
+ updateQueryEngineUser(params: nVizzlyApi.UpdateQueryEngineUserParams): Promise<import("../types").Response<unknown>>;
76
+ buildUpdateQueryEngineUserRequest(params: nVizzlyApi.UpdateQueryEngineUserParams): Request<{}>;
77
+ }