@oxy-hq/sdk 0.1.0 → 0.1.1

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.d.mts CHANGED
@@ -24,6 +24,18 @@ interface OxyConfig {
24
24
  * Request timeout in milliseconds (default: 30000)
25
25
  */
26
26
  timeout?: number;
27
+ /**
28
+ * Parent window origin for postMessage authentication (iframe scenarios)
29
+ * Required when using postMessage auth for security.
30
+ * Example: 'https://app.example.com'
31
+ * Use '*' only in development!
32
+ */
33
+ parentOrigin?: string;
34
+ /**
35
+ * Disable automatic postMessage authentication even if in iframe
36
+ * Set to true if you want to provide API key manually in iframe context
37
+ */
38
+ disableAutoAuth?: boolean;
27
39
  }
28
40
  /**
29
41
  * Creates an Oxy configuration from environment variables
@@ -39,6 +51,40 @@ interface OxyConfig {
39
51
  * @throws Error if required environment variables are missing
40
52
  */
41
53
  declare function createConfig(overrides?: Partial<OxyConfig>): OxyConfig;
54
+ /**
55
+ * Creates an Oxy configuration asynchronously with support for postMessage authentication
56
+ *
57
+ * This is the recommended method for iframe scenarios where authentication
58
+ * needs to be obtained from the parent window via postMessage.
59
+ *
60
+ * When running in an iframe without an API key, this function will:
61
+ * 1. Detect the iframe context
62
+ * 2. Send an authentication request to the parent window
63
+ * 3. Wait for the parent to respond with credentials
64
+ * 4. Return the configured client
65
+ *
66
+ * Environment variables (fallback):
67
+ * - OXY_URL: Base URL of the Oxy API
68
+ * - OXY_API_KEY: API key for authentication
69
+ * - OXY_PROJECT_ID: Project ID (UUID)
70
+ * - OXY_BRANCH: (Optional) Branch name
71
+ *
72
+ * @param overrides - Optional configuration overrides
73
+ * @returns Promise resolving to OxyConfig object
74
+ * @throws Error if required configuration is missing
75
+ * @throws PostMessageAuthTimeoutError if parent doesn't respond
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * // Automatic iframe detection and authentication
80
+ * const config = await createConfigAsync({
81
+ * parentOrigin: 'https://app.example.com',
82
+ * projectId: 'my-project-id',
83
+ * baseUrl: 'https://api.oxy.tech'
84
+ * });
85
+ * ```
86
+ */
87
+ declare function createConfigAsync(overrides?: Partial<OxyConfig>): Promise<OxyConfig>;
42
88
  //#endregion
43
89
  //#region src/types.d.ts
44
90
  /**
@@ -99,6 +145,76 @@ interface QueryResult$1 {
99
145
  rows: any[][];
100
146
  rowCount: number;
101
147
  }
148
+ /**
149
+ * PostMessage authentication protocol types
150
+ */
151
+ /**
152
+ * Request message sent from iframe to parent window
153
+ */
154
+ interface OxyAuthRequestMessage {
155
+ type: 'OXY_AUTH_REQUEST';
156
+ version: '1.0';
157
+ timestamp: number;
158
+ requestId: string;
159
+ }
160
+ /**
161
+ * Response message sent from parent window to iframe
162
+ */
163
+ interface OxyAuthResponseMessage {
164
+ type: 'OXY_AUTH_RESPONSE';
165
+ version: '1.0';
166
+ requestId: string;
167
+ apiKey: string;
168
+ projectId?: string;
169
+ baseUrl?: string;
170
+ }
171
+ /**
172
+ * Options for postMessage authentication
173
+ */
174
+ interface PostMessageAuthOptions {
175
+ /** Required parent window origin for security (e.g., 'https://app.example.com'). Use '*' only in development! */
176
+ parentOrigin?: string;
177
+ /** Timeout in milliseconds (default: 5000) */
178
+ timeout?: number;
179
+ /** Number of retry attempts (default: 0) */
180
+ retries?: number;
181
+ }
182
+ /**
183
+ * Result from successful postMessage authentication
184
+ */
185
+ interface PostMessageAuthResult {
186
+ apiKey: string;
187
+ projectId?: string;
188
+ baseUrl?: string;
189
+ source: 'postmessage';
190
+ }
191
+ /**
192
+ * Custom error classes for postMessage authentication
193
+ */
194
+ /**
195
+ * Error thrown when postMessage authentication times out
196
+ */
197
+ declare class PostMessageAuthTimeoutError extends Error {
198
+ constructor(timeout: number);
199
+ }
200
+ /**
201
+ * Error thrown when authentication response comes from unauthorized origin
202
+ */
203
+ declare class PostMessageAuthInvalidOriginError extends Error {
204
+ constructor(expected: string, actual: string);
205
+ }
206
+ /**
207
+ * Error thrown when postMessage authentication is attempted outside iframe context
208
+ */
209
+ declare class PostMessageAuthNotInIframeError extends Error {
210
+ constructor();
211
+ }
212
+ /**
213
+ * Error thrown when authentication response is malformed or invalid
214
+ */
215
+ declare class PostMessageAuthInvalidResponseError extends Error {
216
+ constructor(reason: string);
217
+ }
102
218
  //#endregion
