@lightdash-tools/client 0.6.0 → 0.7.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 (237) hide show
  1. package/README.md +2 -2
  2. package/dist/agentops/apply-agent-change.d.ts +3 -0
  3. package/dist/agentops/apply-agent-change.js +63 -0
  4. package/dist/agentops/apply-bundle-diff.d.ts +13 -0
  5. package/dist/agentops/apply-bundle-diff.js +44 -0
  6. package/dist/agentops/apply-bundle-diff.test.d.ts +1 -0
  7. package/dist/agentops/apply-bundle-diff.test.js +397 -0
  8. package/dist/agentops/apply-context.d.ts +14 -0
  9. package/dist/agentops/apply-context.js +6 -0
  10. package/dist/agentops/apply-evaluation-change.d.ts +3 -0
  11. package/dist/agentops/apply-evaluation-change.js +78 -0
  12. package/dist/agentops/fetch-bundle-state.d.ts +6 -0
  13. package/dist/agentops/fetch-bundle-state.js +20 -0
  14. package/dist/agentops/fetch-bundle-state.test.d.ts +1 -0
  15. package/dist/agentops/fetch-bundle-state.test.js +44 -0
  16. package/dist/agentops/index.d.ts +3 -0
  17. package/dist/agentops/index.js +9 -0
  18. package/dist/agentops/resolve-evaluation-run.d.ts +15 -0
  19. package/dist/agentops/resolve-evaluation-run.js +48 -0
  20. package/dist/api/v1/ai-agents/admin.d.ts +16 -0
  21. package/dist/api/v1/ai-agents/admin.js +29 -0
  22. package/dist/api/v1/ai-agents/agents.d.ts +18 -0
  23. package/dist/api/v1/ai-agents/agents.js +31 -0
  24. package/dist/api/v1/ai-agents/artifacts.d.ts +26 -0
  25. package/dist/api/v1/ai-agents/artifacts.js +41 -0
  26. package/dist/api/v1/ai-agents/artifacts.test.d.ts +1 -0
  27. package/dist/api/v1/ai-agents/artifacts.test.js +69 -0
  28. package/dist/api/v1/ai-agents/discovery.d.ts +28 -0
  29. package/dist/api/v1/ai-agents/discovery.js +46 -0
  30. package/dist/api/v1/ai-agents/discovery.test.d.ts +1 -0
  31. package/dist/api/v1/ai-agents/discovery.test.js +90 -0
  32. package/dist/api/v1/ai-agents/evaluations.d.ts +39 -0
  33. package/dist/api/v1/ai-agents/evaluations.js +70 -0
  34. package/dist/api/v1/ai-agents/facade.d.ts +79 -0
  35. package/dist/api/v1/ai-agents/facade.js +193 -0
  36. package/dist/api/v1/ai-agents/feedback.d.ts +10 -0
  37. package/dist/api/v1/ai-agents/feedback.js +15 -0
  38. package/dist/api/v1/ai-agents/feedback.test.d.ts +1 -0
  39. package/dist/api/v1/ai-agents/feedback.test.js +23 -0
  40. package/dist/api/v1/ai-agents/index.d.ts +1 -0
  41. package/dist/api/v1/ai-agents/index.js +5 -0
  42. package/dist/api/v1/ai-agents/mcp-servers.d.ts +20 -0
  43. package/dist/api/v1/ai-agents/mcp-servers.js +35 -0
  44. package/dist/api/v1/ai-agents/mcp-servers.test.d.ts +1 -0
  45. package/dist/api/v1/ai-agents/mcp-servers.test.js +66 -0
  46. package/dist/api/v1/ai-agents/request-options.d.ts +12 -0
  47. package/dist/api/v1/ai-agents/request-options.js +20 -0
  48. package/dist/api/v1/ai-agents/sql-approval.d.ts +10 -0
  49. package/dist/api/v1/ai-agents/sql-approval.js +15 -0
  50. package/dist/api/v1/ai-agents/sql-approval.test.d.ts +1 -0
  51. package/dist/api/v1/ai-agents/sql-approval.test.js +25 -0
  52. package/dist/api/v1/ai-agents/threads.d.ts +56 -0
  53. package/dist/api/v1/ai-agents/threads.js +97 -0
  54. package/dist/api/v1/ai-agents/threads.test.d.ts +1 -0
  55. package/dist/api/v1/ai-agents/threads.test.js +39 -0
  56. package/dist/api/v1/ai-agents.test.js +172 -89
  57. package/dist/api/v1/analytics.d.ts +10 -0
  58. package/dist/api/v1/analytics.js +14 -0
  59. package/dist/api/v1/analytics.test.d.ts +4 -0
  60. package/dist/api/v1/analytics.test.js +27 -0
  61. package/dist/api/v1/charts.d.ts +5 -2
  62. package/dist/api/v1/charts.js +18 -30
  63. package/dist/api/v1/charts.test.js +19 -28
  64. package/dist/api/v1/dashboards.d.ts +11 -2
  65. package/dist/api/v1/dashboards.js +19 -14
  66. package/dist/api/v1/dashboards.test.js +54 -12
  67. package/dist/api/v1/explores.d.ts +4 -2
  68. package/dist/api/v1/explores.js +20 -29
  69. package/dist/api/v1/explores.test.js +31 -21
  70. package/dist/api/v1/groups.d.ts +1 -1
  71. package/dist/api/v1/groups.js +25 -51
  72. package/dist/api/v1/groups.test.js +15 -24
  73. package/dist/api/v1/metrics.d.ts +2 -2
  74. package/dist/api/v1/metrics.js +4 -17
  75. package/dist/api/v1/metrics.test.js +6 -15
  76. package/dist/api/v1/organizations.d.ts +1 -1
  77. package/dist/api/v1/organizations.js +2 -13
  78. package/dist/api/v1/organizations.test.js +3 -12
  79. package/dist/api/v1/project-access.d.ts +1 -1
  80. package/dist/api/v1/project-access.js +18 -45
  81. package/dist/api/v1/project-access.test.js +27 -36
  82. package/dist/api/v1/projects.d.ts +1 -1
  83. package/dist/api/v1/projects.js +6 -21
  84. package/dist/api/v1/projects.test.js +9 -18
  85. package/dist/api/v1/query.d.ts +5 -2
  86. package/dist/api/v1/query.js +8 -18
  87. package/dist/api/v1/query.test.js +6 -15
  88. package/dist/api/v1/schedulers.d.ts +2 -2
  89. package/dist/api/v1/schedulers.js +4 -17
  90. package/dist/api/v1/schedulers.test.js +6 -15
  91. package/dist/api/v1/spaces.d.ts +1 -1
  92. package/dist/api/v1/spaces.js +18 -45
  93. package/dist/api/v1/spaces.test.js +27 -36
  94. package/dist/api/v1/tags.d.ts +1 -1
  95. package/dist/api/v1/tags.js +4 -17
  96. package/dist/api/v1/tags.test.js +6 -15
  97. package/dist/api/v1/users.d.ts +3 -1
  98. package/dist/api/v1/users.js +22 -39
  99. package/dist/api/v1/users.test.js +44 -30
  100. package/dist/api/v1/validation.d.ts +5 -2
  101. package/dist/api/v1/validation.js +8 -18
  102. package/dist/api/v1/validation.test.js +6 -15
  103. package/dist/api/v2/content.d.ts +2 -2
  104. package/dist/api/v2/content.js +3 -14
  105. package/dist/api/v2/content.test.js +3 -12
  106. package/dist/api/v2/dashboards.d.ts +10 -0
  107. package/dist/api/v2/dashboards.js +14 -0
  108. package/dist/api/v2/dashboards.test.d.ts +4 -0
  109. package/dist/api/v2/dashboards.test.js +27 -0
  110. package/dist/api/v2/organization-roles.d.ts +1 -1
  111. package/dist/api/v2/organization-roles.js +21 -50
  112. package/dist/api/v2/organization-roles.test.js +33 -42
  113. package/dist/api/v2/project-role-assignments.d.ts +1 -1
  114. package/dist/api/v2/project-role-assignments.js +12 -33
  115. package/dist/api/v2/project-role-assignments.test.js +18 -27
  116. package/dist/api/v2/query.d.ts +1 -1
  117. package/dist/api/v2/query.js +10 -29
  118. package/dist/api/v2/query.test.js +15 -24
  119. package/dist/api/v2/validation.d.ts +28 -0
  120. package/dist/api/v2/validation.js +23 -0
  121. package/dist/client.d.ts +20 -14
  122. package/dist/client.js +22 -16
  123. package/dist/config.d.ts +35 -10
  124. package/dist/http/axios-client.d.ts +4 -4
  125. package/dist/http/http-client.d.ts +3 -3
  126. package/dist/http/http-client.js +19 -40
  127. package/dist/http/interceptors.d.ts +3 -3
  128. package/dist/http/interceptors.js +16 -3
  129. package/dist/http/rate-limiter.js +2 -13
  130. package/dist/http/rate-limiter.test.js +15 -24
  131. package/dist/index.d.ts +7 -2
  132. package/dist/index.js +12 -1
  133. package/dist/index.test.js +13 -5
  134. package/dist/pagination/fetch-all-pages.js +18 -29
  135. package/dist/pagination/fetch-all-pages.test.js +15 -24
  136. package/dist/types/api.d.ts +1 -1
  137. package/dist/utils/env.d.ts +11 -2
  138. package/dist/utils/env.js +41 -8
  139. package/dist/utils/env.test.js +14 -4
  140. package/dist/utils/logger.js +5 -4
  141. package/dist/utils/retry.js +17 -28
  142. package/dist/utils/retry.test.js +9 -18
  143. package/dist/utils/secret-string.test.d.ts +1 -0
  144. package/dist/utils/secret-string.test.js +25 -0
  145. package/package.json +12 -4
  146. package/src/agentops/apply-agent-change.ts +82 -0
  147. package/src/agentops/apply-bundle-diff.test.ts +453 -0
  148. package/src/agentops/apply-bundle-diff.ts +63 -0
  149. package/src/agentops/apply-context.ts +23 -0
  150. package/src/agentops/apply-evaluation-change.ts +125 -0
  151. package/src/agentops/fetch-bundle-state.test.ts +51 -0
  152. package/src/agentops/fetch-bundle-state.ts +33 -0
  153. package/src/agentops/index.ts +11 -0
  154. package/src/agentops/resolve-evaluation-run.ts +94 -0
  155. package/src/api/v1/ai-agents/admin.ts +41 -0
  156. package/src/api/v1/ai-agents/agents.ts +40 -0
  157. package/src/api/v1/ai-agents/artifacts.test.ts +84 -0
  158. package/src/api/v1/ai-agents/artifacts.ts +92 -0
  159. package/src/api/v1/ai-agents/discovery.test.ts +102 -0
  160. package/src/api/v1/ai-agents/discovery.ts +79 -0
  161. package/src/api/v1/ai-agents/evaluations.ts +152 -0
  162. package/src/api/v1/ai-agents/facade.ts +489 -0
  163. package/src/api/v1/ai-agents/feedback.test.ts +30 -0
  164. package/src/api/v1/ai-agents/feedback.ts +24 -0
  165. package/src/api/v1/ai-agents/index.ts +1 -0
  166. package/src/api/v1/ai-agents/mcp-servers.test.ts +81 -0
  167. package/src/api/v1/ai-agents/mcp-servers.ts +67 -0
  168. package/src/api/v1/ai-agents/request-options.ts +32 -0
  169. package/src/api/v1/ai-agents/sql-approval.test.ts +32 -0
  170. package/src/api/v1/ai-agents/sql-approval.ts +24 -0
  171. package/src/api/v1/ai-agents/threads.test.ts +55 -0
  172. package/src/api/v1/ai-agents/threads.ts +216 -0
  173. package/src/api/v1/ai-agents.test.ts +189 -13
  174. package/src/api/v1/analytics.test.ts +32 -0
  175. package/src/api/v1/analytics.ts +16 -0
  176. package/src/api/v1/charts.test.ts +8 -6
  177. package/src/api/v1/charts.ts +8 -4
  178. package/src/api/v1/dashboards.test.ts +57 -0
  179. package/src/api/v1/dashboards.ts +41 -2
  180. package/src/api/v1/explores.test.ts +22 -0
  181. package/src/api/v1/explores.ts +13 -2
  182. package/src/api/v1/groups.test.ts +2 -0
  183. package/src/api/v1/groups.ts +9 -7
  184. package/src/api/v1/metrics.test.ts +2 -0
  185. package/src/api/v1/metrics.ts +3 -2
  186. package/src/api/v1/organizations.test.ts +2 -0
  187. package/src/api/v1/organizations.ts +2 -1
  188. package/src/api/v1/project-access.test.ts +3 -1
  189. package/src/api/v1/project-access.ts +2 -1
  190. package/src/api/v1/projects.test.ts +2 -0
  191. package/src/api/v1/projects.ts +2 -1
  192. package/src/api/v1/query.test.ts +2 -0
  193. package/src/api/v1/query.ts +6 -2
  194. package/src/api/v1/schedulers.test.ts +2 -0
  195. package/src/api/v1/schedulers.ts +3 -2
  196. package/src/api/v1/spaces.test.ts +2 -0
  197. package/src/api/v1/spaces.ts +2 -1
  198. package/src/api/v1/tags.test.ts +2 -0
  199. package/src/api/v1/tags.ts +2 -1
  200. package/src/api/v1/users.test.ts +26 -0
  201. package/src/api/v1/users.ts +8 -2
  202. package/src/api/v1/validation.test.ts +2 -0
  203. package/src/api/v1/validation.ts +6 -2
  204. package/src/api/v2/content.test.ts +2 -0
  205. package/src/api/v2/content.ts +3 -2
  206. package/src/api/v2/dashboards.test.ts +32 -0
  207. package/src/api/v2/dashboards.ts +18 -0
  208. package/src/api/v2/organization-roles.test.ts +2 -0
  209. package/src/api/v2/organization-roles.ts +2 -1
  210. package/src/api/v2/project-role-assignments.test.ts +2 -0
  211. package/src/api/v2/project-role-assignments.ts +2 -1
  212. package/src/api/v2/query.test.ts +2 -0
  213. package/src/api/v2/query.ts +2 -1
  214. package/src/api/v2/validation.ts +48 -0
  215. package/src/client.test.ts +1 -0
  216. package/src/client.ts +27 -17
  217. package/src/config.ts +38 -10
  218. package/src/errors.test.ts +1 -0
  219. package/src/http/axios-client.ts +8 -5
  220. package/src/http/http-client.ts +8 -6
  221. package/src/http/interceptors.ts +23 -10
  222. package/src/http/rate-limiter.test.ts +1 -0
  223. package/src/http/rate-limiter.ts +3 -1
  224. package/src/index.test.ts +16 -7
  225. package/src/index.ts +16 -1
  226. package/src/pagination/fetch-all-pages.test.ts +2 -0
  227. package/src/types/api.ts +1 -1
  228. package/src/utils/env.test.ts +16 -4
  229. package/src/utils/env.ts +61 -10
  230. package/src/utils/logger.ts +6 -4
  231. package/src/utils/retry.test.ts +1 -0
  232. package/src/utils/retry.ts +2 -1
  233. package/src/utils/secret-string.test.ts +27 -0
  234. package/tsconfig.json +4 -2
  235. package/dist/api/v1/ai-agents.d.ts +0 -63
  236. package/dist/api/v1/ai-agents.js +0 -173
  237. package/src/api/v1/ai-agents.ts +0 -252
