@oxy-hq/sdk 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/dist/index.cjs ADDED
@@ -0,0 +1,482 @@
1
+ // @oxy/sdk - TypeScript SDK for Oxy data platform
2
+ //#region rolldown:runtime
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+
28
+ //#endregion
29
+ let _duckdb_duckdb_wasm = require("@duckdb/duckdb-wasm");
30
+ _duckdb_duckdb_wasm = __toESM(_duckdb_duckdb_wasm);
31
+
32
+ //#region src/parquet.ts
33
+ let dbInstance = null;
34
+ let connection = null;
35
+ /**
36
+ * Initialize DuckDB-WASM instance
37
+ */
38
+ async function initializeDuckDB() {
39
+ if (dbInstance) return dbInstance;
40
+ const JSDELIVR_BUNDLES = _duckdb_duckdb_wasm.getJsDelivrBundles();
41
+ const bundle = await _duckdb_duckdb_wasm.selectBundle(JSDELIVR_BUNDLES);
42
+ const worker_url = URL.createObjectURL(new Blob([`importScripts("${bundle.mainWorker}");`], { type: "text/javascript" }));
43
+ const worker = new Worker(worker_url);
44
+ const logger = new _duckdb_duckdb_wasm.ConsoleLogger();
45
+ dbInstance = new _duckdb_duckdb_wasm.AsyncDuckDB(logger, worker);
46
+ await dbInstance.instantiate(bundle.mainModule, bundle.pthreadWorker);
47
+ URL.revokeObjectURL(worker_url);
48
+ return dbInstance;
49
+ }
50
+ /**
51
+ * Get or create a DuckDB connection
52
+ */
53
+ async function getConnection() {
54
+ if (connection) return connection;
55
+ connection = await (await initializeDuckDB()).connect();
56
+ return connection;
57
+ }
58
+ /**
59
+ * ParquetReader provides methods to read and query Parquet files
60
+ */
61
+ var ParquetReader = class {
62
+ constructor(tableName = "data") {
63
+ this.registered = false;
64
+ this.tableName = tableName;
65
+ }
66
+ /**
67
+ * Register a Parquet file from a Blob
68
+ *
69
+ * @param blob - Parquet file as Blob
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const blob = await client.getFile('data/sales.parquet');
74
+ * const reader = new ParquetReader('sales');
75
+ * await reader.registerParquet(blob);
76
+ * ```
77
+ */
78
+ async registerParquet(blob) {
79
+ const conn = await getConnection();
80
+ const db = await initializeDuckDB();
81
+ const arrayBuffer = await blob.arrayBuffer();
82
+ const uint8Array = new Uint8Array(arrayBuffer);
83
+ await db.registerFileBuffer(`${this.tableName}.parquet`, uint8Array);
84
+ try {
85
+ await conn.query(`DROP TABLE IF EXISTS ${this.tableName}`);
86
+ } catch (e) {}
87
+ await conn.query(`CREATE TABLE ${this.tableName} AS SELECT * FROM '${this.tableName}.parquet'`);
88
+ this.registered = true;
89
+ }
90
+ /**
91
+ * Execute a SQL query against the registered Parquet data
92
+ *
93
+ * @param sql - SQL query string
94
+ * @returns Query result with columns and rows
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const result = await reader.query('SELECT * FROM sales LIMIT 10');
99
+ * console.log(result.columns);
100
+ * console.log(result.rows);
101
+ * ```
102
+ */
103
+ async query(sql) {
104
+ if (!this.registered) throw new Error("Parquet file not registered. Call registerParquet() first.");
105
+ const result = await (await getConnection()).query(sql);
106
+ const columns = result.schema.fields.map((field) => field.name);
107
+ const rows = [];
108
+ for (let i = 0; i < result.numRows; i++) {
109
+ const row = [];
110
+ for (let j = 0; j < result.numCols; j++) {
111
+ const col = result.getChildAt(j);
112
+ row.push(col?.get(i));
113
+ }
114
+ rows.push(row);
115
+ }
116
+ return {
117
+ columns,
118
+ rows,
119
+ rowCount: result.numRows
120
+ };
121
+ }
122
+ /**
123
+ * Get all data from the registered table
124
+ *
125
+ * @param limit - Maximum number of rows to return (default: all)
126
+ * @returns Query result
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const allData = await reader.getAll();
131
+ * const first100 = await reader.getAll(100);
132
+ * ```
133
+ */
134
+ async getAll(limit) {
135
+ const limitClause = limit ? ` LIMIT ${limit}` : "";
136
+ return this.query(`SELECT * FROM ${this.tableName}${limitClause}`);
137
+ }
138
+ /**
139
+ * Get table schema information
140
+ *
141
+ * @returns Schema information
142
+ *
143
+ * @example
144
+ * ```typescript
145
+ * const schema = await reader.getSchema();
146
+ * console.log(schema.columns); // ['id', 'name', 'sales']
147
+ * console.log(schema.rows); // [['id', 'INTEGER'], ['name', 'VARCHAR'], ...]
148
+ * ```
149
+ */
150
+ async getSchema() {
151
+ return this.query(`DESCRIBE ${this.tableName}`);
152
+ }
153
+ /**
154
+ * Get row count
155
+ *
156
+ * @returns Number of rows in the table
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * const count = await reader.count();
161
+ * console.log(`Total rows: ${count}`);
162
+ * ```
163
+ */
164
+ async count() {
165
+ return (await this.query(`SELECT COUNT(*) as count FROM ${this.tableName}`)).rows[0][0];
166
+ }
167
+ /**
168
+ * Close and cleanup resources
169
+ */
170
+ async close() {
171
+ if (this.registered) {
172
+ const conn = await getConnection();
173
+ const db = await initializeDuckDB();
174
+ try {
175
+ await conn.query(`DROP TABLE IF EXISTS ${this.tableName}`);
176
+ } catch (e) {}
177
+ try {
178
+ await db.dropFile(`${this.tableName}.parquet`);
179
+ } catch (e) {}
180
+ this.registered = false;
181
+ }
182
+ }
183
+ };
184
+ /**
185
+ * Helper function to quickly read a Parquet blob and execute a query
186
+ *
187
+ * @param blob - Parquet file as Blob
188
+ * @param sql - SQL query to execute (optional, defaults to SELECT *)
189
+ * @returns Query result
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * const blob = await client.getFile('data/sales.parquet');
194
+ * const result = await queryParquet(blob, 'SELECT product, SUM(amount) as total FROM data GROUP BY product');
195
+ * console.log(result);
196
+ * ```
197
+ */
198
+ async function queryParquet(blob, sql) {
199
+ const uniqueId = `temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
200
+ const reader = new ParquetReader(uniqueId);
201
+ await reader.registerParquet(blob);
202
+ const query = sql || `SELECT * FROM ${uniqueId}`;
203
+ const result = await reader.query(query);
204
+ await reader.close();
205
+ return result;
206
+ }
207
+ /**
208
+ * Helper function to read Parquet file and get all data
209
+ *
210
+ * @param blob - Parquet file as Blob
211
+ * @param limit - Maximum number of rows (optional)
212
+ * @returns Query result
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * const blob = await client.getFile('data/sales.parquet');
217
+ * const data = await readParquet(blob, 1000);
218
+ * console.log(`Loaded ${data.rowCount} rows`);
219
+ * ```
220
+ */
221
+ async function readParquet(blob, limit) {
222
+ const reader = new ParquetReader(`temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`);
223
+ await reader.registerParquet(blob);
224
+ const result = await reader.getAll(limit);
225
+ await reader.close();
226
+ return result;
227
+ }
228
+
229
+ //#endregion
230
+ //#region src/client.ts
231
+ /**
232
+ * Oxy API Client for interacting with Oxy data
233
+ */
234
+ var OxyClient = class {
235
+ constructor(config) {
236
+ this.config = config;
237
+ }
238
+ /**
239
+ * Encodes a file path to base64 for use in API URLs
240
+ */
241
+ encodePathBase64(path) {
242
+ if (typeof Buffer !== "undefined") return Buffer.from(path).toString("base64");
243
+ else return btoa(path);
244
+ }
245
+ /**
246
+ * Makes an authenticated HTTP request to the Oxy API
247
+ */
248
+ async request(endpoint, options = {}) {
249
+ const url = `${this.config.baseUrl}${endpoint}`;
250
+ const headers = {
251
+ "Content-Type": "application/json",
252
+ ...options.headers || {}
253
+ };
254
+ if (this.config.apiKey) headers["X-API-Key"] = `${this.config.apiKey}`;
255
+ const controller = new AbortController();
256
+ const timeoutId = setTimeout(() => controller.abort(), this.config.timeout || 3e4);
257
+ try {
258
+ const response = await fetch(url, {
259
+ ...options,
260
+ headers,
261
+ signal: controller.signal
262
+ });
263
+ clearTimeout(timeoutId);
264
+ if (!response.ok) {
265
+ const errorText = await response.text().catch(() => "Unknown error");
266
+ throw {
267
+ message: `API request failed: ${response.statusText}`,
268
+ status: response.status,
269
+ details: errorText
270
+ };
271
+ }
272
+ if ((typeof options.headers === "object" && options.headers !== null ? options.headers["Accept"] : void 0) === "application/octet-stream") return response.blob();
273
+ return response.json();
274
+ } catch (error) {
275
+ clearTimeout(timeoutId);
276
+ if (error.name === "AbortError") throw new Error(`Request timeout after ${this.config.timeout || 3e4}ms`);
277
+ throw error;
278
+ }
279
+ }
280
+ /**
281
+ * Builds query parameters including optional branch
282
+ */
283
+ buildQueryParams(additionalParams = {}) {
284
+ const params = { ...additionalParams };
285
+ if (this.config.branch) params.branch = this.config.branch;
286
+ const queryString = new URLSearchParams(params).toString();
287
+ return queryString ? `?${queryString}` : "";
288
+ }
289
+ /**
290
+ * Lists all apps in the project
291
+ *
292
+ * @returns Array of app items
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * const apps = await client.listApps();
297
+ * console.log('Available apps:', apps);
298
+ * ```
299
+ */
300
+ async listApps() {
301
+ const query = this.buildQueryParams();
302
+ return this.request(`/${this.config.projectId}/app${query}`);
303
+ }
304
+ /**
305
+ * Gets data for a specific app
306
+ *
307
+ * @param appPath - Relative path to the app file (e.g., 'my-app.app.yml')
308
+ * @returns App data response
309
+ *
310
+ * @example
311
+ * ```typescript
312
+ * const data = await client.getAppData('dashboard.app.yml');
313
+ * if (data.error) {
314
+ * console.error('Error:', data.error);
315
+ * } else {
316
+ * console.log('App data:', data.data);
317
+ * }
318
+ * ```
319
+ */
320
+ async getAppData(appPath) {
321
+ const pathb64 = this.encodePathBase64(appPath);
322
+ const query = this.buildQueryParams();
323
+ return this.request(`/${this.config.projectId}/app/${pathb64}${query}`);
324
+ }
325
+ /**
326
+ * Runs an app and returns fresh data (bypasses cache)
327
+ *
328
+ * @param appPath - Relative path to the app file
329
+ * @returns App data response
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * const data = await client.runApp('dashboard.app.yml');
334
+ * console.log('Fresh app data:', data.data);
335
+ * ```
336
+ */
337
+ async runApp(appPath) {
338
+ const pathb64 = this.encodePathBase64(appPath);
339
+ const query = this.buildQueryParams();
340
+ return this.request(`/${this.config.projectId}/app/${pathb64}/run${query}`, { method: "POST" });
341
+ }
342
+ /**
343
+ * Gets display configurations for an app
344
+ *
345
+ * @param appPath - Relative path to the app file
346
+ * @returns Display configurations with potential errors
347
+ *
348
+ * @example
349
+ * ```typescript
350
+ * const displays = await client.getDisplays('dashboard.app.yml');
351
+ * displays.displays.forEach(d => {
352
+ * if (d.error) {
353
+ * console.error('Display error:', d.error);
354
+ * } else {
355
+ * console.log('Display:', d.display);
356
+ * }
357
+ * });
358
+ * ```
359
+ */
360
+ async getDisplays(appPath) {
361
+ const pathb64 = this.encodePathBase64(appPath);
362
+ const query = this.buildQueryParams();
363
+ return this.request(`/${this.config.projectId}/app/${pathb64}/displays${query}`);
364
+ }
365
+ /**
366
+ * Gets a file from the app state directory (e.g., generated charts, images)
367
+ *
368
+ * This is useful for retrieving generated assets like charts, images, or other
369
+ * files produced by app workflows and stored in the state directory.
370
+ *
371
+ * @param filePath - Relative path to the file in state directory
372
+ * @returns Blob containing the file data
373
+ *
374
+ * @example
375
+ * ```typescript
376
+ * // Get a generated chart image
377
+ * const blob = await client.getFile('charts/sales-chart.png');
378
+ * const imageUrl = URL.createObjectURL(blob);
379
+ *
380
+ * // Use in an img tag
381
+ * document.querySelector('img').src = imageUrl;
382
+ * ```
383
+ *
384
+ * @example
385
+ * ```typescript
386
+ * // Download a file
387
+ * const blob = await client.getFile('exports/data.csv');
388
+ * const a = document.createElement('a');
389
+ * a.href = URL.createObjectURL(blob);
390
+ * a.download = 'data.csv';
391
+ * a.click();
392
+ * ```
393
+ */
394
+ async getFile(filePath) {
395
+ const pathb64 = this.encodePathBase64(filePath);
396
+ const query = this.buildQueryParams();
397
+ return this.request(`/${this.config.projectId}/app/file/${pathb64}${query}`, { headers: { "Accept": "application/octet-stream" } });
398
+ }
399
+ /**
400
+ * Gets a file URL for direct browser access
401
+ *
402
+ * This returns a URL that can be used directly in img tags, fetch calls, etc.
403
+ * The URL includes authentication via query parameters.
404
+ *
405
+ * @param filePath - Relative path to the file in state directory
406
+ * @returns Full URL to the file
407
+ *
408
+ * @example
409
+ * ```typescript
410
+ * const imageUrl = client.getFileUrl('charts/sales-chart.png');
411
+ *
412
+ * // Use directly in img tag (in environments where query-based auth is supported)
413
+ * document.querySelector('img').src = imageUrl;
414
+ * ```
415
+ */
416
+ getFileUrl(filePath) {
417
+ const pathb64 = this.encodePathBase64(filePath);
418
+ const query = this.buildQueryParams();
419
+ return `${this.config.baseUrl}/${this.config.projectId}/app/file/${pathb64}${query}`;
420
+ }
421
+ /**
422
+ * Fetches a parquet file and parses it into table data
423
+ *
424
+ * @param filePath - Relative path to the parquet file
425
+ * @param limit - Maximum number of rows to return (default: 100)
426
+ * @returns TableData with columns and rows
427
+ *
428
+ * @example
429
+ * ```typescript
430
+ * const tableData = await client.getTableData('data/sales.parquet', 50);
431
+ * console.log(tableData.columns);
432
+ * console.log(tableData.rows);
433
+ * console.log(`Total rows: ${tableData.total_rows}`);
434
+ * ```
435
+ */
436
+ async getTableData(filePath, limit = 100) {
437
+ const result = await readParquet(await this.getFile(filePath), limit);
438
+ return {
439
+ columns: result.columns,
440
+ rows: result.rows,
441
+ total_rows: result.rowCount
442
+ };
443
+ }
444
+ };
445
+
446
+ //#endregion
447
+ //#region src/config.ts
448
+ /**
449
+ * Creates an Oxy configuration from environment variables
450
+ *
451
+ * Environment variables:
452
+ * - OXY_URL: Base URL of the Oxy API
453
+ * - OXY_API_KEY: API key for authentication
454
+ * - OXY_PROJECT_ID: Project ID (UUID)
455
+ * - OXY_BRANCH: (Optional) Branch name
456
+ *
457
+ * @param overrides - Optional configuration overrides
458
+ * @returns OxyConfig object
459
+ * @throws Error if required environment variables are missing
460
+ */
461
+ function createConfig(overrides) {
462
+ const baseUrl = overrides?.baseUrl || process.env.OXY_URL;
463
+ const apiKey = overrides?.apiKey || process.env.OXY_API_KEY;
464
+ const projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;
465
+ if (!baseUrl) throw new Error("OXY_URL environment variable or baseUrl config is required");
466
+ if (!projectId) throw new Error("OXY_PROJECT_ID environment variable or projectId config is required");
467
+ return {
468
+ baseUrl: baseUrl.replace(/\/$/, ""),
469
+ apiKey,
470
+ projectId,
471
+ branch: overrides?.branch || process.env.OXY_BRANCH,
472
+ timeout: overrides?.timeout || 3e4
473
+ };
474
+ }
475
+
476
+ //#endregion
477
+ exports.OxyClient = OxyClient;
478
+ exports.ParquetReader = ParquetReader;
479
+ exports.createConfig = createConfig;
480
+ exports.queryParquet = queryParquet;
481
+ exports.readParquet = readParquet;
482
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["duckdb"],"sources":["../src/parquet.ts","../src/client.ts","../src/config.ts"],"sourcesContent":["import * as duckdb from '@duckdb/duckdb-wasm';\n\nlet dbInstance: duckdb.AsyncDuckDB | null = null;\nlet connection: duckdb.AsyncDuckDBConnection | null = null;\n\n/**\n * Initialize DuckDB-WASM instance\n */\nasync function initializeDuckDB(): Promise<duckdb.AsyncDuckDB> {\n if (dbInstance) {\n return dbInstance;\n }\n\n const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();\n\n // Select a bundle based on browser features\n const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);\n\n const worker_url = URL.createObjectURL(\n new Blob([`importScripts(\"${bundle.mainWorker}\");`], {\n type: 'text/javascript',\n })\n );\n\n const worker = new Worker(worker_url);\n const logger = new duckdb.ConsoleLogger();\n\n dbInstance = new duckdb.AsyncDuckDB(logger, worker);\n await dbInstance.instantiate(bundle.mainModule, bundle.pthreadWorker);\n URL.revokeObjectURL(worker_url);\n\n return dbInstance;\n}\n\n/**\n * Get or create a DuckDB connection\n */\nasync function getConnection(): Promise<duckdb.AsyncDuckDBConnection> {\n if (connection) {\n return connection;\n }\n\n const db = await initializeDuckDB();\n connection = await db.connect();\n return connection;\n}\n\n/**\n * Query result interface\n */\nexport interface QueryResult {\n columns: string[];\n rows: any[][];\n rowCount: number;\n}\n\n/**\n * ParquetReader provides methods to read and query Parquet files\n */\nexport class ParquetReader {\n private tableName: string;\n private registered: boolean = false;\n\n constructor(tableName: string = 'data') {\n this.tableName = tableName;\n }\n\n /**\n * Register a Parquet file from a Blob\n *\n * @param blob - Parquet file as Blob\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const reader = new ParquetReader('sales');\n * await reader.registerParquet(blob);\n * ```\n */\n async registerParquet(blob: Blob): Promise<void> {\n const conn = await getConnection();\n const db = await initializeDuckDB();\n\n // Convert blob to Uint8Array\n const arrayBuffer = await blob.arrayBuffer();\n const uint8Array = new Uint8Array(arrayBuffer);\n\n // Register the file with DuckDB\n await db.registerFileBuffer(\n `${this.tableName}.parquet`,\n uint8Array\n );\n\n // Drop table if it exists\n try {\n await conn.query(`DROP TABLE IF EXISTS ${this.tableName}`);\n } catch (e) {\n // Ignore error if table doesn't exist\n }\n\n // Create table from parquet\n await conn.query(\n `CREATE TABLE ${this.tableName} AS SELECT * FROM '${this.tableName}.parquet'`\n );\n\n this.registered = true;\n }\n\n /**\n * Execute a SQL query against the registered Parquet data\n *\n * @param sql - SQL query string\n * @returns Query result with columns and rows\n *\n * @example\n * ```typescript\n * const result = await reader.query('SELECT * FROM sales LIMIT 10');\n * console.log(result.columns);\n * console.log(result.rows);\n * ```\n */\n async query(sql: string): Promise<QueryResult> {\n if (!this.registered) {\n throw new Error('Parquet file not registered. Call registerParquet() first.');\n }\n\n const conn = await getConnection();\n const result = await conn.query(sql);\n\n const columns = result.schema.fields.map((field) => field.name);\n const rows: any[][] = [];\n\n // Convert Arrow table to rows\n for (let i = 0; i < result.numRows; i++) {\n const row: any[] = [];\n for (let j = 0; j < result.numCols; j++) {\n const col = result.getChildAt(j);\n row.push(col?.get(i));\n }\n rows.push(row);\n }\n\n return {\n columns,\n rows,\n rowCount: result.numRows,\n };\n }\n\n /**\n * Get all data from the registered table\n *\n * @param limit - Maximum number of rows to return (default: all)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const allData = await reader.getAll();\n * const first100 = await reader.getAll(100);\n * ```\n */\n async getAll(limit?: number): Promise<QueryResult> {\n const limitClause = limit ? ` LIMIT ${limit}` : '';\n return this.query(`SELECT * FROM ${this.tableName}${limitClause}`);\n }\n\n /**\n * Get table schema information\n *\n * @returns Schema information\n *\n * @example\n * ```typescript\n * const schema = await reader.getSchema();\n * console.log(schema.columns); // ['id', 'name', 'sales']\n * console.log(schema.rows); // [['id', 'INTEGER'], ['name', 'VARCHAR'], ...]\n * ```\n */\n async getSchema(): Promise<QueryResult> {\n return this.query(`DESCRIBE ${this.tableName}`);\n }\n\n /**\n * Get row count\n *\n * @returns Number of rows in the table\n *\n * @example\n * ```typescript\n * const count = await reader.count();\n * console.log(`Total rows: ${count}`);\n * ```\n */\n async count(): Promise<number> {\n const result = await this.query(`SELECT COUNT(*) as count FROM ${this.tableName}`);\n return result.rows[0][0];\n }\n\n /**\n * Close and cleanup resources\n */\n async close(): Promise<void> {\n if (this.registered) {\n const conn = await getConnection();\n const db = await initializeDuckDB();\n\n // Drop the table\n try {\n await conn.query(`DROP TABLE IF EXISTS ${this.tableName}`);\n } catch (e) {\n // Ignore error\n }\n\n // Drop the registered file buffer\n try {\n await db.dropFile(`${this.tableName}.parquet`);\n } catch (e) {\n // Ignore error if file doesn't exist\n }\n\n this.registered = false;\n }\n }\n}\n\n/**\n * Helper function to quickly read a Parquet blob and execute a query\n *\n * @param blob - Parquet file as Blob\n * @param sql - SQL query to execute (optional, defaults to SELECT *)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const result = await queryParquet(blob, 'SELECT product, SUM(amount) as total FROM data GROUP BY product');\n * console.log(result);\n * ```\n */\nexport async function queryParquet(\n blob: Blob,\n sql?: string\n): Promise<QueryResult> {\n // Generate unique table name to avoid conflicts\n const uniqueId = `temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n const reader = new ParquetReader(uniqueId);\n\n await reader.registerParquet(blob);\n\n const query = sql || `SELECT * FROM ${uniqueId}`;\n const result = await reader.query(query);\n\n await reader.close();\n return result;\n}\n\n/**\n * Helper function to read Parquet file and get all data\n *\n * @param blob - Parquet file as Blob\n * @param limit - Maximum number of rows (optional)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const data = await readParquet(blob, 1000);\n * console.log(`Loaded ${data.rowCount} rows`);\n * ```\n */\nexport async function readParquet(\n blob: Blob,\n limit?: number\n): Promise<QueryResult> {\n // Generate unique table name to avoid conflicts\n const uniqueId = `temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n const reader = new ParquetReader(uniqueId);\n\n await reader.registerParquet(blob);\n\n const result = await reader.getAll(limit);\n\n await reader.close();\n return result;\n}\n","import { OxyConfig } from './config';\nimport {\n AppItem,\n AppDataResponse,\n GetDisplaysResponse,\n ApiError,\n TableData,\n} from './types';\nimport { readParquet } from './parquet';\n\n/**\n * Oxy API Client for interacting with Oxy data\n */\nexport class OxyClient {\n private config: OxyConfig;\n\n constructor(config: OxyConfig) {\n this.config = config;\n }\n\n /**\n * Encodes a file path to base64 for use in API URLs\n */\n private encodePathBase64(path: string): string {\n if (typeof Buffer !== 'undefined') {\n // Node.js environment\n return Buffer.from(path).toString('base64');\n } else {\n // Browser environment\n return btoa(path);\n }\n }\n\n /**\n * Makes an authenticated HTTP request to the Oxy API\n */\n private async request<T>(\n endpoint: string,\n options: RequestInit = {}\n ): Promise<T> {\n const url = `${this.config.baseUrl}${endpoint}`;\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n ...((options.headers as Record<string, string>) || {}),\n };\n\n // Only add Authorization header if API key is provided (optional for local dev)\n if (this.config.apiKey) {\n headers['X-API-Key'] = `${this.config.apiKey}`;\n }\n\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.config.timeout || 30000);\n\n try {\n const response = await fetch(url, {\n ...options,\n headers,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => 'Unknown error');\n const error: ApiError = {\n message: `API request failed: ${response.statusText}`,\n status: response.status,\n details: errorText,\n };\n throw error;\n }\n\n // Handle binary responses\n const acceptHeader =\n typeof options.headers === 'object' && options.headers !== null\n ? (options.headers as Record<string, string>)['Accept']\n : undefined;\n if (acceptHeader === 'application/octet-stream') {\n return response.blob() as Promise<T>;\n }\n\n return response.json();\n } catch (error: any) {\n clearTimeout(timeoutId);\n\n if (error.name === 'AbortError') {\n throw new Error(`Request timeout after ${this.config.timeout || 30000}ms`);\n }\n\n throw error;\n }\n }\n\n /**\n * Builds query parameters including optional branch\n */\n private buildQueryParams(additionalParams: Record<string, string> = {}): string {\n const params: Record<string, string> = { ...additionalParams };\n\n if (this.config.branch) {\n params.branch = this.config.branch;\n }\n\n const searchParams = new URLSearchParams(params);\n const queryString = searchParams.toString();\n return queryString ? `?${queryString}` : '';\n }\n\n /**\n * Lists all apps in the project\n *\n * @returns Array of app items\n *\n * @example\n * ```typescript\n * const apps = await client.listApps();\n * console.log('Available apps:', apps);\n * ```\n */\n async listApps(): Promise<AppItem[]> {\n const query = this.buildQueryParams();\n return this.request<AppItem[]>(`/${this.config.projectId}/app${query}`);\n }\n\n /**\n * Gets data for a specific app\n *\n * @param appPath - Relative path to the app file (e.g., 'my-app.app.yml')\n * @returns App data response\n *\n * @example\n * ```typescript\n * const data = await client.getAppData('dashboard.app.yml');\n * if (data.error) {\n * console.error('Error:', data.error);\n * } else {\n * console.log('App data:', data.data);\n * }\n * ```\n */\n async getAppData(appPath: string): Promise<AppDataResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<AppDataResponse>(\n `/${this.config.projectId}/app/${pathb64}${query}`\n );\n }\n\n /**\n * Runs an app and returns fresh data (bypasses cache)\n *\n * @param appPath - Relative path to the app file\n * @returns App data response\n *\n * @example\n * ```typescript\n * const data = await client.runApp('dashboard.app.yml');\n * console.log('Fresh app data:', data.data);\n * ```\n */\n async runApp(appPath: string): Promise<AppDataResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<AppDataResponse>(\n `/${this.config.projectId}/app/${pathb64}/run${query}`,\n { method: 'POST' }\n );\n }\n\n /**\n * Gets display configurations for an app\n *\n * @param appPath - Relative path to the app file\n * @returns Display configurations with potential errors\n *\n * @example\n * ```typescript\n * const displays = await client.getDisplays('dashboard.app.yml');\n * displays.displays.forEach(d => {\n * if (d.error) {\n * console.error('Display error:', d.error);\n * } else {\n * console.log('Display:', d.display);\n * }\n * });\n * ```\n */\n async getDisplays(appPath: string): Promise<GetDisplaysResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<GetDisplaysResponse>(\n `/${this.config.projectId}/app/${pathb64}/displays${query}`\n );\n }\n\n /**\n * Gets a file from the app state directory (e.g., generated charts, images)\n *\n * This is useful for retrieving generated assets like charts, images, or other\n * files produced by app workflows and stored in the state directory.\n *\n * @param filePath - Relative path to the file in state directory\n * @returns Blob containing the file data\n *\n * @example\n * ```typescript\n * // Get a generated chart image\n * const blob = await client.getFile('charts/sales-chart.png');\n * const imageUrl = URL.createObjectURL(blob);\n *\n * // Use in an img tag\n * document.querySelector('img').src = imageUrl;\n * ```\n *\n * @example\n * ```typescript\n * // Download a file\n * const blob = await client.getFile('exports/data.csv');\n * const a = document.createElement('a');\n * a.href = URL.createObjectURL(blob);\n * a.download = 'data.csv';\n * a.click();\n * ```\n */\n async getFile(filePath: string): Promise<Blob> {\n const pathb64 = this.encodePathBase64(filePath);\n const query = this.buildQueryParams();\n return this.request<Blob>(\n `/${this.config.projectId}/app/file/${pathb64}${query}`,\n {\n headers: {\n 'Accept': 'application/octet-stream',\n },\n }\n );\n }\n\n /**\n * Gets a file URL for direct browser access\n *\n * This returns a URL that can be used directly in img tags, fetch calls, etc.\n * The URL includes authentication via query parameters.\n *\n * @param filePath - Relative path to the file in state directory\n * @returns Full URL to the file\n *\n * @example\n * ```typescript\n * const imageUrl = client.getFileUrl('charts/sales-chart.png');\n *\n * // Use directly in img tag (in environments where query-based auth is supported)\n * document.querySelector('img').src = imageUrl;\n * ```\n */\n getFileUrl(filePath: string): string {\n const pathb64 = this.encodePathBase64(filePath);\n const query = this.buildQueryParams();\n return `${this.config.baseUrl}/${this.config.projectId}/app/file/${pathb64}${query}`;\n }\n\n /**\n * Fetches a parquet file and parses it into table data\n *\n * @param filePath - Relative path to the parquet file\n * @param limit - Maximum number of rows to return (default: 100)\n * @returns TableData with columns and rows\n *\n * @example\n * ```typescript\n * const tableData = await client.getTableData('data/sales.parquet', 50);\n * console.log(tableData.columns);\n * console.log(tableData.rows);\n * console.log(`Total rows: ${tableData.total_rows}`);\n * ```\n */\n async getTableData(filePath: string, limit: number = 100): Promise<TableData> {\n const blob = await this.getFile(filePath);\n const result = await readParquet(blob, limit);\n\n return {\n columns: result.columns,\n rows: result.rows,\n total_rows: result.rowCount,\n };\n }\n}\n","/**\n * Configuration for the Oxy SDK\n */\nexport interface OxyConfig {\n /**\n * Base URL of the Oxy API (e.g., 'https://api.oxy.tech' or 'http://localhost:3000')\n */\n baseUrl: string;\n\n /**\n * API key for authentication (optional for local development)\n */\n apiKey?: string;\n\n /**\n * Project ID (UUID)\n */\n projectId: string;\n\n /**\n * Optional branch name (defaults to current branch if not specified)\n */\n branch?: string;\n\n /**\n * Request timeout in milliseconds (default: 30000)\n */\n timeout?: number;\n}\n\n/**\n * Creates an Oxy configuration from environment variables\n *\n * Environment variables:\n * - OXY_URL: Base URL of the Oxy API\n * - OXY_API_KEY: API key for authentication\n * - OXY_PROJECT_ID: Project ID (UUID)\n * - OXY_BRANCH: (Optional) Branch name\n *\n * @param overrides - Optional configuration overrides\n * @returns OxyConfig object\n * @throws Error if required environment variables are missing\n */\nexport function createConfig(overrides?: Partial<OxyConfig>): OxyConfig {\n const baseUrl = overrides?.baseUrl || process.env.OXY_URL;\n const apiKey = overrides?.apiKey || process.env.OXY_API_KEY;\n const projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;\n\n if (!baseUrl) {\n throw new Error('OXY_URL environment variable or baseUrl config is required');\n }\n\n if (!projectId) {\n throw new Error('OXY_PROJECT_ID environment variable or projectId config is required');\n }\n\n return {\n baseUrl: baseUrl.replace(/\\/$/, ''), // Remove trailing slash\n apiKey,\n projectId,\n branch: overrides?.branch || process.env.OXY_BRANCH,\n timeout: overrides?.timeout || 30000,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAI,aAAwC;AAC5C,IAAI,aAAkD;;;;AAKtD,eAAe,mBAAgD;AAC7D,KAAI,WACF,QAAO;CAGT,MAAM,mBAAmBA,oBAAO,oBAAoB;CAGpD,MAAM,SAAS,MAAMA,oBAAO,aAAa,iBAAiB;CAE1D,MAAM,aAAa,IAAI,gBACrB,IAAI,KAAK,CAAC,kBAAkB,OAAO,WAAW,KAAK,EAAE,EACnD,MAAM,mBACP,CAAC,CACH;CAED,MAAM,SAAS,IAAI,OAAO,WAAW;CACrC,MAAM,SAAS,IAAIA,oBAAO,eAAe;AAEzC,cAAa,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