@vizzly/api-client 0.0.19 → 0.0.21
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.
- package/dist/models/Api.js +4 -4
- package/dist/models/DashboardRepository.d.ts +1 -0
- package/dist/models/DashboardRepository.js +34 -19
- package/dist/models/GlobalLibraryRepository.d.ts +2 -2
- package/dist/models/GlobalLibraryRepository.js +33 -18
- package/dist/models/Permission.d.ts +1 -0
- package/dist/models/Permission.js +20 -0
- package/dist/models/Repository.d.ts +17 -0
- package/dist/models/Repository.js +47 -0
- package/package.json +1 -1
package/dist/models/Api.js
CHANGED
|
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.Api = void 0;
|
|
16
16
|
const isomorphic_fetch_1 = __importDefault(require("isomorphic-fetch"));
|
|
17
17
|
const Monitor_1 = require("./Monitor");
|
|
18
|
+
const VizzlyApiClientLogger_1 = require("./VizzlyApiClientLogger");
|
|
18
19
|
class Api {
|
|
19
20
|
constructor(auth, host, extraHeaders = () => ({})) {
|
|
20
21
|
this.host = host;
|
|
@@ -37,10 +38,9 @@ class Api {
|
|
|
37
38
|
signal: request.abortSignal,
|
|
38
39
|
});
|
|
39
40
|
let body = ((_a = res.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('json')) ? yield res.json() : res.text();
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
41
|
+
const queryEngineRequestId = res.headers.get('vizzly-query-engine-request-id') || '-';
|
|
42
|
+
const vizzlyApiRequestId = res.headers.get('x-request-id') || '-';
|
|
43
|
+
VizzlyApiClientLogger_1.VizzlyApiClientLogger.debug(`[TRACING] QE; ${queryEngineRequestId} - API; ${vizzlyApiRequestId}`);
|
|
44
44
|
return { body: body, status: res.status, headers: res.headers };
|
|
45
45
|
});
|
|
46
46
|
}
|
|
@@ -2,6 +2,7 @@ import { CreateDashboardParams, Dashboard, DashboardFromAPI, DashboardPermission
|
|
|
2
2
|
import { Vizzly } from './Vizzly';
|
|
3
3
|
export declare class DashboardRepository {
|
|
4
4
|
private vizzly;
|
|
5
|
+
private repo;
|
|
5
6
|
constructor(vizzly: Vizzly);
|
|
6
7
|
createDashboard(createParams: CreateDashboardParams): Promise<{
|
|
7
8
|
dashboard: Dashboard;
|
|
@@ -16,9 +16,12 @@ const FailedToDecryptDashboardBeforeClient_1 = require("../errors/FailedToDecryp
|
|
|
16
16
|
const FailedToFetchDashboardAccessTokens_1 = require("../errors/FailedToFetchDashboardAccessTokens");
|
|
17
17
|
const FailedToUpdateDashboard_1 = require("../errors/FailedToUpdateDashboard");
|
|
18
18
|
const Permission_1 = require("./Permission");
|
|
19
|
+
const Repository_1 = require("./Repository");
|
|
19
20
|
class DashboardRepository {
|
|
21
|
+
// private repo: { dashboards: Dashboard[]; permissions: DashboardPermission[] } | null;
|
|
20
22
|
constructor(vizzly) {
|
|
21
23
|
this.vizzly = vizzly;
|
|
24
|
+
this.repo = new Repository_1.Repository('id');
|
|
22
25
|
}
|
|
23
26
|
createDashboard(createParams) {
|
|
24
27
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -33,8 +36,10 @@ class DashboardRepository {
|
|
|
33
36
|
}
|
|
34
37
|
if (builtDashboard.status === 200) {
|
|
35
38
|
const formattedPermissions = builtDashboard.body.permissions.map(Permission_1.Permission.formatDashboardPermission);
|
|
39
|
+
const embellishedDashboard = DashboardRepository.embellishDashboardFromAPI(builtDashboard.body.dashboard, formattedPermissions);
|
|
40
|
+
this.repo.addItem(embellishedDashboard, formattedPermissions);
|
|
36
41
|
return {
|
|
37
|
-
dashboard:
|
|
42
|
+
dashboard: embellishedDashboard,
|
|
38
43
|
permissions: formattedPermissions,
|
|
39
44
|
};
|
|
40
45
|
}
|
|
@@ -59,11 +64,15 @@ class DashboardRepository {
|
|
|
59
64
|
updatedDashboard = yield vizzlyApi.execute(vizzlyApi.buildUpdateDashboardRequest(params));
|
|
60
65
|
}
|
|
61
66
|
if (updatedDashboard.status === 200) {
|
|
62
|
-
|
|
67
|
+
const embellishedDashboard = DashboardRepository.embellishDashboardFromAPI(updatedDashboard.body.dashboard, [
|
|
63
68
|
params.dashboardPermission,
|
|
64
69
|
]);
|
|
70
|
+
this.repo.updateItem(embellishedDashboard);
|
|
71
|
+
return embellishedDashboard;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new FailedToUpdateDashboard_1.FailedToUpdateDashboard(`Failed to update dashboard, got status ${updatedDashboard.status}`);
|
|
65
75
|
}
|
|
66
|
-
throw new FailedToUpdateDashboard_1.FailedToUpdateDashboard(`Failed to update dashboard, got status ${updatedDashboard.status}`);
|
|
67
76
|
});
|
|
68
77
|
}
|
|
69
78
|
fetchDashboards(params) {
|
|
@@ -85,26 +94,32 @@ class DashboardRepository {
|
|
|
85
94
|
fetchDashboardAccessTokens(params) {
|
|
86
95
|
return __awaiter(this, void 0, void 0, function* () {
|
|
87
96
|
let accessTokens;
|
|
88
|
-
if (this.
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
if (!this.repo.isPopulated()) {
|
|
98
|
+
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) {
|
|
99
|
+
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI();
|
|
100
|
+
accessTokens = yield vizzlyQueryEngineApi.execute(vizzlyQueryEngineApi.buildFetchDashboardAccessTokensRequest(params));
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const vizzlyApi = this.vizzly.getVizzlyAPI();
|
|
104
|
+
accessTokens = yield vizzlyApi.execute(vizzlyApi.buildFetchDashboardAccessTokensRequest(params));
|
|
105
|
+
}
|
|
106
|
+
if (accessTokens.status === 200) {
|
|
107
|
+
const formattedPermissions = accessTokens.body.permissions.map(Permission_1.Permission.formatDashboardPermission);
|
|
108
|
+
const embellishedDashboards = accessTokens.body.dashboards.map((dashboard) => {
|
|
100
109
|
if (typeof dashboard.definition === 'string')
|
|
101
110
|
throw new FailedToDecryptDashboardBeforeClient_1.FailedToDecryptDashboardBeforeClient();
|
|
102
111
|
return DashboardRepository.embellishDashboardFromAPI(dashboard, formattedPermissions);
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
}
|
|
112
|
+
});
|
|
113
|
+
this.repo.initRepo(embellishedDashboards, formattedPermissions);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
throw new FailedToFetchDashboardAccessTokens_1.FailedToFetchDashboardAccessTokens('Failed to fetch dashboard access tokens.');
|
|
117
|
+
}
|
|
106
118
|
}
|
|
107
|
-
|
|
119
|
+
return {
|
|
120
|
+
dashboards: this.repo.getRepo().storedItems,
|
|
121
|
+
permissions: this.repo.getRepo().permissions,
|
|
122
|
+
};
|
|
108
123
|
});
|
|
109
124
|
}
|
|
110
125
|
}
|
|
@@ -2,11 +2,11 @@ import { CreateGlobalLibraryParams, GlobalLibrary, GlobalLibraryPermission, Requ
|
|
|
2
2
|
import { Vizzly } from './Vizzly';
|
|
3
3
|
export declare class GlobalLibraryRepository {
|
|
4
4
|
private vizzly;
|
|
5
|
-
private
|
|
5
|
+
private repo;
|
|
6
6
|
constructor(vizzly: Vizzly);
|
|
7
7
|
createGlobalLibrary(createParams: CreateGlobalLibraryParams): Promise<{
|
|
8
|
-
global_library: GlobalLibrary;
|
|
9
8
|
permissions: GlobalLibraryPermission[];
|
|
9
|
+
global_library: GlobalLibrary;
|
|
10
10
|
}>;
|
|
11
11
|
fetchGlobalLibraries(params?: RequestParams): Promise<{
|
|
12
12
|
global_libraries: GlobalLibrary[];
|
|
@@ -13,21 +13,25 @@ exports.GlobalLibraryRepository = void 0;
|
|
|
13
13
|
const lodash_1 = require("lodash");
|
|
14
14
|
const Permission_1 = require("./Permission");
|
|
15
15
|
const FailedToFetchGlobalLibraryAccessTokens_1 = require("../errors/FailedToFetchGlobalLibraryAccessTokens");
|
|
16
|
+
const Repository_1 = require("./Repository");
|
|
16
17
|
class GlobalLibraryRepository {
|
|
17
18
|
constructor(vizzly) {
|
|
18
|
-
this.cachedPermissions = { permissions: [] };
|
|
19
19
|
this.vizzly = vizzly;
|
|
20
|
+
this.repo = new Repository_1.Repository('id');
|
|
20
21
|
}
|
|
21
22
|
createGlobalLibrary(createParams) {
|
|
22
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
24
|
const vizzlyApi = this.vizzly.getVizzlyAPI();
|
|
25
|
+
let globalLibrary;
|
|
24
26
|
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) {
|
|
25
27
|
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI();
|
|
26
|
-
|
|
28
|
+
globalLibrary = yield vizzlyQueryEngineApi.createGlobalLibrary(createParams);
|
|
27
29
|
}
|
|
28
30
|
else {
|
|
29
|
-
|
|
31
|
+
globalLibrary = yield vizzlyApi.createGlobalLibrary(createParams);
|
|
30
32
|
}
|
|
33
|
+
this.repo.addItem(globalLibrary.global_library, globalLibrary.permissions);
|
|
34
|
+
return globalLibrary;
|
|
31
35
|
});
|
|
32
36
|
}
|
|
33
37
|
fetchGlobalLibraries(params) {
|
|
@@ -40,32 +44,43 @@ class GlobalLibraryRepository {
|
|
|
40
44
|
return __awaiter(this, void 0, void 0, function* () {
|
|
41
45
|
const vizzlyApi = this.vizzly.getVizzlyAPI();
|
|
42
46
|
let accessTokens;
|
|
43
|
-
if (this.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
47
|
+
if (!this.repo.isPopulated()) {
|
|
48
|
+
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) {
|
|
49
|
+
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI();
|
|
50
|
+
accessTokens = yield vizzlyQueryEngineApi.fetchGlobalLibraryAccessTokens(params);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
accessTokens = yield vizzlyApi.fetchGlobalLibraryAccessTokens(params);
|
|
54
|
+
}
|
|
55
|
+
if (accessTokens.status === 200) {
|
|
56
|
+
const formattedPermissions = accessTokens.body.permissions.map(Permission_1.Permission.formatGlobalLibraryPermission);
|
|
57
|
+
this.repo.initRepo(accessTokens.body.global_libraries, formattedPermissions);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
throw new FailedToFetchGlobalLibraryAccessTokens_1.FailedToFetchGlobalLibraryAccessTokens('Failed to load access tokens for the global library.');
|
|
61
|
+
}
|
|
55
62
|
}
|
|
56
|
-
|
|
63
|
+
return {
|
|
64
|
+
global_libraries: this.repo.getRepo().storedItems,
|
|
65
|
+
permissions: this.repo.getRepo().permissions,
|
|
66
|
+
};
|
|
57
67
|
});
|
|
58
68
|
}
|
|
59
69
|
updateGlobalLibrary(params) {
|
|
60
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
61
71
|
const vizzlyApi = this.vizzly.getVizzlyAPI();
|
|
72
|
+
let updateResponse;
|
|
62
73
|
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) {
|
|
63
74
|
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI();
|
|
64
|
-
|
|
75
|
+
updateResponse = yield vizzlyQueryEngineApi.updateGlobalLibrary(params);
|
|
65
76
|
}
|
|
66
77
|
else {
|
|
67
|
-
|
|
78
|
+
updateResponse = yield vizzlyApi.updateGlobalLibrary(params);
|
|
79
|
+
}
|
|
80
|
+
if (updateResponse.status === 200) {
|
|
81
|
+
this.repo.updateItem(updateResponse.body.global_library);
|
|
68
82
|
}
|
|
83
|
+
return updateResponse;
|
|
69
84
|
});
|
|
70
85
|
}
|
|
71
86
|
getChildGlobalLibrariesByParentGlobalLibraryId(parentGlobalLibraryId) {
|
|
@@ -7,4 +7,5 @@ export declare class Permission {
|
|
|
7
7
|
scope: 'read' | 'read_write';
|
|
8
8
|
}): DashboardPermission;
|
|
9
9
|
static formatGlobalLibraryPermission(permission: GlobalLibraryPermissionFromAPI): GlobalLibraryPermission;
|
|
10
|
+
static validateJwtExpiry(token: string, freshnessBufferInSeconds?: number): boolean;
|
|
10
11
|
}
|
|
@@ -18,5 +18,25 @@ class Permission {
|
|
|
18
18
|
globalLibraryId: permission.global_library_id,
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
+
static validateJwtExpiry(token, freshnessBufferInSeconds = 10) {
|
|
22
|
+
try {
|
|
23
|
+
// Split the token into its parts
|
|
24
|
+
const [_header, payload, _signature] = token.split('.');
|
|
25
|
+
// Decode the payload (Base64 decoding)
|
|
26
|
+
const decodedPayload = Buffer.from(payload, 'base64').toString('utf-8');
|
|
27
|
+
const payloadObj = JSON.parse(decodedPayload);
|
|
28
|
+
// Get the expiration time from the payload
|
|
29
|
+
const expirationTime = payloadObj.exp;
|
|
30
|
+
// Get the current timestamp
|
|
31
|
+
const currentTimestamp = Math.floor(Date.now() / 1000);
|
|
32
|
+
// Check if the token has expired
|
|
33
|
+
const hasExpired = expirationTime < currentTimestamp + freshnessBufferInSeconds;
|
|
34
|
+
return !hasExpired;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
// Token decoding or validation failed
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
21
41
|
}
|
|
22
42
|
exports.Permission = Permission;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class Repository<StoreType, PermissionType extends {
|
|
2
|
+
token: string;
|
|
3
|
+
}> {
|
|
4
|
+
private storedItems;
|
|
5
|
+
private permissions;
|
|
6
|
+
private itemKey;
|
|
7
|
+
constructor(itemKey: keyof StoreType);
|
|
8
|
+
getRepo(): {
|
|
9
|
+
storedItems: StoreType[];
|
|
10
|
+
permissions: PermissionType[];
|
|
11
|
+
};
|
|
12
|
+
initRepo(storedItems: Array<StoreType>, permissions: Array<PermissionType>): void;
|
|
13
|
+
addItem(item: StoreType, permissions: PermissionType[]): void;
|
|
14
|
+
updateItem(newValue: StoreType): void;
|
|
15
|
+
isPopulated(): boolean;
|
|
16
|
+
private permissionsAreFresh;
|
|
17
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Repository = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const Permission_1 = require("./Permission");
|
|
6
|
+
class Repository {
|
|
7
|
+
constructor(itemKey) {
|
|
8
|
+
this.itemKey = itemKey;
|
|
9
|
+
this.storedItems = [];
|
|
10
|
+
this.permissions = [];
|
|
11
|
+
}
|
|
12
|
+
getRepo() {
|
|
13
|
+
return {
|
|
14
|
+
storedItems: this.storedItems,
|
|
15
|
+
permissions: this.permissions,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
initRepo(storedItems, permissions) {
|
|
19
|
+
this.storedItems = storedItems;
|
|
20
|
+
this.permissions = permissions;
|
|
21
|
+
}
|
|
22
|
+
addItem(item, permissions) {
|
|
23
|
+
// Can only add an item to the store that has been
|
|
24
|
+
// initialized using initRepo
|
|
25
|
+
if (this.isPopulated()) {
|
|
26
|
+
this.storedItems = [...this.storedItems, item];
|
|
27
|
+
this.permissions = [...this.permissions, ...permissions];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
updateItem(newValue) {
|
|
31
|
+
this.storedItems = this.storedItems.map((item) => {
|
|
32
|
+
if (item[this.itemKey] == newValue[this.itemKey])
|
|
33
|
+
return newValue;
|
|
34
|
+
return item;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
isPopulated() {
|
|
38
|
+
const populated = this.storedItems.length > 0 || this.permissions.length > 0;
|
|
39
|
+
return populated && this.permissionsAreFresh();
|
|
40
|
+
}
|
|
41
|
+
permissionsAreFresh() {
|
|
42
|
+
return (0, lodash_1.every)(this.permissions, (permission) => {
|
|
43
|
+
return Permission_1.Permission.validateJwtExpiry(permission.token);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.Repository = Repository;
|