@vizzly/api-client 0.0.37 → 0.0.39

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.
@@ -0,0 +1,3 @@
1
+ export declare class PermissionDenied extends Error {
2
+ constructor(message: string);
3
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PermissionDenied = void 0;
4
+ class PermissionDenied extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ }
8
+ }
9
+ exports.PermissionDenied = PermissionDenied;
@@ -21,3 +21,4 @@ export * from './FailedToCreateQueryEngineUser';
21
21
  export * from './FailedToFetchQueryEngineUser';
22
22
  export * from './ProjectEncryptedSecretAlreadySaved';
23
23
  export * from './FailedToSaveProjectEncryptedEncryptionSecret';
24
+ export * from './PermissionDenied';
@@ -37,3 +37,4 @@ __exportStar(require("./FailedToCreateQueryEngineUser"), exports);
37
37
  __exportStar(require("./FailedToFetchQueryEngineUser"), exports);
38
38
  __exportStar(require("./ProjectEncryptedSecretAlreadySaved"), exports);
39
39
  __exportStar(require("./FailedToSaveProjectEncryptedEncryptionSecret"), exports);
40
+ __exportStar(require("./PermissionDenied"), exports);
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Api = void 0;
16
- const isomorphic_fetch_1 = __importDefault(require("isomorphic-fetch"));
16
+ const cross_fetch_1 = __importDefault(require("cross-fetch"));
17
17
  const Monitor_1 = require("./Monitor");
18
18
  const VizzlyApiClientLogger_1 = require("./VizzlyApiClientLogger");
19
19
  const Path_1 = require("./Path");
