@xylex-group/athena 2.3.0 → 2.4.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.
Files changed (42) hide show
  1. package/README.md +195 -106
  2. package/dist/browser.cjs +870 -154
  3. package/dist/browser.cjs.map +1 -1
  4. package/dist/browser.d.cts +7 -7
  5. package/dist/browser.d.ts +7 -7
  6. package/dist/browser.js +869 -155
  7. package/dist/browser.js.map +1 -1
  8. package/dist/cli/index.cjs +905 -144
  9. package/dist/cli/index.cjs.map +1 -1
  10. package/dist/cli/index.d.cts +3 -3
  11. package/dist/cli/index.d.ts +3 -3
  12. package/dist/cli/index.js +905 -144
  13. package/dist/cli/index.js.map +1 -1
  14. package/dist/index.cjs +870 -154
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +7 -7
  17. package/dist/index.d.ts +7 -7
  18. package/dist/index.js +869 -155
  19. package/dist/index.js.map +1 -1
  20. package/dist/{model-form-hoE2jHIi.d.cts → model-form-4LPnOPAF.d.cts} +9 -5
  21. package/dist/{model-form-BpDXlbxb.d.ts → model-form-CO4-LmNC.d.ts} +9 -5
  22. package/dist/{pipeline-BNIw8pDQ.d.ts → pipeline-CR4V15jF.d.ts} +1 -1
  23. package/dist/{pipeline-DNIpEsN8.d.cts → pipeline-DZeExYMA.d.cts} +1 -1
  24. package/dist/{react-email-qPA1wjFV.d.ts → react-email-6mOyxBo4.d.cts} +60 -13
  25. package/dist/{react-email-BvyCZnfW.d.cts → react-email-Buhcpglm.d.ts} +60 -13
  26. package/dist/react.cjs +333 -16
  27. package/dist/react.cjs.map +1 -1
  28. package/dist/react.d.cts +4 -4
  29. package/dist/react.d.ts +4 -4
  30. package/dist/react.js +333 -16
  31. package/dist/react.js.map +1 -1
  32. package/dist/{types-A5e97acl.d.cts → types-09Q4D86N.d.cts} +23 -2
  33. package/dist/{types-A5e97acl.d.ts → types-09Q4D86N.d.ts} +23 -2
  34. package/dist/{types-bDlr4u7p.d.cts → types-D1JvL21V.d.cts} +1 -1
  35. package/dist/{types-BnD22-vb.d.ts → types-DU3gNdFv.d.ts} +1 -1
  36. package/dist/utils.cjs +131 -0
  37. package/dist/utils.cjs.map +1 -1
  38. package/dist/utils.d.cts +42 -1
  39. package/dist/utils.d.ts +42 -1
  40. package/dist/utils.js +117 -1
  41. package/dist/utils.js.map +1 -1
  42. package/package.json +40 -40
package/dist/react.cjs CHANGED
@@ -65,13 +65,114 @@ function isAthenaGatewayError(error) {
65
65
  return error instanceof AthenaGatewayError;
66
66
  }
67
67
 
