@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,103 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { ChartsClient } from './charts';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('ChartsClient', () => {
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('listCharts should call GET /projects/{projectUuid}/charts', async () => {
19
+ const client = new ChartsClient(mockHttp);
20
+ const charts = [{ uuid: 'c1', name: 'Chart 1', projectUuid: 'p1' }];
21
+ vi.mocked(mockHttp.get).mockResolvedValue(charts);
22
+ const result = await client.listCharts('p1');
23
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/charts');
24
+ expect(result).toEqual(charts);
25
+ });
26
+
27
+ it('getChartsAsCode should call GET /projects/{projectUuid}/charts/code with no params when no options', async () => {
28
+ const client = new ChartsClient(mockHttp);
29
+ const results = { offset: 0, total: 1, missingIds: [], charts: [] };
30
+ vi.mocked(mockHttp.get).mockResolvedValue(results);
31
+ const result = await client.getChartsAsCode('p1');
32
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/charts/code', undefined);
33
+ expect(result).toEqual(results);
34
+ });
35
+
36
+ it('getChartsAsCode should call GET with query params when options provided', async () => {
37
+ const client = new ChartsClient(mockHttp);
38
+ const results = { offset: 0, total: 0, missingIds: [], charts: [] };
39
+ vi.mocked(mockHttp.get).mockResolvedValue(results);
40
+ await client.getChartsAsCode('p1', {
41
+ ids: ['slug-a', 'slug-b'],
42
+ offset: 10,
43
+ languageMap: true,
44
+ });
45
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/charts/code', {
46
+ params: { ids: ['slug-a', 'slug-b'], offset: 10, languageMap: true },
47
+ });
48
+ });
49
+
50
+ it('upsertChartAsCode should call POST /projects/{projectUuid}/charts/{slug}/code with body', async () => {
51
+ const client = new ChartsClient(mockHttp);
52
+ const body = {
53
+ name: 'My Chart',
54
+ slug: 'my-chart',
55
+ tableName: 'orders',
56
+ metricQuery: {
57
+ dimensions: [],
58
+ metrics: [],
59
+ filters: {},
60
+ exploreName: 'orders',
61
+ sorts: [],
62
+ limit: 500,
63
+ tableCalculations: [],
64
+ },
65
+ tableConfig: { columnOrder: [] },
66
+ updatedAt: '2024-01-01T00:00:00Z',
67
+ version: 1,
68
+ spaceSlug: 'my-space',
69
+ chartConfig: { type: 'cartesian', config: {} },
70
+ } as Parameters<ChartsClient['upsertChartAsCode']>[2];
71
+ const apiResult = { promoted: [], errors: [] };
72
+ vi.mocked(mockHttp.post).mockResolvedValue(apiResult);
73
+ const result = await client.upsertChartAsCode('p1', 'my-chart', body);
74
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/charts/my-chart/code', body);
75
+ expect(result).toEqual(apiResult);
76
+ });
77
+
78
+ it('upsertChartAsCode should encode slug in path', async () => {
79
+ const client = new ChartsClient(mockHttp);
80
+ const body = {
81
+ name: 'Chart',
82
+ slug: 'chart/slug',
83
+ tableName: 't',
84
+ metricQuery: {
85
+ dimensions: [],
86
+ metrics: [],
87
+ filters: {},
88
+ exploreName: 't',
89
+ sorts: [],
90
+ limit: 500,
91
+ tableCalculations: [],
92
+ },
93
+ tableConfig: { columnOrder: [] },
94
+ updatedAt: '2024-01-01T00:00:00Z',
95
+ version: 1,
96
+ spaceSlug: 'space',
97
+ chartConfig: {},
98
+ } as Parameters<ChartsClient['upsertChartAsCode']>[2];
99
+ vi.mocked(mockHttp.post).mockResolvedValue({});
100
+ await client.upsertChartAsCode('p1', 'chart/slug', body);
101
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/charts/chart%2Fslug/code', body);
102
+ });
103
+ });
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Charts API client. Endpoints for saved charts and chart-as-code operations.
3
+ */
4
+
5
+ import type {
6
+ SpaceQuery,
7
+ ChartAsCodeListResults,
8
+ ChartAsCodeUpsertResults,
9
+ UpsertChartAsCodeBody,
10
+ } from '@lightdash-tools/common';
11
+ import { BaseApiClient } from '../base-client';
12
+
13
+ export interface GetChartsAsCodeOptions {
14
+ ids?: string[];
15
+ offset?: number;
16
+ languageMap?: boolean;
17
+ }
18
+
19
+ export class ChartsClient extends BaseApiClient {
20
+ /** List charts in a project (returns SpaceQuery array). */
21
+ async listCharts(projectUuid: string): Promise<SpaceQuery[]> {
22
+ return this.http.get<SpaceQuery[]>(`/projects/${projectUuid}/charts`);
23
+ }
24
+
25
+ /** Get charts in code representation (for charts-as-code workflows). */
26
+ async getChartsAsCode(
27
+ projectUuid: string,
28
+ options?: GetChartsAsCodeOptions,
29
+ ): Promise<ChartAsCodeListResults> {
30
+ const params: Record<string, unknown> = {};
31
+ if (options?.ids?.length) params.ids = options.ids;
32
+ if (options?.offset != null) params.offset = options.offset;
33
+ if (options?.languageMap != null) params.languageMap = options.languageMap;
34
+ return this.http.get<ChartAsCodeListResults>(
35
+ `/projects/${projectUuid}/charts/code`,
36
+ Object.keys(params).length ? { params } : undefined,
37
+ );
38
+ }
39
+
40
+ /** Upsert a chart from code representation (create or update by slug). */
41
+ async upsertChartAsCode(
42
+ projectUuid: string,
43
+ slug: string,
44
+ body: UpsertChartAsCodeBody,
45
+ ): Promise<ChartAsCodeUpsertResults> {
46
+ return this.http.post<ChartAsCodeUpsertResults>(
47
+ `/projects/${projectUuid}/charts/${encodeURIComponent(slug)}/code`,
48
+ body,
49
+ );
50
+ }
51
+ }
@@ -0,0 +1,26 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { DashboardsClient } from './dashboards';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('DashboardsClient', () => {
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('listDashboards should call GET /projects/{projectUuid}/dashboards', async () => {
19
+ const client = new DashboardsClient(mockHttp);
20
+ const dashboards = [{ uuid: 'd1', name: 'Dashboard 1', projectUuid: 'p1' }];
21
+ vi.mocked(mockHttp.get).mockResolvedValue(dashboards);
22
+ const result = await client.listDashboards('p1');
23
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/dashboards');
24
+ expect(result).toEqual(dashboards);
25
+ });
26
+ });
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Dashboards API client.
3
+ */
4
+
5
+ import type { DashboardBasicDetailsWithTileTypes } from '@lightdash-tools/common';
6
+ import { BaseApiClient } from '../base-client';
7
+
8
+ export class DashboardsClient extends BaseApiClient {
9
+ /** List dashboards in a project. */
10
+ async listDashboards(projectUuid: string): Promise<DashboardBasicDetailsWithTileTypes[]> {
11
+ return this.http.get<DashboardBasicDetailsWithTileTypes[]>(
12
+ `/projects/${projectUuid}/dashboards`,
13
+ );
14
+ }
15
+ }
@@ -0,0 +1,40 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { ExploresClient } from './explores';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('ExploresClient', () => {
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('listExplores should call GET /projects/{projectUuid}/explores', async () => {
19
+ const client = new ExploresClient(mockHttp);
20
+ const explores = [{ name: 'e1', label: 'Explore 1', databaseName: 'db1' }];
21
+ vi.mocked(mockHttp.get).mockResolvedValue(explores);
22
+ const result = await client.listExplores('p1');
23
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/explores');
24
+ expect(result).toEqual(explores);
25
+ });
26
+
27
+ it('getExplore should call GET /projects/{projectUuid}/explores/{exploreId}', async () => {
28
+ const client = new ExploresClient(mockHttp);
29
+ const explore = {
30
+ name: 'e1',
31
+ label: 'Explore 1',
32
+ baseTable: 't1',
33
+ joinedTables: [],
34
+ };
35
+ vi.mocked(mockHttp.get).mockResolvedValue(explore);
36
+ const result = await client.getExplore('p1', 'e1');
37
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/explores/e1');
38
+ expect(result).toEqual(explore);
39
+ });
40
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Explores API client (v1).
3
+ */
4
+
5
+ import type { ApiExploresResults, ApiExploreResults } from '@lightdash-tools/common';
6
+ import { BaseApiClient } from '../base-client';
7
+
8
+ export class ExploresClient extends BaseApiClient {
9
+ /** List all explores for a project. */
10
+ async listExplores(projectUuid: string): Promise<ApiExploresResults> {
11
+ return this.http.get<ApiExploresResults>(`/projects/${projectUuid}/explores`);
12
+ }
13
+
14
+ /** Get a specific explore by ID. */
15
+ async getExplore(projectUuid: string, exploreId: string): Promise<ApiExploreResults> {
16
+ return this.http.get<ApiExploreResults>(`/projects/${projectUuid}/explores/${exploreId}`);
17
+ }
18
+ }
@@ -0,0 +1,108 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { GroupsClient } from './groups';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('GroupsClient', () => {
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('listGroups should call GET /org/groups with optional params', async () => {
19
+ const client = new GroupsClient(mockHttp);
20
+ const results = { data: [], pagination: { totalResults: 0, totalPageCount: 0 } };
21
+ vi.mocked(mockHttp.get).mockResolvedValue(results);
22
+ const result = await client.listGroups({ pageSize: 10 });
23
+ expect(mockHttp.get).toHaveBeenCalledWith('/org/groups', {
24
+ params: { pageSize: 10 },
25
+ });
26
+ expect(result).toEqual(results);
27
+ });
28
+
29
+ it('createGroup should call POST /org/groups with body', async () => {
30
+ const client = new GroupsClient(mockHttp);
31
+ const group = {
32
+ name: 'Admins',
33
+ groupUuid: 'g1',
34
+ organizationUuid: 'o1',
35
+ memberUuids: [],
36
+ members: [],
37
+ };
38
+ vi.mocked(mockHttp.post).mockResolvedValue(group);
39
+ const result = await client.createGroup({ name: 'Admins' });
40
+ expect(mockHttp.post).toHaveBeenCalledWith('/org/groups', { name: 'Admins' });
41
+ expect(result).toEqual(group);
42
+ });
43
+
44
+ it('getGroupMembers should call GET /groups/{groupUuid}/members', async () => {
45
+ const client = new GroupsClient(mockHttp);
46
+ const members = [{ userUuid: 'u1', email: 'u@example.com', firstName: 'U', lastName: 'Ser' }];
47
+ vi.mocked(mockHttp.get).mockResolvedValue(members);
48
+ const result = await client.getGroupMembers('g1');
49
+ expect(mockHttp.get).toHaveBeenCalledWith('/groups/g1/members');
50
+ expect(result).toEqual(members);
51
+ });
52
+
53
+ it('listAllGroups should fetch all pages and return concatenated array', async () => {
54
+ const client = new GroupsClient(mockHttp);
55
+ const page1 = {
56
+ data: [
57
+ {
58
+ name: 'G1',
59
+ groupUuid: 'g1',
60
+ organizationUuid: 'o1',
61
+ memberUuids: [],
62
+ members: [],
63
+ },
64
+ ],
65
+ pagination: { page: 1, pageSize: 1, totalResults: 2, totalPageCount: 2 },
66
+ };
67
+ const page2 = {
68
+ data: [
69
+ {
70
+ name: 'G2',
71
+ groupUuid: 'g2',
72
+ organizationUuid: 'o1',
73
+ memberUuids: [],
74
+ members: [],
75
+ },
76
+ ],
77
+ pagination: { page: 2, pageSize: 1, totalResults: 2, totalPageCount: 2 },
78
+ };
79
+ vi.mocked(mockHttp.get).mockResolvedValueOnce(page1).mockResolvedValueOnce(page2);
80
+
81
+ const result = await client.listAllGroups(undefined, { pageSize: 1 });
82
+
83
+ expect(result).toHaveLength(2);
84
+ expect(result[0]).toMatchObject({ groupUuid: 'g1', name: 'G1' });
85
+ expect(result[1]).toMatchObject({ groupUuid: 'g2', name: 'G2' });
86
+ expect(mockHttp.get).toHaveBeenCalledTimes(2);
87
+ expect(mockHttp.get).toHaveBeenNthCalledWith(1, '/org/groups', {
88
+ params: { page: 1, pageSize: 1 },
89
+ });
90
+ expect(mockHttp.get).toHaveBeenNthCalledWith(2, '/org/groups', {
91
+ params: { page: 2, pageSize: 1 },
92
+ });
93
+ });
94
+
95
+ it('listAllGroups should pass through list params (e.g. searchQuery, includeMembers)', async () => {
96
+ const client = new GroupsClient(mockHttp);
97
+ vi.mocked(mockHttp.get).mockResolvedValueOnce({
98
+ data: [],
99
+ pagination: { page: 1, pageSize: 100, totalResults: 0, totalPageCount: 1 },
100
+ });
101
+
102
+ await client.listAllGroups({ searchQuery: 'admins', includeMembers: 1 });
103
+
104
+ expect(mockHttp.get).toHaveBeenCalledWith('/org/groups', {
105
+ params: { searchQuery: 'admins', includeMembers: 1, page: 1, pageSize: 100 },
106
+ });
107
+ });
108
+ });
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Groups API client.
3
+ */
4
+
5
+ import type { LightdashApi } from '@lightdash-tools/common';
6
+ import { BaseApiClient } from '../base-client';
7
+ import { fetchAllPages } from '../../pagination/fetch-all-pages';
8
+
9
+ /** Query params for listing organization groups. */
10
+ export interface ListGroupsParams {
11
+ page?: number;
12
+ pageSize?: number;
13
+ includeMembers?: number;
14
+ searchQuery?: string;
15
+ }
16
+
17
+ /** Params for listAllGroups (page and pageSize are controlled by the helper). */
18
+ export type ListAllGroupsParams = Omit<ListGroupsParams, 'page' | 'pageSize'>;
19
+
20
+ /** Query params for getGroup. */
21
+ export interface GetGroupParams {
22
+ includeMembers?: number;
23
+ offset?: number;
24
+ }
25
+
26
+ export type GroupOrGroupWithMembers =
27
+ | LightdashApi.Groups.Group
28
+ | LightdashApi.Groups.GroupWithMembers;
29
+
30
+ export class GroupsClient extends BaseApiClient {
31
+ /** List one page of groups (use listAllGroups for all pages). */
32
+ async listGroups(params?: ListGroupsParams): Promise<LightdashApi.Groups.GroupListResult> {
33
+ return this.http.get<LightdashApi.Groups.GroupListResult>('/org/groups', {
34
+ params,
35
+ });
36
+ }
37
+
38
+ /** List all groups in the current organization (fetches all pages). */
39
+ async listAllGroups(
40
+ params?: ListAllGroupsParams,
41
+ options?: { pageSize?: number },
42
+ ): Promise<GroupOrGroupWithMembers[]> {
43
+ return fetchAllPages<GroupOrGroupWithMembers>({
44
+ fetchPage: (page, pageSize) =>
45
+ this.http.get<LightdashApi.Groups.GroupListResult>('/org/groups', {
46
+ params: { ...params, page, pageSize },
47
+ }),
48
+ pageSize: options?.pageSize,
49
+ });
50
+ }
51
+
52
+ /** Create a new group in the current organization. */
53
+ async createGroup(
54
+ body: LightdashApi.Groups.CreateGroup,
55
+ ): Promise<LightdashApi.Groups.GroupWithMembers> {
56
+ return this.http.post<LightdashApi.Groups.GroupWithMembers>('/org/groups', body);
57
+ }
58
+
59
+ /** Get a group by UUID. */
60
+ async getGroup(
61
+ groupUuid: string,
62
+ params?: GetGroupParams,
63
+ ): Promise<LightdashApi.Groups.Group | LightdashApi.Groups.GroupWithMembers> {
64
+ return this.http.get<LightdashApi.Groups.Group | LightdashApi.Groups.GroupWithMembers>(
65
+ `/groups/${groupUuid}`,
66
+ { params },
67
+ );
68
+ }
69
+
70
+ /** Update a group (name and/or members). */
71
+ async updateGroup(
72
+ groupUuid: string,
73
+ body: LightdashApi.Groups.UpdateGroupWithMembers,
74
+ ): Promise<LightdashApi.Groups.Group | LightdashApi.Groups.GroupWithMembers> {
75
+ return this.http.patch<LightdashApi.Groups.Group | LightdashApi.Groups.GroupWithMembers>(
76
+ `/groups/${groupUuid}`,
77
+ body,
78
+ );
79
+ }
80
+
81
+ /** Delete a group. */
82
+ async deleteGroup(groupUuid: string): Promise<void> {
83
+ await this.http.delete(`/groups/${groupUuid}`);
84
+ }
85
+
86
+ /** Get members of a group. */
87
+ async getGroupMembers(groupUuid: string): Promise<LightdashApi.Groups.GroupMember[]> {
88
+ return this.http.get<LightdashApi.Groups.GroupMember[]>(`/groups/${groupUuid}/members`);
89
+ }
90
+
91
+ /** Add a user to a group. */
92
+ async addUserToGroup(groupUuid: string, userUuid: string): Promise<void> {
93
+ await this.http.put(`/groups/${groupUuid}/members/${userUuid}`);
94
+ }
95
+
96
+ /** Remove a user from a group. */
97
+ async removeUserFromGroup(groupUuid: string, userUuid: string): Promise<void> {
98
+ await this.http.delete(`/groups/${groupUuid}/members/${userUuid}`);
99
+ }
100
+ }
@@ -0,0 +1,26 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { OrganizationsClient } from './organizations';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('OrganizationsClient', () => {
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('getCurrentOrganization should call GET /org', async () => {
19
+ const client = new OrganizationsClient(mockHttp);
20
+ const org = { organizationUuid: 'o1', name: 'My Org' };
21
+ vi.mocked(mockHttp.get).mockResolvedValue(org);
22
+ const result = await client.getCurrentOrganization();
23
+ expect(mockHttp.get).toHaveBeenCalledWith('/org');
24
+ expect(result).toEqual(org);
25
+ });
26
+ });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Organizations API client.
3
+ */
4
+
5
+ import type { Organization } from '@lightdash-tools/common';
6
+ import { BaseApiClient } from '../base-client';
7
+
8
+ export class OrganizationsClient extends BaseApiClient {
9
+ /** Get the current user's organization. */
10
+ async getCurrentOrganization(): Promise<Organization> {
11
+ return this.http.get<Organization>('/org');
12
+ }
13
+ }
@@ -0,0 +1,110 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { ProjectAccessClient } from './project-access';
3
+ import type { HttpClient } from '../../http/http-client';
4
+
5
+ describe('ProjectAccessClient', () => {
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('listProjectAccess should call GET /projects/{projectUuid}/access', async () => {
19
+ const client = new ProjectAccessClient(mockHttp);
20
+ const list = [
21
+ {
22
+ userUuid: 'u1',
23
+ email: 'u@example.com',
24
+ firstName: 'U',
25
+ lastName: 'Ser',
26
+ role: 'editor',
27
+ projectUuid: 'p1',
28
+ },
29
+ ];
30
+ vi.mocked(mockHttp.get).mockResolvedValue(list);
31
+ const result = await client.listProjectAccess('p1');
32
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/access');
33
+ expect(result).toEqual(list);
34
+ });
35
+
36
+ it('grantProjectAccessToUser should call POST /projects/{projectUuid}/access with body', async () => {
37
+ const client = new ProjectAccessClient(mockHttp);
38
+ vi.mocked(mockHttp.post).mockResolvedValue(undefined);
39
+ const body = { email: 'u@example.com', role: 'viewer' as const, sendEmail: true };
40
+ await client.grantProjectAccessToUser('p1', body);
41
+ expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/access', body);
42
+ });
43
+
44
+ it('getProjectMemberAccess should call GET /projects/{projectUuid}/user/{userUuid}', async () => {
45
+ const client = new ProjectAccessClient(mockHttp);
46
+ const member = {
47
+ userUuid: 'u1',
48
+ email: 'u@example.com',
49
+ firstName: 'U',
50
+ lastName: 'Ser',
51
+ role: 'editor',
52
+ projectUuid: 'p1',
53
+ };
54
+ vi.mocked(mockHttp.get).mockResolvedValue(member);
55
+ const result = await client.getProjectMemberAccess('p1', 'u1');
56
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/user/u1');
57
+ expect(result).toEqual(member);
58
+ });
59
+
60
+ it('updateProjectAccessForUser should call PATCH /projects/{projectUuid}/access/{userUuid}', async () => {
61
+ const client = new ProjectAccessClient(mockHttp);
62
+ vi.mocked(mockHttp.patch).mockResolvedValue(undefined);
63
+ const body = { role: 'editor' as const };
64
+ await client.updateProjectAccessForUser('p1', 'u1', body);
65
+ expect(mockHttp.patch).toHaveBeenCalledWith('/projects/p1/access/u1', body);
66
+ });
67
+
68
+ it('revokeProjectAccessForUser should call DELETE /projects/{projectUuid}/access/{userUuid}', async () => {
69
+ const client = new ProjectAccessClient(mockHttp);
70
+ vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
71
+ await client.revokeProjectAccessForUser('p1', 'u1');
72
+ expect(mockHttp.delete).toHaveBeenCalledWith('/projects/p1/access/u1');
73
+ });
74
+
75
+ it('listProjectGroupAccesses should call GET /projects/{projectUuid}/groupAccesses', async () => {
76
+ const client = new ProjectAccessClient(mockHttp);
77
+ const list = [{ groupUuid: 'g1', projectUuid: 'p1', role: 'viewer' }];
78
+ vi.mocked(mockHttp.get).mockResolvedValue(list);
79
+ const result = await client.listProjectGroupAccesses('p1');
80
+ expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/groupAccesses');
81
+ expect(result).toEqual(list);
82
+ });
83
+
84
+ it('addProjectAccessToGroup should call POST /groups/{groupUuid}/projects/{projectUuid}', async () => {
85
+ const client = new ProjectAccessClient(mockHttp);
86
+ const created = { groupUuid: 'g1', projectUuid: 'p1', role: 'viewer' };
87
+ vi.mocked(mockHttp.post).mockResolvedValue(created);
88
+ const body = { role: 'viewer' };
89
+ const result = await client.addProjectAccessToGroup('g1', 'p1', body);
90
+ expect(mockHttp.post).toHaveBeenCalledWith('/groups/g1/projects/p1', body);
91
+ expect(result).toEqual(created);
92
+ });
93
+
94
+ it('removeProjectAccessFromGroup should call DELETE /groups/{groupUuid}/projects/{projectUuid}', async () => {
95
+ const client = new ProjectAccessClient(mockHttp);
96
+ vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
97
+ await client.removeProjectAccessFromGroup('g1', 'p1');
98
+ expect(mockHttp.delete).toHaveBeenCalledWith('/groups/g1/projects/p1');
99
+ });
100
+
101
+ it('updateProjectAccessForGroup should call PATCH /groups/{groupUuid}/projects/{projectUuid}', async () => {
102
+ const client = new ProjectAccessClient(mockHttp);
103
+ const updated = { groupUuid: 'g1', projectUuid: 'p1', role: 'editor' };
104
+ vi.mocked(mockHttp.patch).mockResolvedValue(updated);
105
+ const body = { role: 'editor' as const };
106
+ const result = await client.updateProjectAccessForGroup('g1', 'p1', body);
107
+ expect(mockHttp.patch).toHaveBeenCalledWith('/groups/g1/projects/p1', body);
108
+ expect(result).toEqual(updated);
109
+ });
110
+ });