package/src/config.ts CHANGED
@@ -55,15 +55,20 @@ export interface ObservabilityHooks {
55
55
  }): void;
56
56
  }
57
57
 
58
- /**
59
- * Full configuration for the Lightdash HTTP client.
60
- * All fields except rateLimit, timeout, retry, and logger can be provided via environment variables.
61
- */
62
- export interface LightdashClientConfig {
58
+ /** Upstream Lightdash authentication scheme. */
59
+ export type LightdashAuthConfig =
60
+ | {
61
+ type: 'api-key';
62
+ token: SecretString;
63
+ }
64
+ | {
65
+ type: 'bearer';
66
+ token: SecretString;
67
+ };
68
+
69
+ interface LightdashClientConfigBase {
63
70
  /** Lightdash server base URL (e.g. https://app.lightdash.cloud). */
64
71
  baseUrl: string;
65
- /** Personal access token for API authentication (without ldpat_ prefix). */
66
- personalAccessToken: SecretString;
67
72
  /** Optional proxy authorization header value for proxied access. */
68
73
  proxyAuthorization?: SecretString;
69
74
  /** Override default rate limits. */
@@ -78,15 +83,38 @@ export interface LightdashClientConfig {
78
83
  observability?: ObservabilityHooks;
79
84
  }
80
85
 
86
+ /**
87
+ * Resolved client configuration after mergeConfig().
88
+ * Runtime code paths always receive this shape with auth populated.
89
+ */
90
+ export type ResolvedLightdashClientConfig = LightdashClientConfigBase & {
91
+ auth: LightdashAuthConfig;
92
+ /**
93
+ * @deprecated Use `auth: { type: 'api-key', token }`. Retained when auth is api-key.
94
+ */
95
+ personalAccessToken?: SecretString;
96
+ };
97
+
98
+ /**
99
+ * Full configuration for the Lightdash HTTP client.
100
+ * Accepts explicit auth or deprecated personalAccessToken-only input.
101
+ */
102
+ export type LightdashClientConfig =
103
+ | ResolvedLightdashClientConfig
104
+ | (LightdashClientConfigBase & {
105
+ personalAccessToken: SecretString;
106
+ });
107
+
81
108
  /**
82
109
  * Partial config for merging with environment variables.
83
110
  * Used when constructing the client with optional explicit overrides.
84
111
  */
85
112
  export type PartialLightdashClientConfig = Partial<
86
- Omit<LightdashClientConfig, 'personalAccessToken' | 'proxyAuthorization'>
113
+ Omit<LightdashClientConfigBase, 'proxyAuthorization'>
87
114
  > & {
88
- personalAccessToken?: string | SecretString;
89
- proxyAuthorization?: string | SecretString;
115
+ auth?: LightdashAuthConfig | { type: 'api-key' | 'bearer'; token: SecretString | string };
116
+ personalAccessToken?: SecretString | string;
117
+ proxyAuthorization?: SecretString | string;
90
118
  };
91
119
 
92
120
  /** Default rate limit: 10 req/sec, 5 concurrent, token bucket. */
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
+
2
3
  import { LightdashApiError, RateLimitError, NetworkError } from './errors';
3
4
 
4
5
  describe('LightdashApiError', () => {
@@ -3,10 +3,13 @@
3
3
  */
4
4
 
5
5
  import axios, { type AxiosInstance } from 'axios';
6
- import type { LightdashClientConfig } from '../config';
6
+
7
7
  import { DEFAULT_TIMEOUT } from '../config';
8
+
8
9
  import { attachRequestInterceptors, attachResponseInterceptors } from './interceptors';
9
10
 
11
+ import type { ResolvedLightdashClientConfig } from '../config';
12
+
10
13
  /**
11
14
  * API version type.
12
15
  */
@@ -16,7 +19,7 @@ export type ApiVersion = 'v1' | 'v2';
16
19
  * Creates a configured Axios instance for the Lightdash API with a specific version.
17
20
  */
18
21
  function createAxiosInstanceForVersion(
19
- config: LightdashClientConfig,
22
+ config: ResolvedLightdashClientConfig,
20
23
  version: ApiVersion,
21
24
  ): AxiosInstance {
22
25
  const baseURL = config.baseUrl.replace(/\/$/, '') + `/api/${version}`;
@@ -39,14 +42,14 @@ function createAxiosInstanceForVersion(
39
42
  /**
40
43
  * Creates a configured Axios instance for the Lightdash API v1.
41
44
  */
42
- export function createAxiosInstanceV1(config: LightdashClientConfig): AxiosInstance {
45
+ export function createAxiosInstanceV1(config: ResolvedLightdashClientConfig): AxiosInstance {
43
46
  return createAxiosInstanceForVersion(config, 'v1');
44
47
  }
45
48
 
46
49
  /**
47
50
  * Creates a configured Axios instance for the Lightdash API v2.
48
51
  */
49
- export function createAxiosInstanceV2(config: LightdashClientConfig): AxiosInstance {
52
+ export function createAxiosInstanceV2(config: ResolvedLightdashClientConfig): AxiosInstance {
50
53
  return createAxiosInstanceForVersion(config, 'v2');
51
54
  }
52
55
 
@@ -54,6 +57,6 @@ export function createAxiosInstanceV2(config: LightdashClientConfig): AxiosInsta
54
57
  * Creates a configured Axios instance for the Lightdash API v1.
55
58
  * @deprecated Use `createAxiosInstanceV1` instead. This function is kept for backward compatibility.
56
59
  */
57
- export function createAxiosInstance(config: LightdashClientConfig): AxiosInstance {
60
+ export function createAxiosInstance(config: ResolvedLightdashClientConfig): AxiosInstance {
58
61
  return createAxiosInstanceV1(config);
59
62
  }
@@ -3,12 +3,14 @@
3
3
  * All requests go through the rate limiter; retry applies to 5xx and network errors.
4
4
  */
5
5
 
6
- import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, Method } from 'axios';
7
- import type { LightdashClientConfig } from '../config';
8
6
  import { type ApiErrorPayload, LightdashApiError } from '../errors';
9
- import { type RateLimiter } from './rate-limiter';
10
7
  import { withRetry } from '../utils/retry';
11
8
 
9
+ import { type RateLimiter } from './rate-limiter';
10
+
11
+ import type { ResolvedLightdashClientConfig } from '../config';
12
+ import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, Method } from 'axios';
13
+
12
14
  /** Lightdash API success response shape. */
13
15
  export interface ApiSuccessBody<T = unknown> {
14
16
  status: 'ok';
@@ -21,7 +23,7 @@ export interface ApiErrorBody {
21
23
  error: ApiErrorPayload['error'];
22
24
  }
23
25
 
24
- function assertSuccess<T>(data: ApiSuccessBody<T> | ApiErrorBody): data is ApiSuccessBody<T> {
26
+ function assertSuccess<T>(data: ApiErrorBody | ApiSuccessBody<T>): data is ApiSuccessBody<T> {
25
27
  return data.status === 'ok';
26
28
  }
27
29
 
@@ -32,12 +34,12 @@ export class HttpClient {
32
34
  constructor(
33
35
  private readonly axiosInstance: AxiosInstance,
34
36
  private readonly rateLimiter: RateLimiter,
35
- private readonly config: LightdashClientConfig,
37
+ private readonly config: ResolvedLightdashClientConfig,
36
38
  ) {}
37
39
 
38
40
  private async request<T>(method: Method, url: string, config?: AxiosRequestConfig): Promise<T> {
39
41
  const doRequest = () =>
40
- this.axiosInstance.request<ApiSuccessBody<T> | ApiErrorBody>({
42
+ this.axiosInstance.request<ApiErrorBody | ApiSuccessBody<T>>({
41
43
  ...config,
42
44
  method,
43
45
  url,
@@ -2,11 +2,12 @@
2
2
  * Request and response interceptors for auth, proxy, and error handling.
3
3
  */
4
4
 
5
- import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
6
- import type { LightdashClientConfig } from '../config';
7
- import type { ApiErrorPayload } from '../errors';
8
5
  import { LightdashApiError, RateLimitError, NetworkError } from '../errors';
9
6
 
7
+ import type { LightdashAuthConfig, ResolvedLightdashClientConfig } from '../config';
8
+ import type { ApiErrorPayload } from '../errors';
9
+ import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
10
+
10
11
  function getRetryAfterSeconds(value: string | undefined): number | undefined {
11
12
  if (value === undefined) return undefined;
12
13
  const n = parseInt(value, 10);
@@ -18,13 +19,26 @@ function getRetryAfterSeconds(value: string | undefined): number | undefined {
18
19
  */
19
20
  export function attachRequestInterceptors(
20
21
  axiosInstance: AxiosInstance,
21
- config: LightdashClientConfig,
22
+ config: ResolvedLightdashClientConfig,
22
23
  ): void {
23
24
  axiosInstance.interceptors.request.use(
24
25
  (req: InternalAxiosRequestConfig) => {
25
- const rawToken = config.personalAccessToken.expose();
26
- const token = rawToken.startsWith('ldpat_') ? rawToken : `ldpat_${rawToken}`;
27
- req.headers.Authorization = `ApiKey ${token}`;
26
+ switch (config.auth.type) {
27
+ case 'api-key': {
28
+ const rawToken = config.auth.token.expose();
29
+ const token = rawToken.startsWith('ldpat_') ? rawToken : `ldpat_${rawToken}`;
30
+ req.headers.Authorization = `ApiKey ${token}`;
31
+ break;
32
+ }
33
+ case 'bearer': {
34
+ req.headers.Authorization = `Bearer ${config.auth.token.expose()}`;
35
+ break;
36
+ }
37
+ default: {
38
+ const _exhaustive: never = config.auth;
39
+ throw new Error(`Unsupported auth type: ${(_exhaustive as LightdashAuthConfig).type}`);
40
+ }
41
+ }
28
42
 
29
43
  if (config.proxyAuthorization) {
30
44
  req.headers['Proxy-Authorization'] = config.proxyAuthorization.expose();
@@ -48,7 +62,7 @@ export function attachRequestInterceptors(
48
62
  */
49
63
  export function attachResponseInterceptors(
50
64
  axiosInstance: AxiosInstance,
51
- config?: Pick<LightdashClientConfig, 'logger' | 'observability'>,
65
+ config?: Pick<ResolvedLightdashClientConfig, 'logger' | 'observability'>,
52
66
  ): void {
53
67
  const logger = config?.logger;
54
68
  axiosInstance.interceptors.response.use(
@@ -72,8 +86,7 @@ export function attachResponseInterceptors(
72
86
  },
73
87
  (error) => {
74
88
  const reqConfig = error.config as
75
- | (InternalAxiosRequestConfig & { _lightdashStartTime?: number })
76
- | undefined;
89
+ (InternalAxiosRequestConfig & { _lightdashStartTime?: number }) | undefined;
77
90
  const start = reqConfig?._lightdashStartTime;
78
91
  config?.observability?.onRequestComplete?.({
79
92
  method: reqConfig?.method ?? 'GET',
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
+
2
3
  import { RateLimiter } from './rate-limiter';
3
4
 
4
5
  describe('RateLimiter', () => {
@@ -4,9 +4,11 @@
4
4
  */
5
5
 
6
6
  import Bottleneck from 'bottleneck';
7
- import type { RateLimitConfig } from '../config';
7
+
8
8
  import { DEFAULT_RATE_LIMIT } from '../config';
9
9
 
10
+ import type { RateLimitConfig } from '../config';
11
+
10
12
  export type RateLimiterOptions = RateLimitConfig;
11
13
 
12
14
  /**
package/src/index.test.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect, beforeEach, vi } from 'vitest';
2
+
2
3
  import {
3
4
  LightdashClient,
4
5
  mergeConfig,
@@ -12,6 +13,8 @@ import {
12
13
  SecretString,
13
14
  } from './index';
14
15
 
16
+ import type { LightdashClientConfig } from './index';
17
+
15
18
  describe('LightdashClient', () => {
16
19
  beforeEach(() => {
17
20
  vi.stubEnv(ENV_LIGHTDASH_API_KEY, '');
@@ -31,10 +34,16 @@ describe('LightdashClient', () => {
31
34
  expect(client.getHttpClientV1()).toBeDefined();
32
35
  });
33
36
 
34
- it('should throw when baseUrl and personalAccessToken are missing and not in env', () => {
35
- expect(() => new LightdashClient({})).toThrow(
36
- /baseUrl and personalAccessToken|LIGHTDASH_URL and LIGHTDASH_API_KEY/,
37
- );
37
+ it('allows deprecated personalAccessToken-only exported config shape', () => {
38
+ const cfg: LightdashClientConfig = {
39
+ baseUrl: 'https://app.lightdash.cloud',
40
+ personalAccessToken: new SecretString('pat'),
41
+ };
42
+ expect(cfg.personalAccessToken?.expose()).toBe('pat');
43
+ });
44
+
45
+ it('should throw when baseUrl and auth credentials are missing and not in env', () => {
46
+ expect(() => new LightdashClient({})).toThrow(/baseUrl|auth credentials|LIGHTDASH_URL/);
38
47
  });
39
48
 
40
49
  it('should merge explicit config with env (explicit wins)', () => {
@@ -47,8 +56,8 @@ describe('LightdashClient', () => {
47
56
  };
48
57
  const merged = mergeConfig(explicit);
49
58
  expect(merged.baseUrl).toBe('https://custom.example.com');
50
- expect(merged.personalAccessToken).toBeInstanceOf(SecretString);
51
- expect(merged.personalAccessToken.expose()).toBe('explicit-token');
59
+ expect(merged.auth.token).toBeInstanceOf(SecretString);
60
+ expect(merged.auth.token.expose()).toBe('explicit-token');
52
61
  });
53
62
  });
54
63
 
@@ -59,7 +68,7 @@ describe('mergeConfig', () => {
59
68
  vi.stubEnv(ENV_LIGHTDASH_PROXY_AUTHORIZATION, '');
60
69
  });
61
70
 
62
- it('should require baseUrl and personalAccessToken', () => {
71
+ it('should require baseUrl and auth credentials', () => {
63
72
  expect(() => mergeConfig({})).toThrow();
64
73
  expect(() => mergeConfig({ baseUrl: 'https://x.com' })).toThrow();
65
74
  expect(() => mergeConfig({ personalAccessToken: 't' })).toThrow();
package/src/index.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  export { LightdashClient, V1ApiClients, V2ApiClients } from './client';
2
+ export {
3
+ fetchBundleCurrentState,
4
+ applyBundleDiff,
5
+ resolveEvaluationRun,
6
+ type ApplyBundleDiffFailure,
7
+ type ApplyBundleDiffResult,
8
+ type ResolveEvaluationRunOptions,
9
+ type ResolveEvaluationRunResult,
10
+ } from './agentops';
2
11
  export { UsersClient } from './api/v1/users';
3
12
  export { GroupsClient } from './api/v1/groups';
4
13
  export { OrganizationRolesClient } from './api/v2/organization-roles';
@@ -6,12 +15,16 @@ export { ProjectRoleAssignmentsClient } from './api/v2/project-role-assignments'
6
15
  export { AiAgentsClient } from './api/v1/ai-agents';
7
16
  export { ProjectAccessClient } from './api/v1/project-access';
8
17
  export { ValidationClient } from './api/v1/validation';
18
+ export { ValidationClientV2 } from './api/v2/validation';
9
19
  export { MetricsClient } from './api/v1/metrics';
10
20
  export { SchedulersClient } from './api/v1/schedulers';
11
21
  export { TagsClient } from './api/v1/tags';
22
+ export { AnalyticsClient } from './api/v1/analytics';
12
23
  export { ContentClient } from './api/v2/content';
24
+ export { DashboardsClientV2 } from './api/v2/dashboards';
13
25
  export type { ListMembersParams, ListAllMembersParams } from './api/v1/users';
14
26
  export type { GetValidationResultsParams, ValidateProjectBody } from './api/v1/validation';
27
+ export type { ListValidationResultsParams } from './api/v2/validation';
15
28
  export type { ListMetricsParams } from './api/v1/metrics';
16
29
  export type { ListSchedulersParams } from './api/v1/schedulers';
17
30
  export type { SearchContentParams } from './api/v2/content';
@@ -29,6 +42,8 @@ export type {
29
42
  export type { PartialLightdashClientConfig } from './config';
30
43
  export type {
31
44
  LightdashClientConfig,
45
+ LightdashAuthConfig,
46
+ ResolvedLightdashClientConfig,
32
47
  RateLimitConfig,
33
48
  RetryConfig,
34
49
  Logger,
@@ -38,7 +53,7 @@ export { noopLogger, consoleLogger } from './utils/logger';
38
53
  export { DEFAULT_RATE_LIMIT, DEFAULT_TIMEOUT, DEFAULT_RETRY } from './config';
39
54
  export { LightdashApiError, RateLimitError, NetworkError } from './errors';
40
55
  export type { ApiErrorPayload } from './errors';
41
- export { loadConfigFromEnv, mergeConfig } from './utils/env';
56
+ export { loadConfigFromEnv, mergeConfig, createBearerConfig } from './utils/env';
42
57
  export {
43
58
  ENV_LIGHTDASH_API_KEY,
44
59
  ENV_LIGHTDASH_URL,
@@ -1,5 +1,7 @@
1
1
  import { describe, it, expect, vi } from 'vitest';
2
+
2
3
  import { fetchAllPages } from './fetch-all-pages';
4
+
3
5
  import type { KnexPage } from './types';
4
6
 
5
7
  describe('fetchAllPages', () => {
package/src/types/api.ts CHANGED
@@ -25,4 +25,4 @@ export interface ApiResponseError {
25
25
  }
26
26
 
27
27
  /** Discriminated union for API response body. */
28
- export type ApiResponseBody<T> = ApiResponseOk<T> | ApiResponseError;
28
+ export type ApiResponseBody<T> = ApiResponseError | ApiResponseOk<T>;
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
+
2
3
  import {
3
4
  loadConfigFromEnv,
4
5
  mergeConfig,
@@ -60,7 +61,8 @@ describe('mergeConfig', () => {
60
61
  personalAccessToken: 'explicit-token',
61
62
  });
62
63
  expect(merged.baseUrl).toBe('https://explicit.example.com');
63
- expect(merged.personalAccessToken.expose()).toBe('explicit-token');
64
+ expect(merged.auth.type).toBe('api-key');
65
+ expect(merged.auth.token.expose()).toBe('explicit-token');
64
66
  });
65
67
 
66
68
  it('should use env when explicit config omits a field', () => {
@@ -68,11 +70,11 @@ describe('mergeConfig', () => {
68
70
  process.env[ENV_LIGHTDASH_API_KEY] = 'env-token';
69
71
  const merged = mergeConfig({ baseUrl: 'https://explicit.example.com' });
70
72
  expect(merged.baseUrl).toBe('https://explicit.example.com');
71
- expect(merged.personalAccessToken.expose()).toBe('env-token');
73
+ expect(merged.auth.token.expose()).toBe('env-token');
72
74
  });
73
75
 
74
- it('should throw when baseUrl and personalAccessToken cannot be resolved', () => {
75
- expect(() => mergeConfig(undefined)).toThrow(/baseUrl and personalAccessToken/);
76
+ it('should throw when baseUrl and auth credentials cannot be resolved', () => {
77
+ expect(() => mergeConfig(undefined)).toThrow(/baseUrl|auth credentials/);
76
78
  expect(() => mergeConfig({})).toThrow();
77
79
  });
78
80
 
@@ -84,5 +86,15 @@ describe('mergeConfig', () => {
84
86
  observability: { onRequestComplete },
85
87
  });
86
88
  expect(merged.observability?.onRequestComplete).toBe(onRequestComplete);
89
+ expect(merged.auth).toEqual({ type: 'api-key', token: expect.objectContaining({}) });
90
+ });
91
+
92
+ it('should normalize bearer auth config', () => {
93
+ const merged = mergeConfig({
94
+ baseUrl: 'https://app.lightdash.cloud',
95
+ auth: { type: 'bearer', token: 'oauth-token' },
96
+ });
97
+ expect(merged.auth.type).toBe('bearer');
98
+ expect(merged.auth.token.expose()).toBe('oauth-token');
87
99
  });
88
100
  });
package/src/utils/env.ts CHANGED
@@ -4,14 +4,49 @@
4
4
  * Priority: explicit config > environment variables > no default for required fields.
5
5
  */
6
6
 
7
- import type { LightdashClientConfig, PartialLightdashClientConfig } from '../config';
8
7
  import { SecretString } from './secret-string';
9
8
 
9
+ import type {
10
+ LightdashAuthConfig,
11
+ PartialLightdashClientConfig,
12
+ ResolvedLightdashClientConfig,
13
+ } from '../config';
14
+
10
15
  /** Environment variable names (aligned with Lightdash CLI). */
11
16
  export const ENV_LIGHTDASH_API_KEY = 'LIGHTDASH_API_KEY';
12
17
  export const ENV_LIGHTDASH_URL = 'LIGHTDASH_URL';
13
18
  export const ENV_LIGHTDASH_PROXY_AUTHORIZATION = 'LIGHTDASH_PROXY_AUTHORIZATION';
14
19
 
20
+ function toSecretString(value: SecretString | string): SecretString {
21
+ return typeof value === 'string' ? new SecretString(value) : value;
22
+ }
23
+
24
+ function normalizeAuthConfig(
25
+ auth: NonNullable<PartialLightdashClientConfig['auth']>,
26
+ ): LightdashAuthConfig {
27
+ const token = toSecretString(auth.token);
28
+ if (auth.type === 'bearer') {
29
+ return { type: 'bearer', token };
30
+ }
31
+ return { type: 'api-key', token };
32
+ }
33
+
34
+ function resolveAuthConfig(
35
+ explicit: PartialLightdashClientConfig | undefined,
36
+ fromEnv: PartialLightdashClientConfig,
37
+ ): LightdashAuthConfig {
38
+ if (explicit?.auth) {
39
+ return normalizeAuthConfig(explicit.auth);
40
+ }
41
+ const pat = explicit?.personalAccessToken ?? fromEnv.personalAccessToken;
42
+ if (pat) {
43
+ return { type: 'api-key', token: toSecretString(pat) };
44
+ }
45
+ throw new Error(
46
+ 'Lightdash client requires auth credentials. Set auth, personalAccessToken, or LIGHTDASH_API_KEY.',
47
+ );
48
+ }
49
+
15
50
  /**
16
51
  * Reads configuration from environment variables.
17
52
  * Returns only the fields that are set in the environment.
@@ -38,30 +73,46 @@ export function loadConfigFromEnv(): PartialLightdashClientConfig {
38
73
  return out;
39
74
  }
40
75
 
76
+ /**
77
+ * Builds a bearer-token client config for OAuth access tokens.
78
+ */
79
+ export function createBearerConfig(options: {
80
+ baseUrl: string;
81
+ accessToken: SecretString | string;
82
+ proxyAuthorization?: SecretString | string;
83
+ }): PartialLightdashClientConfig {
84
+ return {
85
+ baseUrl: options.baseUrl.replace(/\/$/, ''),
86
+ auth: { type: 'bearer', token: options.accessToken },
87
+ proxyAuthorization: options.proxyAuthorization,
88
+ };
89
+ }
90
+
41
91
  /**
42
92
  * Merges explicit config with environment-derived config.
43
93
  * Explicit config takes priority over environment variables.
44
94
  */
45
95
  export function mergeConfig(
46
96
  explicit: PartialLightdashClientConfig | undefined,
47
- ): LightdashClientConfig {
97
+ ): ResolvedLightdashClientConfig {
48
98
  const fromEnv = loadConfigFromEnv();
49
99
 
50
100
  const baseUrl = explicit?.baseUrl ?? fromEnv.baseUrl ?? '';
51
- const pat = explicit?.personalAccessToken ?? fromEnv.personalAccessToken ?? '';
52
101
  const proxyAuth = explicit?.proxyAuthorization ?? fromEnv.proxyAuthorization;
53
102
 
54
- if (!baseUrl || !pat) {
55
- throw new Error(
56
- 'Lightdash client requires baseUrl and personalAccessToken. ' +
57
- 'Set them in config or via LIGHTDASH_URL and LIGHTDASH_API_KEY.',
58
- );
103
+ if (!baseUrl) {
104
+ throw new Error('Lightdash client requires baseUrl. Set it in config or via LIGHTDASH_URL.');
59
105
  }
60
106
 
107
+ const auth = resolveAuthConfig(explicit, fromEnv);
108
+ const personalAccessToken = auth.type === 'api-key' ? auth.token : explicit?.personalAccessToken;
109
+
61
110
  return {
62
111
  baseUrl,
63
- personalAccessToken: typeof pat === 'string' ? new SecretString(pat) : pat,
64
- proxyAuthorization: typeof proxyAuth === 'string' ? new SecretString(proxyAuth) : proxyAuth,
112
+ auth,
113
+ personalAccessToken:
114
+ personalAccessToken !== undefined ? toSecretString(personalAccessToken) : undefined,
115
+ proxyAuthorization: proxyAuth !== undefined ? toSecretString(proxyAuth) : undefined,
65
116
  rateLimit: explicit?.rateLimit,
66
117
  timeout: explicit?.timeout,
67
118
  retry: explicit?.retry,
@@ -4,6 +4,8 @@
4
4
 
5
5
  import type { Logger } from '../config';
6
6
 
7
+ const LOG_PREFIX = '[Lightdash]';
8
+
7
9
  /** No-op logger for when logging is disabled. */
8
10
  export const noopLogger: Logger = {
9
11
  debug: () => {},
@@ -14,8 +16,8 @@ export const noopLogger: Logger = {
14
16
 
15
17
  /** Console-based logger. */
16
18
  export const consoleLogger: Logger = {
17
- debug: (...args) => console.debug('[Lightdash]', ...args),
18
- info: (...args) => console.info('[Lightdash]', ...args),
19
- warn: (...args) => console.warn('[Lightdash]', ...args),
20
- error: (...args) => console.error('[Lightdash]', ...args),
19
+ debug: (...args) => console.debug(LOG_PREFIX, ...args),
20
+ info: (...args) => console.info(LOG_PREFIX, ...args),
21
+ warn: (...args) => console.warn(LOG_PREFIX, ...args),
22
+ error: (...args) => console.error(LOG_PREFIX, ...args),
21
23
  };
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect, vi } from 'vitest';
2
+
2
3
  import { withRetry, isRetryableError } from './retry';
3
4
 
4
5
  describe('isRetryableError', () => {
@@ -3,9 +3,10 @@
3
3
  * Retries on 5xx and network errors; no retry for 4xx (except 429 handled by rate limiter).
4
4
  */
5
5
 
6
- import type { RetryConfig } from '../config';
7
6
  import { DEFAULT_RETRY } from '../config';
8
7
 
8
+ import type { RetryConfig } from '../config';
9
+
9
10
  export type RetryOptions = RetryConfig;
10
11
 
11
12
  function delay(ms: number): Promise<void> {
@@ -0,0 +1,27 @@
1
+ import util from 'node:util';
2
+
3
+ import { describe, it, expect } from 'vitest';
4
+
5
+ import { SecretString } from './secret-string';
6
+
7
+ describe('SecretString', () => {
8
+ const secret = new SecretString('my-secret-token');
9
+
10
+ it('toString() masks the value', () => {
11
+ expect(secret.toString()).toBe('**********');
12
+ expect(String(secret)).toBe('**********');
13
+ });
14
+
15
+ it('toJSON() masks the value', () => {
16
+ expect(secret.toJSON()).toBe('**********');
17
+ expect(JSON.stringify({ token: secret })).toBe('{"token":"**********"}');
18
+ });
19
+
20
+ it('custom inspect masks the value', () => {
21
+ expect(util.inspect(secret)).toBe('SecretString(**********)');
22
+ });
23
+
24
+ it('expose() returns the underlying value', () => {
25
+ expect(secret.expose()).toBe('my-secret-token');
26
+ });
27
+ });
package/tsconfig.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2016",
3
+ "target": "es2017",
4
4
  "module": "commonjs",
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
+ "rootDir": "./src",
7
8
  "strict": true,
8
9
  "esModuleInterop": true,
9
10
  "skipLibCheck": true,
10
- "forceConsistentCasingInFileNames": true
11
+ "forceConsistentCasingInFileNames": true,
12
+ "types": ["node"]
11
13
  },
12
14
  "include": ["src"]
13
15
  }