@lightdash-tools/client 0.1.0

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 (154) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +144 -0
  3. package/dist/api/base-client.d.ts +13 -0
  4. package/dist/api/base-client.js +17 -0
  5. package/dist/api/v1/ai-agents.d.ts +17 -0
  6. package/dist/api/v1/ai-agents.js +47 -0
  7. package/dist/api/v1/ai-agents.test.d.ts +1 -0
  8. package/dist/api/v1/ai-agents.test.js +77 -0
  9. package/dist/api/v1/charts.d.ts +18 -0
  10. package/dist/api/v1/charts.js +45 -0
  11. package/dist/api/v1/charts.test.d.ts +1 -0
  12. package/dist/api/v1/charts.test.js +106 -0
  13. package/dist/api/v1/dashboards.d.ts +9 -0
  14. package/dist/api/v1/dashboards.js +25 -0
  15. package/dist/api/v1/dashboards.test.d.ts +1 -0
  16. package/dist/api/v1/dashboards.test.js +33 -0
  17. package/dist/api/v1/explores.d.ts +11 -0
  18. package/dist/api/v1/explores.js +31 -0
  19. package/dist/api/v1/explores.test.d.ts +1 -0
  20. package/dist/api/v1/explores.test.js +46 -0
  21. package/dist/api/v1/groups.d.ts +42 -0
  22. package/dist/api/v1/groups.js +81 -0
  23. package/dist/api/v1/groups.test.d.ts +1 -0
  24. package/dist/api/v1/groups.test.js +107 -0
  25. package/dist/api/v1/organizations.d.ts +9 -0
  26. package/dist/api/v1/organizations.js +25 -0
  27. package/dist/api/v1/organizations.test.d.ts +1 -0
  28. package/dist/api/v1/organizations.test.js +33 -0
  29. package/dist/api/v1/project-access.d.ts +40 -0
  30. package/dist/api/v1/project-access.js +88 -0
  31. package/dist/api/v1/project-access.test.d.ts +1 -0
  32. package/dist/api/v1/project-access.test.js +109 -0
  33. package/dist/api/v1/projects.d.ts +13 -0
  34. package/dist/api/v1/projects.js +37 -0
  35. package/dist/api/v1/projects.test.d.ts +1 -0
  36. package/dist/api/v1/projects.test.js +49 -0
  37. package/dist/api/v1/query.d.ts +11 -0
  38. package/dist/api/v1/query.js +31 -0
  39. package/dist/api/v1/query.test.d.ts +1 -0
  40. package/dist/api/v1/query.test.js +51 -0
  41. package/dist/api/v1/spaces.d.ts +25 -0
  42. package/dist/api/v1/spaces.js +73 -0
  43. package/dist/api/v1/spaces.test.d.ts +1 -0
  44. package/dist/api/v1/spaces.test.js +91 -0
  45. package/dist/api/v1/users.d.ts +35 -0
  46. package/dist/api/v1/users.js +66 -0
  47. package/dist/api/v1/users.test.d.ts +1 -0
  48. package/dist/api/v1/users.test.js +122 -0
  49. package/dist/api/v2/organization-roles.d.ts +45 -0
  50. package/dist/api/v2/organization-roles.js +81 -0
  51. package/dist/api/v2/organization-roles.test.d.ts +1 -0
  52. package/dist/api/v2/organization-roles.test.js +112 -0
  53. package/dist/api/v2/project-role-assignments.d.ts +23 -0
  54. package/dist/api/v2/project-role-assignments.js +55 -0
  55. package/dist/api/v2/project-role-assignments.test.d.ts +1 -0
  56. package/dist/api/v2/project-role-assignments.test.js +66 -0
  57. package/dist/api/v2/query.d.ts +17 -0
  58. package/dist/api/v2/query.js +49 -0
  59. package/dist/api/v2/query.test.d.ts +1 -0
  60. package/dist/api/v2/query.test.js +78 -0
  61. package/dist/client.d.ts +118 -0
  62. package/dist/client.js +113 -0
  63. package/dist/client.test.d.ts +1 -0
  64. package/dist/client.test.js +94 -0
  65. package/dist/config.d.ts +83 -0
  66. package/dist/config.js +22 -0
  67. package/dist/errors.d.ts +39 -0
  68. package/dist/errors.js +46 -0
  69. package/dist/errors.test.d.ts +1 -0
  70. package/dist/errors.test.js +29 -0
  71. package/dist/http/axios-client.d.ts +22 -0
  72. package/dist/http/axios-client.js +51 -0
  73. package/dist/http/http-client.d.ts +33 -0
  74. package/dist/http/http-client.js +69 -0
  75. package/dist/http/interceptors.d.ts +13 -0
  76. package/dist/http/interceptors.js +82 -0
  77. package/dist/http/rate-limiter.d.ts +23 -0
  78. package/dist/http/rate-limiter.js +60 -0
  79. package/dist/http/rate-limiter.test.d.ts +1 -0
  80. package/dist/http/rate-limiter.test.js +40 -0
  81. package/dist/index.d.ts +22 -0
  82. package/dist/index.js +41 -0
  83. package/dist/index.test.d.ts +1 -0
  84. package/dist/index.test.js +65 -0
  85. package/dist/pagination/fetch-all-pages.d.ts +15 -0
  86. package/dist/pagination/fetch-all-pages.js +41 -0
  87. package/dist/pagination/fetch-all-pages.test.d.ts +1 -0
  88. package/dist/pagination/fetch-all-pages.test.js +70 -0
  89. package/dist/pagination/types.d.ts +14 -0
  90. package/dist/pagination/types.js +6 -0
  91. package/dist/types/api.d.ts +22 -0
  92. package/dist/types/api.js +8 -0
  93. package/dist/utils/env.d.ts +20 -0
  94. package/dist/utils/env.js +59 -0
  95. package/dist/utils/env.test.d.ts +1 -0
  96. package/dist/utils/env.test.js +73 -0
  97. package/dist/utils/logger.d.ts +8 -0
  98. package/dist/utils/logger.js +20 -0
  99. package/dist/utils/retry.d.ts +14 -0
  100. package/dist/utils/retry.js +67 -0
  101. package/dist/utils/retry.test.d.ts +1 -0
  102. package/dist/utils/retry.test.js +53 -0
  103. package/package.json +21 -0
  104. package/src/api/base-client.ts +14 -0
  105. package/src/api/v1/ai-agents.test.ts +74 -0
  106. package/src/api/v1/ai-agents.ts +41 -0
  107. package/src/api/v1/charts.test.ts +103 -0
  108. package/src/api/v1/charts.ts +51 -0
  109. package/src/api/v1/dashboards.test.ts +26 -0
  110. package/src/api/v1/dashboards.ts +15 -0
  111. package/src/api/v1/explores.test.ts +40 -0
  112. package/src/api/v1/explores.ts +18 -0
  113. package/src/api/v1/groups.test.ts +108 -0
  114. package/src/api/v1/groups.ts +100 -0
  115. package/src/api/v1/organizations.test.ts +26 -0
  116. package/src/api/v1/organizations.ts +13 -0
  117. package/src/api/v1/project-access.test.ts +110 -0
  118. package/src/api/v1/project-access.ts +102 -0
  119. package/src/api/v1/projects.test.ts +44 -0
  120. package/src/api/v1/projects.ts +23 -0
  121. package/src/api/v1/query.test.ts +46 -0
  122. package/src/api/v1/query.ts +37 -0
  123. package/src/api/v1/spaces.test.ts +92 -0
  124. package/src/api/v1/spaces.ts +83 -0
  125. package/src/api/v1/users.test.ts +125 -0
  126. package/src/api/v1/users.ts +76 -0
  127. package/src/api/v2/organization-roles.test.ts +115 -0
  128. package/src/api/v2/organization-roles.ts +90 -0
  129. package/src/api/v2/project-role-assignments.test.ts +75 -0
  130. package/src/api/v2/project-role-assignments.ts +63 -0
  131. package/src/api/v2/query.test.ts +81 -0
  132. package/src/api/v2/query.ts +69 -0
  133. package/src/client.test.ts +105 -0
  134. package/src/client.ts +182 -0
  135. package/src/config.ts +113 -0
  136. package/src/errors.test.ts +45 -0
  137. package/src/errors.ts +67 -0
  138. package/src/http/axios-client.ts +59 -0
  139. package/src/http/http-client.ts +79 -0
  140. package/src/http/interceptors.ts +110 -0
  141. package/src/http/rate-limiter.test.ts +34 -0
  142. package/src/http/rate-limiter.ts +54 -0
  143. package/src/index.test.ts +91 -0
  144. package/src/index.ts +41 -0
  145. package/src/pagination/fetch-all-pages.test.ts +75 -0
  146. package/src/pagination/fetch-all-pages.ts +39 -0
  147. package/src/pagination/types.ts +16 -0
  148. package/src/types/api.ts +28 -0
  149. package/src/utils/env.test.ts +88 -0
  150. package/src/utils/env.ts +69 -0
  151. package/src/utils/logger.ts +21 -0
  152. package/src/utils/retry.test.ts +49 -0
  153. package/src/utils/retry.ts +60 -0
  154. package/tsconfig.json +13 -0