103
219
  //#region src/client.d.ts
104
220
  /**
@@ -107,6 +223,31 @@ interface QueryResult$1 {
107
223
  declare class OxyClient {
108
224
  private config;
109
225
  constructor(config: OxyConfig);
226
+ /**
227
+ * Creates an OxyClient instance asynchronously with support for postMessage authentication
228
+ *
229
+ * This is the recommended method when using the SDK in an iframe that needs to
230
+ * obtain authentication from the parent window via postMessage.
231
+ *
232
+ * @param config - Optional configuration overrides
233
+ * @returns Promise resolving to OxyClient instance
234
+ * @throws Error if required configuration is missing
235
+ * @throws PostMessageAuthTimeoutError if parent doesn't respond
236
+ *
237
+ * @example
238
+ * ```typescript
239
+ * // In an iframe - automatic postMessage auth
240
+ * const client = await OxyClient.create({
241
+ * parentOrigin: 'https://app.example.com',
242
+ * projectId: 'my-project-id',
243
+ * baseUrl: 'https://api.oxy.tech'
244
+ * });
245
+ *
246
+ * // Use the client normally
247
+ * const apps = await client.listApps();
248
+ * ```
249
+ */
250
+ static create(config?: Partial<OxyConfig>): Promise<OxyClient>;
110
251
  /**
111
252
  * Encodes a file path to base64 for use in API URLs
112
253
  */
@@ -363,5 +504,36 @@ declare function queryParquet(blob: Blob, sql?: string): Promise<QueryResult>;
363
504
  */
364
505
  declare function readParquet(blob: Blob, limit?: number): Promise<QueryResult>;
365
506
  //#endregion
