@lightdash-tools/client 0.6.0 → 0.8.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 (256) 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 +13 -2
  62. package/dist/api/v1/charts.js +26 -30
  63. package/dist/api/v1/charts.test.js +35 -28
  64. package/dist/api/v1/dashboards.d.ts +42 -2
  65. package/dist/api/v1/dashboards.js +39 -14
  66. package/dist/api/v1/dashboards.test.js +126 -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 +9 -2
  101. package/dist/api/v1/validation.js +16 -18
  102. package/dist/api/v1/validation.test.js +22 -15
  103. package/dist/api/v2/charts.d.ts +12 -0
  104. package/dist/api/v2/charts.js +18 -0
  105. package/dist/api/v2/charts.test.d.ts +4 -0
  106. package/dist/api/v2/charts.test.js +45 -0
  107. package/dist/api/v2/content.d.ts +20 -3
  108. package/dist/api/v2/content.js +18 -14
  109. package/dist/api/v2/content.test.js +44 -12
  110. package/dist/api/v2/dashboards.d.ts +16 -0
  111. package/dist/api/v2/dashboards.js +22 -0
  112. package/dist/api/v2/dashboards.test.d.ts +4 -0
  113. package/dist/api/v2/dashboards.test.js +61 -0
  114. package/dist/api/v2/organization-roles.d.ts +1 -1
  115. package/dist/api/v2/organization-roles.js +21 -50
  116. package/dist/api/v2/organization-roles.test.js +33 -42
  117. package/dist/api/v2/parameters.d.ts +20 -0
  118. package/dist/api/v2/parameters.js +22 -0
  119. package/dist/api/v2/parameters.test.d.ts +4 -0
  120. package/dist/api/v2/parameters.test.js +39 -0
  121. package/dist/api/v2/project-role-assignments.d.ts +1 -1
  122. package/dist/api/v2/project-role-assignments.js +12 -33
  123. package/dist/api/v2/project-role-assignments.test.js +18 -27
  124. package/dist/api/v2/query.d.ts +16 -1
  125. package/dist/api/v2/query.js +26 -29
  126. package/dist/api/v2/query.test.js +31 -24
  127. package/dist/api/v2/validation.d.ts +28 -0
  128. package/dist/api/v2/validation.js +23 -0
  129. package/dist/client.d.ts +24 -14
  130. package/dist/client.js +26 -16
  131. package/dist/client.test.js +2 -0
  132. package/dist/config.d.ts +35 -10
  133. package/dist/http/axios-client.d.ts +4 -4
  134. package/dist/http/http-client.d.ts +3 -14
  135. package/dist/http/http-client.js +20 -43
  136. package/dist/http/interceptors.d.ts +3 -3
  137. package/dist/http/interceptors.js +16 -3
  138. package/dist/http/rate-limiter.js +2 -13
  139. package/dist/http/rate-limiter.test.js +15 -24
  140. package/dist/http/unwrap-api-success.d.ts +26 -0
  141. package/dist/http/unwrap-api-success.js +25 -0
  142. package/dist/http/unwrap-api-success.test.d.ts +1 -0
  143. package/dist/http/unwrap-api-success.test.js +19 -0
  144. package/dist/index.d.ts +13 -3
  145. package/dist/index.js +17 -1
  146. package/dist/index.test.js +13 -5
  147. package/dist/pagination/fetch-all-pages.js +18 -29
  148. package/dist/pagination/fetch-all-pages.test.js +15 -24
  149. package/dist/types/api.d.ts +1 -1
  150. package/dist/utils/env.d.ts +11 -2
  151. package/dist/utils/env.js +41 -8
  152. package/dist/utils/env.test.js +14 -4
  153. package/dist/utils/logger.js +5 -4
  154. package/dist/utils/retry.js +17 -28
  155. package/dist/utils/retry.test.js +9 -18
  156. package/dist/utils/secret-string.test.d.ts +1 -0
  157. package/dist/utils/secret-string.test.js +25 -0
  158. package/package.json +12 -4
  159. package/src/agentops/apply-agent-change.ts +82 -0
  160. package/src/agentops/apply-bundle-diff.test.ts +453 -0
  161. package/src/agentops/apply-bundle-diff.ts +63 -0
  162. package/src/agentops/apply-context.ts +23 -0
  163. package/src/agentops/apply-evaluation-change.ts +125 -0
  164. package/src/agentops/fetch-bundle-state.test.ts +51 -0
  165. package/src/agentops/fetch-bundle-state.ts +33 -0
  166. package/src/agentops/index.ts +11 -0
  167. package/src/agentops/resolve-evaluation-run.ts +94 -0
  168. package/src/api/v1/ai-agents/admin.ts +41 -0
  169. package/src/api/v1/ai-agents/agents.ts +40 -0
  170. package/src/api/v1/ai-agents/artifacts.test.ts +84 -0
  171. package/src/api/v1/ai-agents/artifacts.ts +92 -0
  172. package/src/api/v1/ai-agents/discovery.test.ts +102 -0
  173. package/src/api/v1/ai-agents/discovery.ts +79 -0
  174. package/src/api/v1/ai-agents/evaluations.ts +152 -0
  175. package/src/api/v1/ai-agents/facade.ts +489 -0
  176. package/src/api/v1/ai-agents/feedback.test.ts +30 -0
  177. package/src/api/v1/ai-agents/feedback.ts +24 -0
  178. package/src/api/v1/ai-agents/index.ts +1 -0
  179. package/src/api/v1/ai-agents/mcp-servers.test.ts +81 -0
  180. package/src/api/v1/ai-agents/mcp-servers.ts +67 -0
  181. package/src/api/v1/ai-agents/request-options.ts +32 -0
  182. package/src/api/v1/ai-agents/sql-approval.test.ts +32 -0
  183. package/src/api/v1/ai-agents/sql-approval.ts +24 -0
  184. package/src/api/v1/ai-agents/threads.test.ts +55 -0
  185. package/src/api/v1/ai-agents/threads.ts +216 -0
  186. package/src/api/v1/ai-agents.test.ts +189 -13
  187. package/src/api/v1/analytics.test.ts +32 -0
  188. package/src/api/v1/analytics.ts +16 -0
  189. package/src/api/v1/charts.test.ts +26 -6
  190. package/src/api/v1/charts.ts +25 -4
  191. package/src/api/v1/dashboards.test.ts +142 -0
  192. package/src/api/v1/dashboards.ts +127 -2
  193. package/src/api/v1/explores.test.ts +22 -0
  194. package/src/api/v1/explores.ts +13 -2
  195. package/src/api/v1/groups.test.ts +2 -0
  196. package/src/api/v1/groups.ts +9 -7
  197. package/src/api/v1/metrics.test.ts +2 -0
  198. package/src/api/v1/metrics.ts +3 -2
  199. package/src/api/v1/organizations.test.ts +2 -0
  200. package/src/api/v1/organizations.ts +2 -1
  201. package/src/api/v1/project-access.test.ts +3 -1
  202. package/src/api/v1/project-access.ts +2 -1
  203. package/src/api/v1/projects.test.ts +2 -0
  204. package/src/api/v1/projects.ts +2 -1
  205. package/src/api/v1/query.test.ts +2 -0
  206. package/src/api/v1/query.ts +6 -2
  207. package/src/api/v1/schedulers.test.ts +2 -0
  208. package/src/api/v1/schedulers.ts +3 -2
  209. package/src/api/v1/spaces.test.ts +2 -0
  210. package/src/api/v1/spaces.ts +2 -1
  211. package/src/api/v1/tags.test.ts +2 -0
  212. package/src/api/v1/tags.ts +2 -1
  213. package/src/api/v1/users.test.ts +26 -0
  214. package/src/api/v1/users.ts +8 -2
  215. package/src/api/v1/validation.test.ts +20 -0
  216. package/src/api/v1/validation.ts +26 -2
  217. package/src/api/v2/charts.test.ts +53 -0
  218. package/src/api/v2/charts.ts +23 -0
  219. package/src/api/v2/content.test.ts +47 -0
  220. package/src/api/v2/content.ts +37 -3
  221. package/src/api/v2/dashboards.test.ts +71 -0
  222. package/src/api/v2/dashboards.ts +40 -0
  223. package/src/api/v2/organization-roles.test.ts +2 -0
  224. package/src/api/v2/organization-roles.ts +2 -1
  225. package/src/api/v2/parameters.test.ts +45 -0
  226. package/src/api/v2/parameters.ts +38 -0
  227. package/src/api/v2/project-role-assignments.test.ts +2 -0
  228. package/src/api/v2/project-role-assignments.ts +2 -1
  229. package/src/api/v2/query.test.ts +20 -0
  230. package/src/api/v2/query.ts +55 -1
  231. package/src/api/v2/validation.ts +48 -0
  232. package/src/client.test.ts +3 -0
  233. package/src/client.ts +33 -17
  234. package/src/config.ts +38 -10
  235. package/src/errors.test.ts +1 -0
  236. package/src/http/axios-client.ts +8 -5
  237. package/src/http/http-client.ts +8 -21
  238. package/src/http/interceptors.ts +23 -10
  239. package/src/http/rate-limiter.test.ts +1 -0
  240. package/src/http/rate-limiter.ts +3 -1
  241. package/src/http/unwrap-api-success.test.ts +23 -0
  242. package/src/http/unwrap-api-success.ts +40 -0
  243. package/src/index.test.ts +16 -7
  244. package/src/index.ts +37 -2
  245. package/src/pagination/fetch-all-pages.test.ts +2 -0
  246. package/src/types/api.ts +1 -1
  247. package/src/utils/env.test.ts +16 -4
  248. package/src/utils/env.ts +61 -10
  249. package/src/utils/logger.ts +6 -4
  250. package/src/utils/retry.test.ts +1 -0
  251. package/src/utils/retry.ts +2 -1
  252. package/src/utils/secret-string.test.ts +27 -0
  253. package/tsconfig.json +4 -2
  254. package/dist/api/v1/ai-agents.d.ts +0 -63
  255. package/dist/api/v1/ai-agents.js +0 -173
  256. package/src/api/v1/ai-agents.ts +0 -252
