@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.
- package/LICENSE +201 -0
- package/README.md +144 -0
- package/dist/api/base-client.d.ts +13 -0
- package/dist/api/base-client.js +17 -0
- package/dist/api/v1/ai-agents.d.ts +17 -0
- package/dist/api/v1/ai-agents.js +47 -0
- package/dist/api/v1/ai-agents.test.d.ts +1 -0
- package/dist/api/v1/ai-agents.test.js +77 -0
- package/dist/api/v1/charts.d.ts +18 -0
- package/dist/api/v1/charts.js +45 -0
- package/dist/api/v1/charts.test.d.ts +1 -0
- package/dist/api/v1/charts.test.js +106 -0
- package/dist/api/v1/dashboards.d.ts +9 -0
- package/dist/api/v1/dashboards.js +25 -0
- package/dist/api/v1/dashboards.test.d.ts +1 -0
- package/dist/api/v1/dashboards.test.js +33 -0
- package/dist/api/v1/explores.d.ts +11 -0
- package/dist/api/v1/explores.js +31 -0
- package/dist/api/v1/explores.test.d.ts +1 -0
- package/dist/api/v1/explores.test.js +46 -0
- package/dist/api/v1/groups.d.ts +42 -0
- package/dist/api/v1/groups.js +81 -0
- package/dist/api/v1/groups.test.d.ts +1 -0
- package/dist/api/v1/groups.test.js +107 -0
- package/dist/api/v1/organizations.d.ts +9 -0
- package/dist/api/v1/organizations.js +25 -0
- package/dist/api/v1/organizations.test.d.ts +1 -0
- package/dist/api/v1/organizations.test.js +33 -0
- package/dist/api/v1/project-access.d.ts +40 -0
- package/dist/api/v1/project-access.js +88 -0
- package/dist/api/v1/project-access.test.d.ts +1 -0
- package/dist/api/v1/project-access.test.js +109 -0
- package/dist/api/v1/projects.d.ts +13 -0
- package/dist/api/v1/projects.js +37 -0
- package/dist/api/v1/projects.test.d.ts +1 -0
- package/dist/api/v1/projects.test.js +49 -0
- package/dist/api/v1/query.d.ts +11 -0
- package/dist/api/v1/query.js +31 -0
- package/dist/api/v1/query.test.d.ts +1 -0
- package/dist/api/v1/query.test.js +51 -0
- package/dist/api/v1/spaces.d.ts +25 -0
- package/dist/api/v1/spaces.js +73 -0
- package/dist/api/v1/spaces.test.d.ts +1 -0
- package/dist/api/v1/spaces.test.js +91 -0
- package/dist/api/v1/users.d.ts +35 -0
- package/dist/api/v1/users.js +66 -0
- package/dist/api/v1/users.test.d.ts +1 -0
- package/dist/api/v1/users.test.js +122 -0
- package/dist/api/v2/organization-roles.d.ts +45 -0
- package/dist/api/v2/organization-roles.js +81 -0
- package/dist/api/v2/organization-roles.test.d.ts +1 -0
- package/dist/api/v2/organization-roles.test.js +112 -0
- package/dist/api/v2/project-role-assignments.d.ts +23 -0
- package/dist/api/v2/project-role-assignments.js +55 -0
- package/dist/api/v2/project-role-assignments.test.d.ts +1 -0
- package/dist/api/v2/project-role-assignments.test.js +66 -0
- package/dist/api/v2/query.d.ts +17 -0
- package/dist/api/v2/query.js +49 -0
- package/dist/api/v2/query.test.d.ts +1 -0
- package/dist/api/v2/query.test.js +78 -0
- package/dist/client.d.ts +118 -0
- package/dist/client.js +113 -0
- package/dist/client.test.d.ts +1 -0
- package/dist/client.test.js +94 -0
- package/dist/config.d.ts +83 -0
- package/dist/config.js +22 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.js +46 -0
- package/dist/errors.test.d.ts +1 -0
- package/dist/errors.test.js +29 -0
- package/dist/http/axios-client.d.ts +22 -0
- package/dist/http/axios-client.js +51 -0
- package/dist/http/http-client.d.ts +33 -0
- package/dist/http/http-client.js +69 -0
- package/dist/http/interceptors.d.ts +13 -0
- package/dist/http/interceptors.js +82 -0
- package/dist/http/rate-limiter.d.ts +23 -0
- package/dist/http/rate-limiter.js +60 -0
- package/dist/http/rate-limiter.test.d.ts +1 -0
- package/dist/http/rate-limiter.test.js +40 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +41 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +65 -0
- package/dist/pagination/fetch-all-pages.d.ts +15 -0
- package/dist/pagination/fetch-all-pages.js +41 -0
- package/dist/pagination/fetch-all-pages.test.d.ts +1 -0
- package/dist/pagination/fetch-all-pages.test.js +70 -0
- package/dist/pagination/types.d.ts +14 -0
- package/dist/pagination/types.js +6 -0
- package/dist/types/api.d.ts +22 -0
- package/dist/types/api.js +8 -0
- package/dist/utils/env.d.ts +20 -0
- package/dist/utils/env.js +59 -0
- package/dist/utils/env.test.d.ts +1 -0
- package/dist/utils/env.test.js +73 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/logger.js +20 -0
- package/dist/utils/retry.d.ts +14 -0
- package/dist/utils/retry.js +67 -0
- package/dist/utils/retry.test.d.ts +1 -0
- package/dist/utils/retry.test.js +53 -0
- package/package.json +21 -0
- package/src/api/base-client.ts +14 -0
- package/src/api/v1/ai-agents.test.ts +74 -0
- package/src/api/v1/ai-agents.ts +41 -0
- package/src/api/v1/charts.test.ts +103 -0
- package/src/api/v1/charts.ts +51 -0
- package/src/api/v1/dashboards.test.ts +26 -0
- package/src/api/v1/dashboards.ts +15 -0
- package/src/api/v1/explores.test.ts +40 -0
- package/src/api/v1/explores.ts +18 -0
- package/src/api/v1/groups.test.ts +108 -0
- package/src/api/v1/groups.ts +100 -0
- package/src/api/v1/organizations.test.ts +26 -0
- package/src/api/v1/organizations.ts +13 -0
- package/src/api/v1/project-access.test.ts +110 -0
- package/src/api/v1/project-access.ts +102 -0
- package/src/api/v1/projects.test.ts +44 -0
- package/src/api/v1/projects.ts +23 -0
- package/src/api/v1/query.test.ts +46 -0
- package/src/api/v1/query.ts +37 -0
- package/src/api/v1/spaces.test.ts +92 -0
- package/src/api/v1/spaces.ts +83 -0
- package/src/api/v1/users.test.ts +125 -0
- package/src/api/v1/users.ts +76 -0
- package/src/api/v2/organization-roles.test.ts +115 -0
- package/src/api/v2/organization-roles.ts +90 -0
- package/src/api/v2/project-role-assignments.test.ts +75 -0
- package/src/api/v2/project-role-assignments.ts +63 -0
- package/src/api/v2/query.test.ts +81 -0
- package/src/api/v2/query.ts +69 -0
- package/src/client.test.ts +105 -0
- package/src/client.ts +182 -0
- package/src/config.ts +113 -0
- package/src/errors.test.ts +45 -0
- package/src/errors.ts +67 -0
- package/src/http/axios-client.ts +59 -0
- package/src/http/http-client.ts +79 -0
- package/src/http/interceptors.ts +110 -0
- package/src/http/rate-limiter.test.ts +34 -0
- package/src/http/rate-limiter.ts +54 -0
- package/src/index.test.ts +91 -0
- package/src/index.ts +41 -0
- package/src/pagination/fetch-all-pages.test.ts +75 -0
- package/src/pagination/fetch-all-pages.ts +39 -0
- package/src/pagination/types.ts +16 -0
- package/src/types/api.ts +28 -0
- package/src/utils/env.test.ts +88 -0
- package/src/utils/env.ts +69 -0
- package/src/utils/logger.ts +21 -0
- package/src/utils/retry.test.ts +49 -0
- package/src/utils/retry.ts +60 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Project access API client (project-level user and group access).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { LightdashApi } from '@lightdash-tools/common';
|
|
6
|
+
import { BaseApiClient } from '../base-client';
|
|
7
|
+
|
|
8
|
+
export class ProjectAccessClient extends BaseApiClient {
|
|
9
|
+
/** List users with explicit project access. */
|
|
10
|
+
async listProjectAccess(
|
|
11
|
+
projectUuid: string,
|
|
12
|
+
): Promise<LightdashApi.ProjectAccess.ProjectMemberProfile[]> {
|
|
13
|
+
return this.http.get<LightdashApi.ProjectAccess.ProjectMemberProfile[]>(
|
|
14
|
+
`/projects/${projectUuid}/access`,
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Grant a user access to a project. */
|
|
19
|
+
async grantProjectAccessToUser(
|
|
20
|
+
projectUuid: string,
|
|
21
|
+
body: LightdashApi.ProjectAccess.CreateProjectMember,
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
await this.http.post(`/projects/${projectUuid}/access`, body);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Get a project member's access. */
|
|
27
|
+
async getProjectMemberAccess(
|
|
28
|
+
projectUuid: string,
|
|
29
|
+
userUuid: string,
|
|
30
|
+
): Promise<LightdashApi.ProjectAccess.ProjectMemberProfile> {
|
|
31
|
+
return this.http.get<LightdashApi.ProjectAccess.ProjectMemberProfile>(
|
|
32
|
+
`/projects/${projectUuid}/user/${userUuid}`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Update a user's project access (role).
|
|
38
|
+
* @deprecated This API is deprecated in Lightdash.
|
|
39
|
+
*/
|
|
40
|
+
async updateProjectAccessForUser(
|
|
41
|
+
projectUuid: string,
|
|
42
|
+
userUuid: string,
|
|
43
|
+
body: LightdashApi.ProjectAccess.UpdateProjectMember,
|
|
44
|
+
): Promise<void> {
|
|
45
|
+
await this.http.patch(`/projects/${projectUuid}/access/${userUuid}`, body);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Revoke a user's project access.
|
|
50
|
+
* @deprecated This API is deprecated in Lightdash.
|
|
51
|
+
*/
|
|
52
|
+
async revokeProjectAccessForUser(projectUuid: string, userUuid: string): Promise<void> {
|
|
53
|
+
await this.http.delete(`/projects/${projectUuid}/access/${userUuid}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** List group access for a project. */
|
|
57
|
+
async listProjectGroupAccesses(
|
|
58
|
+
projectUuid: string,
|
|
59
|
+
): Promise<LightdashApi.ProjectAccess.ProjectGroupAccess[]> {
|
|
60
|
+
return this.http.get<LightdashApi.ProjectAccess.ProjectGroupAccess[]>(
|
|
61
|
+
`/projects/${projectUuid}/groupAccesses`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Add project access to a group.
|
|
67
|
+
* @deprecated This API is deprecated in Lightdash.
|
|
68
|
+
*/
|
|
69
|
+
async addProjectAccessToGroup(
|
|
70
|
+
groupUuid: string,
|
|
71
|
+
projectUuid: string,
|
|
72
|
+
body: LightdashApi.ProjectAccess.CreateProjectGroupAccessBody,
|
|
73
|
+
): Promise<LightdashApi.ProjectAccess.ProjectGroupAccess> {
|
|
74
|
+
return this.http.post<LightdashApi.ProjectAccess.ProjectGroupAccess>(
|
|
75
|
+
`/groups/${groupUuid}/projects/${projectUuid}`,
|
|
76
|
+
body,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Remove project access from a group.
|
|
82
|
+
* @deprecated This API is deprecated in Lightdash.
|
|
83
|
+
*/
|
|
84
|
+
async removeProjectAccessFromGroup(groupUuid: string, projectUuid: string): Promise<void> {
|
|
85
|
+
await this.http.delete(`/groups/${groupUuid}/projects/${projectUuid}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Update a group's project access (role).
|
|
90
|
+
* @deprecated This API is deprecated in Lightdash.
|
|
91
|
+
*/
|
|
92
|
+
async updateProjectAccessForGroup(
|
|
93
|
+
groupUuid: string,
|
|
94
|
+
projectUuid: string,
|
|
95
|
+
body: LightdashApi.ProjectAccess.UpdateProjectGroupAccess,
|
|
96
|
+
): Promise<LightdashApi.ProjectAccess.ProjectGroupAccess> {
|
|
97
|
+
return this.http.patch<LightdashApi.ProjectAccess.ProjectGroupAccess>(
|
|
98
|
+
`/groups/${groupUuid}/projects/${projectUuid}`,
|
|
99
|
+
body,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { ProjectsClient } from './projects';
|
|
3
|
+
import type { HttpClient } from '../../http/http-client';
|
|
4
|
+
|
|
5
|
+
describe('ProjectsClient', () => {
|
|
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('getProject should call GET /projects/{projectUuid}', async () => {
|
|
19
|
+
const client = new ProjectsClient(mockHttp);
|
|
20
|
+
const project = { projectUuid: 'p1', name: 'Test' };
|
|
21
|
+
vi.mocked(mockHttp.get).mockResolvedValue(project);
|
|
22
|
+
const result = await client.getProject('p1');
|
|
23
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1');
|
|
24
|
+
expect(result).toEqual(project);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('listProjects should call GET /org/projects', async () => {
|
|
28
|
+
const client = new ProjectsClient(mockHttp);
|
|
29
|
+
const list = [{ projectUuid: 'p1', name: 'P1' }];
|
|
30
|
+
vi.mocked(mockHttp.get).mockResolvedValue(list);
|
|
31
|
+
const result = await client.listProjects();
|
|
32
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/org/projects');
|
|
33
|
+
expect(result).toEqual(list);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('listChartsInProject should call GET /projects/{projectUuid}/charts', async () => {
|
|
37
|
+
const client = new ProjectsClient(mockHttp);
|
|
38
|
+
const charts: unknown[] = [];
|
|
39
|
+
vi.mocked(mockHttp.get).mockResolvedValue(charts);
|
|
40
|
+
const result = await client.listChartsInProject('p1');
|
|
41
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/charts');
|
|
42
|
+
expect(result).toEqual(charts);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Projects API client. Endpoints for project and project-scoped resources.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Project, OrganizationProject, SpaceQuery } from '@lightdash-tools/common';
|
|
6
|
+
import { BaseApiClient } from '../base-client';
|
|
7
|
+
|
|
8
|
+
export class ProjectsClient extends BaseApiClient {
|
|
9
|
+
/** Get a project by UUID. */
|
|
10
|
+
async getProject(projectUuid: string): Promise<Project> {
|
|
11
|
+
return this.http.get<Project>(`/projects/${projectUuid}`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** List all projects in the current organization. */
|
|
15
|
+
async listProjects(): Promise<OrganizationProject[]> {
|
|
16
|
+
return this.http.get<OrganizationProject[]>('/org/projects');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** List charts in a project. */
|
|
20
|
+
async listChartsInProject(projectUuid: string): Promise<SpaceQuery[]> {
|
|
21
|
+
return this.http.get<SpaceQuery[]>(`/projects/${projectUuid}/charts`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { QueryClient } from './query';
|
|
3
|
+
import type { HttpClient } from '../../http/http-client';
|
|
4
|
+
import type { MetricQueryRequest, CompileQueryRequest } from '@lightdash-tools/common';
|
|
5
|
+
|
|
6
|
+
describe('QueryClient', () => {
|
|
7
|
+
let mockHttp: HttpClient;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
mockHttp = {
|
|
11
|
+
get: vi.fn(),
|
|
12
|
+
post: vi.fn(),
|
|
13
|
+
put: vi.fn(),
|
|
14
|
+
patch: vi.fn(),
|
|
15
|
+
delete: vi.fn(),
|
|
16
|
+
} as unknown as HttpClient;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('runQuery should call POST /projects/{projectUuid}/explores/{exploreId}/runQuery with body', async () => {
|
|
20
|
+
const client = new QueryClient(mockHttp);
|
|
21
|
+
const body = { dimensions: [], metrics: [], filters: {} } as unknown as MetricQueryRequest;
|
|
22
|
+
const results = { rows: [], metricQuery: {} };
|
|
23
|
+
vi.mocked(mockHttp.post).mockResolvedValue(results);
|
|
24
|
+
const result = await client.runQuery('p1', 'explore1', body);
|
|
25
|
+
expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/explores/explore1/runQuery', body);
|
|
26
|
+
expect(result).toEqual(results);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('compileQuery should call POST /projects/{projectUuid}/explores/{exploreId}/compileQuery with body', async () => {
|
|
30
|
+
const client = new QueryClient(mockHttp);
|
|
31
|
+
const body = {
|
|
32
|
+
dimensions: [],
|
|
33
|
+
metrics: [],
|
|
34
|
+
filters: {},
|
|
35
|
+
exploreName: 'test',
|
|
36
|
+
limit: 100,
|
|
37
|
+
sorts: [],
|
|
38
|
+
tableCalculations: [],
|
|
39
|
+
} as unknown as CompileQueryRequest;
|
|
40
|
+
const results = { query: 'SELECT ...', parameterReferences: [] };
|
|
41
|
+
vi.mocked(mockHttp.post).mockResolvedValue(results);
|
|
42
|
+
const result = await client.compileQuery('p1', 'explore1', body);
|
|
43
|
+
expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/explores/explore1/compileQuery', body);
|
|
44
|
+
expect(result).toEqual(results);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query API client. Run metric/SQL/underlying data queries.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
MetricQueryRequest,
|
|
7
|
+
RunQueryResults,
|
|
8
|
+
CompileQueryRequest,
|
|
9
|
+
CompiledQueryResults,
|
|
10
|
+
} from '@lightdash-tools/common';
|
|
11
|
+
import { BaseApiClient } from '../base-client';
|
|
12
|
+
|
|
13
|
+
export class QueryClient extends BaseApiClient {
|
|
14
|
+
/** Run a metric query for an explore. */
|
|
15
|
+
async runQuery(
|
|
16
|
+
projectUuid: string,
|
|
17
|
+
exploreId: string,
|
|
18
|
+
body: MetricQueryRequest,
|
|
19
|
+
): Promise<RunQueryResults> {
|
|
20
|
+
return this.http.post<RunQueryResults>(
|
|
21
|
+
`/projects/${projectUuid}/explores/${exploreId}/runQuery`,
|
|
22
|
+
body,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Compile a metric query for an explore. */
|
|
27
|
+
async compileQuery(
|
|
28
|
+
projectUuid: string,
|
|
29
|
+
exploreId: string,
|
|
30
|
+
body: CompileQueryRequest,
|
|
31
|
+
): Promise<CompiledQueryResults> {
|
|
32
|
+
return this.http.post<CompiledQueryResults>(
|
|
33
|
+
`/projects/${projectUuid}/explores/${exploreId}/compileQuery`,
|
|
34
|
+
body,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { SpacesClient } from './spaces';
|
|
3
|
+
import type { HttpClient } from '../../http/http-client';
|
|
4
|
+
|
|
5
|
+
describe('SpacesClient', () => {
|
|
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('listSpacesInProject should call GET /projects/{projectUuid}/spaces', async () => {
|
|
19
|
+
const client = new SpacesClient(mockHttp);
|
|
20
|
+
const spaces = [{ uuid: 's1', name: 'Space 1', projectUuid: 'p1' }];
|
|
21
|
+
vi.mocked(mockHttp.get).mockResolvedValue(spaces);
|
|
22
|
+
const result = await client.listSpacesInProject('p1');
|
|
23
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/spaces');
|
|
24
|
+
expect(result).toEqual(spaces);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('getSpace should call GET /projects/{projectUuid}/spaces/{spaceUuid}', async () => {
|
|
28
|
+
const client = new SpacesClient(mockHttp);
|
|
29
|
+
const space = { uuid: 's1', name: 'Space 1', projectUuid: 'p1', organizationUuid: 'o1' };
|
|
30
|
+
vi.mocked(mockHttp.get).mockResolvedValue(space);
|
|
31
|
+
const result = await client.getSpace('p1', 's1');
|
|
32
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/spaces/s1');
|
|
33
|
+
expect(result).toEqual(space);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('createSpace should call POST /projects/{projectUuid}/spaces with body', async () => {
|
|
37
|
+
const client = new SpacesClient(mockHttp);
|
|
38
|
+
const created = { uuid: 's1', name: 'New Space', projectUuid: 'p1' };
|
|
39
|
+
vi.mocked(mockHttp.post).mockResolvedValue(created);
|
|
40
|
+
const body = { name: 'New Space' };
|
|
41
|
+
const result = await client.createSpace('p1', body);
|
|
42
|
+
expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/spaces', body);
|
|
43
|
+
expect(result).toEqual(created);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('updateSpace should call PATCH /projects/{projectUuid}/spaces/{spaceUuid} with body', async () => {
|
|
47
|
+
const client = new SpacesClient(mockHttp);
|
|
48
|
+
const updated = { uuid: 's1', name: 'Updated', projectUuid: 'p1' };
|
|
49
|
+
vi.mocked(mockHttp.patch).mockResolvedValue(updated);
|
|
50
|
+
const body = { name: 'Updated' };
|
|
51
|
+
const result = await client.updateSpace('p1', 's1', body);
|
|
52
|
+
expect(mockHttp.patch).toHaveBeenCalledWith('/projects/p1/spaces/s1', body);
|
|
53
|
+
expect(result).toEqual(updated);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('deleteSpace should call DELETE /projects/{projectUuid}/spaces/{spaceUuid}', async () => {
|
|
57
|
+
const client = new SpacesClient(mockHttp);
|
|
58
|
+
vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
|
|
59
|
+
await client.deleteSpace('p1', 's1');
|
|
60
|
+
expect(mockHttp.delete).toHaveBeenCalledWith('/projects/p1/spaces/s1');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('grantUserAccessToSpace should call POST .../share with body', async () => {
|
|
64
|
+
const client = new SpacesClient(mockHttp);
|
|
65
|
+
vi.mocked(mockHttp.post).mockResolvedValue(undefined);
|
|
66
|
+
const body = { userUuid: 'u1', spaceRole: 'editor' as const };
|
|
67
|
+
await client.grantUserAccessToSpace('p1', 's1', body);
|
|
68
|
+
expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/spaces/s1/share', body);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('revokeUserAccessToSpace should call DELETE .../share/{userUuid}', async () => {
|
|
72
|
+
const client = new SpacesClient(mockHttp);
|
|
73
|
+
vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
|
|
74
|
+
await client.revokeUserAccessToSpace('p1', 's1', 'u1');
|
|
75
|
+
expect(mockHttp.delete).toHaveBeenCalledWith('/projects/p1/spaces/s1/share/u1');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('grantGroupAccessToSpace should call POST .../group/share with body', async () => {
|
|
79
|
+
const client = new SpacesClient(mockHttp);
|
|
80
|
+
vi.mocked(mockHttp.post).mockResolvedValue(undefined);
|
|
81
|
+
const body = { groupUuid: 'g1', spaceRole: 'viewer' as const };
|
|
82
|
+
await client.grantGroupAccessToSpace('p1', 's1', body);
|
|
83
|
+
expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/spaces/s1/group/share', body);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('revokeGroupAccessToSpace should call DELETE .../group/share/{groupUuid}', async () => {
|
|
87
|
+
const client = new SpacesClient(mockHttp);
|
|
88
|
+
vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
|
|
89
|
+
await client.revokeGroupAccessToSpace('p1', 's1', 'g1');
|
|
90
|
+
expect(mockHttp.delete).toHaveBeenCalledWith('/projects/p1/spaces/s1/group/share/g1');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spaces API client.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
LightdashApi,
|
|
7
|
+
SpaceSummary,
|
|
8
|
+
CreateSpace,
|
|
9
|
+
UpdateSpace,
|
|
10
|
+
AddSpaceUserAccess,
|
|
11
|
+
AddSpaceGroupAccess,
|
|
12
|
+
} from '@lightdash-tools/common';
|
|
13
|
+
import { BaseApiClient } from '../base-client';
|
|
14
|
+
|
|
15
|
+
export class SpacesClient extends BaseApiClient {
|
|
16
|
+
/** List spaces in a project. */
|
|
17
|
+
async listSpacesInProject(projectUuid: string): Promise<SpaceSummary[]> {
|
|
18
|
+
return this.http.get<SpaceSummary[]>(`/projects/${projectUuid}/spaces`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Get a space by project and space UUID. */
|
|
22
|
+
async getSpace(projectUuid: string, spaceUuid: string): Promise<LightdashApi.Spaces.Space> {
|
|
23
|
+
return this.http.get<LightdashApi.Spaces.Space>(`/projects/${projectUuid}/spaces/${spaceUuid}`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Create a space in a project. */
|
|
27
|
+
async createSpace(projectUuid: string, body: CreateSpace): Promise<LightdashApi.Spaces.Space> {
|
|
28
|
+
return this.http.post<LightdashApi.Spaces.Space>(`/projects/${projectUuid}/spaces`, body);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Update a space. */
|
|
32
|
+
async updateSpace(
|
|
33
|
+
projectUuid: string,
|
|
34
|
+
spaceUuid: string,
|
|
35
|
+
body: UpdateSpace,
|
|
36
|
+
): Promise<LightdashApi.Spaces.Space> {
|
|
37
|
+
return this.http.patch<LightdashApi.Spaces.Space>(
|
|
38
|
+
`/projects/${projectUuid}/spaces/${spaceUuid}`,
|
|
39
|
+
body,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Delete a space. */
|
|
44
|
+
async deleteSpace(projectUuid: string, spaceUuid: string): Promise<void> {
|
|
45
|
+
await this.http.delete(`/projects/${projectUuid}/spaces/${spaceUuid}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Grant a user access to a space. */
|
|
49
|
+
async grantUserAccessToSpace(
|
|
50
|
+
projectUuid: string,
|
|
51
|
+
spaceUuid: string,
|
|
52
|
+
body: AddSpaceUserAccess,
|
|
53
|
+
): Promise<void> {
|
|
54
|
+
await this.http.post(`/projects/${projectUuid}/spaces/${spaceUuid}/share`, body);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Revoke a user's access to a space. */
|
|
58
|
+
async revokeUserAccessToSpace(
|
|
59
|
+
projectUuid: string,
|
|
60
|
+
spaceUuid: string,
|
|
61
|
+
userUuid: string,
|
|
62
|
+
): Promise<void> {
|
|
63
|
+
await this.http.delete(`/projects/${projectUuid}/spaces/${spaceUuid}/share/${userUuid}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Grant a group access to a space. */
|
|
67
|
+
async grantGroupAccessToSpace(
|
|
68
|
+
projectUuid: string,
|
|
69
|
+
spaceUuid: string,
|
|
70
|
+
body: AddSpaceGroupAccess,
|
|
71
|
+
): Promise<void> {
|
|
72
|
+
await this.http.post(`/projects/${projectUuid}/spaces/${spaceUuid}/group/share`, body);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Revoke a group's access to a space. */
|
|
76
|
+
async revokeGroupAccessToSpace(
|
|
77
|
+
projectUuid: string,
|
|
78
|
+
spaceUuid: string,
|
|
79
|
+
groupUuid: string,
|
|
80
|
+
): Promise<void> {
|
|
81
|
+
await this.http.delete(`/projects/${projectUuid}/spaces/${spaceUuid}/group/share/${groupUuid}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { UsersClient } from './users';
|
|
3
|
+
import type { HttpClient } from '../../http/http-client';
|
|
4
|
+
|
|
5
|
+
describe('UsersClient', () => {
|
|
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('listMembers should call GET /org/users with optional params', async () => {
|
|
19
|
+
const client = new UsersClient(mockHttp);
|
|
20
|
+
const results = { data: [], pagination: { totalResults: 0, totalPageCount: 0 } };
|
|
21
|
+
vi.mocked(mockHttp.get).mockResolvedValue(results);
|
|
22
|
+
const result = await client.listMembers({ pageSize: 10, page: 1 });
|
|
23
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/org/users', {
|
|
24
|
+
params: { pageSize: 10, page: 1 },
|
|
25
|
+
});
|
|
26
|
+
expect(result).toEqual(results);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('getMemberByUuid should call GET /org/users/{userUuid}', async () => {
|
|
30
|
+
const client = new UsersClient(mockHttp);
|
|
31
|
+
const member = {
|
|
32
|
+
userUuid: 'u1',
|
|
33
|
+
email: 'u@example.com',
|
|
34
|
+
firstName: 'U',
|
|
35
|
+
lastName: 'Ser',
|
|
36
|
+
role: 'editor',
|
|
37
|
+
};
|
|
38
|
+
vi.mocked(mockHttp.get).mockResolvedValue(member);
|
|
39
|
+
const result = await client.getMemberByUuid('u1');
|
|
40
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/org/users/u1');
|
|
41
|
+
expect(result).toEqual(member);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('getMemberByEmail should call GET /org/users/email/{encodedEmail}', async () => {
|
|
45
|
+
const client = new UsersClient(mockHttp);
|
|
46
|
+
const member = {
|
|
47
|
+
userUuid: 'u1',
|
|
48
|
+
email: 'user+test@example.com',
|
|
49
|
+
firstName: 'U',
|
|
50
|
+
lastName: 'Ser',
|
|
51
|
+
role: 'editor',
|
|
52
|
+
};
|
|
53
|
+
vi.mocked(mockHttp.get).mockResolvedValue(member);
|
|
54
|
+
const result = await client.getMemberByEmail('user+test@example.com');
|
|
55
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/org/users/email/user%2Btest%40example.com');
|
|
56
|
+
expect(result).toEqual(member);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('updateMember should call PATCH /org/users/{userUuid} with body', async () => {
|
|
60
|
+
const client = new UsersClient(mockHttp);
|
|
61
|
+
const body = { role: 'admin' as const };
|
|
62
|
+
const updated = {
|
|
63
|
+
userUuid: 'u1',
|
|
64
|
+
email: 'u@example.com',
|
|
65
|
+
firstName: 'U',
|
|
66
|
+
lastName: 'Ser',
|
|
67
|
+
role: 'admin',
|
|
68
|
+
};
|
|
69
|
+
vi.mocked(mockHttp.patch).mockResolvedValue(updated);
|
|
70
|
+
const result = await client.updateMember('u1', body);
|
|
71
|
+
expect(mockHttp.patch).toHaveBeenCalledWith('/org/users/u1', body);
|
|
72
|
+
expect(result).toEqual(updated);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('deleteMember should call DELETE /org/user/{userUuid}', async () => {
|
|
76
|
+
const client = new UsersClient(mockHttp);
|
|
77
|
+
vi.mocked(mockHttp.delete).mockResolvedValue(undefined);
|
|
78
|
+
await client.deleteMember('u1');
|
|
79
|
+
expect(mockHttp.delete).toHaveBeenCalledWith('/org/user/u1');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('listAllMembers should fetch all pages and return concatenated array', async () => {
|
|
83
|
+
const client = new UsersClient(mockHttp);
|
|
84
|
+
const page1 = {
|
|
85
|
+
data: [
|
|
86
|
+
{ userUuid: 'u1', email: 'a@example.com', firstName: 'A', lastName: 'One', role: 'editor' },
|
|
87
|
+
],
|
|
88
|
+
pagination: { page: 1, pageSize: 1, totalResults: 2, totalPageCount: 2 },
|
|
89
|
+
};
|
|
90
|
+
const page2 = {
|
|
91
|
+
data: [
|
|
92
|
+
{ userUuid: 'u2', email: 'b@example.com', firstName: 'B', lastName: 'Two', role: 'viewer' },
|
|
93
|
+
],
|
|
94
|
+
pagination: { page: 2, pageSize: 1, totalResults: 2, totalPageCount: 2 },
|
|
95
|
+
};
|
|
96
|
+
vi.mocked(mockHttp.get).mockResolvedValueOnce(page1).mockResolvedValueOnce(page2);
|
|
97
|
+
|
|
98
|
+
const result = await client.listAllMembers(undefined, { pageSize: 1 });
|
|
99
|
+
|
|
100
|
+
expect(result).toHaveLength(2);
|
|
101
|
+
expect(result[0]).toMatchObject({ userUuid: 'u1', email: 'a@example.com' });
|
|
102
|
+
expect(result[1]).toMatchObject({ userUuid: 'u2', email: 'b@example.com' });
|
|
103
|
+
expect(mockHttp.get).toHaveBeenCalledTimes(2);
|
|
104
|
+
expect(mockHttp.get).toHaveBeenNthCalledWith(1, '/org/users', {
|
|
105
|
+
params: { page: 1, pageSize: 1 },
|
|
106
|
+
});
|
|
107
|
+
expect(mockHttp.get).toHaveBeenNthCalledWith(2, '/org/users', {
|
|
108
|
+
params: { page: 2, pageSize: 1 },
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('listAllMembers should pass through list params (e.g. searchQuery)', async () => {
|
|
113
|
+
const client = new UsersClient(mockHttp);
|
|
114
|
+
vi.mocked(mockHttp.get).mockResolvedValueOnce({
|
|
115
|
+
data: [],
|
|
116
|
+
pagination: { page: 1, pageSize: 100, totalResults: 0, totalPageCount: 1 },
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
await client.listAllMembers({ searchQuery: 'foo' });
|
|
120
|
+
|
|
121
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/org/users', {
|
|
122
|
+
params: { searchQuery: 'foo', page: 1, pageSize: 100 },
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Users (organization members) 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 members. */
|
|
10
|
+
export interface ListMembersParams {
|
|
11
|
+
includeGroups?: number;
|
|
12
|
+
pageSize?: number;
|
|
13
|
+
page?: number;
|
|
14
|
+
searchQuery?: string;
|
|
15
|
+
projectUuid?: string;
|
|
16
|
+
googleOidcOnly?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Params for listAllMembers (page and pageSize are controlled by the helper). */
|
|
20
|
+
export type ListAllMembersParams = Omit<ListMembersParams, 'page' | 'pageSize'>;
|
|
21
|
+
|
|
22
|
+
export class UsersClient extends BaseApiClient {
|
|
23
|
+
/** List one page of members (use listAllMembers for all pages). */
|
|
24
|
+
async listMembers(
|
|
25
|
+
params?: ListMembersParams,
|
|
26
|
+
): Promise<LightdashApi.Users.OrganizationMemberProfilesResult> {
|
|
27
|
+
return this.http.get<LightdashApi.Users.OrganizationMemberProfilesResult>('/org/users', {
|
|
28
|
+
params,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** List all members of the current organization (fetches all pages). */
|
|
33
|
+
async listAllMembers(
|
|
34
|
+
params?: ListAllMembersParams,
|
|
35
|
+
options?: { pageSize?: number },
|
|
36
|
+
): Promise<LightdashApi.Users.OrganizationMemberProfile[]> {
|
|
37
|
+
return fetchAllPages<LightdashApi.Users.OrganizationMemberProfile>({
|
|
38
|
+
fetchPage: (page, pageSize) =>
|
|
39
|
+
this.http.get<LightdashApi.Users.OrganizationMemberProfilesResult>('/org/users', {
|
|
40
|
+
params: { ...params, page, pageSize },
|
|
41
|
+
}),
|
|
42
|
+
pageSize: options?.pageSize,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Get organization member by UUID. */
|
|
47
|
+
async getMemberByUuid(userUuid: string): Promise<LightdashApi.Users.OrganizationMemberProfile> {
|
|
48
|
+
return this.http.get<LightdashApi.Users.OrganizationMemberProfile>(`/org/users/${userUuid}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Get organization member by email. */
|
|
52
|
+
async getMemberByEmail(email: string): Promise<LightdashApi.Users.OrganizationMemberProfile> {
|
|
53
|
+
return this.http.get<LightdashApi.Users.OrganizationMemberProfile>(
|
|
54
|
+
`/org/users/email/${encodeURIComponent(email)}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Update organization member (e.g. role).
|
|
60
|
+
* @deprecated This API is deprecated in Lightdash.
|
|
61
|
+
*/
|
|
62
|
+
async updateMember(
|
|
63
|
+
userUuid: string,
|
|
64
|
+
body: LightdashApi.Users.OrganizationMemberProfileUpdate,
|
|
65
|
+
): Promise<LightdashApi.Users.OrganizationMemberProfile> {
|
|
66
|
+
return this.http.patch<LightdashApi.Users.OrganizationMemberProfile>(
|
|
67
|
+
`/org/users/${userUuid}`,
|
|
68
|
+
body,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Delete (remove) a user from the current organization. */
|
|
73
|
+
async deleteMember(userUuid: string): Promise<void> {
|
|
74
|
+
await this.http.delete(`/org/user/${userUuid}`);
|
|
75
|
+
}
|
|
76
|
+
}
|