@vizzly/api-client
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -5,2 +5,3 @@ import { CreateDashboardParams, Dashboard, DashboardFromAPI, DashboardPermission, RequestParams, UpdateDashboardParams } from '../types'; | ||
private vizzly; | ||
private repo; | ||
constructor(vizzly: Vizzly); | ||
@@ -7,0 +8,0 @@ createDashboard(createParams: CreateDashboardParams): Promise<{ |
@@ -19,5 +19,8 @@ "use strict"; | ||
const Permission_1 = require("./Permission"); | ||
const Repository_1 = require("./Repository"); | ||
class DashboardRepository { | ||
// private repo: { dashboards: Dashboard[]; permissions: DashboardPermission[] } | null; | ||
constructor(vizzly) { | ||
this.vizzly = vizzly; | ||
this.repo = new Repository_1.Repository('id'); | ||
} | ||
@@ -37,4 +40,6 @@ createDashboard(createParams) { | ||
const formattedPermissions = builtDashboard.body.permissions.map(Permission_1.Permission.formatDashboardPermission); | ||
const embellishedDashboard = DashboardRepository.embellishDashboardFromAPI(builtDashboard.body.dashboard, formattedPermissions); | ||
this.repo.addItem(embellishedDashboard, formattedPermissions); | ||
return { | ||
dashboard: DashboardRepository.embellishDashboardFromAPI(builtDashboard.body.dashboard, formattedPermissions), | ||
dashboard: embellishedDashboard, | ||
permissions: formattedPermissions, | ||
@@ -64,7 +69,11 @@ }; | ||
if (updatedDashboard.status === 200) { | ||
return DashboardRepository.embellishDashboardFromAPI(updatedDashboard.body.dashboard, [ | ||
const embellishedDashboard = DashboardRepository.embellishDashboardFromAPI(updatedDashboard.body.dashboard, [ | ||
params.dashboardPermission, | ||
]); | ||
this.repo.updateItem(embellishedDashboard); | ||
return embellishedDashboard; | ||
} | ||
throw new FailedToUpdateDashboard_1.FailedToUpdateDashboard(`Failed to update dashboard, got status ${updatedDashboard.status}`); | ||
else { | ||
throw new FailedToUpdateDashboard_1.FailedToUpdateDashboard(`Failed to update dashboard, got status ${updatedDashboard.status}`); | ||
} | ||
}); | ||
@@ -91,22 +100,28 @@ } | ||
let accessTokens; | ||
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) { | ||
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI(); | ||
accessTokens = yield vizzlyQueryEngineApi.execute(vizzlyQueryEngineApi.buildFetchDashboardAccessTokensRequest(params)); | ||
} | ||
else { | ||
const vizzlyApi = this.vizzly.getVizzlyAPI(); | ||
accessTokens = yield vizzlyApi.execute(vizzlyApi.buildFetchDashboardAccessTokensRequest(params)); | ||
} | ||
if (accessTokens.status === 200) { | ||
const formattedPermissions = accessTokens.body.permissions.map(Permission_1.Permission.formatDashboardPermission); | ||
return { | ||
dashboards: accessTokens.body.dashboards.map((dashboard) => { | ||
if (!this.repo.isPopulated()) { | ||
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) { | ||
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI(); | ||
accessTokens = yield vizzlyQueryEngineApi.execute(vizzlyQueryEngineApi.buildFetchDashboardAccessTokensRequest(params)); | ||
} | ||
else { | ||
const vizzlyApi = this.vizzly.getVizzlyAPI(); | ||
accessTokens = yield vizzlyApi.execute(vizzlyApi.buildFetchDashboardAccessTokensRequest(params)); | ||
} | ||
if (accessTokens.status === 200) { | ||
const formattedPermissions = accessTokens.body.permissions.map(Permission_1.Permission.formatDashboardPermission); | ||
const embellishedDashboards = accessTokens.body.dashboards.map((dashboard) => { | ||
if (typeof dashboard.definition === 'string') | ||
throw new FailedToDecryptDashboardBeforeClient_1.FailedToDecryptDashboardBeforeClient(); | ||
return DashboardRepository.embellishDashboardFromAPI(dashboard, formattedPermissions); | ||
}), | ||
permissions: formattedPermissions, | ||
}; | ||
}); | ||
this.repo.initRepo(embellishedDashboards, formattedPermissions); | ||
} | ||
else { | ||
throw new FailedToFetchDashboardAccessTokens_1.FailedToFetchDashboardAccessTokens('Failed to fetch dashboard access tokens.'); | ||
} | ||
} | ||
throw new FailedToFetchDashboardAccessTokens_1.FailedToFetchDashboardAccessTokens('Failed to fetch dashboard access tokens.'); | ||
return { | ||
dashboards: this.repo.getRepo().storedItems, | ||
permissions: this.repo.getRepo().permissions, | ||
}; | ||
}); | ||
@@ -113,0 +128,0 @@ } |
@@ -5,7 +5,7 @@ import { CreateGlobalLibraryParams, GlobalLibrary, GlobalLibraryPermission, RequestParams, Response, UpdateGlobalLibraryParams } from '../types'; | ||
private vizzly; | ||
private cachedPermissions; | ||
private repo; | ||
constructor(vizzly: Vizzly); | ||
createGlobalLibrary(createParams: CreateGlobalLibraryParams): Promise<{ | ||
permissions: GlobalLibraryPermission[]; | ||
global_library: GlobalLibrary; | ||
permissions: GlobalLibraryPermission[]; | ||
}>; | ||
@@ -12,0 +12,0 @@ fetchGlobalLibraries(params?: RequestParams): Promise<{ |
@@ -16,6 +16,7 @@ "use strict"; | ||
const FailedToFetchGlobalLibraryAccessTokens_1 = require("../errors/FailedToFetchGlobalLibraryAccessTokens"); | ||
const Repository_1 = require("./Repository"); | ||
class GlobalLibraryRepository { | ||
constructor(vizzly) { | ||
this.cachedPermissions = { permissions: [] }; | ||
this.vizzly = vizzly; | ||
this.repo = new Repository_1.Repository('id'); | ||
} | ||
@@ -25,9 +26,12 @@ createGlobalLibrary(createParams) { | ||
const vizzlyApi = this.vizzly.getVizzlyAPI(); | ||
let globalLibrary; | ||
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) { | ||
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI(); | ||
return yield vizzlyQueryEngineApi.createGlobalLibrary(createParams); | ||
globalLibrary = yield vizzlyQueryEngineApi.createGlobalLibrary(createParams); | ||
} | ||
else { | ||
return yield vizzlyApi.createGlobalLibrary(createParams); | ||
globalLibrary = yield vizzlyApi.createGlobalLibrary(createParams); | ||
} | ||
this.repo.addItem(globalLibrary.global_library, globalLibrary.permissions); | ||
return globalLibrary; | ||
}); | ||
@@ -45,16 +49,22 @@ } | ||
let accessTokens; | ||
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) { | ||
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI(); | ||
accessTokens = yield vizzlyQueryEngineApi.fetchGlobalLibraryAccessTokens(params); | ||
if (!this.repo.isPopulated()) { | ||
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) { | ||
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI(); | ||
accessTokens = yield vizzlyQueryEngineApi.fetchGlobalLibraryAccessTokens(params); | ||
} | ||
else { | ||
accessTokens = yield vizzlyApi.fetchGlobalLibraryAccessTokens(params); | ||
} | ||
if (accessTokens.status === 200) { | ||
const formattedPermissions = accessTokens.body.permissions.map(Permission_1.Permission.formatGlobalLibraryPermission); | ||
this.repo.initRepo(accessTokens.body.global_libraries, formattedPermissions); | ||
} | ||
else { | ||
throw new FailedToFetchGlobalLibraryAccessTokens_1.FailedToFetchGlobalLibraryAccessTokens('Failed to load access tokens for the global library.'); | ||
} | ||
} | ||
else { | ||
accessTokens = yield vizzlyApi.fetchGlobalLibraryAccessTokens(params); | ||
} | ||
if (accessTokens.status === 200) { | ||
return { | ||
global_libraries: accessTokens.body.global_libraries, | ||
permissions: accessTokens.body.permissions.map(Permission_1.Permission.formatGlobalLibraryPermission), | ||
}; | ||
} | ||
throw new FailedToFetchGlobalLibraryAccessTokens_1.FailedToFetchGlobalLibraryAccessTokens('Failed to load access tokens for the global library.'); | ||
return { | ||
global_libraries: this.repo.getRepo().storedItems, | ||
permissions: this.repo.getRepo().permissions, | ||
}; | ||
}); | ||
@@ -65,9 +75,14 @@ } | ||
const vizzlyApi = this.vizzly.getVizzlyAPI(); | ||
let updateResponse; | ||
if (this.vizzly.implementationStrategy.shouldPersistViaQueryEngine()) { | ||
const vizzlyQueryEngineApi = this.vizzly.getVizzlyQueryEngineAPI(); | ||
return yield vizzlyQueryEngineApi.updateGlobalLibrary(params); | ||
updateResponse = yield vizzlyQueryEngineApi.updateGlobalLibrary(params); | ||
} | ||
else { | ||
return yield vizzlyApi.updateGlobalLibrary(params); | ||
updateResponse = yield vizzlyApi.updateGlobalLibrary(params); | ||
} | ||
if (updateResponse.status === 200) { | ||
this.repo.updateItem(updateResponse.body.global_library); | ||
} | ||
return updateResponse; | ||
}); | ||
@@ -74,0 +89,0 @@ } |
@@ -10,2 +10,3 @@ import { DashboardPermission, GlobalLibraryPermission, GlobalLibraryPermissionFromAPI } from '../types'; | ||
static formatGlobalLibraryPermission(permission: GlobalLibraryPermissionFromAPI): GlobalLibraryPermission; | ||
static validateJwtExpiry(token: string, freshnessBufferInSeconds?: number): boolean; | ||
} |
@@ -21,3 +21,23 @@ "use strict"; | ||
} | ||
static validateJwtExpiry(token, freshnessBufferInSeconds = 10) { | ||
try { | ||
// Split the token into its parts | ||
const [_header, payload, _signature] = token.split('.'); | ||
// Decode the payload (Base64 decoding) | ||
const decodedPayload = Buffer.from(payload, 'base64').toString('utf-8'); | ||
const payloadObj = JSON.parse(decodedPayload); | ||
// Get the expiration time from the payload | ||
const expirationTime = payloadObj.exp; | ||
// Get the current timestamp | ||
const currentTimestamp = Math.floor(Date.now() / 1000); | ||
// Check if the token has expired | ||
const hasExpired = expirationTime < currentTimestamp + freshnessBufferInSeconds; | ||
return !hasExpired; | ||
} | ||
catch (error) { | ||
// Token decoding or validation failed | ||
return false; | ||
} | ||
} | ||
} | ||
exports.Permission = Permission; |
{ | ||
"name": "@vizzly/api-client", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "license": "NONE", |
136535
76
3215