package/src/client.ts CHANGED
@@ -2,30 +2,36 @@
2
2
  * Main Lightdash API client. Configuration can be explicit or loaded from environment variables.
3
3
  */
4
4
 
5
- import type { PartialLightdashClientConfig } from './config';
6
- import { mergeConfig } from './utils/env';
7
- import { createAxiosInstanceV1, createAxiosInstanceV2 } from './http/axios-client';
8
- import { RateLimiter } from './http/rate-limiter';
9
- import { HttpClient } from './http/http-client';
10
- import { ProjectsClient } from './api/v1/projects';
11
- import { OrganizationsClient } from './api/v1/organizations';
5
+ import { AiAgentsClient } from './api/v1/ai-agents';
6
+ import { AnalyticsClient } from './api/v1/analytics';
12
7
  import { ChartsClient } from './api/v1/charts';
13
8
  import { DashboardsClient } from './api/v1/dashboards';
14
- import { SpacesClient } from './api/v1/spaces';
15
- import { QueryClient } from './api/v1/query';
16
- import { QueryClientV2 } from './api/v2/query';
17
- import { OrganizationRolesClient } from './api/v2/organization-roles';
18
- import { ProjectRoleAssignmentsClient } from './api/v2/project-role-assignments';
19
- import { UsersClient } from './api/v1/users';
20
- import { GroupsClient } from './api/v1/groups';
21
- import { AiAgentsClient } from './api/v1/ai-agents';
22
- import { ProjectAccessClient } from './api/v1/project-access';
23
9
  import { ExploresClient } from './api/v1/explores';