@@ -0,0 +1,115 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { OrganizationRolesClient } from './organization-roles';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('OrganizationRolesClient', () => {
6
+ let mockHttp: HttpClient;
7
+
8
+ beforeEach(() => {
9
+ mockHttp = {
10
+ get: vi.fn(),
11
+ post: vi.fn(),
12
+ put: vi.fn(),
13
+ patch: vi.fn(),
14
+ delete: vi.fn(),
15
+ } as unknown as HttpClient;
16
+ });
17
+
18
+ it('getRoles should call GET /orgs/{orgUuid}/roles with optional params', async () => {
19
+ const client = new OrganizationRolesClient(mockHttp);
20
+ const results = [{ roleUuid: 'r1', name: 'Role 1' }];
21
+ vi.mocked(mockHttp.get).mockResolvedValue(results);
22
+ const result = await client.getRoles('org-1');
23
+ expect(mockHttp.get).toHaveBeenCalledWith('/orgs/org-1/roles', {
24
+ params: undefined,
25
+ });
26
+ expect(result).toEqual(results);
27
+ });
28
+
29
+ it('getRoles with params should pass query params', async () => {
30
+ const client = new OrganizationRolesClient(mockHttp);
31
+ vi.mocked(mockHttp.get).mockResolvedValue([]);
32
+ await client.getRoles('org-1', { load: 'scopes', roleTypeFilter: 'custom' });
33
+ expect(mockHttp.get).toHaveBeenCalledWith('/orgs/org-1/roles', {
34
+ params: { load: 'scopes', roleTypeFilter: 'custom' },
35
+ });
36
+ });
37
+
38
+ it('createRole should call POST /orgs/{orgUuid}/roles with body', async () => {
39
+ const client = new OrganizationRolesClient(mockHttp);
40
+ const body = { name: 'Editor', description: 'Can edit' };
41
+ const role = { roleUuid: 'r1', name: 'Editor' };
42
+ vi.mocked(mockHttp.post).mockResolvedValue(role);
43
+ const result = await client.createRole('org-1', body);
44
+ expect(mockHttp.post).toHaveBeenCalledWith('/orgs/org-1/roles', body);
45
+ expect(result).toEqual(role);
46
+ });
47
+
48
+ it('getRole should call GET /orgs/{orgUuid}/roles/{roleUuid}', async () => {
49
+ const client = new OrganizationRolesClient(mockHttp);
50
+ const role = { roleUuid: 'r1', name: 'Editor', scopes: [] };
51
+ vi.mocked(mockHttp.get).mockResolvedValue(role);
52
+ const result = await client.getRole('org-1', 'r1');
53
+ expect(mockHttp.get).toHaveBeenCalledWith('/orgs/org-1/roles/r1');
54
+ expect(result).toEqual(role);
55
+ });
56
+
57
+ it('updateRole should call PATCH /orgs/{orgUuid}/roles/{roleUuid}', async () => {
58
+ const client = new OrganizationRolesClient(mockHttp);
59
+ const body = { name: 'Updated' };
60
+ vi.mocked(mockHttp.patch).mockResolvedValue({ roleUuid: 'r1', name: 'Updated' });
61
+ await client.updateRole('org-1', 'r1', body);
62
+ expect(mockHttp.patch).toHaveBeenCalledWith('/orgs/org-1/roles/r1', body);
63
+ });
64
+
65
+ it('deleteRole should call DELETE /orgs/{orgUuid}/roles/{roleUuid}', async () => {
66
+ const client = new OrganizationRolesClient(mockHttp);
67
+ vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
68
+ await client.deleteRole('org-1', 'r1');
69
+ expect(mockHttp.delete).toHaveBeenCalledWith('/orgs/org-1/roles/r1');
70
+ });
71
+
72
+ it('addScopesToRole should call POST .../scopes with body', async () => {
73
+ const client = new OrganizationRolesClient(mockHttp);
74
+ const body = { scopeNames: ['project:read'] };
75
+ vi.mocked(mockHttp.post).mockResolvedValue(undefined);
76
+ await client.addScopesToRole('org-1', 'r1', body);
77
+ expect(mockHttp.post).toHaveBeenCalledWith('/orgs/org-1/roles/r1/scopes', body);
78
+ });
79
+
80
+ it('removeScopeFromRole should call DELETE .../scopes/{scopeName}', async () => {
81
+ const client = new OrganizationRolesClient(mockHttp);
82
+ vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
83
+ await client.removeScopeFromRole('org-1', 'r1', 'project:read');
84
+ expect(mockHttp.delete).toHaveBeenCalledWith('/orgs/org-1/roles/r1/scopes/project%3Aread');
85
+ });
86
+
87
+ it('duplicateRole should call POST .../duplicate with body', async () => {
88
+ const client = new OrganizationRolesClient(mockHttp);
89
+ const body = { name: 'Copy of Role' };
90
+ vi.mocked(mockHttp.post).mockResolvedValue({
91
+ roleUuid: 'r2',
92
+ name: 'Copy of Role',
93
+ scopes: [],
94
+ });
95
+ await client.duplicateRole('org-1', 'r1', body);
96
+ expect(mockHttp.post).toHaveBeenCalledWith('/orgs/org-1/roles/r1/duplicate', body);
97
+ });
98
+
99
+ it('listRoleAssignments should call GET /orgs/{orgUuid}/roles/assignments', async () => {
100
+ const client = new OrganizationRolesClient(mockHttp);
101
+ const assignments = [{ roleId: 'r1', roleName: 'Admin' }];
102
+ vi.mocked(mockHttp.get).mockResolvedValue(assignments);
103
+ const result = await client.listRoleAssignments('org-1');
104
+ expect(mockHttp.get).toHaveBeenCalledWith('/orgs/org-1/roles/assignments');
105
+ expect(result).toEqual(assignments);
106
+ });
107
+
108
+ it('assignRoleToUser should call POST .../assignments/user/{userId}', async () => {
109
+ const client = new OrganizationRolesClient(mockHttp);
110
+ const body = { roleId: 'r1' };
111
+ vi.mocked(mockHttp.post).mockResolvedValue({ roleId: 'r1' });
112
+ await client.assignRoleToUser('org-1', 'user-1', body);
113
+ expect(mockHttp.post).toHaveBeenCalledWith('/orgs/org-1/roles/assignments/user/user-1', body);
114
+ });
115
+ });
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Organization roles API client (v2). Manage org-level roles and role assignments.
3
+ */
4
+
5
+ import type { components } from '@lightdash-tools/common';
6
+ import { BaseApiClient } from '../base-client';
7
+
8
+ type CreateRole = components['schemas']['CreateRole'];
9
+ type UpdateRole = components['schemas']['UpdateRole'];
10
+ type AddScopesToRole = components['schemas']['AddScopesToRole'];
11
+ type Role = components['schemas']['Role'];
12
+ type RoleWithScopes = components['schemas']['RoleWithScopes'];
13
+ type RoleAssignment = components['schemas']['RoleAssignment'];
14
+
15
+ /** Response shape for getRoles (array of Role or RoleWithScopes). */
16
+ export type GetRolesResult = Role[] | RoleWithScopes[];
17
+
18
+ /** Query params for getRoles. */
19
+ export interface GetRolesParams {
20
+ load?: string;
21
+ roleTypeFilter?: string;
22
+ }
23
+
24
+ /** Request body for assigning an org role to a user. */
25
+ export interface AssignOrgRoleToUserBody {
26
+ roleId: string;
27
+ }
28
+
29
+ export class OrganizationRolesClient extends BaseApiClient {
30
+ /** Get roles for an organization. */
31
+ async getRoles(orgUuid: string, params?: GetRolesParams): Promise<GetRolesResult> {
32
+ return this.http.get<GetRolesResult>(`/orgs/${orgUuid}/roles`, {
33
+ params,
34
+ });
35
+ }
36
+
37
+ /** Create a custom role in the organization. */
38
+ async createRole(orgUuid: string, body: CreateRole): Promise<Role> {
39
+ return this.http.post<Role>(`/orgs/${orgUuid}/roles`, body);
40
+ }
41
+
42
+ /** Get a custom role by UUID. */
43
+ async getRole(orgUuid: string, roleUuid: string): Promise<RoleWithScopes> {
44
+ return this.http.get<RoleWithScopes>(`/orgs/${orgUuid}/roles/${roleUuid}`);
45
+ }
46
+
47
+ /** Update a custom role. */
48
+ async updateRole(orgUuid: string, roleUuid: string, body: UpdateRole): Promise<Role> {
49
+ return this.http.patch<Role>(`/orgs/${orgUuid}/roles/${roleUuid}`, body);
50
+ }
51
+
52
+ /** Delete a custom role from the organization. */
53
+ async deleteRole(orgUuid: string, roleUuid: string): Promise<void> {
54
+ await this.http.delete(`/orgs/${orgUuid}/roles/${roleUuid}`);
55
+ }
56
+
57
+ /** Add scopes to a role. */
58
+ async addScopesToRole(orgUuid: string, roleUuid: string, body: AddScopesToRole): Promise<void> {
59
+ await this.http.post(`/orgs/${orgUuid}/roles/${roleUuid}/scopes`, body);
60
+ }
61
+
62
+ /** Remove a scope from a role. */
63
+ async removeScopeFromRole(orgUuid: string, roleUuid: string, scopeName: string): Promise<void> {
64
+ await this.http.delete(
65
+ `/orgs/${orgUuid}/roles/${roleUuid}/scopes/${encodeURIComponent(scopeName)}`,
66
+ );
67
+ }
68
+
69
+ /** Duplicate a role. */
70
+ async duplicateRole(orgUuid: string, roleId: string, body: CreateRole): Promise<RoleWithScopes> {
71
+ return this.http.post<RoleWithScopes>(`/orgs/${orgUuid}/roles/${roleId}/duplicate`, body);
72
+ }
73
+
74
+ /** List organization role assignments (system roles only). */
75
+ async listRoleAssignments(orgUuid: string): Promise<RoleAssignment[]> {
76
+ return this.http.get<RoleAssignment[]>(`/orgs/${orgUuid}/roles/assignments`);
77
+ }
78
+
79
+ /** Assign an organization role to a user. */
80
+ async assignRoleToUser(
81
+ orgUuid: string,
82
+ userId: string,
83
+ body: AssignOrgRoleToUserBody,
84
+ ): Promise<RoleAssignment> {
85
+ return this.http.post<RoleAssignment>(
86
+ `/orgs/${orgUuid}/roles/assignments/user/${userId}`,
87
+ body,
88
+ );
89
+ }
90
+ }
@@ -0,0 +1,75 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { ProjectRoleAssignmentsClient } from './project-role-assignments';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('ProjectRoleAssignmentsClient', () => {
6
+ let mockHttp: HttpClient;
7
+
8
+ beforeEach(() => {
9
+ mockHttp = {
10
+ get: vi.fn(),
11
+ post: vi.fn(),
12
+ put: vi.fn(),
13
+ patch: vi.fn(),
14
+ delete: vi.fn(),
15
+ } as unknown as HttpClient;
16
+ });
17
+
18
+ it('listAssignments should call GET /projects/{projectId}/roles/assignments', async () => {
19
+ const client = new ProjectRoleAssignmentsClient(mockHttp);
20
+ const assignments = [{ roleId: 'r1', roleName: 'Editor' }];
21
+ vi.mocked(mockHttp.get).mockResolvedValue(assignments);
22
+ const result = await client.listAssignments('proj-1');
23
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/proj-1/roles/assignments');
24
+ expect(result).toEqual(assignments);
25
+ });
26
+
27
+ it('upsertUserAssignment should call POST .../user/{userId} with body', async () => {
28
+ const client = new ProjectRoleAssignmentsClient(mockHttp);
29
+ const body = { roleId: 'r1' };
30
+ vi.mocked(mockHttp.post).mockResolvedValue({ roleId: 'r1' });
31
+ await client.upsertUserAssignment('proj-1', 'user-1', body);
32
+ expect(mockHttp.post).toHaveBeenCalledWith(
33
+ '/projects/proj-1/roles/assignments/user/user-1',
34
+ body,
35
+ );
36
+ });
37
+
38
+ it('deleteUserAssignment should call DELETE .../user/{userId}', async () => {
39
+ const client = new ProjectRoleAssignmentsClient(mockHttp);
40
+ vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
41
+ await client.deleteUserAssignment('proj-1', 'user-1');
42
+ expect(mockHttp.delete).toHaveBeenCalledWith('/projects/proj-1/roles/assignments/user/user-1');
43
+ });
44
+
45
+ it('upsertGroupAssignment should call POST .../group/{groupId} with body', async () => {
46
+ const client = new ProjectRoleAssignmentsClient(mockHttp);
47
+ const body = { roleId: 'r1' };
48
+ vi.mocked(mockHttp.post).mockResolvedValue({ roleId: 'r1' });
49
+ await client.upsertGroupAssignment('proj-1', 'group-1', body);
50
+ expect(mockHttp.post).toHaveBeenCalledWith(
51
+ '/projects/proj-1/roles/assignments/group/group-1',
52
+ body,
53
+ );
54
+ });
55
+
56
+ it('deleteGroupAssignment should call DELETE .../group/{groupId}', async () => {
57
+ const client = new ProjectRoleAssignmentsClient(mockHttp);
58
+ vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
59
+ await client.deleteGroupAssignment('proj-1', 'group-1');
60
+ expect(mockHttp.delete).toHaveBeenCalledWith(
61
+ '/projects/proj-1/roles/assignments/group/group-1',
62
+ );
63
+ });
64
+
65
+ it('updateGroupAssignment should call PATCH .../group/{groupId}', async () => {
66
+ const client = new ProjectRoleAssignmentsClient(mockHttp);
67
+ const body = { roleId: 'r2' };
68
+ vi.mocked(mockHttp.patch).mockResolvedValue({ roleId: 'r2' });
69
+ await client.updateGroupAssignment('proj-1', 'group-1', body);
70
+ expect(mockHttp.patch).toHaveBeenCalledWith(
71
+ '/projects/proj-1/roles/assignments/group/group-1',
72
+ body,
73
+ );
74
+ });
75
+ });
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Project role assignments API client (v2). Manage project-level role assignments for users and groups.
3
+ */
4
+
5
+ import type { components } from '@lightdash-tools/common';
6
+ import { BaseApiClient } from '../base-client';
7
+
8
+ type RoleAssignment = components['schemas']['RoleAssignment'];
9
+ type UpsertUserRoleAssignmentRequest = components['schemas']['UpsertUserRoleAssignmentRequest'];
10
+ type UpdateRoleAssignmentRequest = components['schemas']['UpdateRoleAssignmentRequest'];
11
+
12
+ export class ProjectRoleAssignmentsClient extends BaseApiClient {
13
+ /** List project role assignments. */
14
+ async listAssignments(projectId: string): Promise<RoleAssignment[]> {
15
+ return this.http.get<RoleAssignment[]>(`/projects/${projectId}/roles/assignments`);
16
+ }
17
+
18
+ /** Create or update project role assignment for a user (upsert). */
19
+ async upsertUserAssignment(
20
+ projectId: string,
21
+ userId: string,
22
+ body: UpsertUserRoleAssignmentRequest,
23
+ ): Promise<RoleAssignment> {
24
+ return this.http.post<RoleAssignment>(
25
+ `/projects/${projectId}/roles/assignments/user/${userId}`,
26
+ body,
27
+ );
28
+ }
29
+
30
+ /** Delete project role assignment for a user. */
31
+ async deleteUserAssignment(projectId: string, userId: string): Promise<void> {
32
+ await this.http.delete(`/projects/${projectId}/roles/assignments/user/${userId}`);
33
+ }
34
+
35
+ /** Create or update project role assignment for a group (upsert). */
36
+ async upsertGroupAssignment(
37
+ projectId: string,
38
+ groupId: string,
39
+ body: UpsertUserRoleAssignmentRequest,
40
+ ): Promise<RoleAssignment> {
41
+ return this.http.post<RoleAssignment>(
42
+ `/projects/${projectId}/roles/assignments/group/${groupId}`,
43
+ body,
44
+ );
45
+ }
46
+
47
+ /** Delete project role assignment for a group. */
48
+ async deleteGroupAssignment(projectId: string, groupId: string): Promise<void> {
49
+ await this.http.delete(`/projects/${projectId}/roles/assignments/group/${groupId}`);
50
+ }
51
+
52
+ /** Update project role assignment for a group. */
53
+ async updateGroupAssignment(
54
+ projectId: string,
55
+ groupId: string,
56
+ body: UpdateRoleAssignmentRequest,
57
+ ): Promise<RoleAssignment> {
58
+ return this.http.patch<RoleAssignment>(
59
+ `/projects/${projectId}/roles/assignments/group/${groupId}`,
60
+ body,
61
+ );
62
+ }
63
+ }
@@ -0,0 +1,81 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { QueryClientV2 } from './query';
3
+ import type { HttpClient } from '../../http/http-client';
4
+ import type {
5
+ ExecuteAsyncMetricQueryRequestParams,
6
+ ExecuteAsyncSavedChartRequestParams,
7
+ ExecuteAsyncDashboardChartRequestParams,
8
+ ExecuteAsyncUnderlyingDataRequestParams,
9
+ } from '@lightdash-tools/common';
10
+
11
+ describe('QueryClientV2', () => {
12
+ let mockHttp: HttpClient;
13
+
14
+ beforeEach(() => {
15
+ mockHttp = {
16
+ get: vi.fn(),
17
+ post: vi.fn(),
18
+ put: vi.fn(),
19
+ patch: vi.fn(),
20
+ delete: vi.fn(),
21
+ } as unknown as HttpClient;
22
+ });
23
+
24
+ it('runMetricQuery should call POST /projects/{projectUuid}/query/metric-query with body', async () => {
25
+ const client = new QueryClientV2(mockHttp);
26
+ const body = {
27
+ exploreName: 'e1',
28
+ metricQuery: {},
29
+ } as unknown as ExecuteAsyncMetricQueryRequestParams;
30
+ const results = { queryId: 'q1', status: 'running' };
31
+ vi.mocked(mockHttp.post).mockResolvedValue(results);
32
+ const result = await client.runMetricQuery('p1', body);
33
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/query/metric-query', body);
34
+ expect(result).toEqual(results);
35
+ });
36
+
37
+ it('runSqlQuery should call POST /projects/{projectUuid}/query/sql with body', async () => {
38
+ const client = new QueryClientV2(mockHttp);
39
+ const body = { sql: 'SELECT 1' };
40
+ const results = { queryId: 'q1', status: 'running' };
41
+ vi.mocked(mockHttp.post).mockResolvedValue(results);
42
+ const result = await client.runSqlQuery('p1', body);
43
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/query/sql', body);
44
+ expect(result).toEqual(results);
45
+ });
46
+
47
+ it('runChartQuery should call POST /projects/{projectUuid}/query/chart with body', async () => {
48
+ const client = new QueryClientV2(mockHttp);
49
+ const body = { chartUuid: 'c1' } as unknown as ExecuteAsyncSavedChartRequestParams;
50
+ const results = { queryId: 'q1', status: 'running' };
51
+ vi.mocked(mockHttp.post).mockResolvedValue(results);
52
+ const result = await client.runChartQuery('p1', body);
53
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/query/chart', body);
54
+ expect(result).toEqual(results);
55
+ });
56
+
57
+ it('runDashboardChartQuery should call POST /projects/{projectUuid}/query/dashboard-chart with body', async () => {
58
+ const client = new QueryClientV2(mockHttp);
59
+ const body = {
60
+ dashboardChartUuid: 'dc1',
61
+ } as unknown as ExecuteAsyncDashboardChartRequestParams;
62
+ const results = { queryId: 'q1', status: 'running' };
63
+ vi.mocked(mockHttp.post).mockResolvedValue(results);
64
+ const result = await client.runDashboardChartQuery('p1', body);
65
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/query/dashboard-chart', body);
66
+ expect(result).toEqual(results);
67
+ });
68
+
69
+ it('runUnderlyingDataQuery should call POST /projects/{projectUuid}/query/underlying-data with body', async () => {
70
+ const client = new QueryClientV2(mockHttp);
71
+ const body = {
72
+ exploreName: 'e1',
73
+ metricQuery: {},
74
+ } as unknown as ExecuteAsyncUnderlyingDataRequestParams;
75
+ const results = { queryId: 'q1', status: 'running' };
76
+ vi.mocked(mockHttp.post).mockResolvedValue(results);
77
+ const result = await client.runUnderlyingDataQuery('p1', body);
78
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/query/underlying-data', body);
79
+ expect(result).toEqual(results);
80
+ });
81
+ });
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Query API client for v2 endpoints. Run metric/SQL/underlying data queries using v2 API.
3
+ */
4
+
5
+ import type {
6
+ ExecuteAsyncMetricQueryRequestParams,
7
+ ExecuteAsyncSqlQueryRequestParams,
8
+ ExecuteAsyncSavedChartRequestParams,
9
+ ExecuteAsyncDashboardChartRequestParams,
10
+ ExecuteAsyncUnderlyingDataRequestParams,
11
+ ExecuteAsyncMetricQueryResults,
12
+ ExecuteAsyncDashboardChartResults,
13
+ ExecuteAsyncSqlQueryResults,
14
+ } from '@lightdash-tools/common';
15
+ import { BaseApiClient } from '../base-client';
16
+
17
+ export class QueryClientV2 extends BaseApiClient {
18
+ /** Run a metric query (v2 endpoint). */
19
+ async runMetricQuery(
20
+ projectUuid: string,
21
+ body: ExecuteAsyncMetricQueryRequestParams,
22
+ ): Promise<ExecuteAsyncMetricQueryResults> {
23
+ return this.http.post<ExecuteAsyncMetricQueryResults>(
24
+ `/projects/${projectUuid}/query/metric-query`,
25
+ body,
26
+ );
27
+ }
28
+
29
+ /** Run a SQL query (v2 endpoint). */
30
+ async runSqlQuery(
31
+ projectUuid: string,
32
+ body: ExecuteAsyncSqlQueryRequestParams,
33
+ ): Promise<ExecuteAsyncSqlQueryResults> {
34
+ return this.http.post<ExecuteAsyncSqlQueryResults>(`/projects/${projectUuid}/query/sql`, body);
35
+ }
36
+
37
+ /** Run a chart query (v2 endpoint). */
38
+ async runChartQuery(
39
+ projectUuid: string,
40
+ body: ExecuteAsyncSavedChartRequestParams,
41
+ ): Promise<ExecuteAsyncMetricQueryResults> {
42
+ return this.http.post<ExecuteAsyncMetricQueryResults>(
43
+ `/projects/${projectUuid}/query/chart`,
44
+ body,
45
+ );
46
+ }
47
+
48
+ /** Run a dashboard chart query (v2 endpoint). */
49
+ async runDashboardChartQuery(
50
+ projectUuid: string,
51
+ body: ExecuteAsyncDashboardChartRequestParams,
52
+ ): Promise<ExecuteAsyncDashboardChartResults> {
53
+ return this.http.post<ExecuteAsyncDashboardChartResults>(
54
+ `/projects/${projectUuid}/query/dashboard-chart`,
55
+ body,
56
+ );
57
+ }
58
+
59
+ /** Run an underlying data query (v2 endpoint). */
60
+ async runUnderlyingDataQuery(
61
+ projectUuid: string,
62
+ body: ExecuteAsyncUnderlyingDataRequestParams,
63
+ ): Promise<ExecuteAsyncMetricQueryResults> {
64
+ return this.http.post<ExecuteAsyncMetricQueryResults>(
65
+ `/projects/${projectUuid}/query/underlying-data`,
66
+ body,
67
+ );
68
+ }
69
+ }
@@ -0,0 +1,105 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { LightdashClient } from './client';
3
+
4
+ describe('LightdashClient', () => {
5
+ const config = {
6
+ baseUrl: 'https://app.lightdash.cloud',
7
+ personalAccessToken: 'test-token',
8
+ };
9
+
10
+ beforeEach(() => {
11
+ vi.resetModules();
12
+ });
13
+
14
+ describe('V1 namespace', () => {
15
+ it('should expose v1 namespace with all API clients', () => {
16
+ const client = new LightdashClient(config);
17
+ expect(client.v1).toBeDefined();
18
+ expect(client.v1.projects).toBeDefined();
19
+ expect(client.v1.organizations).toBeDefined();
20
+ expect(client.v1.charts).toBeDefined();
21
+ expect(client.v1.dashboards).toBeDefined();
22
+ expect(client.v1.spaces).toBeDefined();
23
+ expect(client.v1.query).toBeDefined();
24
+ expect(client.v1.users).toBeDefined();
25
+ expect(client.v1.groups).toBeDefined();
26
+ });
27
+
28
+ it('v1.projects.getProject should be a function', () => {
29
+ const client = new LightdashClient(config);
30
+ expect(typeof client.v1.projects.getProject).toBe('function');
31
+ });
32
+
33
+ it('v1.organizations.getCurrentOrganization should be a function', () => {
34
+ const client = new LightdashClient(config);
35
+ expect(typeof client.v1.organizations.getCurrentOrganization).toBe('function');
36
+ });
37
+ });
38
+
39
+ describe('V2 namespace', () => {
40
+ it('should expose v2 namespace', () => {
41
+ const client = new LightdashClient(config);
42
+ expect(client.v2).toBeDefined();
43
+ expect(client.v2.query).toBeDefined();
44
+ });
45
+
46
+ it('v2.query.runMetricQuery should be a function', () => {
47
+ const client = new LightdashClient(config);
48
+ expect(typeof client.v2.query.runMetricQuery).toBe('function');
49
+ });
50
+
51
+ it('v2.query.runSqlQuery should be a function', () => {
52
+ const client = new LightdashClient(config);
53
+ expect(typeof client.v2.query.runSqlQuery).toBe('function');
54
+ });
55
+ });
56
+
57
+ describe('Backward compatibility (deprecated aliases)', () => {
58
+ it('should expose deprecated top-level aliases that delegate to v1', () => {
59
+ const client = new LightdashClient(config);
60
+ expect(client.projects).toBeDefined();
61
+ expect(client.organizations).toBeDefined();
62
+ expect(client.charts).toBeDefined();
63
+ expect(client.dashboards).toBeDefined();
64
+ expect(client.spaces).toBeDefined();
65
+ expect(client.query).toBeDefined();
66
+ expect(client.users).toBeDefined();
67
+ expect(client.groups).toBeDefined();
68
+ });
69
+
70
+ it('deprecated aliases should reference v1 clients', () => {
71
+ const client = new LightdashClient(config);
72
+ expect(client.projects).toBe(client.v1.projects);
73
+ expect(client.organizations).toBe(client.v1.organizations);
74
+ expect(client.charts).toBe(client.v1.charts);
75
+ expect(client.dashboards).toBe(client.v1.dashboards);
76
+ expect(client.spaces).toBe(client.v1.spaces);
77
+ expect(client.query).toBe(client.v1.query);
78
+ expect(client.users).toBe(client.v1.users);
79
+ expect(client.groups).toBe(client.v1.groups);
80
+ });
81
+
82
+ it('deprecated projects.getProject should work', () => {
83
+ const client = new LightdashClient(config);
84
+ expect(typeof client.projects.getProject).toBe('function');
85
+ });
86
+ });
87
+
88
+ describe('HTTP clients', () => {
89
+ it('should provide separate HTTP clients for v1 and v2', () => {
90
+ const client = new LightdashClient(config);
91
+ const httpV1 = client.getHttpClientV1();
92
+ const httpV2 = client.getHttpClientV2();
93
+ expect(httpV1).toBeDefined();
94
+ expect(httpV2).toBeDefined();
95
+ expect(httpV1).not.toBe(httpV2);
96
+ });
97
+
98
+ it('getHttpClient should return v1 client (deprecated)', () => {
99
+ const client = new LightdashClient(config);
100
+ const http = client.getHttpClient();
101
+ const httpV1 = client.getHttpClientV1();
102
+ expect(http).toBe(httpV1);
103
+ });
104
+ });
105
+ });