366
- export { type AppDataResponse, type AppItem, type DataContainer, type DisplayData, type DisplayWithError, type FileReference, OxyClient, type OxyConfig, type QueryResult as ParquetQueryResult, ParquetReader, type QueryResult$1 as QueryResult, type TableData, createConfig, queryParquet, readParquet };
507
+ //#region src/auth/postMessage.d.ts
508
+ /**
509
+ * Check if the current context is running inside an iframe
510
+ *
511
+ * @returns true if running in an iframe, false otherwise (including Node.js)
512
+ */
513
+ declare function isInIframe(): boolean;
514
+ /**
515
+ * Request authentication from parent window via postMessage
516
+ *
517
+ * This is the main entry point for iframe-based authentication.
518
+ * It sends a request to the parent window and waits for a response.
519
+ *
520
+ * @param options - Configuration options for the auth request
521
+ * @returns Promise that resolves with authentication credentials
522
+ * @throws {PostMessageAuthNotInIframeError} If not in an iframe
523
+ * @throws {PostMessageAuthTimeoutError} If parent doesn't respond in time
524
+ * @throws {PostMessageAuthInvalidOriginError} If response from wrong origin
525
+ * @throws {PostMessageAuthInvalidResponseError} If response is malformed
526
+ *
527
+ * @example
528
+ * ```typescript
529
+ * const auth = await requestAuthFromParent({
530
+ * parentOrigin: 'https://app.example.com',
531
+ * timeout: 5000
532
+ * });
533
+ * console.log('Received API key:', auth.apiKey);
534
+ * ```
535
+ */
536
+ declare function requestAuthFromParent(options?: PostMessageAuthOptions): Promise<PostMessageAuthResult>;
537
+ //#endregion
538
+ export { type AppDataResponse, type AppItem, type DataContainer, type DisplayData, type DisplayWithError, type FileReference, type OxyAuthRequestMessage, type OxyAuthResponseMessage, OxyClient, type OxyConfig, type QueryResult as ParquetQueryResult, ParquetReader, PostMessageAuthInvalidOriginError, PostMessageAuthInvalidResponseError, PostMessageAuthNotInIframeError, type PostMessageAuthOptions, type PostMessageAuthResult, PostMessageAuthTimeoutError, type QueryResult$1 as QueryResult, type TableData, createConfig, createConfigAsync, isInIframe, queryParquet, readParquet, requestAuthFromParent };
367
539
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/config.ts","../src/types.ts","../src/client.ts","../src/parquet.ts"],"sourcesContent":[],"mappings":";;;AAGA;AAwCA;AAAiD,UAxChC,SAAA,CAwCgC;EAAR;;;;;;ACxCzC;EAQiB,MAAA,CAAA,EAAA,MAAA;EAQA;AAMjB;AAKA;EAQiB,SAAA,EAAA,MAAA;EAQA;AAQjB;AAgBA;;;;ACzDA;EAGsB,OAAA,CAAA,EAAA,MAAA;;;;;;;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,CAAA,ED/G8B,OC+G9B,CD/GsC,eC+GtC,CAAA;EAAO;;;;;;;;;;;;;;;;;;gCDpF4B,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6BAqCX,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kDAmDkB,QAAQ;;;;;AFlRrE;AAwCA;AAAiD,UGOhC,WAAA,CHPgC;EAAR,OAAA,EAAA,MAAA,EAAA;EAAqB,IAAA,EAAA,GAAA,EAAA,EAAA;EAAS,QAAA,EAAA,MAAA;;;;ACxCvE;AAQiB,cEgDJ,aAAA,CFhDiB;EAQb,QAAA,SAAS;EAMd,QAAA,UAAa;EAKR,WAAA,CAAA,SACT,CADwB,EAAA,MACxB;EAOS;AAQjB;AAQA;AAgBA;;;;ACzDA;;;;;EAiIqC,eAAA,CAAA,IAAA,EC/DP,ID+DO,CAAA,EC/DA,OD+DA,CAAA,IAAA,CAAA;EAoBI;;;;;;;;;;;;AChHzC;EASa,KAAA,CAAA,GAAA,EAAA,MAAa,CAAA,EA8DE,OA9DF,CA8DU,WA9DV,CAAA;EAoBI;;;;;;;;;;;AAgK9B;EACQ,MAAA,CAAA,KAAA,CAAA,EAAA,MAAA,CAAA,EA/EwB,OA+ExB,CA/EgC,WA+EhC,CAAA;EAEG;;;AA4BX;;;;;;;;;eA5FqB,QAAQ;;;;;;;;;;;;WAeZ;;;;WAQA;;;;;;;;;;;;;;;;iBAsCK,YAAA,OACd,qBAEL,QAAQ;;;;;;;;;;;;;;;iBA4BW,WAAA,OACd,uBAEL,QAAQ"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/config.ts","../src/types.ts","../src/client.ts","../src/parquet.ts","../src/auth/postMessage.ts"],"sourcesContent":[],"mappings":";;;AAGA;AAsDA;AAAiD,UAtDhC,SAAA,CAsDgC;EAAR;;;EAyDnB,OAAA,EAAA,MAAA;EACA;;;EACnB,MAAA,CAAA,EAAA,MAAA;EAAO;;;;ECjHO;AAQjB;AAQ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B2B,CAAA,EEtBnC,OFsBmC,CEtB3B,qBFsB2B,CAAA"}
package/dist/index.mjs CHANGED
@@ -1,6 +1,104 @@
1
1
  // @oxy/sdk - TypeScript SDK for Oxy data platform
