@pauly4010/evalai-sdk 1.3.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/CHANGELOG.md +289 -0
- package/LICENSE +21 -0
- package/README.md +565 -0
- package/dist/assertions.d.ts +189 -0
- package/dist/assertions.js +596 -0
- package/dist/batch.d.ts +68 -0
- package/dist/batch.js +178 -0
- package/dist/cache.d.ts +65 -0
- package/dist/cache.js +135 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.js +181 -0
- package/dist/client.d.ts +358 -0
- package/dist/client.js +802 -0
- package/dist/context.d.ts +134 -0
- package/dist/context.js +215 -0
- package/dist/errors.d.ts +80 -0
- package/dist/errors.js +285 -0
- package/dist/export.d.ts +195 -0
- package/dist/export.js +334 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +111 -0
- package/dist/integrations/anthropic.d.ts +72 -0
- package/dist/integrations/anthropic.js +159 -0
- package/dist/integrations/openai.d.ts +69 -0
- package/dist/integrations/openai.js +156 -0
- package/dist/local.d.ts +39 -0
- package/dist/local.js +146 -0
- package/dist/logger.d.ts +128 -0
- package/dist/logger.js +227 -0
- package/dist/pagination.d.ts +74 -0
- package/dist/pagination.js +135 -0
- package/dist/snapshot.d.ts +176 -0
- package/dist/snapshot.js +322 -0
- package/dist/streaming.d.ts +173 -0
- package/dist/streaming.js +268 -0
- package/dist/testing.d.ts +204 -0
- package/dist/testing.js +252 -0
- package/dist/types.d.ts +715 -0
- package/dist/types.js +54 -0
- package/dist/workflows.d.ts +378 -0
- package/dist/workflows.js +628 -0
- package/package.json +102 -0
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Debug Mode with Request Logging
|
|
3
|
+
* Tier 4.17: Troubleshooting utilities
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { createLogger } from '@ai-eval-platform/sdk';
|
|
8
|
+
*
|
|
9
|
+
* const logger = createLogger({ level: 'debug', pretty: true });
|
|
10
|
+
*
|
|
11
|
+
* logger.debug('Trace created', { traceId: '123' });
|
|
12
|
+
* logger.error('Request failed', { error: err });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
16
|
+
export interface LoggerOptions {
|
|
17
|
+
/** Log level (default: 'info') */
|
|
18
|
+
level?: LogLevel;
|
|
19
|
+
/** Pretty print logs (default: false) */
|
|
20
|
+
pretty?: boolean;
|
|
21
|
+
/** Include timestamps (default: true) */
|
|
22
|
+
timestamps?: boolean;
|
|
23
|
+
/** Custom log handler */
|
|
24
|
+
handler?: (entry: LogEntry) => void;
|
|
25
|
+
/** Prefix for all logs */
|
|
26
|
+
prefix?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface LogEntry {
|
|
29
|
+
level: LogLevel;
|
|
30
|
+
message: string;
|
|
31
|
+
timestamp: string;
|
|
32
|
+
data?: any;
|
|
33
|
+
prefix?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Logger for SDK debugging and troubleshooting
|
|
37
|
+
*/
|
|
38
|
+
export declare class Logger {
|
|
39
|
+
private options;
|
|
40
|
+
constructor(options?: LoggerOptions);
|
|
41
|
+
/**
|
|
42
|
+
* Log a trace message
|
|
43
|
+
*/
|
|
44
|
+
trace(message: string, data?: any): void;
|
|
45
|
+
/**
|
|
46
|
+
* Log a debug message
|
|
47
|
+
*/
|
|
48
|
+
debug(message: string, data?: any): void;
|
|
49
|
+
/**
|
|
50
|
+
* Log an info message
|
|
51
|
+
*/
|
|
52
|
+
info(message: string, data?: any): void;
|
|
53
|
+
/**
|
|
54
|
+
* Log a warning message
|
|
55
|
+
*/
|
|
56
|
+
warn(message: string, data?: any): void;
|
|
57
|
+
/**
|
|
58
|
+
* Log an error message
|
|
59
|
+
*/
|
|
60
|
+
error(message: string, data?: any): void;
|
|
61
|
+
/**
|
|
62
|
+
* Log HTTP request
|
|
63
|
+
*/
|
|
64
|
+
logRequest(method: string, url: string, data?: any): void;
|
|
65
|
+
/**
|
|
66
|
+
* Log HTTP response
|
|
67
|
+
*/
|
|
68
|
+
logResponse(method: string, url: string, status: number, duration: number, data?: any): void;
|
|
69
|
+
/**
|
|
70
|
+
* Create child logger with prefix
|
|
71
|
+
*/
|
|
72
|
+
child(prefix: string): Logger;
|
|
73
|
+
/**
|
|
74
|
+
* Set log level
|
|
75
|
+
*/
|
|
76
|
+
setLevel(level: LogLevel): void;
|
|
77
|
+
/**
|
|
78
|
+
* Check if level is enabled
|
|
79
|
+
*/
|
|
80
|
+
isLevelEnabled(level: LogLevel): boolean;
|
|
81
|
+
private log;
|
|
82
|
+
private defaultHandler;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create a logger instance
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const logger = createLogger({ level: 'debug', pretty: true });
|
|
90
|
+
* logger.debug('Starting evaluation');
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function createLogger(options?: LoggerOptions): Logger;
|
|
94
|
+
/**
|
|
95
|
+
* Get global logger (creates one if it doesn't exist)
|
|
96
|
+
*/
|
|
97
|
+
export declare function getLogger(): Logger;
|
|
98
|
+
/**
|
|
99
|
+
* Set global logger
|
|
100
|
+
*/
|
|
101
|
+
export declare function setLogger(logger: Logger): void;
|
|
102
|
+
/**
|
|
103
|
+
* Request/Response interceptor for logging
|
|
104
|
+
*/
|
|
105
|
+
export declare class RequestLogger {
|
|
106
|
+
private logger;
|
|
107
|
+
constructor(logger: Logger);
|
|
108
|
+
/**
|
|
109
|
+
* Log request before sending
|
|
110
|
+
*/
|
|
111
|
+
logRequest(request: {
|
|
112
|
+
method: string;
|
|
113
|
+
url: string;
|
|
114
|
+
headers?: Record<string, string>;
|
|
115
|
+
body?: any;
|
|
116
|
+
}): void;
|
|
117
|
+
/**
|
|
118
|
+
* Log response after receiving
|
|
119
|
+
*/
|
|
120
|
+
logResponse(response: {
|
|
121
|
+
method: string;
|
|
122
|
+
url: string;
|
|
123
|
+
status: number;
|
|
124
|
+
duration: number;
|
|
125
|
+
headers?: Record<string, string>;
|
|
126
|
+
body?: any;
|
|
127
|
+
}): void;
|
|
128
|
+
}
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Debug Mode with Request Logging
|
|
4
|
+
* Tier 4.17: Troubleshooting utilities
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { createLogger } from '@ai-eval-platform/sdk';
|
|
9
|
+
*
|
|
10
|
+
* const logger = createLogger({ level: 'debug', pretty: true });
|
|
11
|
+
*
|
|
12
|
+
* logger.debug('Trace created', { traceId: '123' });
|
|
13
|
+
* logger.error('Request failed', { error: err });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.RequestLogger = exports.Logger = void 0;
|
|
18
|
+
exports.createLogger = createLogger;
|
|
19
|
+
exports.getLogger = getLogger;
|
|
20
|
+
exports.setLogger = setLogger;
|
|
21
|
+
const LOG_LEVELS = {
|
|
22
|
+
trace: 0,
|
|
23
|
+
debug: 1,
|
|
24
|
+
info: 2,
|
|
25
|
+
warn: 3,
|
|
26
|
+
error: 4
|
|
27
|
+
};
|
|
28
|
+
const LOG_COLORS = {
|
|
29
|
+
trace: '\x1b[90m', // gray
|
|
30
|
+
debug: '\x1b[36m', // cyan
|
|
31
|
+
info: '\x1b[32m', // green
|
|
32
|
+
warn: '\x1b[33m', // yellow
|
|
33
|
+
error: '\x1b[31m' // red
|
|
34
|
+
};
|
|
35
|
+
const COLOR_RESET = '\x1b[0m';
|
|
36
|
+
/**
|
|
37
|
+
* Logger for SDK debugging and troubleshooting
|
|
38
|
+
*/
|
|
39
|
+
class Logger {
|
|
40
|
+
constructor(options = {}) {
|
|
41
|
+
this.options = {
|
|
42
|
+
level: options.level || 'info',
|
|
43
|
+
pretty: options.pretty ?? false,
|
|
44
|
+
timestamps: options.timestamps ?? true,
|
|
45
|
+
handler: options.handler || this.defaultHandler.bind(this),
|
|
46
|
+
prefix: options.prefix || 'EvalAI'
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Log a trace message
|
|
51
|
+
*/
|
|
52
|
+
trace(message, data) {
|
|
53
|
+
this.log('trace', message, data);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Log a debug message
|
|
57
|
+
*/
|
|
58
|
+
debug(message, data) {
|
|
59
|
+
this.log('debug', message, data);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Log an info message
|
|
63
|
+
*/
|
|
64
|
+
info(message, data) {
|
|
65
|
+
this.log('info', message, data);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Log a warning message
|
|
69
|
+
*/
|
|
70
|
+
warn(message, data) {
|
|
71
|
+
this.log('warn', message, data);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Log an error message
|
|
75
|
+
*/
|
|
76
|
+
error(message, data) {
|
|
77
|
+
this.log('error', message, data);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Log HTTP request
|
|
81
|
+
*/
|
|
82
|
+
logRequest(method, url, data) {
|
|
83
|
+
this.debug(`→ ${method} ${url}`, data);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Log HTTP response
|
|
87
|
+
*/
|
|
88
|
+
logResponse(method, url, status, duration, data) {
|
|
89
|
+
const level = status >= 400 ? 'error' : status >= 300 ? 'warn' : 'debug';
|
|
90
|
+
this.log(level, `← ${method} ${url} ${status} (${duration}ms)`, data);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create child logger with prefix
|
|
94
|
+
*/
|
|
95
|
+
child(prefix) {
|
|
96
|
+
return new Logger({
|
|
97
|
+
...this.options,
|
|
98
|
+
prefix: `${this.options.prefix}:${prefix}`
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Set log level
|
|
103
|
+
*/
|
|
104
|
+
setLevel(level) {
|
|
105
|
+
this.options.level = level;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Check if level is enabled
|
|
109
|
+
*/
|
|
110
|
+
isLevelEnabled(level) {
|
|
111
|
+
return LOG_LEVELS[level] >= LOG_LEVELS[this.options.level];
|
|
112
|
+
}
|
|
113
|
+
log(level, message, data) {
|
|
114
|
+
if (!this.isLevelEnabled(level))
|
|
115
|
+
return;
|
|
116
|
+
const entry = {
|
|
117
|
+
level,
|
|
118
|
+
message,
|
|
119
|
+
timestamp: new Date().toISOString(),
|
|
120
|
+
data,
|
|
121
|
+
prefix: this.options.prefix
|
|
122
|
+
};
|
|
123
|
+
this.options.handler(entry);
|
|
124
|
+
}
|
|
125
|
+
defaultHandler(entry) {
|
|
126
|
+
const parts = [];
|
|
127
|
+
// Timestamp
|
|
128
|
+
if (this.options.timestamps) {
|
|
129
|
+
const time = this.options.pretty
|
|
130
|
+
? new Date(entry.timestamp).toLocaleTimeString()
|
|
131
|
+
: entry.timestamp;
|
|
132
|
+
parts.push(this.options.pretty ? `\x1b[90m${time}${COLOR_RESET}` : time);
|
|
133
|
+
}
|
|
134
|
+
// Level
|
|
135
|
+
const levelStr = entry.level.toUpperCase().padEnd(5);
|
|
136
|
+
parts.push(this.options.pretty
|
|
137
|
+
? `${LOG_COLORS[entry.level]}${levelStr}${COLOR_RESET}`
|
|
138
|
+
: levelStr);
|
|
139
|
+
// Prefix
|
|
140
|
+
if (entry.prefix) {
|
|
141
|
+
parts.push(this.options.pretty
|
|
142
|
+
? `\x1b[35m[${entry.prefix}]${COLOR_RESET}`
|
|
143
|
+
: `[${entry.prefix}]`);
|
|
144
|
+
}
|
|
145
|
+
// Message
|
|
146
|
+
parts.push(entry.message);
|
|
147
|
+
// Log
|
|
148
|
+
const logLine = parts.join(' ');
|
|
149
|
+
if (entry.level === 'error') {
|
|
150
|
+
console.error(logLine);
|
|
151
|
+
}
|
|
152
|
+
else if (entry.level === 'warn') {
|
|
153
|
+
console.warn(logLine);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
console.log(logLine);
|
|
157
|
+
}
|
|
158
|
+
// Data
|
|
159
|
+
if (entry.data !== undefined) {
|
|
160
|
+
if (this.options.pretty) {
|
|
161
|
+
console.log(JSON.stringify(entry.data, null, 2));
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
console.log(JSON.stringify(entry.data));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
exports.Logger = Logger;
|
|
170
|
+
/**
|
|
171
|
+
* Create a logger instance
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const logger = createLogger({ level: 'debug', pretty: true });
|
|
176
|
+
* logger.debug('Starting evaluation');
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
function createLogger(options) {
|
|
180
|
+
return new Logger(options);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Global logger instance
|
|
184
|
+
*/
|
|
185
|
+
let globalLogger;
|
|
186
|
+
/**
|
|
187
|
+
* Get global logger (creates one if it doesn't exist)
|
|
188
|
+
*/
|
|
189
|
+
function getLogger() {
|
|
190
|
+
if (!globalLogger) {
|
|
191
|
+
globalLogger = new Logger();
|
|
192
|
+
}
|
|
193
|
+
return globalLogger;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Set global logger
|
|
197
|
+
*/
|
|
198
|
+
function setLogger(logger) {
|
|
199
|
+
globalLogger = logger;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Request/Response interceptor for logging
|
|
203
|
+
*/
|
|
204
|
+
class RequestLogger {
|
|
205
|
+
constructor(logger) {
|
|
206
|
+
this.logger = logger;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Log request before sending
|
|
210
|
+
*/
|
|
211
|
+
logRequest(request) {
|
|
212
|
+
this.logger.logRequest(request.method, request.url, {
|
|
213
|
+
headers: request.headers,
|
|
214
|
+
body: request.body
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Log response after receiving
|
|
219
|
+
*/
|
|
220
|
+
logResponse(response) {
|
|
221
|
+
this.logger.logResponse(response.method, response.url, response.status, response.duration, {
|
|
222
|
+
headers: response.headers,
|
|
223
|
+
body: response.body
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
exports.RequestLogger = RequestLogger;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor-based pagination utilities for efficient data fetching
|
|
3
|
+
*/
|
|
4
|
+
export interface PaginationParams {
|
|
5
|
+
limit?: number;
|
|
6
|
+
cursor?: string;
|
|
7
|
+
offset?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface PaginatedResponse<T> {
|
|
10
|
+
data: T[];
|
|
11
|
+
pagination: {
|
|
12
|
+
hasMore: boolean;
|
|
13
|
+
nextCursor?: string;
|
|
14
|
+
prevCursor?: string;
|
|
15
|
+
total?: number;
|
|
16
|
+
limit: number;
|
|
17
|
+
offset?: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface PaginationOptions {
|
|
21
|
+
limit?: number;
|
|
22
|
+
offset?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Iterator for paginated results
|
|
26
|
+
* Allows for easy iteration over all pages
|
|
27
|
+
*/
|
|
28
|
+
export declare class PaginatedIterator<T> implements AsyncIterableIterator<T[]> {
|
|
29
|
+
private fetchFn;
|
|
30
|
+
private limit;
|
|
31
|
+
private currentOffset;
|
|
32
|
+
private hasMore;
|
|
33
|
+
constructor(fetchFn: (offset: number, limit: number) => Promise<{
|
|
34
|
+
data: T[];
|
|
35
|
+
hasMore: boolean;
|
|
36
|
+
}>, limit?: number);
|
|
37
|
+
next(): Promise<IteratorResult<T[]>>;
|
|
38
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T[]>;
|
|
39
|
+
/**
|
|
40
|
+
* Collect all pages into a single array
|
|
41
|
+
* Warning: Use with caution on large datasets
|
|
42
|
+
*/
|
|
43
|
+
toArray(): Promise<T[]>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Helper to create paginated iterator
|
|
47
|
+
*/
|
|
48
|
+
export declare function createPaginatedIterator<T>(fetchFn: (offset: number, limit: number) => Promise<{
|
|
49
|
+
data: T[];
|
|
50
|
+
hasMore: boolean;
|
|
51
|
+
}>, limit?: number): PaginatedIterator<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Auto-paginate helper that fetches all pages automatically
|
|
54
|
+
*/
|
|
55
|
+
export declare function autoPaginate<T>(fetchFn: (offset: number, limit: number) => Promise<T[]>, limit?: number): AsyncGenerator<T, void, unknown>;
|
|
56
|
+
/**
|
|
57
|
+
* Encode cursor for pagination (base64)
|
|
58
|
+
*/
|
|
59
|
+
export declare function encodeCursor(data: any): string;
|
|
60
|
+
/**
|
|
61
|
+
* Decode cursor from base64
|
|
62
|
+
*/
|
|
63
|
+
export declare function decodeCursor(cursor: string): any;
|
|
64
|
+
/**
|
|
65
|
+
* Create pagination metadata from response
|
|
66
|
+
*/
|
|
67
|
+
export declare function createPaginationMeta<T>(items: T[], limit: number, offset: number, total?: number): PaginatedResponse<T>['pagination'];
|
|
68
|
+
/**
|
|
69
|
+
* Parse pagination params from cursor or offset
|
|
70
|
+
*/
|
|
71
|
+
export declare function parsePaginationParams(params: PaginationParams): {
|
|
72
|
+
limit: number;
|
|
73
|
+
offset: number;
|
|
74
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Cursor-based pagination utilities for efficient data fetching
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PaginatedIterator = void 0;
|
|
7
|
+
exports.createPaginatedIterator = createPaginatedIterator;
|
|
8
|
+
exports.autoPaginate = autoPaginate;
|
|
9
|
+
exports.encodeCursor = encodeCursor;
|
|
10
|
+
exports.decodeCursor = decodeCursor;
|
|
11
|
+
exports.createPaginationMeta = createPaginationMeta;
|
|
12
|
+
exports.parsePaginationParams = parsePaginationParams;
|
|
13
|
+
/**
|
|
14
|
+
* Iterator for paginated results
|
|
15
|
+
* Allows for easy iteration over all pages
|
|
16
|
+
*/
|
|
17
|
+
class PaginatedIterator {
|
|
18
|
+
constructor(fetchFn, limit = 50) {
|
|
19
|
+
this.fetchFn = fetchFn;
|
|
20
|
+
this.limit = limit;
|
|
21
|
+
this.currentOffset = 0;
|
|
22
|
+
this.hasMore = true;
|
|
23
|
+
}
|
|
24
|
+
async next() {
|
|
25
|
+
if (!this.hasMore) {
|
|
26
|
+
return { done: true, value: undefined };
|
|
27
|
+
}
|
|
28
|
+
const result = await this.fetchFn(this.currentOffset, this.limit);
|
|
29
|
+
this.hasMore = result.hasMore;
|
|
30
|
+
this.currentOffset += this.limit;
|
|
31
|
+
return {
|
|
32
|
+
done: false,
|
|
33
|
+
value: result.data,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
[Symbol.asyncIterator]() {
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Collect all pages into a single array
|
|
41
|
+
* Warning: Use with caution on large datasets
|
|
42
|
+
*/
|
|
43
|
+
async toArray() {
|
|
44
|
+
const allItems = [];
|
|
45
|
+
for await (const page of this) {
|
|
46
|
+
allItems.push(...page);
|
|
47
|
+
}
|
|
48
|
+
return allItems;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.PaginatedIterator = PaginatedIterator;
|
|
52
|
+
/**
|
|
53
|
+
* Helper to create paginated iterator
|
|
54
|
+
*/
|
|
55
|
+
function createPaginatedIterator(fetchFn, limit = 50) {
|
|
56
|
+
return new PaginatedIterator(fetchFn, limit);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Auto-paginate helper that fetches all pages automatically
|
|
60
|
+
*/
|
|
61
|
+
async function* autoPaginate(fetchFn, limit = 50) {
|
|
62
|
+
let offset = 0;
|
|
63
|
+
let hasMore = true;
|
|
64
|
+
while (hasMore) {
|
|
65
|
+
const items = await fetchFn(offset, limit);
|
|
66
|
+
if (items.length === 0) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
for (const item of items) {
|
|
70
|
+
yield item;
|
|
71
|
+
}
|
|
72
|
+
hasMore = items.length === limit;
|
|
73
|
+
offset += limit;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Encode cursor for pagination (base64)
|
|
78
|
+
*/
|
|
79
|
+
function encodeCursor(data) {
|
|
80
|
+
const json = JSON.stringify(data);
|
|
81
|
+
if (typeof globalThis !== 'undefined' && 'btoa' in globalThis) {
|
|
82
|
+
return globalThis.btoa(json);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return Buffer.from(json).toString('base64');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Decode cursor from base64
|
|
90
|
+
*/
|
|
91
|
+
function decodeCursor(cursor) {
|
|
92
|
+
try {
|
|
93
|
+
let json;
|
|
94
|
+
if (typeof globalThis !== 'undefined' && 'atob' in globalThis) {
|
|
95
|
+
json = globalThis.atob(cursor);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
json = Buffer.from(cursor, 'base64').toString('utf-8');
|
|
99
|
+
}
|
|
100
|
+
return JSON.parse(json);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
throw new Error('Invalid cursor format');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create pagination metadata from response
|
|
108
|
+
*/
|
|
109
|
+
function createPaginationMeta(items, limit, offset, total) {
|
|
110
|
+
const hasMore = items.length === limit;
|
|
111
|
+
return {
|
|
112
|
+
hasMore,
|
|
113
|
+
limit,
|
|
114
|
+
offset,
|
|
115
|
+
total,
|
|
116
|
+
nextCursor: hasMore ? encodeCursor({ offset: offset + limit, limit }) : undefined,
|
|
117
|
+
prevCursor: offset > 0 ? encodeCursor({ offset: Math.max(0, offset - limit), limit }) : undefined,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Parse pagination params from cursor or offset
|
|
122
|
+
*/
|
|
123
|
+
function parsePaginationParams(params) {
|
|
124
|
+
if (params.cursor) {
|
|
125
|
+
const decoded = decodeCursor(params.cursor);
|
|
126
|
+
return {
|
|
127
|
+
limit: decoded.limit || 50,
|
|
128
|
+
offset: decoded.offset || 0,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
limit: params.limit || 50,
|
|
133
|
+
offset: params.offset || 0,
|
|
134
|
+
};
|
|
135
|
+
}
|