24
- import { ValidationClient } from './api/v1/validation';
10
+ import { GroupsClient } from './api/v1/groups';
25
11
  import { MetricsClient } from './api/v1/metrics';
12
+ import { OrganizationsClient } from './api/v1/organizations';
13
+ import { ProjectAccessClient } from './api/v1/project-access';
14
+ import { ProjectsClient } from './api/v1/projects';
15
+ import { QueryClient } from './api/v1/query';
26
16
  import { SchedulersClient } from './api/v1/schedulers';
17
+ import { SpacesClient } from './api/v1/spaces';
27
18
  import { TagsClient } from './api/v1/tags';
19
+ import { UsersClient } from './api/v1/users';
20
+ import { ValidationClient } from './api/v1/validation';
21
+ import { ChartsClientV2 } from './api/v2/charts';
28
22
  import { ContentClient } from './api/v2/content';
23
+ import { DashboardsClientV2 } from './api/v2/dashboards';
24
+ import { OrganizationRolesClient } from './api/v2/organization-roles';
25
+ import { ParametersClientV2 } from './api/v2/parameters';
26
+ import { ProjectRoleAssignmentsClient } from './api/v2/project-role-assignments';
27
+ import { QueryClientV2 } from './api/v2/query';
28
+ import { ValidationClientV2 } from './api/v2/validation';
29
+ import { createAxiosInstanceV1, createAxiosInstanceV2 } from './http/axios-client';
30
+ import { HttpClient } from './http/http-client';
31
+ import { RateLimiter } from './http/rate-limiter';
32
+ import { mergeConfig } from './utils/env';
33
+
34
+ import type { PartialLightdashClientConfig } from './config';
29
35
 