68
+ // src/gateway/url.ts
69
+ var ATHENA_DEFAULT_BASE_URL = "https://athena-db.com";
70
+ function describeReceivedValue(value) {
71
+ if (value === void 0) return "undefined";
72
+ if (value === null) return "null";
73
+ if (typeof value === "string") {
74
+ return value.trim().length > 0 ? JSON.stringify(value) : "an empty string";
75
+ }
76
+ return `${typeof value} ${JSON.stringify(value)}`;
77
+ }
78
+ function invalidBaseUrlError(message, hint) {
79
+ return new AthenaGatewayError({
80
+ code: "INVALID_URL",
81
+ message,
82
+ status: 0,
83
+ hint
84
+ });
85
+ }
86
+ function normalizeAthenaGatewayBaseUrl(input, options = {}) {
87
+ const label = options.label ?? "Athena gateway base URL";
88
+ const candidate = input ?? options.defaultBaseUrl;
89
+ if (candidate === void 0 || candidate === null) {
90
+ throw invalidBaseUrlError(
91
+ `${label} must be a non-empty absolute http(s) URL. Received ${describeReceivedValue(input)}.`,
92
+ 'Set ATHENA_URL (or pass createClient(url, ...)) to a full URL such as "https://mirror3.athena-db.com".'
93
+ );
94
+ }
95
+ const trimmed = candidate.trim();
96
+ if (!trimmed) {
97
+ throw invalidBaseUrlError(
98
+ `${label} must be a non-empty absolute http(s) URL. Received ${describeReceivedValue(candidate)}.`,
99
+ 'Set ATHENA_URL (or pass createClient(url, ...)) to a full URL such as "https://mirror3.athena-db.com".'
100
+ );
101
+ }
102
+ let parsed;
103
+ try {
104
+ parsed = new URL(trimmed);
105
+ } catch {
106
+ throw invalidBaseUrlError(
107
+ `${label} must be a valid absolute http(s) URL. Received ${describeReceivedValue(candidate)}.`,
108
+ 'Use a full URL including the protocol, for example "https://mirror3.athena-db.com".'
109
+ );
110
+ }
111
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
112
+ throw invalidBaseUrlError(
113
+ `${label} must use http or https. Received ${JSON.stringify(trimmed)}.`,
114
+ 'Use an Athena gateway URL such as "https://mirror3.athena-db.com".'
115
+ );
116
+ }
117
+ if (parsed.search || parsed.hash) {
118
+ throw invalidBaseUrlError(
119
+ `${label} must not include query parameters or hash fragments. Received ${JSON.stringify(trimmed)}.`,
120
+ 'Pass only the base URL. Endpoint paths such as "/gateway/fetch" are appended by the SDK.'
121
+ );
122
+ }
123
+ return parsed.toString().replace(/\/+$/, "");
124
+ }
125
+ function buildAthenaGatewayUrl(baseUrl, path) {
126
+ if (!path.startsWith("/")) {
127
+ throw invalidBaseUrlError(
128
+ `Athena gateway path must start with "/". Received ${JSON.stringify(path)}.`,
129
+ 'Use a leading slash such as "/gateway/fetch" or "/".'
130
+ );
131
+ }
132
+ return `${baseUrl}${path}`;
133
+ }
134
+
135
+ // src/cookies/cookie-utils.ts
136
+ var SECURE_COOKIE_PREFIX = "__Secure-";
137
+ function parseCookies(cookieHeader) {
138
+ const cookies = cookieHeader.split("; ");
139
+ const cookieMap = /* @__PURE__ */ new Map();
140
+ cookies.forEach((cookie) => {
141
+ const [name, value] = cookie.split(/=(.*)/s);
142
+ cookieMap.set(name, value);
143
+ });
144
+ return cookieMap;
145
+ }
146
+ var getSessionCookie = (request, config) => {
147
+ const cookies = (request instanceof Headers || !("headers" in request) ? request : request.headers).get("cookie");
148
+ if (!cookies) {
149
+ return null;
150
+ }
151
+ const { cookieName = "session_token", cookiePrefix = "athena-auth" } = {};
152
+ const parsedCookie = parseCookies(cookies);
153
+ const getCookie = (name) => parsedCookie.get(name) || parsedCookie.get(`${SECURE_COOKIE_PREFIX}${name}`);
154
+ const sessionToken = getCookie(`${cookiePrefix}.${cookieName}`) || getCookie(`${cookiePrefix}-${cookieName}`);
155
+ if (sessionToken) {
156
+ return sessionToken;
157
+ }
158
+ return null;
159
+ };
160
+
161
+ // package.json
162
+ var package_default = {
163
+ version: "2.4.1"
164
+ };
165
+
166
+ // src/sdk-version.ts
167
+ var PACKAGE_VERSION = package_default.version;
168
+ function buildSdkHeaderValue(sdkName) {
169
+ return `${sdkName} ${PACKAGE_VERSION}`;
170
+ }
171
+
68
172
  // src/gateway/client.ts
69
- var DEFAULT_BASE_URL = "https://athena-db.com";
70
173
  var DEFAULT_CLIENT = "railway_direct";
71
- var FALLBACK_SDK_VERSION = "1.3.0";
72
174
  var SDK_NAME = "xylex-group/athena";