2
+ import { a as PostMessageAuthInvalidOriginError, c as PostMessageAuthTimeoutError, n as isInIframe, o as PostMessageAuthInvalidResponseError, r as requestAuthFromParent, s as PostMessageAuthNotInIframeError } from "./postMessage-Dq9lXKK0.mjs";
2
3
  import * as duckdb from "@duckdb/duckdb-wasm";
3
4
 
5
+ //#region src/config.ts
6
+ /**
7
+ * Creates an Oxy configuration from environment variables
8
+ *
9
+ * Environment variables:
10
+ * - OXY_URL: Base URL of the Oxy API
11
+ * - OXY_API_KEY: API key for authentication
12
+ * - OXY_PROJECT_ID: Project ID (UUID)
13
+ * - OXY_BRANCH: (Optional) Branch name
14
+ *
15
+ * @param overrides - Optional configuration overrides
16
+ * @returns OxyConfig object
17
+ * @throws Error if required environment variables are missing
18
+ */
19
+ function createConfig(overrides) {
20
+ const baseUrl = overrides?.baseUrl || process.env.OXY_URL;
21
+ const apiKey = overrides?.apiKey || process.env.OXY_API_KEY;
22
+ const projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;
23
+ if (!baseUrl) throw new Error("OXY_URL environment variable or baseUrl config is required");
24
+ if (!projectId) throw new Error("OXY_PROJECT_ID environment variable or projectId config is required");
25
+ return {
26
+ baseUrl: baseUrl.replace(/\/$/, ""),
27
+ apiKey,
28
+ projectId,
29
+ branch: overrides?.branch || process.env.OXY_BRANCH,
30
+ timeout: overrides?.timeout || 3e4,
31
+ parentOrigin: overrides?.parentOrigin,
32
+ disableAutoAuth: overrides?.disableAutoAuth
33
+ };
34
+ }
35
+ /**
36
+ * Creates an Oxy configuration asynchronously with support for postMessage authentication
37
+ *
38
+ * This is the recommended method for iframe scenarios where authentication
39
+ * needs to be obtained from the parent window via postMessage.
40
+ *
41
+ * When running in an iframe without an API key, this function will:
42
+ * 1. Detect the iframe context
43
+ * 2. Send an authentication request to the parent window
44
+ * 3. Wait for the parent to respond with credentials
45
+ * 4. Return the configured client
46
+ *
47
+ * Environment variables (fallback):
48
+ * - OXY_URL: Base URL of the Oxy API
49
+ * - OXY_API_KEY: API key for authentication
50
+ * - OXY_PROJECT_ID: Project ID (UUID)
51
+ * - OXY_BRANCH: (Optional) Branch name
52
+ *
53
+ * @param overrides - Optional configuration overrides
54
+ * @returns Promise resolving to OxyConfig object
55
+ * @throws Error if required configuration is missing
56
+ * @throws PostMessageAuthTimeoutError if parent doesn't respond
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Automatic iframe detection and authentication
61
+ * const config = await createConfigAsync({
62
+ * parentOrigin: 'https://app.example.com',
63
+ * projectId: 'my-project-id',
64
+ * baseUrl: 'https://api.oxy.tech'
65
+ * });
66
+ * ```
67
+ */
68
+ async function createConfigAsync(overrides) {
69
+ const { isInIframe: isInIframe$1, requestAuthFromParent: requestAuthFromParent$1 } = await import("./postMessage-DBjb4M8-.mjs");
70
+ let baseUrl = overrides?.baseUrl || process.env.OXY_URL;
71
+ let apiKey = overrides?.apiKey || process.env.OXY_API_KEY;
72
+ let projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;
73
+ const disableAutoAuth = overrides?.disableAutoAuth ?? false;
74
+ const parentOrigin = overrides?.parentOrigin;
75
+ if (!disableAutoAuth && isInIframe$1() && !apiKey) if (!parentOrigin) console.warn("[Oxy SDK] Running in iframe without API key and no parentOrigin specified. PostMessage authentication will be skipped. Provide parentOrigin config to enable automatic authentication.");
76
+ else try {
77
+ const authResult = await requestAuthFromParent$1({
78
+ parentOrigin,
79
+ timeout: overrides?.timeout || 5e3
80
+ });
81
+ apiKey = authResult.apiKey;
82
+ if (authResult.projectId) projectId = authResult.projectId;
83
+ if (authResult.baseUrl) baseUrl = authResult.baseUrl;
84
+ console.log("[Oxy SDK] Successfully authenticated via postMessage");
85
+ } catch (error) {
86
+ console.error("[Oxy SDK] Failed to authenticate via postMessage:", error.message);
87
+ }
88
+ if (!baseUrl) throw new Error("OXY_URL environment variable or baseUrl config is required");
89
+ if (!projectId) throw new Error("OXY_PROJECT_ID environment variable or projectId config is required");
90
+ return {
91
+ baseUrl: baseUrl.replace(/\/$/, ""),
92
+ apiKey,
93
+ projectId,
94
+ branch: overrides?.branch || process.env.OXY_BRANCH,
95
+ timeout: overrides?.timeout || 3e4,
96
+ parentOrigin,
97
+ disableAutoAuth
98
+ };
99
+ }
100
+
101
+ //#endregion
4
102
  //#region src/parquet.ts