@@ -29,7 +29,7 @@ class Api {
29
29
  const authHeaders = this.auth.buildAuthHeaders(request.acceptedAuthParams);
30
30
  const customHeaders = Object.assign(Object.assign({}, (request.headers || {})), (this.extraHeaders() || {}));
31
31
  const path = Path_1.Path.buildPath(this.host, request.path);
32
- const timedFetch = Monitor_1.Monitor.timeRequest(request.method, path, isomorphic_fetch_1.default);
32
+ const timedFetch = Monitor_1.Monitor.timeRequest(request.method, path, cross_fetch_1.default);
33
33
  const res = yield timedFetch(path, {
34
34
  method: request.method,
35
35
  headers: Object.assign(Object.assign({ 'Content-Type': 'application/json', Accept: 'application/json', 'Vizzly-User-Agent': `${VizzlyApiClientLogger_1.VizzlyApiClientLogger.UserAgentProduct}${this.originApp()} (Using client ${(_a = require('../../package.json')) === null || _a === void 0 ? void 0 : _a.version})` }, authHeaders), customHeaders),
@@ -25,7 +25,7 @@ class DashboardManager {
25
25
  }
26
26
  update(params) {
27
27
  return __awaiter(this, void 0, void 0, function* () {
28
- var _a, _b, _c;
28
+ var _a;
29
29
  const dashboardRepository = this.vizzly.getDashboardRepository();
30
30
  const globalLibraryRepository = this.vizzly.getGlobalLibraryRepository();
31
31
  const [{ global_libraries: globalLibraries, permissions: globalLibraryPermissions }, { dashboards, permissions: dashboardPermissions },] = yield Promise.all([
@@ -60,22 +60,22 @@ class DashboardManager {
60
60
  const librariesToUpdate = dashboardToUpdate.parent_dashboard_id === null ? parentGlobalLibraries : childGlobalLibraries;
61
61
  // If it's not a parent dashboard, and there are views or custom fields to save, but there are no libraries
62
62
  // then create one first.
63
- const isParent = dashboardToUpdate.parent_dashboard_id === null;
64
- const hasThingsToSaveInLibrary = libraryViewsToSave.length > 0 || !!((_b = params.definition) === null || _b === void 0 ? void 0 : _b.customFields);
65
- const parentGlobalLibrary = globalLibraries.find((gL) => gL.parent_global_library_id === null);
66
- if (!isParent && parentGlobalLibrary && hasThingsToSaveInLibrary && librariesToUpdate.length === 0) {
67
- yield globalLibraryRepository.createGlobalLibrary({
68
- library: {
69
- views: libraryViewsToSave,
70
- // No concept of being protected, so only option is to save them all
71
- customFields: ((_c = params.definition) === null || _c === void 0 ? void 0 : _c.customFields) || {},
72
- },
73
- parentGlobalLibraryId: parentGlobalLibrary.id,
74
- permissions: [{ scope: 'read_write' }],
75
- });
76
- }
63
+ // const isParent = dashboardToUpdate.parent_dashboard_id === null;
64
+ // const hasThingsToSaveInLibrary = libraryViewsToSave.length > 0 || !!params.definition?.customFields;
65
+ // const parentGlobalLibrary = globalLibraries.find((gL) => gL.parent_global_library_id === null);
66
+ // if (!isParent && parentGlobalLibrary && hasThingsToSaveInLibrary && librariesToUpdate.length === 0) {
67
+ // await globalLibraryRepository.createGlobalLibrary({
68
+ // library: {
69
+ // views: libraryViewsToSave,
70
+ // // No concept of being protected, so only option is to save them all
71
+ // customFields: params.definition?.customFields || {},
72
+ // },
73
+ // parentGlobalLibraryId: parentGlobalLibrary!.id,
74
+ // permissions: [{ scope: 'read_write' }],
75
+ // });
76
+ // }
77
77
  const updatePromises = librariesToUpdate.map((gL) => __awaiter(this, void 0, void 0, function* () {
78
- var _d;
78
+ var _b;
79
79
  const writePermission = globalLibraryPermissions.find((permission) => permission.globalLibraryId === gL.id && permission.scope === 'read_write');
80
80
  if (!writePermission)
81
81
  VizzlyApiClientLogger_1.VizzlyApiClientLogger.error('No write permission found.');
@@ -85,7 +85,7 @@ class DashboardManager {
85
85
  library: {
86
86
  views: libraryViewsToSave,
87
87
  // No concept of being protected, so only option is to save them all
88
- customFields: ((_d = params.definition) === null || _d === void 0 ? void 0 : _d.customFields) || {},
88
+ customFields: ((_b = params.definition) === null || _b === void 0 ? void 0 : _b.customFields) || {},
89
89
  },
90
90
  });
91
91
  }
@@ -5,8 +5,8 @@ export declare class GlobalLibraryManager {
5
5
  private vizzly;
6
6
  constructor(vizzly: Vizzly);
7
7
  create(params: RequestParams<CreateGlobalLibraryParams>): Promise<{
8
- permissions: import("../types").GlobalLibraryPermission[];
9
8
  global_library: GlobalLibrary;
9
+ permissions: import("../types").GlobalLibraryPermission[];
10
10
  }>;
11
11
  update(params: RequestParams<UpdateGlobalLibraryParams & {
12
12
  globalLibraryId: string;
@@ -5,8 +5,8 @@ export declare class GlobalLibraryRepository {
5
5
  private repo;
6
6
  constructor(vizzly: Vizzly);
7
7
  createGlobalLibrary(createParams: CreateGlobalLibraryParams): Promise<{
8
- permissions: GlobalLibraryPermission[];
9
8
  global_library: GlobalLibrary;
9
+ permissions: GlobalLibraryPermission[];
10
10
  }>;
11
11
  fetchGlobalLibraries(params?: RequestParams): Promise<{
12
12
  global_libraries: GlobalLibrary[];
@@ -15,6 +15,7 @@ const Permission_1 = require("./Permission");
15
15
  const FailedToFetchGlobalLibraryAccessTokens_1 = require("../errors/FailedToFetchGlobalLibraryAccessTokens");
16
16
  const Repository_1 = require("./Repository");
17
17
  const FailedToUpdateGlobalLibrary_1 = require("../errors/FailedToUpdateGlobalLibrary");
18
+ const errors_1 = require("../errors");
18
19
  class GlobalLibraryRepository {
19
20
  constructor(vizzly) {
20
21
  this.vizzly = vizzly;
@@ -31,15 +32,49 @@ class GlobalLibraryRepository {
31
32
  else {
32
33
  globalLibrary = yield vizzlyApi.createGlobalLibrary(createParams);
33
34
  }
34
- globalLibrary.global_library = GlobalLibraryRepository.upcast(globalLibrary.global_library);
35
- this.repo.addItem(globalLibrary.global_library, globalLibrary.permissions);
36
- return globalLibrary;
35
+ const upcastedLibrary = GlobalLibraryRepository.upcast(globalLibrary.global_library);
36
+ const formattedPermissions = globalLibrary.permissions.map(Permission_1.Permission.formatGlobalLibraryPermission);
37
+ this.repo.addItem(upcastedLibrary, formattedPermissions);
38
+ return {
39
+ global_library: upcastedLibrary,
40
+ permissions: formattedPermissions,
41
+ };
37
42
  });
38
43
  }
39
44
  fetchGlobalLibraries(params) {
40
45
  return __awaiter(this, void 0, void 0, function* () {
41
46
  // Now, we fetch the libraries and the access tokens from the access call.
42
- return yield this.fetchGlobalLibraryAccessTokens(params);
47
+ let libs = yield this.fetchGlobalLibraryAccessTokens(params);
48
+ // Find the parent library.
49
+ const parentLibrary = libs.global_libraries.find((l) => l.parent_global_library_id === null);
50
+ // Find any child library.
51
+ const childLibrary = libs.global_libraries.find((l) => l.parent_global_library_id != null);
52
+ // If libraries have been returned, but no child library exists
53
+ // then create the first and only one.
54
+ if (libs.global_libraries.length > 0 && parentLibrary && !childLibrary) {
55
+ try {
56
+ const created = yield this.createGlobalLibrary({
57
+ parentGlobalLibraryId: parentLibrary.id,
58
+ library: {
59
+ customFields: {},
60
+ views: [],
61
+ },
62
+ permissions: [{ scope: 'read_write' }],
63
+ });
64
+ libs = {
65
+ global_libraries: [...libs.global_libraries, created.global_library],
66
+ permissions: [...libs.permissions, ...created.permissions],
67
+ };
68
+ }
69
+ catch (e) {
70
+ // Only throw if the error is not a permission denied. A permission denied
71
+ // is expected if the user only has `read` access on their identity JWT token.
72
+ if (!(e instanceof errors_1.PermissionDenied)) {
73
+ throw e;
74
+ }
75
+ }
76
+ }
77
+ return libs;
43
78
  });
44
79
  }
45
80
  fetchGlobalLibraryAccessTokens(params) {
@@ -56,7 +91,7 @@ class GlobalLibraryRepository {
56
91
  }
57
92
  if (accessTokens.status === 200) {
58
93
  const formattedPermissions = accessTokens.body.permissions.map(Permission_1.Permission.formatGlobalLibraryPermission);
59
- this.repo.initRepo(accessTokens.body.global_libraries.map(GlobalLibraryRepository.upcast), formattedPermissions);
94
+ this.repo.initRepo(accessTokens.body.global_libraries, formattedPermissions);
60
95
  }
61
96
  else {
62
97
  throw new FailedToFetchGlobalLibraryAccessTokens_1.FailedToFetchGlobalLibraryAccessTokens('Failed to load access tokens for the global library.');
@@ -1,6 +1,6 @@
1
1
  import { Api } from './Api';
2
2
  import { Authentication } from './Authentication';
3
- import { CreateDashboardParams, CreateGlobalLibraryParams, GlobalLibrary, GlobalLibraryPermission, GlobalLibraryPermissionFromAPI, QueryEngineUserFromAPI, Request, RequestParams, TeamMemberFromAPI, UpdateDashboardParams, UpdateDashboardResponseBody, UpdateGlobalLibraryParams } from '../types';
3
+ import { CreateDashboardParams, CreateGlobalLibraryParams, GlobalLibrary, GlobalLibraryPermissionFromAPI, QueryEngineUserFromAPI, Request, RequestParams, TeamMemberFromAPI, UpdateDashboardParams, UpdateDashboardResponseBody, UpdateGlobalLibraryParams } from '../types';
4
4
  import { nVizzlyApi } from '../types';
5
5
  import { ProjectFromAPI } from '../types/project';
6
6
  export declare class VizzlyApi extends Api {
@@ -40,7 +40,7 @@ export declare class VizzlyApi extends Api {
40
40
  buildCreateParentGlobalLibraryRequest(params: RequestParams<nVizzlyApi.CreateParentGlobalLibraryParams>): Request<{}>;
41
41
  createGlobalLibrary(params: RequestParams<CreateGlobalLibraryParams>): Promise<{
42
42
  global_library: GlobalLibrary;
43
- permissions: Array<GlobalLibraryPermission>;
43
+ permissions: Array<GlobalLibraryPermissionFromAPI>;
44
44
  }>;
45
45
  buildCreateGlobalLibraryRequest(params: RequestParams<CreateGlobalLibraryParams>): Request<{}>;
46
46
  getProjects(params?: RequestParams): Promise<{
@@ -126,7 +126,9 @@ class VizzlyApi extends Api_1.Api {
126
126
  const result = yield this.execute(this.buildCreateGlobalLibraryRequest(params));
127
127
  if (result.status === 200)
128
128
  return result.body;
129
- throw new errors_1.FailedToCreateGlobalLibrary('Failed to create global library');
129
+ if (result.status === 403)
130
+ throw new errors_1.PermissionDenied('Permission denied when creating a global library via the Vizzly API.');
131
+ throw new errors_1.FailedToCreateGlobalLibrary(`Failed to create global library, got status ${result.status}`);
130
132
  });
131
133
  }
132
134
  buildCreateGlobalLibraryRequest(params) {
@@ -1,6 +1,6 @@
1
1
  import { Api } from './Api';
2
2
  import { Authentication } from './Authentication';
3
- import { nVizzlyQueryEngine, Request, extraHeaders, CreateDashboardParams, UpdateDashboardParams, UpdateGlobalLibraryParams, GlobalLibrary, CreateGlobalLibraryParams, GlobalLibraryPermission, GlobalLibraryPermissionFromAPI, RequestParams } from '../types';
3
+ import { nVizzlyQueryEngine, Request, extraHeaders, CreateDashboardParams, UpdateDashboardParams, UpdateGlobalLibraryParams, GlobalLibrary, CreateGlobalLibraryParams, GlobalLibraryPermissionFromAPI, RequestParams } from '../types';
4
4
  export declare class VizzlyQueryEngineApi extends Api {
5
5
  constructor(auth: Authentication, host: string, extraHeaders?: extraHeaders);
6
6
  buildCreateDashboardRequest(params: RequestParams<CreateDashboardParams>): Request<CreateDashboardParams & {
@@ -57,7 +57,7 @@ export declare class VizzlyQueryEngineApi extends Api {
57
57
  buildStatusRequest(params?: RequestParams): Request<{}>;
58
58
  createGlobalLibrary(params: RequestParams<CreateGlobalLibraryParams>): Promise<{
59
59
  global_library: GlobalLibrary;
60
- permissions: Array<GlobalLibraryPermission>;
60
+ permissions: Array<GlobalLibraryPermissionFromAPI>;
61
61
  }>;
62
62
  buildCreateGlobalLibraryRequest(params: RequestParams<CreateGlobalLibraryParams>): Request<{}>;
63
63
  fetchGlobalLibraries(params: RequestParams<{
@@ -310,6 +310,8 @@ class VizzlyQueryEngineApi extends Api_1.Api {
310
310
  const result = yield this.execute(this.buildCreateGlobalLibraryRequest(params));
311
311
  if (result.status === 200)
312
312
  return result.body;
313
+ if (result.status === 403)
314
+ throw new errors_1.PermissionDenied('Permission denied when creating a global library via the Vizzly Query Engine.');
313
315
  throw new errors_1.FailedToCreateGlobalLibrary(`Failed to create a global library. Got status ${result.status}`);
314
316
  });
315
317
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizzly/api-client",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "private": false,
5
5
  "license": "NONE",
6
6
  "source": "src/index.ts",
@@ -11,7 +11,6 @@
11
11
  "dist"
12
12
  ],
13
13
  "devDependencies": {
14
- "@types/isomorphic-fetch": "^0.0.39",
15
14
  "@types/jest": "^29.5.12",
16
15
  "@types/lodash": "^4.17.0",
17
16
  "@types/uuid": "^9.0.8",
@@ -33,6 +32,6 @@
33
32
  "prepublish": "yarn build"
34
33
  },
35
34
  "dependencies": {
36
- "isomorphic-fetch": "^3.0.0"
35
+ "cross-fetch": "^4.0.0"
37
36
  }
38
37
  }