@lightdash-tools/client 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +144 -0
- package/dist/api/base-client.d.ts +13 -0
- package/dist/api/base-client.js +17 -0
- package/dist/api/v1/ai-agents.d.ts +17 -0
- package/dist/api/v1/ai-agents.js +47 -0
- package/dist/api/v1/ai-agents.test.d.ts +1 -0
- package/dist/api/v1/ai-agents.test.js +77 -0
- package/dist/api/v1/charts.d.ts +18 -0
- package/dist/api/v1/charts.js +45 -0
- package/dist/api/v1/charts.test.d.ts +1 -0
- package/dist/api/v1/charts.test.js +106 -0
- package/dist/api/v1/dashboards.d.ts +9 -0
- package/dist/api/v1/dashboards.js +25 -0
- package/dist/api/v1/dashboards.test.d.ts +1 -0
- package/dist/api/v1/dashboards.test.js +33 -0
- package/dist/api/v1/explores.d.ts +11 -0
- package/dist/api/v1/explores.js +31 -0
- package/dist/api/v1/explores.test.d.ts +1 -0
- package/dist/api/v1/explores.test.js +46 -0
- package/dist/api/v1/groups.d.ts +42 -0
- package/dist/api/v1/groups.js +81 -0
- package/dist/api/v1/groups.test.d.ts +1 -0
- package/dist/api/v1/groups.test.js +107 -0
- package/dist/api/v1/organizations.d.ts +9 -0
- package/dist/api/v1/organizations.js +25 -0
- package/dist/api/v1/organizations.test.d.ts +1 -0
- package/dist/api/v1/organizations.test.js +33 -0
- package/dist/api/v1/project-access.d.ts +40 -0
- package/dist/api/v1/project-access.js +88 -0
- package/dist/api/v1/project-access.test.d.ts +1 -0
- package/dist/api/v1/project-access.test.js +109 -0
- package/dist/api/v1/projects.d.ts +13 -0
- package/dist/api/v1/projects.js +37 -0
- package/dist/api/v1/projects.test.d.ts +1 -0
- package/dist/api/v1/projects.test.js +49 -0
- package/dist/api/v1/query.d.ts +11 -0
- package/dist/api/v1/query.js +31 -0
- package/dist/api/v1/query.test.d.ts +1 -0
- package/dist/api/v1/query.test.js +51 -0
- package/dist/api/v1/spaces.d.ts +25 -0
- package/dist/api/v1/spaces.js +73 -0
- package/dist/api/v1/spaces.test.d.ts +1 -0
- package/dist/api/v1/spaces.test.js +91 -0
- package/dist/api/v1/users.d.ts +35 -0
- package/dist/api/v1/users.js +66 -0
- package/dist/api/v1/users.test.d.ts +1 -0
- package/dist/api/v1/users.test.js +122 -0
- package/dist/api/v2/organization-roles.d.ts +45 -0
- package/dist/api/v2/organization-roles.js +81 -0
- package/dist/api/v2/organization-roles.test.d.ts +1 -0
- package/dist/api/v2/organization-roles.test.js +112 -0
- package/dist/api/v2/project-role-assignments.d.ts +23 -0
- package/dist/api/v2/project-role-assignments.js +55 -0
- package/dist/api/v2/project-role-assignments.test.d.ts +1 -0
- package/dist/api/v2/project-role-assignments.test.js +66 -0
- package/dist/api/v2/query.d.ts +17 -0
- package/dist/api/v2/query.js +49 -0
- package/dist/api/v2/query.test.d.ts +1 -0
- package/dist/api/v2/query.test.js +78 -0
- package/dist/client.d.ts +118 -0
- package/dist/client.js +113 -0
- package/dist/client.test.d.ts +1 -0
- package/dist/client.test.js +94 -0
- package/dist/config.d.ts +83 -0
- package/dist/config.js +22 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.js +46 -0
- package/dist/errors.test.d.ts +1 -0
- package/dist/errors.test.js +29 -0
- package/dist/http/axios-client.d.ts +22 -0
- package/dist/http/axios-client.js +51 -0
- package/dist/http/http-client.d.ts +33 -0
- package/dist/http/http-client.js +69 -0
- package/dist/http/interceptors.d.ts +13 -0
- package/dist/http/interceptors.js +82 -0
- package/dist/http/rate-limiter.d.ts +23 -0
- package/dist/http/rate-limiter.js +60 -0
- package/dist/http/rate-limiter.test.d.ts +1 -0
- package/dist/http/rate-limiter.test.js +40 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +41 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +65 -0
- package/dist/pagination/fetch-all-pages.d.ts +15 -0
- package/dist/pagination/fetch-all-pages.js +41 -0
- package/dist/pagination/fetch-all-pages.test.d.ts +1 -0
- package/dist/pagination/fetch-all-pages.test.js +70 -0
- package/dist/pagination/types.d.ts +14 -0
- package/dist/pagination/types.js +6 -0
- package/dist/types/api.d.ts +22 -0
- package/dist/types/api.js +8 -0
- package/dist/utils/env.d.ts +20 -0
- package/dist/utils/env.js +59 -0
- package/dist/utils/env.test.d.ts +1 -0
- package/dist/utils/env.test.js +73 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/logger.js +20 -0
- package/dist/utils/retry.d.ts +14 -0
- package/dist/utils/retry.js +67 -0
- package/dist/utils/retry.test.d.ts +1 -0
- package/dist/utils/retry.test.js +53 -0
- package/package.json +21 -0
- package/src/api/base-client.ts +14 -0
- package/src/api/v1/ai-agents.test.ts +74 -0
- package/src/api/v1/ai-agents.ts +41 -0
- package/src/api/v1/charts.test.ts +103 -0
- package/src/api/v1/charts.ts +51 -0
- package/src/api/v1/dashboards.test.ts +26 -0
- package/src/api/v1/dashboards.ts +15 -0
- package/src/api/v1/explores.test.ts +40 -0
- package/src/api/v1/explores.ts +18 -0
- package/src/api/v1/groups.test.ts +108 -0
- package/src/api/v1/groups.ts +100 -0
- package/src/api/v1/organizations.test.ts +26 -0
- package/src/api/v1/organizations.ts +13 -0
- package/src/api/v1/project-access.test.ts +110 -0
- package/src/api/v1/project-access.ts +102 -0
- package/src/api/v1/projects.test.ts +44 -0
- package/src/api/v1/projects.ts +23 -0
- package/src/api/v1/query.test.ts +46 -0
- package/src/api/v1/query.ts +37 -0
- package/src/api/v1/spaces.test.ts +92 -0
- package/src/api/v1/spaces.ts +83 -0
- package/src/api/v1/users.test.ts +125 -0
- package/src/api/v1/users.ts +76 -0
- package/src/api/v2/organization-roles.test.ts +115 -0
- package/src/api/v2/organization-roles.ts +90 -0
- package/src/api/v2/project-role-assignments.test.ts +75 -0
- package/src/api/v2/project-role-assignments.ts +63 -0
- package/src/api/v2/query.test.ts +81 -0
- package/src/api/v2/query.ts +69 -0
- package/src/client.test.ts +105 -0
- package/src/client.ts +182 -0
- package/src/config.ts +113 -0
- package/src/errors.test.ts +45 -0
- package/src/errors.ts +67 -0
- package/src/http/axios-client.ts +59 -0
- package/src/http/http-client.ts +79 -0
- package/src/http/interceptors.ts +110 -0
- package/src/http/rate-limiter.test.ts +34 -0
- package/src/http/rate-limiter.ts +54 -0
- package/src/index.test.ts +91 -0
- package/src/index.ts +41 -0
- package/src/pagination/fetch-all-pages.test.ts +75 -0
- package/src/pagination/fetch-all-pages.ts +39 -0
- package/src/pagination/types.ts +16 -0
- package/src/types/api.ts +28 -0
- package/src/utils/env.test.ts +88 -0
- package/src/utils/env.ts +69 -0
- package/src/utils/logger.ts +21 -0
- package/src/utils/retry.test.ts +49 -0
- package/src/utils/retry.ts +60 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
(0, vitest_1.describe)('LightdashApiError', () => {
|
|
6
|
+
(0, vitest_1.it)('should set name and message', () => {
|
|
7
|
+
const err = new errors_1.LightdashApiError(400, { name: 'BadRequest', statusCode: 400, message: 'Invalid input' }, { method: 'POST', url: '/api' });
|
|
8
|
+
(0, vitest_1.expect)(err.name).toBe('LightdashApiError');
|
|
9
|
+
(0, vitest_1.expect)(err.message).toBe('Invalid input');
|
|
10
|
+
(0, vitest_1.expect)(err.statusCode).toBe(400);
|
|
11
|
+
});
|
|
12
|
+
(0, vitest_1.it)('should use default message when error.message is missing', () => {
|
|
13
|
+
const err = new errors_1.LightdashApiError(500, { name: 'InternalError', statusCode: 500 }, { method: 'GET', url: '/' });
|
|
14
|
+
(0, vitest_1.expect)(err.message).toContain('500');
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
(0, vitest_1.describe)('RateLimitError', () => {
|
|
18
|
+
(0, vitest_1.it)('should store retryAfter', () => {
|
|
19
|
+
const err = new errors_1.RateLimitError(429, { name: 'TooManyRequests', statusCode: 429 }, { url: '/' }, undefined, 120);
|
|
20
|
+
(0, vitest_1.expect)(err.retryAfter).toBe(120);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
(0, vitest_1.describe)('NetworkError', () => {
|
|
24
|
+
(0, vitest_1.it)('should preserve originalError', () => {
|
|
25
|
+
const original = new Error('fetch failed');
|
|
26
|
+
const err = new errors_1.NetworkError('Network error', original);
|
|
27
|
+
(0, vitest_1.expect)(err.originalError).toBe(original);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Axios instance setup with base URL, timeout, and interceptors.
|
|
3
|
+
*/
|
|
4
|
+
import { type AxiosInstance } from 'axios';
|
|
5
|
+
import type { LightdashClientConfig } from '../config';
|
|
6
|
+
/**
|
|
7
|
+
* API version type.
|
|
8
|
+
*/
|
|
9
|
+
export type ApiVersion = 'v1' | 'v2';
|
|
10
|
+
/**
|
|
11
|
+
* Creates a configured Axios instance for the Lightdash API v1.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createAxiosInstanceV1(config: LightdashClientConfig): AxiosInstance;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a configured Axios instance for the Lightdash API v2.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createAxiosInstanceV2(config: LightdashClientConfig): AxiosInstance;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a configured Axios instance for the Lightdash API v1.
|
|
20
|
+
* @deprecated Use `createAxiosInstanceV1` instead. This function is kept for backward compatibility.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createAxiosInstance(config: LightdashClientConfig): AxiosInstance;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Axios instance setup with base URL, timeout, and interceptors.
|
|
4
|
+
*/
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.createAxiosInstanceV1 = createAxiosInstanceV1;
|
|
10
|
+
exports.createAxiosInstanceV2 = createAxiosInstanceV2;
|
|
11
|
+
exports.createAxiosInstance = createAxiosInstance;
|
|
12
|
+
const axios_1 = __importDefault(require("axios"));
|
|
13
|
+
const config_1 = require("../config");
|
|
14
|
+
const interceptors_1 = require("./interceptors");
|
|
15
|
+
/**
|
|
16
|
+
* Creates a configured Axios instance for the Lightdash API with a specific version.
|
|
17
|
+
*/
|
|
18
|
+
function createAxiosInstanceForVersion(config, version) {
|
|
19
|
+
var _a;
|
|
20
|
+
const baseURL = config.baseUrl.replace(/\/$/, '') + `/api/${version}`;
|
|
21
|
+
const timeout = (_a = config.timeout) !== null && _a !== void 0 ? _a : config_1.DEFAULT_TIMEOUT;
|
|
22
|
+
const instance = axios_1.default.create({
|
|
23
|
+
baseURL,
|
|
24
|
+
timeout,
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
(0, interceptors_1.attachRequestInterceptors)(instance, config);
|
|
30
|
+
(0, interceptors_1.attachResponseInterceptors)(instance, config);
|
|
31
|
+
return instance;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates a configured Axios instance for the Lightdash API v1.
|
|
35
|
+
*/
|
|
36
|
+
function createAxiosInstanceV1(config) {
|
|
37
|
+
return createAxiosInstanceForVersion(config, 'v1');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a configured Axios instance for the Lightdash API v2.
|
|
41
|
+
*/
|
|
42
|
+
function createAxiosInstanceV2(config) {
|
|
43
|
+
return createAxiosInstanceForVersion(config, 'v2');
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates a configured Axios instance for the Lightdash API v1.
|
|
47
|
+
* @deprecated Use `createAxiosInstanceV1` instead. This function is kept for backward compatibility.
|
|
48
|
+
*/
|
|
49
|
+
function createAxiosInstance(config) {
|
|
50
|
+
return createAxiosInstanceV1(config);
|
|
51
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client wrapper: rate limiter + axios + retry.
|
|
3
|
+
* All requests go through the rate limiter; retry applies to 5xx and network errors.
|
|
4
|
+
*/
|
|
5
|
+
import type { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
6
|
+
import type { LightdashClientConfig } from '../config';
|
|
7
|
+
import { type ApiErrorPayload } from '../errors';
|
|
8
|
+
import { type RateLimiter } from './rate-limiter';
|
|
9
|
+
/** Lightdash API success response shape. */
|
|
10
|
+
export interface ApiSuccessBody<T = unknown> {
|
|
11
|
+
status: 'ok';
|
|
12
|
+
results: T;
|
|
13
|
+
}
|
|
14
|
+
/** Lightdash API error response shape (body). */
|
|
15
|
+
export interface ApiErrorBody {
|
|
16
|
+
status: 'error';
|
|
17
|
+
error: ApiErrorPayload['error'];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* HTTP client that wraps Axios with rate limiting and retry.
|
|
21
|
+
*/
|
|
22
|
+
export declare class HttpClient {
|
|
23
|
+
private readonly axiosInstance;
|
|
24
|
+
private readonly rateLimiter;
|
|
25
|
+
private readonly config;
|
|
26
|
+
constructor(axiosInstance: AxiosInstance, rateLimiter: RateLimiter, config: LightdashClientConfig);
|
|
27
|
+
private request;
|
|
28
|
+
get<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
29
|
+
post<T>(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
30
|
+
put<T>(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
31
|
+
patch<T>(url: string, body?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
32
|
+
delete<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* HTTP client wrapper: rate limiter + axios + retry.
|
|
4
|
+
* All requests go through the rate limiter; retry applies to 5xx and network errors.
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.HttpClient = void 0;
|
|
17
|
+
const errors_1 = require("../errors");
|
|
18
|
+
const retry_1 = require("../utils/retry");
|
|
19
|
+
function assertSuccess(data) {
|
|
20
|
+
return data.status === 'ok';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* HTTP client that wraps Axios with rate limiting and retry.
|
|
24
|
+
*/
|
|
25
|
+
class HttpClient {
|
|
26
|
+
constructor(axiosInstance, rateLimiter, config) {
|
|
27
|
+
this.axiosInstance = axiosInstance;
|
|
28
|
+
this.rateLimiter = rateLimiter;
|
|
29
|
+
this.config = config;
|
|
30
|
+
}
|
|
31
|
+
request(method, url, config) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
const doRequest = () => this.axiosInstance.request(Object.assign(Object.assign({}, config), { method,
|
|
34
|
+
url }));
|
|
35
|
+
const response = yield this.rateLimiter.schedule(() => (0, retry_1.withRetry)(doRequest, this.config.retry));
|
|
36
|
+
const data = response.data;
|
|
37
|
+
if (!assertSuccess(data)) {
|
|
38
|
+
throw new errors_1.LightdashApiError(data.error.statusCode, data.error, response.config, response);
|
|
39
|
+
}
|
|
40
|
+
return data.results;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
get(url, config) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
return this.request('GET', url, config);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
post(url, body, config) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
return this.request('POST', url, Object.assign(Object.assign({}, config), { data: body }));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
put(url, body, config) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
return this.request('PUT', url, Object.assign(Object.assign({}, config), { data: body }));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
patch(url, body, config) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
return this.request('PATCH', url, Object.assign(Object.assign({}, config), { data: body }));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
delete(url, config) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
return this.request('DELETE', url, config);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.HttpClient = HttpClient;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request and response interceptors for auth, proxy, and error handling.
|
|
3
|
+
*/
|
|
4
|
+
import type { AxiosInstance } from 'axios';
|
|
5
|
+
import type { LightdashClientConfig } from '../config';
|
|
6
|
+
/**
|
|
7
|
+
* Attaches request interceptors: Authorization, Proxy-Authorization, optional request ID and logging.
|
|
8
|
+
*/
|
|
9
|
+
export declare function attachRequestInterceptors(axiosInstance: AxiosInstance, config: LightdashClientConfig): void;
|
|
10
|
+
/**
|
|
11
|
+
* Attaches response interceptors: parse API responses, transform errors to LightdashApiError / RateLimitError.
|
|
12
|
+
*/
|
|
13
|
+
export declare function attachResponseInterceptors(axiosInstance: AxiosInstance, config?: Pick<LightdashClientConfig, 'logger' | 'observability'>): void;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Request and response interceptors for auth, proxy, and error handling.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.attachRequestInterceptors = attachRequestInterceptors;
|
|
7
|
+
exports.attachResponseInterceptors = attachResponseInterceptors;
|
|
8
|
+
const errors_1 = require("../errors");
|
|
9
|
+
function getRetryAfterSeconds(value) {
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
return undefined;
|
|
12
|
+
const n = parseInt(value, 10);
|
|
13
|
+
return Number.isNaN(n) ? undefined : n;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Attaches request interceptors: Authorization, Proxy-Authorization, optional request ID and logging.
|
|
17
|
+
*/
|
|
18
|
+
function attachRequestInterceptors(axiosInstance, config) {
|
|
19
|
+
axiosInstance.interceptors.request.use((req) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const token = config.personalAccessToken.startsWith('ldpat_')
|
|
22
|
+
? config.personalAccessToken
|
|
23
|
+
: `ldpat_${config.personalAccessToken}`;
|
|
24
|
+
req.headers.Authorization = `ApiKey ${token}`;
|
|
25
|
+
if (config.proxyAuthorization) {
|
|
26
|
+
req.headers['Proxy-Authorization'] = config.proxyAuthorization;
|
|
27
|
+
}
|
|
28
|
+
req._lightdashStartTime =
|
|
29
|
+
Date.now();
|
|
30
|
+
if ((_a = config.logger) === null || _a === void 0 ? void 0 : _a.debug) {
|
|
31
|
+
config.logger.debug('Lightdash request: %s %s', req.method, req.url);
|
|
32
|
+
}
|
|
33
|
+
return req;
|
|
34
|
+
}, (err) => Promise.reject(err));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Attaches response interceptors: parse API responses, transform errors to LightdashApiError / RateLimitError.
|
|
38
|
+
*/
|
|
39
|
+
function attachResponseInterceptors(axiosInstance, config) {
|
|
40
|
+
const logger = config === null || config === void 0 ? void 0 : config.logger;
|
|
41
|
+
axiosInstance.interceptors.response.use((response) => {
|
|
42
|
+
var _a, _b, _c, _d;
|
|
43
|
+
if (logger === null || logger === void 0 ? void 0 : logger.debug) {
|
|
44
|
+
logger.debug('Lightdash response: %s %s %s', response.config.method, response.config.url, response.status);
|
|
45
|
+
}
|
|
46
|
+
const start = response.config._lightdashStartTime;
|
|
47
|
+
(_b = (_a = config === null || config === void 0 ? void 0 : config.observability) === null || _a === void 0 ? void 0 : _a.onRequestComplete) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
48
|
+
method: (_c = response.config.method) !== null && _c !== void 0 ? _c : 'GET',
|
|
49
|
+
url: (_d = response.config.url) !== null && _d !== void 0 ? _d : '',
|
|
50
|
+
statusCode: response.status,
|
|
51
|
+
durationMs: start !== undefined ? Date.now() - start : 0,
|
|
52
|
+
});
|
|
53
|
+
return response;
|
|
54
|
+
}, (error) => {
|
|
55
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
56
|
+
const reqConfig = error.config;
|
|
57
|
+
const start = reqConfig === null || reqConfig === void 0 ? void 0 : reqConfig._lightdashStartTime;
|
|
58
|
+
(_b = (_a = config === null || config === void 0 ? void 0 : config.observability) === null || _a === void 0 ? void 0 : _a.onRequestComplete) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
59
|
+
method: (_c = reqConfig === null || reqConfig === void 0 ? void 0 : reqConfig.method) !== null && _c !== void 0 ? _c : 'GET',
|
|
60
|
+
url: (_d = reqConfig === null || reqConfig === void 0 ? void 0 : reqConfig.url) !== null && _d !== void 0 ? _d : '',
|
|
61
|
+
statusCode: (_e = error.response) === null || _e === void 0 ? void 0 : _e.status,
|
|
62
|
+
durationMs: start !== undefined ? Date.now() - start : 0,
|
|
63
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
64
|
+
});
|
|
65
|
+
if (error.response) {
|
|
66
|
+
const status = error.response.status;
|
|
67
|
+
const data = error.response.data;
|
|
68
|
+
const retryAfter = getRetryAfterSeconds((_f = error.response.headers) === null || _f === void 0 ? void 0 : _f['retry-after']);
|
|
69
|
+
const payload = (_g = data === null || data === void 0 ? void 0 : data.error) !== null && _g !== void 0 ? _g : {
|
|
70
|
+
name: 'UnknownError',
|
|
71
|
+
statusCode: status,
|
|
72
|
+
message: error.message,
|
|
73
|
+
};
|
|
74
|
+
if (status === 429) {
|
|
75
|
+
return Promise.reject(new errors_1.RateLimitError(status, payload, error.config, error.response, retryAfter));
|
|
76
|
+
}
|
|
77
|
+
return Promise.reject(new errors_1.LightdashApiError(status, payload, error.config, error.response));
|
|
78
|
+
}
|
|
79
|
+
const msg = error.message || (error.code ? `Network error: ${error.code}` : 'Network error');
|
|
80
|
+
return Promise.reject(new errors_1.NetworkError(msg, error));
|
|
81
|
+
});
|
|
82
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate limiter wrapping Bottleneck for centralized request throttling.
|
|
3
|
+
* Uses token bucket algorithm; handles 429 retries via Bottleneck options.
|
|
4
|
+
*/
|
|
5
|
+
import type { RateLimitConfig } from '../config';
|
|
6
|
+
export type RateLimiterOptions = RateLimitConfig;
|
|
7
|
+
/**
|
|
8
|
+
* Wraps Bottleneck to provide configurable rate limiting for API requests.
|
|
9
|
+
*/
|
|
10
|
+
export declare class RateLimiter {
|
|
11
|
+
private readonly limiter;
|
|
12
|
+
constructor(options?: RateLimiterOptions);
|
|
13
|
+
/**
|
|
14
|
+
* Schedules a function to run when the rate limit allows.
|
|
15
|
+
*/
|
|
16
|
+
schedule<T>(fn: () => Promise<T>): Promise<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Wraps an async function so that each call goes through the rate limiter.
|
|
19
|
+
*/
|
|
20
|
+
wrap<T extends unknown[], R>(fn: (...args: T) => Promise<R>): (...args: T) => Promise<R>;
|
|
21
|
+
/** Stops the limiter and drains remaining jobs. */
|
|
22
|
+
disconnect(): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Rate limiter wrapping Bottleneck for centralized request throttling.
|
|
4
|
+
* Uses token bucket algorithm; handles 429 retries via Bottleneck options.
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
16
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.RateLimiter = void 0;
|
|
20
|
+
const bottleneck_1 = __importDefault(require("bottleneck"));
|
|
21
|
+
const config_1 = require("../config");
|
|
22
|
+
/**
|
|
23
|
+
* Wraps Bottleneck to provide configurable rate limiting for API requests.
|
|
24
|
+
*/
|
|
25
|
+
class RateLimiter {
|
|
26
|
+
constructor(options = {}) {
|
|
27
|
+
var _a, _b, _c, _d, _e;
|
|
28
|
+
const minTime = (_a = options.minTime) !== null && _a !== void 0 ? _a : config_1.DEFAULT_RATE_LIMIT.minTime;
|
|
29
|
+
const maxConcurrent = (_b = options.maxConcurrent) !== null && _b !== void 0 ? _b : config_1.DEFAULT_RATE_LIMIT.maxConcurrent;
|
|
30
|
+
const reservoir = (_c = options.reservoir) !== null && _c !== void 0 ? _c : config_1.DEFAULT_RATE_LIMIT.reservoir;
|
|
31
|
+
const reservoirRefreshInterval = (_d = options.reservoirRefreshInterval) !== null && _d !== void 0 ? _d : config_1.DEFAULT_RATE_LIMIT.reservoirRefreshInterval;
|
|
32
|
+
const reservoirRefreshAmount = (_e = options.reservoirRefreshAmount) !== null && _e !== void 0 ? _e : config_1.DEFAULT_RATE_LIMIT.reservoirRefreshAmount;
|
|
33
|
+
this.limiter = new bottleneck_1.default({
|
|
34
|
+
minTime,
|
|
35
|
+
maxConcurrent,
|
|
36
|
+
reservoir,
|
|
37
|
+
reservoirRefreshInterval,
|
|
38
|
+
reservoirRefreshAmount,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Schedules a function to run when the rate limit allows.
|
|
43
|
+
*/
|
|
44
|
+
schedule(fn) {
|
|
45
|
+
return this.limiter.schedule(fn);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Wraps an async function so that each call goes through the rate limiter.
|
|
49
|
+
*/
|
|
50
|
+
wrap(fn) {
|
|
51
|
+
return (...args) => this.limiter.schedule(() => fn(...args));
|
|
52
|
+
}
|
|
53
|
+
/** Stops the limiter and drains remaining jobs. */
|
|
54
|
+
disconnect() {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
return this.limiter.disconnect(true);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.RateLimiter = RateLimiter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const vitest_1 = require("vitest");
|
|
13
|
+
const rate_limiter_1 = require("./rate-limiter");
|
|
14
|
+
(0, vitest_1.describe)('RateLimiter', () => {
|
|
15
|
+
(0, vitest_1.it)('should schedule and run a single task', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const limiter = new rate_limiter_1.RateLimiter({ minTime: 0, maxConcurrent: 1 });
|
|
17
|
+
const result = yield limiter.schedule(() => __awaiter(void 0, void 0, void 0, function* () { return 42; }));
|
|
18
|
+
(0, vitest_1.expect)(result).toBe(42);
|
|
19
|
+
yield limiter.disconnect();
|
|
20
|
+
}));
|
|
21
|
+
(0, vitest_1.it)('should run tasks sequentially when maxConcurrent is 1', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
const limiter = new rate_limiter_1.RateLimiter({ minTime: 10, maxConcurrent: 1 });
|
|
23
|
+
const order = [];
|
|
24
|
+
const promises = [1, 2, 3].map((n) => limiter.schedule(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
order.push(n);
|
|
26
|
+
return n;
|
|
27
|
+
})));
|
|
28
|
+
const results = yield Promise.all(promises);
|
|
29
|
+
(0, vitest_1.expect)(results).toEqual([1, 2, 3]);
|
|
30
|
+
(0, vitest_1.expect)(order).toEqual([1, 2, 3]);
|
|
31
|
+
yield limiter.disconnect();
|
|
32
|
+
}));
|
|
33
|
+
(0, vitest_1.it)('should wrap a function', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const limiter = new rate_limiter_1.RateLimiter({ minTime: 0 });
|
|
35
|
+
const wrapped = limiter.wrap((a, b) => __awaiter(void 0, void 0, void 0, function* () { return a + b; }));
|
|
36
|
+
const result = yield wrapped(1, 2);
|
|
37
|
+
(0, vitest_1.expect)(result).toBe(3);
|
|
38
|
+
yield limiter.disconnect();
|
|
39
|
+
}));
|
|
40
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { LightdashClient, V1ApiClients, V2ApiClients } from './client';
|
|
2
|
+
export { UsersClient } from './api/v1/users';
|
|
3
|
+
export { GroupsClient } from './api/v1/groups';
|
|
4
|
+
export { OrganizationRolesClient } from './api/v2/organization-roles';
|
|
5
|
+
export { ProjectRoleAssignmentsClient } from './api/v2/project-role-assignments';
|
|
6
|
+
export { AiAgentsClient } from './api/v1/ai-agents';
|
|
7
|
+
export { ProjectAccessClient } from './api/v1/project-access';
|
|
8
|
+
export type { ListMembersParams, ListAllMembersParams } from './api/v1/users';
|
|
9
|
+
export type { ListGroupsParams, ListAllGroupsParams, GetGroupParams, GroupOrGroupWithMembers, } from './api/v1/groups';
|
|
10
|
+
export type { GetRolesResult, GetRolesParams, AssignOrgRoleToUserBody, } from './api/v2/organization-roles';
|
|
11
|
+
export type { PartialLightdashClientConfig } from './config';
|
|
12
|
+
export type { LightdashClientConfig, RateLimitConfig, RetryConfig, Logger, ObservabilityHooks, } from './config';
|
|
13
|
+
export { noopLogger, consoleLogger } from './utils/logger';
|
|
14
|
+
export { DEFAULT_RATE_LIMIT, DEFAULT_TIMEOUT, DEFAULT_RETRY } from './config';
|
|
15
|
+
export { LightdashApiError, RateLimitError, NetworkError } from './errors';
|
|
16
|
+
export type { ApiErrorPayload } from './errors';
|
|
17
|
+
export { loadConfigFromEnv, mergeConfig } from './utils/env';
|
|
18
|
+
export { ENV_LIGHTDASH_API_KEY, ENV_LIGHTDASH_URL, ENV_LIGHTDASH_PROXY_AUTHORIZATION, } from './utils/env';
|
|
19
|
+
export { HttpClient } from './http/http-client';
|
|
20
|
+
export { RateLimiter } from './http/rate-limiter';
|
|
21
|
+
export type { ApiResponseOk, ApiResponseError, ApiResponseBody, ApiError } from './types/api';
|
|
22
|
+
export type { paths, components, operations } from './types/api';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
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;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "LightdashClient", { enumerable: true, get: function () { return client_1.LightdashClient; } });
|
|
6
|
+
Object.defineProperty(exports, "V1ApiClients", { enumerable: true, get: function () { return client_1.V1ApiClients; } });
|
|
7
|
+
Object.defineProperty(exports, "V2ApiClients", { enumerable: true, get: function () { return client_1.V2ApiClients; } });
|
|
8
|
+
var users_1 = require("./api/v1/users");
|
|
9
|
+
Object.defineProperty(exports, "UsersClient", { enumerable: true, get: function () { return users_1.UsersClient; } });
|
|
10
|
+
var groups_1 = require("./api/v1/groups");
|
|
11
|
+
Object.defineProperty(exports, "GroupsClient", { enumerable: true, get: function () { return groups_1.GroupsClient; } });
|
|
12
|
+
var organization_roles_1 = require("./api/v2/organization-roles");
|
|
13
|
+
Object.defineProperty(exports, "OrganizationRolesClient", { enumerable: true, get: function () { return organization_roles_1.OrganizationRolesClient; } });
|
|
14
|
+
var project_role_assignments_1 = require("./api/v2/project-role-assignments");
|
|
15
|
+
Object.defineProperty(exports, "ProjectRoleAssignmentsClient", { enumerable: true, get: function () { return project_role_assignments_1.ProjectRoleAssignmentsClient; } });
|
|
16
|
+
var ai_agents_1 = require("./api/v1/ai-agents");
|
|
17
|
+
Object.defineProperty(exports, "AiAgentsClient", { enumerable: true, get: function () { return ai_agents_1.AiAgentsClient; } });
|
|
18
|
+
var project_access_1 = require("./api/v1/project-access");
|
|
19
|
+
Object.defineProperty(exports, "ProjectAccessClient", { enumerable: true, get: function () { return project_access_1.ProjectAccessClient; } });
|
|
20
|
+
var logger_1 = require("./utils/logger");
|
|
21
|
+
Object.defineProperty(exports, "noopLogger", { enumerable: true, get: function () { return logger_1.noopLogger; } });
|
|
22
|
+
Object.defineProperty(exports, "consoleLogger", { enumerable: true, get: function () { return logger_1.consoleLogger; } });
|
|
23
|
+
var config_1 = require("./config");
|
|
24
|
+
Object.defineProperty(exports, "DEFAULT_RATE_LIMIT", { enumerable: true, get: function () { return config_1.DEFAULT_RATE_LIMIT; } });
|
|
25
|
+
Object.defineProperty(exports, "DEFAULT_TIMEOUT", { enumerable: true, get: function () { return config_1.DEFAULT_TIMEOUT; } });
|
|
26
|
+
Object.defineProperty(exports, "DEFAULT_RETRY", { enumerable: true, get: function () { return config_1.DEFAULT_RETRY; } });
|
|
27
|
+
var errors_1 = require("./errors");
|
|
28
|
+
Object.defineProperty(exports, "LightdashApiError", { enumerable: true, get: function () { return errors_1.LightdashApiError; } });
|
|
29
|
+
Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return errors_1.RateLimitError; } });
|
|
30
|
+
Object.defineProperty(exports, "NetworkError", { enumerable: true, get: function () { return errors_1.NetworkError; } });
|
|
31
|
+
var env_1 = require("./utils/env");
|
|
32
|
+
Object.defineProperty(exports, "loadConfigFromEnv", { enumerable: true, get: function () { return env_1.loadConfigFromEnv; } });
|
|
33
|
+
Object.defineProperty(exports, "mergeConfig", { enumerable: true, get: function () { return env_1.mergeConfig; } });
|
|
34
|
+
var env_2 = require("./utils/env");
|
|
35
|
+
Object.defineProperty(exports, "ENV_LIGHTDASH_API_KEY", { enumerable: true, get: function () { return env_2.ENV_LIGHTDASH_API_KEY; } });
|
|
36
|
+
Object.defineProperty(exports, "ENV_LIGHTDASH_URL", { enumerable: true, get: function () { return env_2.ENV_LIGHTDASH_URL; } });
|
|
37
|
+
Object.defineProperty(exports, "ENV_LIGHTDASH_PROXY_AUTHORIZATION", { enumerable: true, get: function () { return env_2.ENV_LIGHTDASH_PROXY_AUTHORIZATION; } });
|
|
38
|
+
var http_client_1 = require("./http/http-client");
|
|
39
|
+
Object.defineProperty(exports, "HttpClient", { enumerable: true, get: function () { return http_client_1.HttpClient; } });
|
|
40
|
+
var rate_limiter_1 = require("./http/rate-limiter");
|
|
41
|
+
Object.defineProperty(exports, "RateLimiter", { enumerable: true, get: function () { return rate_limiter_1.RateLimiter; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
(0, vitest_1.describe)('LightdashClient', () => {
|
|
6
|
+
(0, vitest_1.it)('should export LightdashClient', () => {
|
|
7
|
+
(0, vitest_1.expect)(index_1.LightdashClient).toBeDefined();
|
|
8
|
+
});
|
|
9
|
+
(0, vitest_1.it)('should instantiate with explicit config', () => {
|
|
10
|
+
const client = new index_1.LightdashClient({
|
|
11
|
+
baseUrl: 'https://app.lightdash.cloud',
|
|
12
|
+
personalAccessToken: 'test-token',
|
|
13
|
+
});
|
|
14
|
+
(0, vitest_1.expect)(client.getHttpClient()).toBeDefined();
|
|
15
|
+
});
|
|
16
|
+
(0, vitest_1.it)('should throw when baseUrl and personalAccessToken are missing and not in env', () => {
|
|
17
|
+
(0, vitest_1.expect)(() => new index_1.LightdashClient({})).toThrow(/baseUrl and personalAccessToken|LIGHTDASH_URL and LIGHTDASH_API_KEY/);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
(0, vitest_1.describe)('mergeConfig', () => {
|
|
21
|
+
(0, vitest_1.it)('should merge explicit config with env (explicit wins)', () => {
|
|
22
|
+
const explicit = {
|
|
23
|
+
baseUrl: 'https://custom.example.com',
|
|
24
|
+
personalAccessToken: 'explicit-token',
|
|
25
|
+
};
|
|
26
|
+
const merged = (0, index_1.mergeConfig)(explicit);
|
|
27
|
+
(0, vitest_1.expect)(merged.baseUrl).toBe('https://custom.example.com');
|
|
28
|
+
(0, vitest_1.expect)(merged.personalAccessToken).toBe('explicit-token');
|
|
29
|
+
});
|
|
30
|
+
(0, vitest_1.it)('should require baseUrl and personalAccessToken', () => {
|
|
31
|
+
(0, vitest_1.expect)(() => (0, index_1.mergeConfig)({})).toThrow();
|
|
32
|
+
(0, vitest_1.expect)(() => (0, index_1.mergeConfig)({ baseUrl: 'https://x.com' })).toThrow();
|
|
33
|
+
(0, vitest_1.expect)(() => (0, index_1.mergeConfig)({ personalAccessToken: 't' })).toThrow();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
(0, vitest_1.describe)('loadConfigFromEnv', () => {
|
|
37
|
+
(0, vitest_1.it)('should return empty object when env vars are not set', () => {
|
|
38
|
+
const config = (0, index_1.loadConfigFromEnv)();
|
|
39
|
+
(0, vitest_1.expect)(config).toEqual({});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
(0, vitest_1.describe)('LightdashApiError', () => {
|
|
43
|
+
(0, vitest_1.it)('should have statusCode and error payload', () => {
|
|
44
|
+
const err = new index_1.LightdashApiError(404, { name: 'NotFound', statusCode: 404, message: 'Not found' }, { url: '/test', method: 'GET' });
|
|
45
|
+
(0, vitest_1.expect)(err.name).toBe('LightdashApiError');
|
|
46
|
+
(0, vitest_1.expect)(err.statusCode).toBe(404);
|
|
47
|
+
(0, vitest_1.expect)(err.error.name).toBe('NotFound');
|
|
48
|
+
(0, vitest_1.expect)(err.message).toContain('Not found');
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
(0, vitest_1.describe)('RateLimitError', () => {
|
|
52
|
+
(0, vitest_1.it)('should extend LightdashApiError and have retryAfter', () => {
|
|
53
|
+
const err = new index_1.RateLimitError(429, { name: 'TooManyRequests', statusCode: 429 }, { url: '/test', method: 'GET' }, undefined, 60);
|
|
54
|
+
(0, vitest_1.expect)(err).toBeInstanceOf(index_1.LightdashApiError);
|
|
55
|
+
(0, vitest_1.expect)(err.retryAfter).toBe(60);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
(0, vitest_1.describe)('NetworkError', () => {
|
|
59
|
+
(0, vitest_1.it)('should wrap original error', () => {
|
|
60
|
+
const original = new Error('ECONNRESET');
|
|
61
|
+
const err = new index_1.NetworkError('Connection reset', original);
|
|
62
|
+
(0, vitest_1.expect)(err.originalError).toBe(original);
|
|
63
|
+
(0, vitest_1.expect)(err.message).toBe('Connection reset');
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic helper to fetch all pages of a Knex-paginated API and return a single array.
|
|
3
|
+
*/
|
|
4
|
+
import type { KnexPage } from './types';
|
|
5
|
+
export interface FetchAllPagesOptions<T> {
|
|
6
|
+
/** Fetches one page. Called with 1-based page number and page size. */
|
|
7
|
+
fetchPage: (page: number, pageSize: number) => Promise<KnexPage<T>>;
|
|
8
|
+
/** Page size per request. Default 100. */
|
|
9
|
+
pageSize?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetches all pages by repeatedly calling fetchPage until no more pages, then returns
|
|
13
|
+
* the concatenated data array.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fetchAllPages<T>(options: FetchAllPagesOptions<T>): Promise<T[]>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Generic helper to fetch all pages of a Knex-paginated API and return a single array.
|
|
4
|
+
*/
|
|
5
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
6
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
7
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
9
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
10
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
11
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.fetchAllPages = fetchAllPages;
|
|
16
|
+
const DEFAULT_PAGE_SIZE = 100;
|
|
17
|
+
/**
|
|
18
|
+
* Fetches all pages by repeatedly calling fetchPage until no more pages, then returns
|
|
19
|
+
* the concatenated data array.
|
|
20
|
+
*/
|
|
21
|
+
function fetchAllPages(options) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
var _a;
|
|
24
|
+
const pageSize = (_a = options.pageSize) !== null && _a !== void 0 ? _a : DEFAULT_PAGE_SIZE;
|
|
25
|
+
const all = [];
|
|
26
|
+
let page = 1;
|
|
27
|
+
let totalPageCount;
|
|
28
|
+
do {
|
|
29
|
+
const result = yield options.fetchPage(page, pageSize);
|
|
30
|
+
all.push(...result.data);
|
|
31
|
+
if (result.pagination) {
|
|
32
|
+
totalPageCount = result.pagination.totalPageCount;
|
|
33
|
+
page += 1;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
totalPageCount = undefined;
|
|
37
|
+
}
|
|
38
|
+
} while (totalPageCount !== undefined && page <= totalPageCount);
|
|
39
|
+
return all;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|