@nexus-ai-fs/api-client 0.9.18
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/README.md +12 -0
- package/dist/index.cjs +769 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +352 -0
- package/dist/index.d.ts +352 -0
- package/dist/index.js +749 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error hierarchy for all Nexus API interactions.
|
|
3
|
+
*
|
|
4
|
+
* HTTP-level errors live here; domain-specific errors (e.g. InsufficientCreditsError)
|
|
5
|
+
* belong in consumer packages that extend NexusApiError.
|
|
6
|
+
*
|
|
7
|
+
* Hierarchy:
|
|
8
|
+
* NexusApiError (base)
|
|
9
|
+
* ├── AuthenticationError (401)
|
|
10
|
+
* ├── ForbiddenError (403)
|
|
11
|
+
* ├── NotFoundError (404)
|
|
12
|
+
* ├── ConflictError (409)
|
|
13
|
+
* ├── RateLimitError (429)
|
|
14
|
+
* ├── ServerError (5xx)
|
|
15
|
+
* ├── NetworkError (fetch failed)
|
|
16
|
+
* ├── TimeoutError (request timed out)
|
|
17
|
+
* └── AbortError (user-cancelled)
|
|
18
|
+
*/
|
|
19
|
+
declare class NexusApiError extends Error {
|
|
20
|
+
readonly status: number;
|
|
21
|
+
readonly code: string;
|
|
22
|
+
constructor(message: string, status: number, code: string);
|
|
23
|
+
}
|
|
24
|
+
declare class AuthenticationError extends NexusApiError {
|
|
25
|
+
constructor(message: string);
|
|
26
|
+
}
|
|
27
|
+
declare class ForbiddenError extends NexusApiError {
|
|
28
|
+
constructor(message: string);
|
|
29
|
+
}
|
|
30
|
+
declare class NotFoundError extends NexusApiError {
|
|
31
|
+
constructor(message: string);
|
|
32
|
+
}
|
|
33
|
+
declare class ConflictError extends NexusApiError {
|
|
34
|
+
constructor(message: string);
|
|
35
|
+
}
|
|
36
|
+
declare class RateLimitError extends NexusApiError {
|
|
37
|
+
readonly retryAfter: number | undefined;
|
|
38
|
+
constructor(message: string, retryAfter?: number);
|
|
39
|
+
}
|
|
40
|
+
declare class ServerError extends NexusApiError {
|
|
41
|
+
constructor(message: string, status: number);
|
|
42
|
+
}
|
|
43
|
+
declare class NetworkError extends NexusApiError {
|
|
44
|
+
constructor(message: string);
|
|
45
|
+
}
|
|
46
|
+
declare class TimeoutError extends NexusApiError {
|
|
47
|
+
constructor(message: string);
|
|
48
|
+
}
|
|
49
|
+
declare class AbortError extends NexusApiError {
|
|
50
|
+
constructor(message: string);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Shared types for the Nexus API client.
|
|
55
|
+
*/
|
|
56
|
+
interface NexusClientOptions {
|
|
57
|
+
/** API key (e.g. `nx_live_<id>`, `nx_test_<id>`, or any bearer token). */
|
|
58
|
+
readonly apiKey: string;
|
|
59
|
+
/** Base URL of the Nexus API server. Default: "http://localhost:2026" */
|
|
60
|
+
readonly baseUrl?: string;
|
|
61
|
+
/** Request timeout in milliseconds. Default: 30000 */
|
|
62
|
+
readonly timeout?: number;
|
|
63
|
+
/** Maximum retries for retryable errors. Default: 3. Set to 0 to disable. */
|
|
64
|
+
readonly maxRetries?: number;
|
|
65
|
+
/** Custom fetch implementation for testing or proxying. */
|
|
66
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
67
|
+
/** Disable automatic snake_case → camelCase key transformation. Default: true */
|
|
68
|
+
readonly transformKeys?: boolean;
|
|
69
|
+
/** Agent identity sent as X-Agent-ID header. */
|
|
70
|
+
readonly agentId?: string;
|
|
71
|
+
/** Subject identity sent as X-Nexus-Subject header. */
|
|
72
|
+
readonly subject?: string;
|
|
73
|
+
/** Zone ID sent as X-Nexus-Zone-ID header. */
|
|
74
|
+
readonly zoneId?: string;
|
|
75
|
+
}
|
|
76
|
+
interface RequestOptions {
|
|
77
|
+
/** Per-request timeout override in milliseconds. */
|
|
78
|
+
readonly timeout?: number;
|
|
79
|
+
/** AbortSignal for cancellation. */
|
|
80
|
+
readonly signal?: AbortSignal;
|
|
81
|
+
/** Idempotency key for retry-safe operations. */
|
|
82
|
+
readonly idempotencyKey?: string;
|
|
83
|
+
/** Extra headers merged with defaults. */
|
|
84
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
85
|
+
}
|
|
86
|
+
interface ApiErrorResponse {
|
|
87
|
+
readonly detail: string;
|
|
88
|
+
readonly error_code?: string;
|
|
89
|
+
}
|
|
90
|
+
interface PaginatedResponse<T> {
|
|
91
|
+
readonly items: readonly T[];
|
|
92
|
+
readonly total: number;
|
|
93
|
+
readonly page: number;
|
|
94
|
+
readonly limit: number;
|
|
95
|
+
}
|
|
96
|
+
interface SseEvent {
|
|
97
|
+
readonly id?: string;
|
|
98
|
+
readonly event: string;
|
|
99
|
+
readonly data: string;
|
|
100
|
+
readonly retry?: number;
|
|
101
|
+
}
|
|
102
|
+
interface AspectEnvelope {
|
|
103
|
+
readonly entityUrn: string;
|
|
104
|
+
readonly aspectName: string;
|
|
105
|
+
readonly version: number;
|
|
106
|
+
readonly payload: Record<string, unknown>;
|
|
107
|
+
readonly createdBy: string;
|
|
108
|
+
readonly createdAt: string | null;
|
|
109
|
+
}
|
|
110
|
+
interface AspectListResponse {
|
|
111
|
+
readonly entityUrn: string;
|
|
112
|
+
readonly aspects: readonly string[];
|
|
113
|
+
}
|
|
114
|
+
interface DatasetSchema {
|
|
115
|
+
readonly columns: readonly {
|
|
116
|
+
name: string;
|
|
117
|
+
type: string;
|
|
118
|
+
nullable: string;
|
|
119
|
+
}[];
|
|
120
|
+
readonly format: string;
|
|
121
|
+
readonly rowCount: number | null;
|
|
122
|
+
readonly confidence: number;
|
|
123
|
+
readonly warnings: readonly string[];
|
|
124
|
+
}
|
|
125
|
+
interface CatalogSchemaResponse {
|
|
126
|
+
readonly entityUrn: string;
|
|
127
|
+
readonly path: string;
|
|
128
|
+
readonly schema: DatasetSchema | null;
|
|
129
|
+
}
|
|
130
|
+
interface ColumnSearchResult {
|
|
131
|
+
readonly entityUrn: string;
|
|
132
|
+
readonly columnName: string;
|
|
133
|
+
readonly columnType: string;
|
|
134
|
+
readonly schema: Record<string, unknown>;
|
|
135
|
+
}
|
|
136
|
+
interface ColumnSearchResponse {
|
|
137
|
+
readonly results: readonly ColumnSearchResult[];
|
|
138
|
+
readonly total: number;
|
|
139
|
+
readonly capped: boolean;
|
|
140
|
+
}
|
|
141
|
+
interface ReplayRecord {
|
|
142
|
+
readonly sequenceNumber: number;
|
|
143
|
+
readonly entityUrn: string;
|
|
144
|
+
readonly aspectName: string;
|
|
145
|
+
readonly changeType: string;
|
|
146
|
+
readonly timestamp: string;
|
|
147
|
+
readonly operationType: string;
|
|
148
|
+
}
|
|
149
|
+
interface ReplayResponse {
|
|
150
|
+
readonly records: readonly ReplayRecord[];
|
|
151
|
+
readonly nextCursor: number | null;
|
|
152
|
+
readonly hasMore: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Core HTTP client with retry, timeout, auth, error mapping,
|
|
157
|
+
* and automatic snake_case ↔ camelCase key transformation.
|
|
158
|
+
*
|
|
159
|
+
* Generalized from nexus-pay-ts's FetchClient. Consumer packages
|
|
160
|
+
* can subclass and override `buildError()` for domain-specific error mapping.
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
declare class FetchClient {
|
|
164
|
+
private readonly apiKey;
|
|
165
|
+
private readonly baseUrl;
|
|
166
|
+
private readonly timeout;
|
|
167
|
+
private readonly maxRetries;
|
|
168
|
+
private readonly fetchFn;
|
|
169
|
+
private readonly transformEnabled;
|
|
170
|
+
private readonly agentId;
|
|
171
|
+
private readonly subject;
|
|
172
|
+
private readonly zoneId;
|
|
173
|
+
constructor(options: NexusClientOptions);
|
|
174
|
+
get<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
175
|
+
post<T>(path: string, body: unknown, options?: RequestOptions): Promise<T>;
|
|
176
|
+
put<T>(path: string, body: unknown, options?: RequestOptions): Promise<T>;
|
|
177
|
+
patch<T>(path: string, body: unknown, options?: RequestOptions): Promise<T>;
|
|
178
|
+
delete<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
179
|
+
postNoContent(path: string, body?: unknown, options?: RequestOptions): Promise<void>;
|
|
180
|
+
deleteNoContent(path: string, options?: RequestOptions): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* Execute an arbitrary HTTP request through the authenticated client.
|
|
183
|
+
*
|
|
184
|
+
* Returns the raw `Response` — no JSON parsing, no key transformation,
|
|
185
|
+
* no retries. Auth and identity headers are injected automatically.
|
|
186
|
+
* Timeout is enforced (defaults to client timeout, overridable per-request).
|
|
187
|
+
*
|
|
188
|
+
* Body is sent as-is (no JSON.stringify) since callers provide pre-formatted strings.
|
|
189
|
+
*
|
|
190
|
+
* Intended for the API Console and similar exploratory tools that need
|
|
191
|
+
* full control over the request/response while still using real auth.
|
|
192
|
+
*/
|
|
193
|
+
rawRequest(method: string, path: string, body?: string, options?: RequestOptions): Promise<Response>;
|
|
194
|
+
private request;
|
|
195
|
+
private requestRaw;
|
|
196
|
+
private executeFetch;
|
|
197
|
+
private buildHeaders;
|
|
198
|
+
/**
|
|
199
|
+
* Map HTTP response to a typed error.
|
|
200
|
+
*
|
|
201
|
+
* Subclasses can override this to add domain-specific error mapping
|
|
202
|
+
* (e.g. 402 → InsufficientCreditsError in nexus-pay-ts).
|
|
203
|
+
*/
|
|
204
|
+
protected buildError(response: Response): Promise<NexusApiError>;
|
|
205
|
+
private computeRetryDelay;
|
|
206
|
+
/**
|
|
207
|
+
* List all aspect names attached to an entity.
|
|
208
|
+
*/
|
|
209
|
+
getAspects(urn: string): Promise<string[]>;
|
|
210
|
+
/**
|
|
211
|
+
* Get a specific aspect for an entity. Returns null if not found.
|
|
212
|
+
*/
|
|
213
|
+
getAspect(urn: string, name: string): Promise<AspectEnvelope | null>;
|
|
214
|
+
/**
|
|
215
|
+
* Get extracted schema for a data file. Returns null if no schema.
|
|
216
|
+
*/
|
|
217
|
+
getCatalogSchema(path: string): Promise<CatalogSchemaResponse["schema"]>;
|
|
218
|
+
/**
|
|
219
|
+
* Search for data files containing a specific column name.
|
|
220
|
+
*/
|
|
221
|
+
searchByColumn(column: string): Promise<ColumnSearchResponse["results"]>;
|
|
222
|
+
/**
|
|
223
|
+
* Replay metadata change log records.
|
|
224
|
+
*/
|
|
225
|
+
replayChanges(opts?: {
|
|
226
|
+
fromSequence?: number;
|
|
227
|
+
entityUrn?: string;
|
|
228
|
+
limit?: number;
|
|
229
|
+
}): Promise<ReplayResponse>;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* SSE connection manager with ring buffer, throttled flush,
|
|
234
|
+
* and exponential backoff reconnection.
|
|
235
|
+
*/
|
|
236
|
+
|
|
237
|
+
/** Fixed-size circular buffer that drops oldest entries when full. */
|
|
238
|
+
declare class RingBuffer<T> {
|
|
239
|
+
readonly capacity: number;
|
|
240
|
+
private readonly buffer;
|
|
241
|
+
private head;
|
|
242
|
+
private count;
|
|
243
|
+
/** Monotonically increasing counter of total items ever pushed (never wraps). */
|
|
244
|
+
private _totalPushed;
|
|
245
|
+
constructor(capacity: number);
|
|
246
|
+
get size(): number;
|
|
247
|
+
/** Total number of items pushed since creation/clear (never decreases). */
|
|
248
|
+
get totalPushed(): number;
|
|
249
|
+
push(item: T): void;
|
|
250
|
+
/** Return items in insertion order (oldest first). */
|
|
251
|
+
toArray(): readonly T[];
|
|
252
|
+
/** Return the last N items (newest first becomes oldest first). */
|
|
253
|
+
lastN(n: number): readonly T[];
|
|
254
|
+
clear(): void;
|
|
255
|
+
}
|
|
256
|
+
interface SseClientOptions {
|
|
257
|
+
readonly baseUrl: string;
|
|
258
|
+
readonly apiKey: string;
|
|
259
|
+
readonly bufferCapacity?: number;
|
|
260
|
+
readonly flushIntervalMs?: number;
|
|
261
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
262
|
+
/** Agent identity sent as X-Agent-ID header. */
|
|
263
|
+
readonly agentId?: string;
|
|
264
|
+
/** Subject identity sent as X-Nexus-Subject header. */
|
|
265
|
+
readonly subject?: string;
|
|
266
|
+
/** Zone ID sent as X-Nexus-Zone-ID header. */
|
|
267
|
+
readonly zoneId?: string;
|
|
268
|
+
}
|
|
269
|
+
type SseEventHandler = (events: readonly SseEvent[]) => void;
|
|
270
|
+
type SseErrorHandler = (error: Error) => void;
|
|
271
|
+
type SseReconnectHandler = (attempt: number) => void;
|
|
272
|
+
declare class SseClient {
|
|
273
|
+
private readonly baseUrl;
|
|
274
|
+
private readonly apiKey;
|
|
275
|
+
private readonly fetchFn;
|
|
276
|
+
private readonly buffer;
|
|
277
|
+
private readonly flushIntervalMs;
|
|
278
|
+
private readonly agentId;
|
|
279
|
+
private readonly subject;
|
|
280
|
+
private readonly zoneId;
|
|
281
|
+
private abortController;
|
|
282
|
+
private flushTimer;
|
|
283
|
+
private reconnectAttempt;
|
|
284
|
+
private lastEventId;
|
|
285
|
+
private connected;
|
|
286
|
+
private eventHandler;
|
|
287
|
+
private errorHandler;
|
|
288
|
+
private reconnectHandler;
|
|
289
|
+
constructor(options: SseClientOptions);
|
|
290
|
+
onEvent(handler: SseEventHandler): void;
|
|
291
|
+
onError(handler: SseErrorHandler): void;
|
|
292
|
+
onReconnect(handler: SseReconnectHandler): void;
|
|
293
|
+
get isConnected(): boolean;
|
|
294
|
+
connect(path: string): Promise<void>;
|
|
295
|
+
disconnect(): void;
|
|
296
|
+
getBufferedEvents(): readonly SseEvent[];
|
|
297
|
+
clearBuffer(): void;
|
|
298
|
+
private connectWithRetry;
|
|
299
|
+
private streamEvents;
|
|
300
|
+
private parseEvents;
|
|
301
|
+
/** Tracks total events flushed via the monotonic totalPushed counter. */
|
|
302
|
+
private lastFlushedTotal;
|
|
303
|
+
private startFlushTimer;
|
|
304
|
+
private stopFlushTimer;
|
|
305
|
+
private computeReconnectDelay;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Config resolution from multiple sources with precedence:
|
|
310
|
+
*
|
|
311
|
+
* 1. Explicit overrides (constructor args / CLI flags)
|
|
312
|
+
* 2. Config file (./nexus.yaml → ./nexus.yml → ~/.nexus/config.yaml)
|
|
313
|
+
* 3. Environment variables (NEXUS_URL, NEXUS_API_KEY, etc.)
|
|
314
|
+
* 4. Defaults
|
|
315
|
+
*
|
|
316
|
+
* Matches the Python CLI (`config.py:_auto_discover`): if a config file
|
|
317
|
+
* exists, its values take priority over environment variables.
|
|
318
|
+
*/
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Resolve Nexus client config from multiple sources.
|
|
322
|
+
*
|
|
323
|
+
* Works in Node, Bun, and Deno. Gracefully degrades in browsers
|
|
324
|
+
* (no env vars or filesystem — uses defaults + overrides only).
|
|
325
|
+
*/
|
|
326
|
+
declare function resolveConfig(overrides?: Partial<NexusClientOptions>): NexusClientOptions;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Pure utility functions for snake_case ↔ camelCase key transformation.
|
|
330
|
+
*
|
|
331
|
+
* Handles nested objects, arrays, null, and preserves non-plain values (Date, etc.).
|
|
332
|
+
*/
|
|
333
|
+
/** Convert a single snake_case string to camelCase. */
|
|
334
|
+
declare function snakeToCamel(str: string): string;
|
|
335
|
+
/** Convert a single camelCase string to snake_case. */
|
|
336
|
+
declare function camelToSnake(str: string): string;
|
|
337
|
+
/**
|
|
338
|
+
* Recursively transform all keys in an object/array using the given transformer.
|
|
339
|
+
*
|
|
340
|
+
* - Arrays: each element is recursively transformed
|
|
341
|
+
* - null/undefined: returned as-is
|
|
342
|
+
* - Primitives (string, number, boolean): returned as-is
|
|
343
|
+
* - Date: returned as-is (not a plain object)
|
|
344
|
+
* - Plain objects: keys are transformed, values are recursively transformed
|
|
345
|
+
*/
|
|
346
|
+
declare function transformKeys<T>(value: unknown, transformer: (key: string) => string): T;
|
|
347
|
+
/** Transform all keys from snake_case to camelCase. */
|
|
348
|
+
declare function snakeToCamelKeys<T>(value: unknown): T;
|
|
349
|
+
/** Transform all keys from camelCase to snake_case. */
|
|
350
|
+
declare function camelToSnakeKeys<T>(value: unknown): T;
|
|
351
|
+
|
|
352
|
+
export { AbortError, type ApiErrorResponse, type AspectEnvelope, type AspectListResponse, AuthenticationError, type CatalogSchemaResponse, type ColumnSearchResponse, type ColumnSearchResult, ConflictError, type DatasetSchema, FetchClient, ForbiddenError, NetworkError, NexusApiError, type NexusClientOptions, NotFoundError, type PaginatedResponse, RateLimitError, type ReplayRecord, type ReplayResponse, type RequestOptions, RingBuffer, ServerError, SseClient, type SseClientOptions, type SseErrorHandler, type SseEvent, type SseEventHandler, type SseReconnectHandler, TimeoutError, camelToSnake, camelToSnakeKeys, resolveConfig, snakeToCamel, snakeToCamelKeys, transformKeys };
|