73
- var SDK_VERSION = typeof process !== "undefined" && process?.env?.npm_package_version ? process.env.npm_package_version : FALLBACK_SDK_VERSION;
74
- var SDK_HEADER_VALUE = `${SDK_NAME} ${SDK_VERSION}`;
175
+ var SDK_HEADER_VALUE = buildSdkHeaderValue(SDK_NAME);
75
176
  function parseResponseBody(rawText, contentType) {
76
177
  if (!rawText) {
77
178
  return { parsed: null, parseFailed: false };
@@ -90,6 +191,37 @@ function parseResponseBody(rawText, contentType) {
90
191
  function normalizeHeaderValue(value) {
91
192
  return value ? value : void 0;
92
193
  }
194
+ function resolveHeaderValue(headers, candidates) {
195
+ for (const candidate of candidates) {
196
+ const direct = normalizeHeaderValue(headers[candidate]);
197
+ if (direct) return direct;
198
+ }
199
+ const loweredCandidates = new Set(candidates.map((candidate) => candidate.toLowerCase()));
200
+ for (const [key, value] of Object.entries(headers)) {
201
+ if (!loweredCandidates.has(key.toLowerCase())) {
202
+ continue;
203
+ }
204
+ const normalized = normalizeHeaderValue(value);
205
+ if (normalized) return normalized;
206
+ }
207
+ return void 0;
208
+ }
209
+ function resolveBearerTokenFromAuthorizationHeader(headers) {
210
+ const authorization = resolveHeaderValue(headers, ["Authorization"]);
211
+ if (!authorization) {
212
+ return void 0;
213
+ }
214
+ const match = authorization.match(/^Bearer\s+(.+)$/i);
215
+ const token = match?.[1]?.trim();
216
+ return token ? token : void 0;
217
+ }
218
+ function resolveSessionTokenFromCookieHeader(headers) {
219
+ const cookie = resolveHeaderValue(headers, ["Cookie"]);
220
+ if (!cookie) {
221
+ return void 0;
222
+ }
223
+ return getSessionCookie(new Headers({ cookie })) ?? void 0;
224
+ }
93
225
  function isRecord(value) {
94
226
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
95
227
  }
@@ -207,7 +339,7 @@ function buildRpcGetEndpoint(payload) {
207
339
  status: 0
208
340
  });
209
341
  }
210
- query.set(filter.column, toRpcFilterQueryValue(filter));
342
+ query.append(filter.column, toRpcFilterQueryValue(filter));
211
343
  }
212
344
  }
213
345
  const endpoint = `/rpc/${encodeURIComponent(functionName)}`;
@@ -253,6 +385,20 @@ function buildHeaders(config, options) {
253
385
  headers["apikey"] = finalApiKey;
254
386
  headers["x-api-key"] = headers["x-api-key"] ?? finalApiKey;
255
387
  }
388
+ const explicitSessionToken = resolveHeaderValue(extraHeaders, [
389
+ "X-Athena-Auth-Session-Token"
390
+ ]);
391
+ const derivedSessionToken = explicitSessionToken ?? resolveSessionTokenFromCookieHeader(extraHeaders);
392
+ if (derivedSessionToken) {
393
+ headers["X-Athena-Auth-Session-Token"] = derivedSessionToken;
394
+ }
395
+ const explicitBearerToken = resolveHeaderValue(extraHeaders, [
396
+ "X-Athena-Auth-Bearer-Token"
397
+ ]);
398
+ const derivedBearerToken = explicitBearerToken ?? resolveBearerTokenFromAuthorizationHeader(extraHeaders);
399
+ if (derivedBearerToken) {
400
+ headers["X-Athena-Auth-Bearer-Token"] = derivedBearerToken;
401
+ }
256
402
  const athenaClientKeys = ["x-athena-client", "X-Athena-Client"];
257
403
  Object.entries(extraHeaders).forEach(([key, value]) => {
258
404
  if (athenaClientKeys.includes(key)) return;
@@ -263,9 +409,168 @@ function buildHeaders(config, options) {
263
409
  });
264
410
  return headers;
265
411
  }
