@lightdash-tools/client 0.1.1 → 0.2.1
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/README.md +14 -1
- package/dist/api/v1/explores.d.ts +4 -0
- package/dist/api/v1/explores.js +19 -0
- package/dist/api/v1/explores.test.js +32 -0
- package/dist/api/v1/metrics.d.ts +23 -0
- package/dist/api/v1/metrics.js +31 -0
- package/dist/api/v1/metrics.test.d.ts +1 -0
- package/dist/api/v1/metrics.test.js +43 -0
- package/dist/api/v1/schedulers.d.ts +25 -0
- package/dist/api/v1/schedulers.js +31 -0
- package/dist/api/v1/schedulers.test.d.ts +1 -0
- package/dist/api/v1/schedulers.test.js +43 -0
- package/dist/api/v1/tags.d.ts +11 -0
- package/dist/api/v1/tags.js +31 -0
- package/dist/api/v1/tags.test.d.ts +1 -0
- package/dist/api/v1/tags.test.js +41 -0
- package/dist/api/v1/validation.d.ts +22 -0
- package/dist/api/v1/validation.js +31 -0
- package/dist/api/v1/validation.test.d.ts +1 -0
- package/dist/api/v1/validation.test.js +43 -0
- package/dist/api/v2/content.d.ts +21 -0
- package/dist/api/v2/content.js +27 -0
- package/dist/api/v2/content.test.d.ts +1 -0
- package/dist/api/v2/content.test.js +35 -0
- package/dist/client.d.ts +30 -0
- package/dist/client.js +15 -0
- package/dist/config.d.ts +7 -3
- package/dist/http/interceptors.js +3 -4
- package/dist/index.d.ts +10 -0
- package/dist/index.js +13 -1
- package/dist/index.test.js +21 -3
- package/dist/utils/env.js +6 -4
- package/dist/utils/env.test.js +2 -2
- package/dist/utils/secret-string.d.ts +21 -0
- package/dist/utils/secret-string.js +38 -0
- package/package.json +2 -2
- package/src/api/v1/explores.test.ts +34 -0
- package/src/api/v1/explores.ts +20 -1
- package/src/api/v1/metrics.test.ts +37 -0
- package/src/api/v1/metrics.ts +43 -0
- package/src/api/v1/schedulers.test.ts +37 -0
- package/src/api/v1/schedulers.ts +43 -0
- package/src/api/v1/tags.test.ts +35 -0
- package/src/api/v1/tags.ts +20 -0
- package/src/api/v1/validation.test.ts +37 -0
- package/src/api/v1/validation.ts +43 -0
- package/src/api/v2/content.test.ts +28 -0
- package/src/api/v2/content.ts +30 -0
- package/src/client.ts +40 -0
- package/src/config.ts +9 -14
- package/src/http/interceptors.ts +3 -4
- package/src/index.test.ts +30 -4
- package/src/index.ts +10 -0
- package/src/utils/env.test.ts +2 -2
- package/src/utils/env.ts +6 -4
- package/src/utils/secret-string.ts +36 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { TagsClient } from './tags';
|
|
3
|
+
import type { HttpClient } from '../../http/http-client';
|
|
4
|
+
|
|
5
|
+
describe('TagsClient', () => {
|
|
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('listTags should call GET /projects/{projectUuid}/tags', async () => {
|
|
19
|
+
const client = new TagsClient(mockHttp);
|
|
20
|
+
const mockResponse = { status: 'ok', results: [] };
|
|
21
|
+
vi.mocked(mockHttp.get).mockResolvedValue(mockResponse);
|
|
22
|
+
const result = await client.listTags('p1');
|
|
23
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/tags');
|
|
24
|
+
expect(result).toEqual(mockResponse);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('getTag should call GET /projects/{projectUuid}/tags/{tagUuid}', async () => {
|
|
28
|
+
const client = new TagsClient(mockHttp);
|
|
29
|
+
const mockResponse = { status: 'ok', results: {} };
|
|
30
|
+
vi.mocked(mockHttp.get).mockResolvedValue(mockResponse);
|
|
31
|
+
const result = await client.getTag('p1', 't1');
|
|
32
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/tags/t1');
|
|
33
|
+
expect(result).toEqual(mockResponse);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tags API client.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { LightdashApi } from '@lightdash-tools/common';
|
|
6
|
+
import { BaseApiClient } from '../base-client';
|
|
7
|
+
|
|
8
|
+
export class TagsClient extends BaseApiClient {
|
|
9
|
+
/** List tags in a project. */
|
|
10
|
+
async listTags(projectUuid: string): Promise<LightdashApi.Tags.ApiGetTagsResponse> {
|
|
11
|
+
return this.http.get<LightdashApi.Tags.ApiGetTagsResponse>(`/projects/${projectUuid}/tags`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Get a tag by UUID. */
|
|
15
|
+
async getTag(projectUuid: string, tagUuid: string): Promise<LightdashApi.Tags.ApiGetTagResponse> {
|
|
16
|
+
return this.http.get<LightdashApi.Tags.ApiGetTagResponse>(
|
|
17
|
+
`/projects/${projectUuid}/tags/${tagUuid}`,
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { ValidationClient } from './validation';
|
|
3
|
+
import type { HttpClient } from '../../http/http-client';
|
|
4
|
+
|
|
5
|
+
describe('ValidationClient', () => {
|
|
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('getValidationResults should call GET /projects/{projectUuid}/validate', async () => {
|
|
19
|
+
const client = new ValidationClient(mockHttp);
|
|
20
|
+
const mockResponse = { status: 'ok', results: [] };
|
|
21
|
+
vi.mocked(mockHttp.get).mockResolvedValue(mockResponse);
|
|
22
|
+
const result = await client.getValidationResults('p1', { jobId: 'j1' });
|
|
23
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/projects/p1/validate', {
|
|
24
|
+
params: { jobId: 'j1' },
|
|
25
|
+
});
|
|
26
|
+
expect(result).toEqual(mockResponse);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('validateProject should call POST /projects/{projectUuid}/validate', async () => {
|
|
30
|
+
const client = new ValidationClient(mockHttp);
|
|
31
|
+
const mockResponse = { status: 'ok', results: { jobId: 'j1' } };
|
|
32
|
+
vi.mocked(mockHttp.post).mockResolvedValue(mockResponse);
|
|
33
|
+
const result = await client.validateProject('p1');
|
|
34
|
+
expect(mockHttp.post).toHaveBeenCalledWith('/projects/p1/validate', undefined);
|
|
35
|
+
expect(result).toEqual(mockResponse);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation API client.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { LightdashApi } from '@lightdash-tools/common';
|
|
6
|
+
import { BaseApiClient } from '../base-client';
|
|
7
|
+
|
|
8
|
+
/** Query params for getting validation results. */
|
|
9
|
+
export interface GetValidationResultsParams {
|
|
10
|
+
fromSettings?: boolean;
|
|
11
|
+
jobId?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Body for triggering project validation. */
|
|
15
|
+
export interface ValidateProjectBody {
|
|
16
|
+
validationTargets?: LightdashApi.Validation.ValidationTarget[];
|
|
17
|
+
explores?: unknown[];
|
|
18
|
+
onlyValidateExploresInArgs?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class ValidationClient extends BaseApiClient {
|
|
22
|
+
/** Get validation results for a project. */
|
|
23
|
+
async getValidationResults(
|
|
24
|
+
projectUuid: string,
|
|
25
|
+
params?: GetValidationResultsParams,
|
|
26
|
+
): Promise<LightdashApi.Validation.ApiValidateResponse> {
|
|
27
|
+
return this.http.get<LightdashApi.Validation.ApiValidateResponse>(
|
|
28
|
+
`/projects/${projectUuid}/validate`,
|
|
29
|
+
{ params },
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Validate content inside a project. Starts a validation job and returns the job ID. */
|
|
34
|
+
async validateProject(
|
|
35
|
+
projectUuid: string,
|
|
36
|
+
body?: ValidateProjectBody,
|
|
37
|
+
): Promise<LightdashApi.Validation.ApiJobScheduledResponse> {
|
|
38
|
+
return this.http.post<LightdashApi.Validation.ApiJobScheduledResponse>(
|
|
39
|
+
`/projects/${projectUuid}/validate`,
|
|
40
|
+
body,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { ContentClient } from './content';
|
|
3
|
+
import type { HttpClient } from '../../http/http-client';
|
|
4
|
+
|
|
5
|
+
describe('ContentClient', () => {
|
|
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('searchContent should call GET /content', async () => {
|
|
19
|
+
const client = new ContentClient(mockHttp);
|
|
20
|
+
const mockResponse = { status: 'ok', results: [] };
|
|
21
|
+
vi.mocked(mockHttp.get).mockResolvedValue(mockResponse);
|
|
22
|
+
const result = await client.searchContent({ search: 'test' });
|
|
23
|
+
expect(mockHttp.get).toHaveBeenCalledWith('/content', {
|
|
24
|
+
params: { search: 'test' },
|
|
25
|
+
});
|
|
26
|
+
expect(result).toEqual(mockResponse);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content API client (v2).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { LightdashApi } from '@lightdash-tools/common';
|
|
6
|
+
import { BaseApiClient } from '../base-client';
|
|
7
|
+
|
|
8
|
+
/** Query params for searching content. */
|
|
9
|
+
export interface SearchContentParams {
|
|
10
|
+
projectUuids?: string[];
|
|
11
|
+
spaceUuids?: string[];
|
|
12
|
+
parentSpaceUuid?: string;
|
|
13
|
+
contentTypes?: ('chart' | 'dashboard' | 'space')[];
|
|
14
|
+
pageSize?: number;
|
|
15
|
+
page?: number;
|
|
16
|
+
search?: string;
|
|
17
|
+
sortBy?: 'name' | 'createdAt' | 'updatedAt' | 'lastViewedAt' | 'views';
|
|
18
|
+
sortDirection?: 'asc' | 'desc';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class ContentClient extends BaseApiClient {
|
|
22
|
+
/** Search project content (charts, dashboards, spaces). */
|
|
23
|
+
async searchContent(
|
|
24
|
+
params?: SearchContentParams,
|
|
25
|
+
): Promise<LightdashApi.Content.ApiContentResponse> {
|
|
26
|
+
return this.http.get<LightdashApi.Content.ApiContentResponse>('/content', {
|
|
27
|
+
params,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/client.ts
CHANGED
|
@@ -21,6 +21,11 @@ import { GroupsClient } from './api/v1/groups';
|
|
|
21
21
|
import { AiAgentsClient } from './api/v1/ai-agents';
|
|
22
22
|
import { ProjectAccessClient } from './api/v1/project-access';
|
|
23
23
|
import { ExploresClient } from './api/v1/explores';
|
|
24
|
+
import { ValidationClient } from './api/v1/validation';
|
|
25
|
+
import { MetricsClient } from './api/v1/metrics';
|
|
26
|
+
import { SchedulersClient } from './api/v1/schedulers';
|
|
27
|
+
import { TagsClient } from './api/v1/tags';
|
|
28
|
+
import { ContentClient } from './api/v2/content';
|
|
24
29
|
|
|
25
30
|
/**
|
|
26
31
|
* V1 API clients namespace. Contains all v1 API clients.
|
|
@@ -37,6 +42,10 @@ export class V1ApiClients {
|
|
|
37
42
|
readonly aiAgents: AiAgentsClient;
|
|
38
43
|
readonly projectAccess: ProjectAccessClient;
|
|
39
44
|
readonly explores: ExploresClient;
|
|
45
|
+
readonly validation: ValidationClient;
|
|
46
|
+
readonly metrics: MetricsClient;
|
|
47
|
+
readonly schedulers: SchedulersClient;
|
|
48
|
+
readonly tags: TagsClient;
|
|
40
49
|
|
|
41
50
|
constructor(http: HttpClient) {
|
|
42
51
|
this.projects = new ProjectsClient(http);
|
|
@@ -50,6 +59,10 @@ export class V1ApiClients {
|
|
|
50
59
|
this.aiAgents = new AiAgentsClient(http);
|
|
51
60
|
this.projectAccess = new ProjectAccessClient(http);
|
|
52
61
|
this.explores = new ExploresClient(http);
|
|
62
|
+
this.validation = new ValidationClient(http);
|
|
63
|
+
this.metrics = new MetricsClient(http);
|
|
64
|
+
this.schedulers = new SchedulersClient(http);
|
|
65
|
+
this.tags = new TagsClient(http);
|
|
53
66
|
}
|
|
54
67
|
}
|
|
55
68
|
|
|
@@ -60,11 +73,13 @@ export class V2ApiClients {
|
|
|
60
73
|
readonly query: QueryClientV2;
|
|
61
74
|
readonly organizationRoles: OrganizationRolesClient;
|
|
62
75
|
readonly projectRoleAssignments: ProjectRoleAssignmentsClient;
|
|
76
|
+
readonly content: ContentClient;
|
|
63
77
|
|
|
64
78
|
constructor(http: HttpClient) {
|
|
65
79
|
this.query = new QueryClientV2(http);
|
|
66
80
|
this.organizationRoles = new OrganizationRolesClient(http);
|
|
67
81
|
this.projectRoleAssignments = new ProjectRoleAssignmentsClient(http);
|
|
82
|
+
this.content = new ContentClient(http);
|
|
68
83
|
}
|
|
69
84
|
}
|
|
70
85
|
|
|
@@ -128,6 +143,26 @@ export class LightdashClient {
|
|
|
128
143
|
* @deprecated Use `client.v1.explores` instead. This alias will be removed in a future major version.
|
|
129
144
|
*/
|
|
130
145
|
readonly explores: ExploresClient;
|
|
146
|
+
/**
|
|
147
|
+
* @deprecated Use `client.v1.validation` instead. This alias will be removed in a future major version.
|
|
148
|
+
*/
|
|
149
|
+
readonly validation: ValidationClient;
|
|
150
|
+
/**
|
|
151
|
+
* @deprecated Use `client.v1.metrics` instead. This alias will be removed in a future major version.
|
|
152
|
+
*/
|
|
153
|
+
readonly metrics: MetricsClient;
|
|
154
|
+
/**
|
|
155
|
+
* @deprecated Use `client.v1.schedulers` instead. This alias will be removed in a future major version.
|
|
156
|
+
*/
|
|
157
|
+
readonly schedulers: SchedulersClient;
|
|
158
|
+
/**
|
|
159
|
+
* @deprecated Use `client.v1.tags` instead. This alias will be removed in a future major version.
|
|
160
|
+
*/
|
|
161
|
+
readonly tags: TagsClient;
|
|
162
|
+
/**
|
|
163
|
+
* @deprecated Use `client.v2.content` instead. This alias will be removed in a future major version.
|
|
164
|
+
*/
|
|
165
|
+
readonly content: ContentClient;
|
|
131
166
|
|
|
132
167
|
constructor(config?: PartialLightdashClientConfig) {
|
|
133
168
|
const merged = mergeConfig(config);
|
|
@@ -156,6 +191,11 @@ export class LightdashClient {
|
|
|
156
191
|
this.groups = this.v1.groups;
|
|
157
192
|
this.aiAgents = this.v1.aiAgents;
|
|
158
193
|
this.explores = this.v1.explores;
|
|
194
|
+
this.validation = this.v1.validation;
|
|
195
|
+
this.metrics = this.v1.metrics;
|
|
196
|
+
this.schedulers = this.v1.schedulers;
|
|
197
|
+
this.tags = this.v1.tags;
|
|
198
|
+
this.content = this.v2.content;
|
|
159
199
|
}
|
|
160
200
|
|
|
161
201
|
/**
|
package/src/config.ts
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* Supports explicit config and environment variables (LIGHTDASH_API_KEY, LIGHTDASH_URL, LIGHTDASH_PROXY_AUTHORIZATION).
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import type { SecretString } from './utils/secret-string';
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
9
|
* Rate limit configuration for Bottleneck (token bucket algorithm).
|
|
8
10
|
*/
|
|
@@ -61,9 +63,9 @@ export interface LightdashClientConfig {
|
|
|
61
63
|
/** Lightdash server base URL (e.g. https://app.lightdash.cloud). */
|
|
62
64
|
baseUrl: string;
|
|
63
65
|
/** Personal access token for API authentication (without ldpat_ prefix). */
|
|
64
|
-
personalAccessToken:
|
|
66
|
+
personalAccessToken: SecretString;
|
|
65
67
|
/** Optional proxy authorization header value for proxied access. */
|
|
66
|
-
proxyAuthorization?:
|
|
68
|
+
proxyAuthorization?: SecretString;
|
|
67
69
|
/** Override default rate limits. */
|
|
68
70
|
rateLimit?: RateLimitConfig;
|
|
69
71
|
/** Request timeout in milliseconds. Default 30000. */
|
|
@@ -81,18 +83,11 @@ export interface LightdashClientConfig {
|
|
|
81
83
|
* Used when constructing the client with optional explicit overrides.
|
|
82
84
|
*/
|
|
83
85
|
export type PartialLightdashClientConfig = Partial<
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
| 'rateLimit'
|
|
90
|
-
| 'timeout'
|
|
91
|
-
| 'retry'
|
|
92
|
-
| 'logger'
|
|
93
|
-
| 'observability'
|
|
94
|
-
>
|
|
95
|
-
>;
|
|
86
|
+
Omit<LightdashClientConfig, 'personalAccessToken' | 'proxyAuthorization'>
|
|
87
|
+
> & {
|
|
88
|
+
personalAccessToken?: string | SecretString;
|
|
89
|
+
proxyAuthorization?: string | SecretString;
|
|
90
|
+
};
|
|
96
91
|
|
|
97
92
|
/** Default rate limit: 10 req/sec, 5 concurrent, token bucket. */
|
|
98
93
|
export const DEFAULT_RATE_LIMIT: Required<RateLimitConfig> = {
|
package/src/http/interceptors.ts
CHANGED
|
@@ -22,13 +22,12 @@ export function attachRequestInterceptors(
|
|
|
22
22
|
): void {
|
|
23
23
|
axiosInstance.interceptors.request.use(
|
|
24
24
|
(req: InternalAxiosRequestConfig) => {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
: `ldpat_${config.personalAccessToken}`;
|
|
25
|
+
const rawToken = config.personalAccessToken.expose();
|
|
26
|
+
const token = rawToken.startsWith('ldpat_') ? rawToken : `ldpat_${rawToken}`;
|
|
28
27
|
req.headers.Authorization = `ApiKey ${token}`;
|
|
29
28
|
|
|
30
29
|
if (config.proxyAuthorization) {
|
|
31
|
-
req.headers['Proxy-Authorization'] = config.proxyAuthorization;
|
|
30
|
+
req.headers['Proxy-Authorization'] = config.proxyAuthorization.expose();
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
(req as InternalAxiosRequestConfig & { _lightdashStartTime?: number })._lightdashStartTime =
|
package/src/index.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
2
2
|
import {
|
|
3
3
|
LightdashClient,
|
|
4
4
|
mergeConfig,
|
|
@@ -6,9 +6,19 @@ import {
|
|
|
6
6
|
LightdashApiError,
|
|
7
7
|
RateLimitError,
|
|
8
8
|
NetworkError,
|
|
9
|
+
ENV_LIGHTDASH_API_KEY,
|
|
10
|
+
ENV_LIGHTDASH_URL,
|
|
11
|
+
ENV_LIGHTDASH_PROXY_AUTHORIZATION,
|
|
12
|
+
SecretString,
|
|
9
13
|
} from './index';
|
|
10
14
|
|
|
11
15
|
describe('LightdashClient', () => {
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
vi.stubEnv(ENV_LIGHTDASH_API_KEY, '');
|
|
18
|
+
vi.stubEnv(ENV_LIGHTDASH_URL, '');
|
|
19
|
+
vi.stubEnv(ENV_LIGHTDASH_PROXY_AUTHORIZATION, '');
|
|
20
|
+
});
|
|
21
|
+
|
|
12
22
|
it('should export LightdashClient', () => {
|
|
13
23
|
expect(LightdashClient).toBeDefined();
|
|
14
24
|
});
|
|
@@ -26,17 +36,27 @@ describe('LightdashClient', () => {
|
|
|
26
36
|
/baseUrl and personalAccessToken|LIGHTDASH_URL and LIGHTDASH_API_KEY/,
|
|
27
37
|
);
|
|
28
38
|
});
|
|
29
|
-
});
|
|
30
39
|
|
|
31
|
-
describe('mergeConfig', () => {
|
|
32
40
|
it('should merge explicit config with env (explicit wins)', () => {
|
|
41
|
+
vi.stubEnv(ENV_LIGHTDASH_URL, 'https://env.example.com');
|
|
42
|
+
vi.stubEnv(ENV_LIGHTDASH_API_KEY, 'env-token');
|
|
43
|
+
|
|
33
44
|
const explicit = {
|
|
34
45
|
baseUrl: 'https://custom.example.com',
|
|
35
46
|
personalAccessToken: 'explicit-token',
|
|
36
47
|
};
|
|
37
48
|
const merged = mergeConfig(explicit);
|
|
38
49
|
expect(merged.baseUrl).toBe('https://custom.example.com');
|
|
39
|
-
expect(merged.personalAccessToken).
|
|
50
|
+
expect(merged.personalAccessToken).toBeInstanceOf(SecretString);
|
|
51
|
+
expect(merged.personalAccessToken.expose()).toBe('explicit-token');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('mergeConfig', () => {
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
vi.stubEnv(ENV_LIGHTDASH_API_KEY, '');
|
|
58
|
+
vi.stubEnv(ENV_LIGHTDASH_URL, '');
|
|
59
|
+
vi.stubEnv(ENV_LIGHTDASH_PROXY_AUTHORIZATION, '');
|
|
40
60
|
});
|
|
41
61
|
|
|
42
62
|
it('should require baseUrl and personalAccessToken', () => {
|
|
@@ -47,6 +67,12 @@ describe('mergeConfig', () => {
|
|
|
47
67
|
});
|
|
48
68
|
|
|
49
69
|
describe('loadConfigFromEnv', () => {
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
vi.stubEnv(ENV_LIGHTDASH_API_KEY, '');
|
|
72
|
+
vi.stubEnv(ENV_LIGHTDASH_URL, '');
|
|
73
|
+
vi.stubEnv(ENV_LIGHTDASH_PROXY_AUTHORIZATION, '');
|
|
74
|
+
});
|
|
75
|
+
|
|
50
76
|
it('should return empty object when env vars are not set', () => {
|
|
51
77
|
const config = loadConfigFromEnv();
|
|
52
78
|
expect(config).toEqual({});
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,16 @@ export { OrganizationRolesClient } from './api/v2/organization-roles';
|
|
|
5
5
|
export { ProjectRoleAssignmentsClient } from './api/v2/project-role-assignments';
|
|
6
6
|
export { AiAgentsClient } from './api/v1/ai-agents';
|
|
7
7
|
export { ProjectAccessClient } from './api/v1/project-access';
|
|
8
|
+
export { ValidationClient } from './api/v1/validation';
|
|
9
|
+
export { MetricsClient } from './api/v1/metrics';
|
|
10
|
+
export { SchedulersClient } from './api/v1/schedulers';
|
|
11
|
+
export { TagsClient } from './api/v1/tags';
|
|
12
|
+
export { ContentClient } from './api/v2/content';
|
|
8
13
|
export type { ListMembersParams, ListAllMembersParams } from './api/v1/users';
|
|
14
|
+
export type { GetValidationResultsParams, ValidateProjectBody } from './api/v1/validation';
|
|
15
|
+
export type { ListMetricsParams } from './api/v1/metrics';
|
|
16
|
+
export type { ListSchedulersParams } from './api/v1/schedulers';
|
|
17
|
+
export type { SearchContentParams } from './api/v2/content';
|
|
9
18
|
export type {
|
|
10
19
|
ListGroupsParams,
|
|
11
20
|
ListAllGroupsParams,
|
|
@@ -37,5 +46,6 @@ export {
|
|
|
37
46
|
} from './utils/env';
|
|
38
47
|
export { HttpClient } from './http/http-client';
|
|
39
48
|
export { RateLimiter } from './http/rate-limiter';
|
|
49
|
+
export { SecretString } from './utils/secret-string';
|
|
40
50
|
export type { ApiResponseOk, ApiResponseError, ApiResponseBody, ApiError } from './types/api';
|
|
41
51
|
export type { paths, components, operations } from './types/api';
|
package/src/utils/env.test.ts
CHANGED
|
@@ -60,7 +60,7 @@ describe('mergeConfig', () => {
|
|
|
60
60
|
personalAccessToken: 'explicit-token',
|
|
61
61
|
});
|
|
62
62
|
expect(merged.baseUrl).toBe('https://explicit.example.com');
|
|
63
|
-
expect(merged.personalAccessToken).toBe('explicit-token');
|
|
63
|
+
expect(merged.personalAccessToken.expose()).toBe('explicit-token');
|
|
64
64
|
});
|
|
65
65
|
|
|
66
66
|
it('should use env when explicit config omits a field', () => {
|
|
@@ -68,7 +68,7 @@ describe('mergeConfig', () => {
|
|
|
68
68
|
process.env[ENV_LIGHTDASH_API_KEY] = 'env-token';
|
|
69
69
|
const merged = mergeConfig({ baseUrl: 'https://explicit.example.com' });
|
|
70
70
|
expect(merged.baseUrl).toBe('https://explicit.example.com');
|
|
71
|
-
expect(merged.personalAccessToken).toBe('env-token');
|
|
71
|
+
expect(merged.personalAccessToken.expose()).toBe('env-token');
|
|
72
72
|
});
|
|
73
73
|
|
|
74
74
|
it('should throw when baseUrl and personalAccessToken cannot be resolved', () => {
|
package/src/utils/env.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { LightdashClientConfig, PartialLightdashClientConfig } from '../config';
|
|
8
|
+
import { SecretString } from './secret-string';
|
|
8
9
|
|
|
9
10
|
/** Environment variable names (aligned with Lightdash CLI). */
|
|
10
11
|
export const ENV_LIGHTDASH_API_KEY = 'LIGHTDASH_API_KEY';
|
|
@@ -47,9 +48,10 @@ export function mergeConfig(
|
|
|
47
48
|
const fromEnv = loadConfigFromEnv();
|
|
48
49
|
|
|
49
50
|
const baseUrl = explicit?.baseUrl ?? fromEnv.baseUrl ?? '';
|
|
50
|
-
const
|
|
51
|
+
const pat = explicit?.personalAccessToken ?? fromEnv.personalAccessToken ?? '';
|
|
52
|
+
const proxyAuth = explicit?.proxyAuthorization ?? fromEnv.proxyAuthorization;
|
|
51
53
|
|
|
52
|
-
if (!baseUrl || !
|
|
54
|
+
if (!baseUrl || !pat) {
|
|
53
55
|
throw new Error(
|
|
54
56
|
'Lightdash client requires baseUrl and personalAccessToken. ' +
|
|
55
57
|
'Set them in config or via LIGHTDASH_URL and LIGHTDASH_API_KEY.',
|
|
@@ -58,8 +60,8 @@ export function mergeConfig(
|
|
|
58
60
|
|
|
59
61
|
return {
|
|
60
62
|
baseUrl,
|
|
61
|
-
personalAccessToken,
|
|
62
|
-
proxyAuthorization:
|
|
63
|
+
personalAccessToken: typeof pat === 'string' ? new SecretString(pat) : pat,
|
|
64
|
+
proxyAuthorization: typeof proxyAuth === 'string' ? new SecretString(proxyAuth) : proxyAuth,
|
|
63
65
|
rateLimit: explicit?.rateLimit,
|
|
64
66
|
timeout: explicit?.timeout,
|
|
65
67
|
retry: explicit?.retry,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A wrapper for sensitive strings that prevents accidental exposure in logs,
|
|
3
|
+
* console output, or JSON serialization.
|
|
4
|
+
*/
|
|
5
|
+
export class SecretString {
|
|
6
|
+
constructor(private readonly value: string) {}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Prevents the actual value from being shown when converted to a string.
|
|
10
|
+
*/
|
|
11
|
+
toString(): string {
|
|
12
|
+
return '**********';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Prevents the actual value from being shown when serialized to JSON.
|
|
17
|
+
*/
|
|
18
|
+
toJSON(): string {
|
|
19
|
+
return '**********';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Node.js custom inspect symbol to prevent exposure in console.log/util.inspect.
|
|
24
|
+
*/
|
|
25
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
26
|
+
return 'SecretString(**********)';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Explicitly expose the underlying sensitive value.
|
|
31
|
+
* Use this only when necessary, e.g., when setting HTTP headers.
|
|
32
|
+
*/
|
|
33
|
+
expose(): string {
|
|
34
|
+
return this.value;
|
|
35
|
+
}
|
|
36
|
+
}
|