30
36
  /**
31
37
  * V1 API clients namespace. Contains all v1 API clients.
@@ -46,6 +52,7 @@ export class V1ApiClients {
46
52
  readonly metrics: MetricsClient;
47
53
  readonly schedulers: SchedulersClient;
48
54
  readonly tags: TagsClient;
55
+ readonly analytics: AnalyticsClient;
49
56
 
50
57
  constructor(http: HttpClient) {
51
58
  this.projects = new ProjectsClient(http);
@@ -63,6 +70,7 @@ export class V1ApiClients {
63
70
  this.metrics = new MetricsClient(http);
64
71
  this.schedulers = new SchedulersClient(http);
65
72
  this.tags = new TagsClient(http);
73
+ this.analytics = new AnalyticsClient(http);
66
74
  }
67
75
  }
68
76
 
@@ -74,12 +82,20 @@ export class V2ApiClients {
74
82
  readonly organizationRoles: OrganizationRolesClient;
75
83
  readonly projectRoleAssignments: ProjectRoleAssignmentsClient;
76
84
  readonly content: ContentClient;
85
+ readonly validation: ValidationClientV2;
86
+ readonly dashboards: DashboardsClientV2;
87
+ readonly charts: ChartsClientV2;
88
+ readonly parameters: ParametersClientV2;
77
89
 
78
90
  constructor(http: HttpClient) {
79
91
  this.query = new QueryClientV2(http);
80
92
  this.organizationRoles = new OrganizationRolesClient(http);
81
93
  this.projectRoleAssignments = new ProjectRoleAssignmentsClient(http);
82
94
  this.content = new ContentClient(http);
95
+ this.validation = new ValidationClientV2(http);
96
+ this.dashboards = new DashboardsClientV2(http);
97
+ this.charts = new ChartsClientV2(http);
98
+ this.parameters = new ParametersClientV2(http);
83
99
  }
84
100
  }
85
101
 
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,27 +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
 
12
- /** Lightdash API success response shape. */
13
- export interface ApiSuccessBody<T = unknown> {
14
- status: 'ok';
15
- results: T;
16
- }
17
-
18
- /** Lightdash API error response shape (body). */
19
- export interface ApiErrorBody {
20
- status: 'error';
21
- error: ApiErrorPayload['error'];
22
- }
9
+ import { type RateLimiter } from './rate-limiter';
10
+ import { isApiSuccessEnvelope, type ApiEnvelope } from './unwrap-api-success';
23
11
 