5
103
  let dbInstance = null;
6
104
  let connection = null;
@@ -203,11 +301,38 @@ async function readParquet(blob, limit) {
203
301
  /**
204
302
  * Oxy API Client for interacting with Oxy data
205
303
  */
206
- var OxyClient = class {
304
+ var OxyClient = class OxyClient {
207
305
  constructor(config) {
208
306
  this.config = config;
209
307
  }
210
308
  /**
309
+ * Creates an OxyClient instance asynchronously with support for postMessage authentication
310
+ *
311
+ * This is the recommended method when using the SDK in an iframe that needs to
312
+ * obtain authentication from the parent window via postMessage.
313
+ *
314
+ * @param config - Optional configuration overrides
315
+ * @returns Promise resolving to OxyClient instance
316
+ * @throws Error if required configuration is missing
317
+ * @throws PostMessageAuthTimeoutError if parent doesn't respond
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * // In an iframe - automatic postMessage auth
322
+ * const client = await OxyClient.create({
323
+ * parentOrigin: 'https://app.example.com',
324
+ * projectId: 'my-project-id',
325
+ * baseUrl: 'https://api.oxy.tech'
326
+ * });
327
+ *
328
+ * // Use the client normally
329
+ * const apps = await client.listApps();
330
+ * ```
331
+ */
332
+ static async create(config) {
333
+ return new OxyClient(await createConfigAsync(config));
334
+ }
335
+ /**
211
336
  * Encodes a file path to base64 for use in API URLs
212
337
  */
213
338
  encodePathBase64(path) {
@@ -416,35 +541,5 @@ var OxyClient = class {
416
541
  };
417
542
 
418
543
  //#endregion
419
- //#region src/config.ts
420
- /**
421
- * Creates an Oxy configuration from environment variables
422
- *
423
- * Environment variables:
424
- * - OXY_URL: Base URL of the Oxy API
425
- * - OXY_API_KEY: API key for authentication
426
- * - OXY_PROJECT_ID: Project ID (UUID)
427
- * - OXY_BRANCH: (Optional) Branch name
428
- *
429
- * @param overrides - Optional configuration overrides
430
- * @returns OxyConfig object
431
- * @throws Error if required environment variables are missing
432
- */
433
- function createConfig(overrides) {
434
- const baseUrl = overrides?.baseUrl || process.env.OXY_URL;
435
- const apiKey = overrides?.apiKey || process.env.OXY_API_KEY;
436
- const projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;
437
- if (!baseUrl) throw new Error("OXY_URL environment variable or baseUrl config is required");
438
- if (!projectId) throw new Error("OXY_PROJECT_ID environment variable or projectId config is required");
439
- return {
440
- baseUrl: baseUrl.replace(/\/$/, ""),
441
- apiKey,
442
- projectId,
443
- branch: overrides?.branch || process.env.OXY_BRANCH,
444
- timeout: overrides?.timeout || 3e4
445
- };
446
- }
447
-
448
- //#endregion
449
- export { OxyClient, ParquetReader, createConfig, queryParquet, readParquet };
544
+ export { OxyClient, ParquetReader, PostMessageAuthInvalidOriginError, PostMessageAuthInvalidResponseError, PostMessageAuthNotInIframeError, PostMessageAuthTimeoutError, createConfig, createConfigAsync, isInIframe, queryParquet, readParquet, requestAuthFromParent };
450
545
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"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,mBAAmB,OAAO,oBAAoB;CAGpD,MAAM,SAAS,MAAM,OAAO,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,IAAI,OAAO,eAAe;AAEzC,cAAa,IAAI,OAAO,YAAY,QAAQ,OAAO;AACnD,OAAM,WAAW,YAAY,OAAO,YAAY,OAAO,cAAc;AACrE,KAAI,gBAAgB,WAAW;AAE/B,QAAO;;;;;AAMT,eAAe,gBAAuD;AACpE,KA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AAY,OAAO;GACpB;;;;;;;;;;;;;;;;;;;AClPL,SAAgB,aAAa,WAA2C;CACtE,MAAM,UAAU,WAAW,WAAW,QAAQ,IAAI;CAClD,MAAM,SAAS,WAAW,UAAU,QAAQ,IAAI;CAChD,MAAM,YAAY,WAAW,aAAa,QAAQ,IAAI;AAEtD,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,6DAA6D;AAG/E,KAAI,CAAC,UACH,OAAM,IAAI,MAAM,sEAAsE;AAGxF,QAAO;EACL,SAAS,QAAQ,QAAQ,OAAO,GAAG;EACnC;EACA;EACA,QAAQ,WAAW,UAAU,QAAQ,IAAI;EACzC,SAAS,WAAW,WAAW;EAChC"}
1
+ {"version":3,"file":"index.mjs","names":["isInIframe","requestAuthFromParent"],"sources":["../src/config.ts","../src/parquet.ts","../src/client.ts"],"sourcesContent":["/**\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 * Parent window origin for postMessage authentication (iframe scenarios)\n * Required when using postMessage auth for security.\n * Example: 'https://app.example.com'\n * Use '*' only in development!\n */\n parentOrigin?: string;\n\n /**\n * Disable automatic postMessage authentication even if in iframe\n * Set to true if you want to provide API key manually in iframe context\n */\n disableAutoAuth?: boolean;\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 parentOrigin: overrides?.parentOrigin,\n disableAutoAuth: overrides?.disableAutoAuth,\n };\n}\n\n/**\n * Creates an Oxy configuration asynchronously with support for postMessage authentication\n *\n * This is the recommended method for iframe scenarios where authentication\n * needs to be obtained from the parent window via postMessage.\n *\n * When running in an iframe without an API key, this function will:\n * 1. Detect the iframe context\n * 2. Send an authentication request to the parent window\n * 3. Wait for the parent to respond with credentials\n * 4. Return the configured client\n *\n * Environment variables (fallback):\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 Promise resolving to OxyConfig object\n * @throws Error if required configuration is missing\n * @throws PostMessageAuthTimeoutError if parent doesn't respond\n *\n * @example\n * ```typescript\n * // Automatic iframe detection and authentication\n * const config = await createConfigAsync({\n * parentOrigin: 'https://app.example.com',\n * projectId: 'my-project-id',\n * baseUrl: 'https://api.oxy.tech'\n * });\n * ```\n */\nexport async function createConfigAsync(\n overrides?: Partial<OxyConfig>\n): Promise<OxyConfig> {\n // Import postMessage utilities (dynamic to avoid circular deps)\n const { isInIframe, requestAuthFromParent } = await import('./auth/postMessage');\n\n // Start with environment variables and overrides\n let baseUrl = overrides?.baseUrl || process.env.OXY_URL;\n let apiKey = overrides?.apiKey || process.env.OXY_API_KEY;\n let projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;\n\n const disableAutoAuth = overrides?.disableAutoAuth ?? false;\n const parentOrigin = overrides?.parentOrigin;\n\n // Automatic iframe detection and authentication\n if (!disableAutoAuth && isInIframe() && !apiKey) {\n if (!parentOrigin) {\n console.warn(\n '[Oxy SDK] Running in iframe without API key and no parentOrigin specified. ' +\n 'PostMessage authentication will be skipped. ' +\n 'Provide parentOrigin config to enable automatic authentication.'\n );\n } else {\n try {\n const authResult = await requestAuthFromParent({\n parentOrigin,\n timeout: overrides?.timeout || 5000,\n });\n\n apiKey = authResult.apiKey;\n if (authResult.projectId) {\n projectId = authResult.projectId;\n }\n if (authResult.baseUrl) {\n baseUrl = authResult.baseUrl;\n }\n\n console.log('[Oxy SDK] Successfully authenticated via postMessage');\n } catch (error) {\n console.error(\n '[Oxy SDK] Failed to authenticate via postMessage:',\n (error as Error).message\n );\n // Fall through to use environment variables if available\n }\n }\n }\n\n // Validation\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 parentOrigin,\n disableAutoAuth,\n };\n}\n","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, createConfigAsync } 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 * Creates an OxyClient instance asynchronously with support for postMessage authentication\n *\n * This is the recommended method when using the SDK in an iframe that needs to\n * obtain authentication from the parent window via postMessage.\n *\n * @param config - Optional configuration overrides\n * @returns Promise resolving to OxyClient instance\n * @throws Error if required configuration is missing\n * @throws PostMessageAuthTimeoutError if parent doesn't respond\n *\n * @example\n * ```typescript\n * // In an iframe - automatic postMessage auth\n * const client = await OxyClient.create({\n * parentOrigin: 'https://app.example.com',\n * projectId: 'my-project-id',\n * baseUrl: 'https://api.oxy.tech'\n * });\n *\n * // Use the client normally\n * const apps = await client.listApps();\n * ```\n */\n static async create(config?: Partial<OxyConfig>): Promise<OxyClient> {\n const resolvedConfig = await createConfigAsync(config);\n return new OxyClient(resolvedConfig);\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"],"mappings":";;;;;;;;;;;;;;;;;;AAyDA,SAAgB,aAAa,WAA2C;CACtE,MAAM,UAAU,WAAW,WAAW,QAAQ,IAAI;CAClD,MAAM,SAAS,WAAW,UAAU,QAAQ,IAAI;CAChD,MAAM,YAAY,WAAW,aAAa,QAAQ,IAAI;AAEtD,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,6DAA6D;AAG/E,KAAI,CAAC,UACH,OAAM,IAAI,MAAM,sEAAsE;AAGxF,QAAO;EACL,SAAS,QAAQ,QAAQ,OAAO,GAAG;EACnC;EACA;EACA,QAAQ,WAAW,UAAU,QAAQ,IAAI;EACzC,SAAS,WAAW,WAAW;EAC/B,cAAc,WAAW;EACzB,iBAAiB,WAAW;EAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCH,eAAsB,kBACpB,WACoB;CAEpB,MAAM,EAAE,0BAAY,mDAA0B,MAAM,OAAO;CAG3D,IAAI,UAAU,WAAW,WAAW,QAAQ,IAAI;CAChD,IAAI,SAAS,WAAW,UAAU,QAAQ,IAAI;CAC9C,IAAI,YAAY,WAAW,aAAa,QAAQ,IAAI;CAEpD,MAAM,kBAAkB,WAAW,mBAAmB;CACtD,MAAM,eAAe,WAAW;AAGhC,KAAI,CAAC,mBAAmBA,cAAY,IAAI,CAAC,OACvC,KAAI,CAAC,aACH,SAAQ,KACN,yLAGD;KAED,KAAI;EACF,MAAM,aAAa,MAAMC,wBAAsB;GAC7C;GACA,SAAS,WAAW,WAAW;GAChC,CAAC;AAEF,WAAS,WAAW;AACpB,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