@lightdash-tools/client 0.1.1 → 0.1.2
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/dist/config.d.ts +7 -3
- package/dist/http/interceptors.js +3 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -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/config.ts +9 -14
- package/src/http/interceptors.ts +3 -4
- package/src/index.test.ts +30 -4
- package/src/index.ts +1 -0
- package/src/utils/env.test.ts +2 -2
- package/src/utils/env.ts +6 -4
- package/src/utils/secret-string.ts +36 -0
package/dist/config.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Configuration types for the Lightdash HTTP client.
|
|
3
3
|
* Supports explicit config and environment variables (LIGHTDASH_API_KEY, LIGHTDASH_URL, LIGHTDASH_PROXY_AUTHORIZATION).
|
|
4
4
|
*/
|
|
5
|
+
import type { SecretString } from './utils/secret-string';
|
|
5
6
|
/**
|
|
6
7
|
* Rate limit configuration for Bottleneck (token bucket algorithm).
|
|
7
8
|
*/
|
|
@@ -56,9 +57,9 @@ export interface LightdashClientConfig {
|
|
|
56
57
|
/** Lightdash server base URL (e.g. https://app.lightdash.cloud). */
|
|
57
58
|
baseUrl: string;
|
|
58
59
|
/** Personal access token for API authentication (without ldpat_ prefix). */
|
|
59
|
-
personalAccessToken:
|
|
60
|
+
personalAccessToken: SecretString;
|
|
60
61
|
/** Optional proxy authorization header value for proxied access. */
|
|
61
|
-
proxyAuthorization?:
|
|
62
|
+
proxyAuthorization?: SecretString;
|
|
62
63
|
/** Override default rate limits. */
|
|
63
64
|
rateLimit?: RateLimitConfig;
|
|
64
65
|
/** Request timeout in milliseconds. Default 30000. */
|
|
@@ -74,7 +75,10 @@ export interface LightdashClientConfig {
|
|
|
74
75
|
* Partial config for merging with environment variables.
|
|
75
76
|
* Used when constructing the client with optional explicit overrides.
|
|
76
77
|
*/
|
|
77
|
-
export type PartialLightdashClientConfig = Partial<
|
|
78
|
+
export type PartialLightdashClientConfig = Partial<Omit<LightdashClientConfig, 'personalAccessToken' | 'proxyAuthorization'>> & {
|
|
79
|
+
personalAccessToken?: string | SecretString;
|
|
80
|
+
proxyAuthorization?: string | SecretString;
|
|
81
|
+
};
|
|
78
82
|
/** Default rate limit: 10 req/sec, 5 concurrent, token bucket. */
|
|
79
83
|
export declare const DEFAULT_RATE_LIMIT: Required<RateLimitConfig>;
|
|
80
84
|
/** Default request timeout in ms. */
|
|
@@ -18,12 +18,11 @@ function getRetryAfterSeconds(value) {
|
|
|
18
18
|
function attachRequestInterceptors(axiosInstance, config) {
|
|
19
19
|
axiosInstance.interceptors.request.use((req) => {
|
|
20
20
|
var _a;
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
: `ldpat_${config.personalAccessToken}`;
|
|
21
|
+
const rawToken = config.personalAccessToken.expose();
|
|
22
|
+
const token = rawToken.startsWith('ldpat_') ? rawToken : `ldpat_${rawToken}`;
|
|
24
23
|
req.headers.Authorization = `ApiKey ${token}`;
|
|
25
24
|
if (config.proxyAuthorization) {
|
|
26
|
-
req.headers['Proxy-Authorization'] = config.proxyAuthorization;
|
|
25
|
+
req.headers['Proxy-Authorization'] = config.proxyAuthorization.expose();
|
|
27
26
|
}
|
|
28
27
|
req._lightdashStartTime =
|
|
29
28
|
Date.now();
|
package/dist/index.d.ts
CHANGED
|
@@ -18,5 +18,6 @@ export { loadConfigFromEnv, mergeConfig } from './utils/env';
|
|
|
18
18
|
export { ENV_LIGHTDASH_API_KEY, ENV_LIGHTDASH_URL, ENV_LIGHTDASH_PROXY_AUTHORIZATION, } from './utils/env';
|
|
19
19
|
export { HttpClient } from './http/http-client';
|
|
20
20
|
export { RateLimiter } from './http/rate-limiter';
|
|
21
|
+
export { SecretString } from './utils/secret-string';
|
|
21
22
|
export type { ApiResponseOk, ApiResponseError, ApiResponseBody, ApiError } from './types/api';
|
|
22
23
|
export type { paths, components, operations } from './types/api';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RateLimiter = exports.HttpClient = exports.ENV_LIGHTDASH_PROXY_AUTHORIZATION = exports.ENV_LIGHTDASH_URL = exports.ENV_LIGHTDASH_API_KEY = exports.mergeConfig = exports.loadConfigFromEnv = exports.NetworkError = exports.RateLimitError = exports.LightdashApiError = exports.DEFAULT_RETRY = exports.DEFAULT_TIMEOUT = exports.DEFAULT_RATE_LIMIT = exports.consoleLogger = exports.noopLogger = exports.ProjectAccessClient = exports.AiAgentsClient = exports.ProjectRoleAssignmentsClient = exports.OrganizationRolesClient = exports.GroupsClient = exports.UsersClient = exports.V2ApiClients = exports.V1ApiClients = exports.LightdashClient = void 0;
|
|
3
|
+
exports.SecretString = exports.RateLimiter = exports.HttpClient = exports.ENV_LIGHTDASH_PROXY_AUTHORIZATION = exports.ENV_LIGHTDASH_URL = exports.ENV_LIGHTDASH_API_KEY = exports.mergeConfig = exports.loadConfigFromEnv = exports.NetworkError = exports.RateLimitError = exports.LightdashApiError = exports.DEFAULT_RETRY = exports.DEFAULT_TIMEOUT = exports.DEFAULT_RATE_LIMIT = exports.consoleLogger = exports.noopLogger = exports.ProjectAccessClient = exports.AiAgentsClient = exports.ProjectRoleAssignmentsClient = exports.OrganizationRolesClient = exports.GroupsClient = exports.UsersClient = exports.V2ApiClients = exports.V1ApiClients = exports.LightdashClient = void 0;
|
|
4
4
|
var client_1 = require("./client");
|
|
5
5
|
Object.defineProperty(exports, "LightdashClient", { enumerable: true, get: function () { return client_1.LightdashClient; } });
|
|
6
6
|
Object.defineProperty(exports, "V1ApiClients", { enumerable: true, get: function () { return client_1.V1ApiClients; } });
|
|
@@ -39,3 +39,5 @@ var http_client_1 = require("./http/http-client");
|
|
|
39
39
|
Object.defineProperty(exports, "HttpClient", { enumerable: true, get: function () { return http_client_1.HttpClient; } });
|
|
40
40
|
var rate_limiter_1 = require("./http/rate-limiter");
|
|
41
41
|
Object.defineProperty(exports, "RateLimiter", { enumerable: true, get: function () { return rate_limiter_1.RateLimiter; } });
|
|
42
|
+
var secret_string_1 = require("./utils/secret-string");
|
|
43
|
+
Object.defineProperty(exports, "SecretString", { enumerable: true, get: function () { return secret_string_1.SecretString; } });
|
package/dist/index.test.js
CHANGED
|
@@ -3,6 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const vitest_1 = require("vitest");
|
|
4
4
|
const index_1 = require("./index");
|
|
5
5
|
(0, vitest_1.describe)('LightdashClient', () => {
|
|
6
|
+
(0, vitest_1.beforeEach)(() => {
|
|
7
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_API_KEY, '');
|
|
8
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_URL, '');
|
|
9
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_PROXY_AUTHORIZATION, '');
|
|
10
|
+
});
|
|
6
11
|
(0, vitest_1.it)('should export LightdashClient', () => {
|
|
7
12
|
(0, vitest_1.expect)(index_1.LightdashClient).toBeDefined();
|
|
8
13
|
});
|
|
@@ -16,16 +21,24 @@ const index_1 = require("./index");
|
|
|
16
21
|
(0, vitest_1.it)('should throw when baseUrl and personalAccessToken are missing and not in env', () => {
|
|
17
22
|
(0, vitest_1.expect)(() => new index_1.LightdashClient({})).toThrow(/baseUrl and personalAccessToken|LIGHTDASH_URL and LIGHTDASH_API_KEY/);
|
|
18
23
|
});
|
|
19
|
-
});
|
|
20
|
-
(0, vitest_1.describe)('mergeConfig', () => {
|
|
21
24
|
(0, vitest_1.it)('should merge explicit config with env (explicit wins)', () => {
|
|
25
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_URL, 'https://env.example.com');
|
|
26
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_API_KEY, 'env-token');
|
|
22
27
|
const explicit = {
|
|
23
28
|
baseUrl: 'https://custom.example.com',
|
|
24
29
|
personalAccessToken: 'explicit-token',
|
|
25
30
|
};
|
|
26
31
|
const merged = (0, index_1.mergeConfig)(explicit);
|
|
27
32
|
(0, vitest_1.expect)(merged.baseUrl).toBe('https://custom.example.com');
|
|
28
|
-
(0, vitest_1.expect)(merged.personalAccessToken).
|
|
33
|
+
(0, vitest_1.expect)(merged.personalAccessToken).toBeInstanceOf(index_1.SecretString);
|
|
34
|
+
(0, vitest_1.expect)(merged.personalAccessToken.expose()).toBe('explicit-token');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
(0, vitest_1.describe)('mergeConfig', () => {
|
|
38
|
+
(0, vitest_1.beforeEach)(() => {
|
|
39
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_API_KEY, '');
|
|
40
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_URL, '');
|
|
41
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_PROXY_AUTHORIZATION, '');
|
|
29
42
|
});
|
|
30
43
|
(0, vitest_1.it)('should require baseUrl and personalAccessToken', () => {
|
|
31
44
|
(0, vitest_1.expect)(() => (0, index_1.mergeConfig)({})).toThrow();
|
|
@@ -34,6 +47,11 @@ const index_1 = require("./index");
|
|
|
34
47
|
});
|
|
35
48
|
});
|
|
36
49
|
(0, vitest_1.describe)('loadConfigFromEnv', () => {
|
|
50
|
+
(0, vitest_1.beforeEach)(() => {
|
|
51
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_API_KEY, '');
|
|
52
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_URL, '');
|
|
53
|
+
vitest_1.vi.stubEnv(index_1.ENV_LIGHTDASH_PROXY_AUTHORIZATION, '');
|
|
54
|
+
});
|
|
37
55
|
(0, vitest_1.it)('should return empty object when env vars are not set', () => {
|
|
38
56
|
const config = (0, index_1.loadConfigFromEnv)();
|
|
39
57
|
(0, vitest_1.expect)(config).toEqual({});
|
package/dist/utils/env.js
CHANGED
|
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.ENV_LIGHTDASH_PROXY_AUTHORIZATION = exports.ENV_LIGHTDASH_URL = exports.ENV_LIGHTDASH_API_KEY = void 0;
|
|
9
9
|
exports.loadConfigFromEnv = loadConfigFromEnv;
|
|
10
10
|
exports.mergeConfig = mergeConfig;
|
|
11
|
+
const secret_string_1 = require("./secret-string");
|
|
11
12
|
/** Environment variable names (aligned with Lightdash CLI). */
|
|
12
13
|
exports.ENV_LIGHTDASH_API_KEY = 'LIGHTDASH_API_KEY';
|
|
13
14
|
exports.ENV_LIGHTDASH_URL = 'LIGHTDASH_URL';
|
|
@@ -41,15 +42,16 @@ function mergeConfig(explicit) {
|
|
|
41
42
|
var _a, _b, _c, _d, _e;
|
|
42
43
|
const fromEnv = loadConfigFromEnv();
|
|
43
44
|
const baseUrl = (_b = (_a = explicit === null || explicit === void 0 ? void 0 : explicit.baseUrl) !== null && _a !== void 0 ? _a : fromEnv.baseUrl) !== null && _b !== void 0 ? _b : '';
|
|
44
|
-
const
|
|
45
|
-
|
|
45
|
+
const pat = (_d = (_c = explicit === null || explicit === void 0 ? void 0 : explicit.personalAccessToken) !== null && _c !== void 0 ? _c : fromEnv.personalAccessToken) !== null && _d !== void 0 ? _d : '';
|
|
46
|
+
const proxyAuth = (_e = explicit === null || explicit === void 0 ? void 0 : explicit.proxyAuthorization) !== null && _e !== void 0 ? _e : fromEnv.proxyAuthorization;
|
|
47
|
+
if (!baseUrl || !pat) {
|
|
46
48
|
throw new Error('Lightdash client requires baseUrl and personalAccessToken. ' +
|
|
47
49
|
'Set them in config or via LIGHTDASH_URL and LIGHTDASH_API_KEY.');
|
|
48
50
|
}
|
|
49
51
|
return {
|
|
50
52
|
baseUrl,
|
|
51
|
-
personalAccessToken,
|
|
52
|
-
proxyAuthorization:
|
|
53
|
+
personalAccessToken: typeof pat === 'string' ? new secret_string_1.SecretString(pat) : pat,
|
|
54
|
+
proxyAuthorization: typeof proxyAuth === 'string' ? new secret_string_1.SecretString(proxyAuth) : proxyAuth,
|
|
53
55
|
rateLimit: explicit === null || explicit === void 0 ? void 0 : explicit.rateLimit,
|
|
54
56
|
timeout: explicit === null || explicit === void 0 ? void 0 : explicit.timeout,
|
|
55
57
|
retry: explicit === null || explicit === void 0 ? void 0 : explicit.retry,
|
package/dist/utils/env.test.js
CHANGED
|
@@ -47,14 +47,14 @@ const originalEnv = Object.assign({}, process.env);
|
|
|
47
47
|
personalAccessToken: 'explicit-token',
|
|
48
48
|
});
|
|
49
49
|
(0, vitest_1.expect)(merged.baseUrl).toBe('https://explicit.example.com');
|
|
50
|
-
(0, vitest_1.expect)(merged.personalAccessToken).toBe('explicit-token');
|
|
50
|
+
(0, vitest_1.expect)(merged.personalAccessToken.expose()).toBe('explicit-token');
|
|
51
51
|
});
|
|
52
52
|
(0, vitest_1.it)('should use env when explicit config omits a field', () => {
|
|
53
53
|
process.env[env_1.ENV_LIGHTDASH_URL] = 'https://env.example.com';
|
|
54
54
|
process.env[env_1.ENV_LIGHTDASH_API_KEY] = 'env-token';
|
|
55
55
|
const merged = (0, env_1.mergeConfig)({ baseUrl: 'https://explicit.example.com' });
|
|
56
56
|
(0, vitest_1.expect)(merged.baseUrl).toBe('https://explicit.example.com');
|
|
57
|
-
(0, vitest_1.expect)(merged.personalAccessToken).toBe('env-token');
|
|
57
|
+
(0, vitest_1.expect)(merged.personalAccessToken.expose()).toBe('env-token');
|
|
58
58
|
});
|
|
59
59
|
(0, vitest_1.it)('should throw when baseUrl and personalAccessToken cannot be resolved', () => {
|
|
60
60
|
(0, vitest_1.expect)(() => (0, env_1.mergeConfig)(undefined)).toThrow(/baseUrl and personalAccessToken/);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A wrapper for sensitive strings that prevents accidental exposure in logs,
|
|
3
|
+
* console output, or JSON serialization.
|
|
4
|
+
*/
|
|
5
|
+
export declare class SecretString {
|
|
6
|
+
private readonly value;
|
|
7
|
+
constructor(value: string);
|
|
8
|
+
/**
|
|
9
|
+
* Prevents the actual value from being shown when converted to a string.
|
|
10
|
+
*/
|
|
11
|
+
toString(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Prevents the actual value from being shown when serialized to JSON.
|
|
14
|
+
*/
|
|
15
|
+
toJSON(): string;
|
|
16
|
+
/**
|
|
17
|
+
* Explicitly expose the underlying sensitive value.
|
|
18
|
+
* Use this only when necessary, e.g., when setting HTTP headers.
|
|
19
|
+
*/
|
|
20
|
+
expose(): string;
|
|
21
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SecretString = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper for sensitive strings that prevents accidental exposure in logs,
|
|
6
|
+
* console output, or JSON serialization.
|
|
7
|
+
*/
|
|
8
|
+
class SecretString {
|
|
9
|
+
constructor(value) {
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Prevents the actual value from being shown when converted to a string.
|
|
14
|
+
*/
|
|
15
|
+
toString() {
|
|
16
|
+
return '**********';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Prevents the actual value from being shown when serialized to JSON.
|
|
20
|
+
*/
|
|
21
|
+
toJSON() {
|
|
22
|
+
return '**********';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Node.js custom inspect symbol to prevent exposure in console.log/util.inspect.
|
|
26
|
+
*/
|
|
27
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
28
|
+
return 'SecretString(**********)';
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Explicitly expose the underlying sensitive value.
|
|
32
|
+
* Use this only when necessary, e.g., when setting HTTP headers.
|
|
33
|
+
*/
|
|
34
|
+
expose() {
|
|
35
|
+
return this.value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.SecretString = SecretString;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightdash-tools/client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Client library for Lightdash AI.",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"axios": "^1.13.5",
|
|
12
12
|
"bottleneck": "^2.19.5",
|
|
13
|
-
"@lightdash-tools/common": "0.1.
|
|
13
|
+
"@lightdash-tools/common": "0.1.2"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/node": "^25.2.3"
|
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
|
@@ -37,5 +37,6 @@ export {
|
|
|
37
37
|
} from './utils/env';
|
|
38
38
|
export { HttpClient } from './http/http-client';
|
|
39
39
|
export { RateLimiter } from './http/rate-limiter';
|
|
40
|
+
export { SecretString } from './utils/secret-string';
|
|
40
41
|
export type { ApiResponseOk, ApiResponseError, ApiResponseBody, ApiError } from './types/api';
|
|
41
42
|
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
|
+
}
|