24
- function assertSuccess<T>(data: ApiSuccessBody<T> | ApiErrorBody): data is ApiSuccessBody<T> {
25
- return data.status === 'ok';
26
- }
12
+ import type { ResolvedLightdashClientConfig } from '../config';
13
+ import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, Method } from 'axios';
27
14
 
28
15
  /**
29
16
  * HTTP client that wraps Axios with rate limiting and retry.
@@ -32,12 +19,12 @@ export class HttpClient {
32
19
  constructor(
33
20
  private readonly axiosInstance: AxiosInstance,
34
21
  private readonly rateLimiter: RateLimiter,
35
- private readonly config: LightdashClientConfig,
22
+ private readonly config: ResolvedLightdashClientConfig,
36
23
  ) {}
37
24
 
38
25
  private async request<T>(method: Method, url: string, config?: AxiosRequestConfig): Promise<T> {
39
26
  const doRequest = () =>
40
- this.axiosInstance.request<ApiSuccessBody<T> | ApiErrorBody>({
27
+ this.axiosInstance.request<ApiEnvelope<T>>({
41
28
  ...config,
42
29
  method,
43
30
  url,
@@ -46,10 +33,10 @@ export class HttpClient {
46
33
  const response = await this.rateLimiter.schedule(() => withRetry(doRequest, this.config.retry));
47
34
 
48
35
  const data = response.data;
49
- if (!assertSuccess(data)) {
36
+ if (!isApiSuccessEnvelope(data)) {
50
37
  throw new LightdashApiError(
51
38
  data.error.statusCode,
52
- data.error,
39
+ data.error as ApiErrorPayload['error'],
53
40
  response.config,
54
41
  response as AxiosResponse<ApiErrorPayload>,
55
42
  );
@@ -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
  /**
@@ -0,0 +1,23 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { isApiSuccessEnvelope, unwrapApiSuccessResults } from './unwrap-api-success';
4
+
5
+ describe('unwrapApiSuccessResults', () => {
6
+ it('returns results for ok envelopes', () => {
7
+ expect(unwrapApiSuccessResults({ status: 'ok', results: { id: '1' } })).toEqual({ id: '1' });
8
+ });
9
+
10
+ it('throws for error envelopes', () => {
11
+ expect(() =>
12
+ unwrapApiSuccessResults({
13
+ status: 'error',
14
+ error: { statusCode: 400, message: 'bad request' },
15
+ }),
16
+ ).toThrow(/bad request/);
17
+ });
18
+
19
+ it('narrows success envelopes', () => {
20
+ const body = { status: 'ok' as const, results: [1, 2] };
21
+ expect(isApiSuccessEnvelope(body)).toBe(true);
22
+ });
23
+ });
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Pure helpers for Lightdash API success envelopes.
3
+ * HttpClient uses `isApiSuccessEnvelope` then maps failures to `LightdashApiError`.
4
+ */
5
+
6
+ export type ApiSuccessEnvelope<T> = {
7
+ status: 'ok';
8
+ results: T;
9
+ };
10
+
11
+ export type ApiErrorEnvelope = {
12
+ status: 'error';
13
+ error: {
14
+ statusCode: number;
15
+ name?: string;
16
+ message?: string;
17
+ [key: string]: unknown;
18
+ };
19
+ };
20
+
21
+ export type ApiEnvelope<T> = ApiErrorEnvelope | ApiSuccessEnvelope<T>;
22
+
23
+ /** Type guard for Lightdash `{ status: 'ok', results }` responses. */
24
+ export function isApiSuccessEnvelope<T>(data: ApiEnvelope<T>): data is ApiSuccessEnvelope<T> {
25
+ return data.status === 'ok';
26
+ }
27
+
28
+ /**
29
+ * Unwraps a Lightdash success envelope to `results`.
30
+ * Throws a plain `Error` when `status` is not `ok` (message from `error.message` when present).
31
+ * Prefer `HttpClient` in production paths that need `LightdashApiError` + axios context.
32
+ */
33
+ export function unwrapApiSuccessResults<T>(data: ApiEnvelope<T>): T {
34
+ if (!isApiSuccessEnvelope(data)) {
35
+ const message =
36
+ typeof data.error?.message === 'string' ? data.error.message : 'Lightdash API error';
37
+ throw new Error(message);
38
+ }
39
+ return data.results;
40
+ }
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,15 +15,32 @@ 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 { ChartsClientV2 } from './api/v2/charts';
25
+ export { DashboardsClientV2 } from './api/v2/dashboards';
13
26
  export type { ListMembersParams, ListAllMembersParams } from './api/v1/users';