412
+ function toInvalidUrlResponse(error, endpoint, method) {
413
+ const message = error instanceof Error ? error.message : String(error);
414
+ const gatewayError = error instanceof AthenaGatewayError ? error : new AthenaGatewayError({
415
+ code: "INVALID_URL",
416
+ message,
417
+ status: 0,
418
+ endpoint,
419
+ method,
420
+ cause: message,
421
+ hint: "Set ATHENA_URL to a full http(s) URL before running queries."
422
+ });
423
+ return {
424
+ ok: false,
425
+ status: 0,
426
+ statusText: null,
427
+ data: null,
428
+ error: gatewayError.message,
429
+ errorDetails: detailsFromError(
430
+ new AthenaGatewayError({
431
+ code: gatewayError.code,
432
+ message: gatewayError.message,
433
+ status: gatewayError.status,
434
+ endpoint,
435
+ method,
436
+ requestId: gatewayError.requestId,
437
+ hint: gatewayError.hint,
438
+ cause: gatewayError.causeDetail
439
+ })
440
+ ),
441
+ raw: null
442
+ };
443
+ }
444
+ function resolveGatewayBaseUrl(input) {
445
+ return normalizeAthenaGatewayBaseUrl(input, {
446
+ defaultBaseUrl: ATHENA_DEFAULT_BASE_URL
447
+ });
448
+ }
449
+ function resolveProbePath(path) {
450
+ if (!path) return "/";
451
+ if (!path.startsWith("/")) {
452
+ throw new AthenaGatewayError({
453
+ code: "INVALID_URL",
454
+ message: `Athena gateway probe path must start with "/". Received ${JSON.stringify(path)}.`,
455
+ status: 0,
456
+ hint: 'Use a leading slash such as "/" or "/health".'
457
+ });
458
+ }
459
+ return path;
460
+ }
461
+ function mergeConnectionHeaders(baseHeaders, headers) {
462
+ const merged = {
463
+ ...baseHeaders,
464
+ ...headers ?? {}
465
+ };
466
+ if (!merged["X-Athena-Sdk"] && !merged["x-athena-sdk"]) {
467
+ merged["X-Athena-Sdk"] = SDK_HEADER_VALUE;
468
+ }
469
+ return merged;
470
+ }
471
+ async function performConnectionCheck(baseUrl, requestHeaders, options) {
472
+ const path = resolveProbePath(options?.path);
473
+ const url = buildAthenaGatewayUrl(baseUrl, path);
474
+ try {
475
+ const response = await fetch(url, {
476
+ method: "GET",
477
+ headers: mergeConnectionHeaders(requestHeaders, options?.headers),
478
+ signal: options?.signal
479
+ });
480
+ const rawText = await response.text();
481
+ const requestId = resolveRequestId(response.headers);
482
+ const parsedBody = parseResponseBody(
483
+ rawText ?? "",
484
+ response.headers.get("content-type")
485
+ );
486
+ if (parsedBody.parseFailed) {
487
+ const invalidJsonError = new AthenaGatewayError({
488
+ code: "INVALID_JSON",
489
+ message: "Gateway probe returned malformed JSON",
490
+ status: response.status,
491
+ method: "GET",
492
+ requestId,
493
+ hint: "Verify the gateway response body is valid JSON.",
494
+ cause: rawText.slice(0, 300)
495
+ });
496
+ return {
497
+ ok: false,
498
+ reachable: true,
499
+ status: response.status,
500
+ statusText: resolveStatusText(response, parsedBody.parsed),
501
+ baseUrl,
502
+ url,
503
+ error: invalidJsonError.message,
504
+ errorDetails: detailsFromError(invalidJsonError),
505
+ raw: parsedBody.parsed
506
+ };
507
+ }
508
+ const parsed = parsedBody.parsed;
509
+ if (!response.ok) {
510
+ const httpError = new AthenaGatewayError({
511
+ code: "HTTP_ERROR",
512
+ message: resolveErrorMessage(
513
+ parsed,
514
+ `Athena gateway GET ${path} failed with status ${response.status}`
515
+ ),
516
+ status: response.status,
517
+ method: "GET",
518
+ requestId,
519
+ hint: resolveErrorHint(parsed)
520
+ });
521
+ return {
522
+ ok: false,
523
+ reachable: true,
524
+ status: response.status,
525
+ statusText: resolveStatusText(response, parsed),
526
+ baseUrl,
527
+ url,
528
+ error: httpError.message,
529
+ errorDetails: detailsFromError(httpError),
530
+ raw: parsed
531
+ };
532
+ }
533
+ return {
534
+ ok: true,
535
+ reachable: true,
536
+ status: response.status,
537
+ statusText: resolveStatusText(response, parsed),
538
+ baseUrl,
539
+ url,
540
+ error: void 0,
541
+ errorDetails: null,
542
+ raw: parsed
543
+ };
544
+ } catch (callError) {
545
+ const message = callError instanceof Error ? callError.message : String(callError);
546
+ const networkError = new AthenaGatewayError({
547
+ code: "NETWORK_ERROR",
548
+ message: `Network error while probing Athena gateway ${url}: ${message}`,
549
+ method: "GET",
550
+ cause: message,
551
+ hint: "Check gateway URL, DNS, and network reachability."
552
+ });
553
+ return {
554
+ ok: false,
555
+ reachable: false,
556
+ status: 0,
557
+ statusText: null,
558
+ baseUrl,
559
+ url,
560
+ error: networkError.message,
561
+ errorDetails: detailsFromError(networkError),
562
+ raw: null
563
+ };
564
+ }
565
+ }
266
566
  async function callAthena(config, endpoint, method, payload, options) {
267
- const baseUrl = (options?.baseUrl ?? config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
268
- const url = `${baseUrl}${endpoint}`;
567
+ let baseUrl;
568
+ try {
569
+ baseUrl = resolveGatewayBaseUrl(options?.baseUrl ?? config.baseUrl);
570
+ } catch (error) {
571
+ return toInvalidUrlResponse(error, endpoint, method);
572
+ }
573
+ const url = buildAthenaGatewayUrl(baseUrl, endpoint);
269
574
  const headers = buildHeaders(config, options);
270
575
  try {
271
576
  const requestInit = {
@@ -362,32 +667,44 @@ async function callAthena(config, endpoint, method, payload, options) {
362
667
  }
363
668
  }
364
669
  function createAthenaGatewayClient(config = {}) {
670
+ const normalizedBaseUrl = resolveGatewayBaseUrl(config.baseUrl);
671
+ const normalizedConfig = {
672
+ ...config,
673
+ baseUrl: normalizedBaseUrl
674
+ };
365
675
  return {
366
- baseUrl: (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, ""),
676
+ baseUrl: normalizedBaseUrl,
367
677
  buildHeaders(options) {
368
- return buildHeaders(config, options);
678
+ return buildHeaders(normalizedConfig, options);
679
+ },
680
+ verifyConnection(options) {
681
+ return performConnectionCheck(
682
+ normalizedBaseUrl,
683
+ buildHeaders(normalizedConfig),
684
+ options
685
+ );
369
686
  },
370
687
  fetchGateway(payload, options) {
371
- return callAthena(config, "/gateway/fetch", "POST", payload, options);
688
+ return callAthena(normalizedConfig, "/gateway/fetch", "POST", payload, options);
372
689
  },
373
690
  insertGateway(payload, options) {
374
- return callAthena(config, "/gateway/insert", "PUT", payload, options);
691
+ return callAthena(normalizedConfig, "/gateway/insert", "PUT", payload, options);
375
692
  },
376
693
  updateGateway(payload, options) {
377
- return callAthena(config, "/gateway/update", "POST", payload, options);
694
+ return callAthena(normalizedConfig, "/gateway/update", "POST", payload, options);
378
695
  },
379
696
  deleteGateway(payload, options) {
380
- return callAthena(config, "/gateway/delete", "DELETE", payload, options);
697
+ return callAthena(normalizedConfig, "/gateway/delete", "DELETE", payload, options);
381
698
  },
382
699
  rpcGateway(payload, options) {
383
700
  if (options?.get) {
384
701
  const endpoint = buildRpcGetEndpoint(payload);
385
- return callAthena(config, endpoint, "GET", null, options);
702
+ return callAthena(normalizedConfig, endpoint, "GET", null, options);
386
703
  }
387
- return callAthena(config, "/gateway/rpc", "POST", payload, options);
704
+ return callAthena(normalizedConfig, "/gateway/rpc", "POST", payload, options);
388
705
  },
389
706
  queryGateway(payload, options) {
390
- return callAthena(config, "/gateway/query", "POST", payload, options);
707
+ return callAthena(normalizedConfig, "/gateway/query", "POST", payload, options);
391
708
  }
392
709
  };
393
710
  }