14
27
  export type { GetValidationResultsParams, ValidateProjectBody } from './api/v1/validation';
28
+ export type { ListValidationResultsParams } from './api/v2/validation';
15
29
  export type { ListMetricsParams } from './api/v1/metrics';
16
30
  export type { ListSchedulersParams } from './api/v1/schedulers';
17
- export type { SearchContentParams } from './api/v2/content';
31
+ export type {
32
+ BulkMoveContentBody,
33
+ MoveContentBody,
34
+ PermanentlyDeleteContentBody,
35
+ SearchContentParams,
36
+ } from './api/v2/content';
37
+ export type { SavedChart } from './api/v2/charts';
38
+ export type { Dashboard, UpdateDashboardBody } from './api/v2/dashboards';
39
+ export type {
40
+ DashboardPromoteDiffResults,
41
+ DashboardPromoteOptions,
42
+ PromoteDashboardResult,
43
+ } from './api/v1/dashboards';
18
44
  export type {
19
45
  ListGroupsParams,
20
46
  ListAllGroupsParams,
@@ -29,6 +55,8 @@ export type {
29
55
  export type { PartialLightdashClientConfig } from './config';
30
56
  export type {
31
57
  LightdashClientConfig,
58
+ LightdashAuthConfig,
59
+ ResolvedLightdashClientConfig,
32
60
  RateLimitConfig,
33
61
  RetryConfig,
34
62
  Logger,
@@ -38,13 +66,20 @@ export { noopLogger, consoleLogger } from './utils/logger';
38
66
  export { DEFAULT_RATE_LIMIT, DEFAULT_TIMEOUT, DEFAULT_RETRY } from './config';
39
67
  export { LightdashApiError, RateLimitError, NetworkError } from './errors';
40
68
  export type { ApiErrorPayload } from './errors';
41
- export { loadConfigFromEnv, mergeConfig } from './utils/env';
69
+ export { loadConfigFromEnv, mergeConfig, createBearerConfig } from './utils/env';
42
70
  export {
43
71
  ENV_LIGHTDASH_API_KEY,
44
72
  ENV_LIGHTDASH_URL,
45
73
  ENV_LIGHTDASH_PROXY_AUTHORIZATION,
46
74
  } from './utils/env';
47
75
  export { HttpClient } from './http/http-client';
76
+ export {
77
+ isApiSuccessEnvelope,
78
+ unwrapApiSuccessResults,
79
+ type ApiEnvelope,
80
+ type ApiErrorEnvelope,
81
+ type ApiSuccessEnvelope,
82
+ } from './http/unwrap-api-success';
48
83
  export { RateLimiter } from './http/rate-limiter';
49
84
  export { SecretString } from './utils/secret-string';
50
85
  export type { ApiResponseOk, ApiResponseError, ApiResponseBody, ApiError } from './types/api';
@@ -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
  });