@dashai/sdk 0.8.0 → 0.10.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.js CHANGED
@@ -343,7 +343,7 @@ function createHttp(config) {
343
343
  async function request(method, path, body) {
344
344
  const normalizedPath = `/${path.replace(LEADING_SLASH, "")}`;
345
345
  const url = normalizedPath.startsWith("/api/") ? `${config.baseUrl.replace(/\/+$/, "")}${normalizedPath}` : `${base}${normalizedPath}`;
346
- const token = config.getToken ? await config.getToken() : null;
346
+ const token = (config.getToken ? await config.getToken() : null) ?? config.apiKey ?? null;
347
347
  const headers = { ...staticHeaders };
348
348
  if (body !== void 0) {
349
349
  headers["Content-Type"] = "application/json";
@@ -527,6 +527,7 @@ function createClient(config) {
527
527
  deps(providerSlug) {
528
528
  return makeDeps(transport, providerSlug);
529
529
  },
530
+ datahub: makeDatahubClient(transport),
530
531
  request(method, path, body) {
531
532
  return transport.request(method, path, body);
532
533
  },
@@ -591,6 +592,148 @@ function assertValidTableSlug2(slug) {
591
592
  );
592
593
  }
593
594
  }
595
+ function makeDatahubClient(transport) {
596
+ return {
597
+ table(tableId) {
598
+ assertValidTableId(tableId);
599
+ const root = `/datahub/tables/${tableId}/rows`;
600
+ return {
601
+ async list(opts) {
602
+ const arg = opts;
603
+ const flat = isDatahubListOpts(
604
+ arg
605
+ ) ? compileDatahubListOpts(arg) : arg;
606
+ const qs = flat ? new URLSearchParams(flat).toString() : "";
607
+ const path = qs ? `${root}?${qs}` : root;
608
+ return transport.request("GET", path);
609
+ },
610
+ async get(rowId) {
611
+ return transport.request(
612
+ "GET",
613
+ `${root}/${rowId}`
614
+ );
615
+ },
616
+ async create(body) {
617
+ return transport.request("POST", root, body);
618
+ },
619
+ async update(rowId, body) {
620
+ return transport.request(
621
+ "PATCH",
622
+ `${root}/${rowId}`,
623
+ body
624
+ );
625
+ },
626
+ async delete(rowId) {
627
+ return transport.request(
628
+ "DELETE",
629
+ `${root}/${rowId}`
630
+ );
631
+ }
632
+ };
633
+ }
634
+ };
635
+ }
636
+ function assertValidTableId(tableId) {
637
+ if (!Number.isInteger(tableId) || tableId <= 0) {
638
+ throw new Error(
639
+ `@dashai/sdk: invalid Data Hub table id "${tableId}". Must be a positive integer.`
640
+ );
641
+ }
642
+ }
643
+ var DATAHUB_LIST_OPT_KEYS = /* @__PURE__ */ new Set([
644
+ "filter",
645
+ "sort",
646
+ "search",
647
+ "limit",
648
+ "page"
649
+ ]);
650
+ var FILTER_GROUP_KEYS = /* @__PURE__ */ new Set(["AND", "OR"]);
651
+ function isDatahubListOpts(opts) {
652
+ if (opts === void 0 || opts === null || typeof opts !== "object") {
653
+ return false;
654
+ }
655
+ return Object.keys(opts).some((k) => DATAHUB_LIST_OPT_KEYS.has(k));
656
+ }
657
+ function compileDatahubListOpts(opts) {
658
+ const out = {};
659
+ if (opts.filter !== void 0) {
660
+ const where = opts.filter;
661
+ if (hasFilterGrouping(where)) {
662
+ out.filters = JSON.stringify(whereToBackendTree(where));
663
+ } else {
664
+ for (const [field, value] of Object.entries(where)) {
665
+ if (value === void 0) continue;
666
+ if (isFilterOpsObject(value)) {
667
+ for (const [op, opValue] of Object.entries(value)) {
668
+ if (opValue === void 0) continue;
669
+ out[`filter__${field}__${op}`] = stringifyFilterValue(opValue);
670
+ }
671
+ } else {
672
+ out[`filter__${field}__equal`] = stringifyFilterValue(value);
673
+ }
674
+ }
675
+ }
676
+ }
677
+ if (opts.sort !== void 0 && opts.sort.length > 0) {
678
+ out.order_by = opts.sort.map(
679
+ (s) => s.dir === "desc" ? `-${s.field}` : s.field
680
+ ).join(",");
681
+ }
682
+ if (opts.search !== void 0) out.search = opts.search;
683
+ if (typeof opts.limit === "number") out.size = String(opts.limit);
684
+ if (typeof opts.page === "number") out.page = String(opts.page);
685
+ if (opts.filter !== void 0 || opts.sort !== void 0 && opts.sort.length > 0) {
686
+ out.user_field_names = "true";
687
+ }
688
+ return out;
689
+ }
690
+ function hasFilterGrouping(where) {
691
+ return Object.keys(where).some((k) => FILTER_GROUP_KEYS.has(k));
692
+ }
693
+ function isFilterGroupNode(node) {
694
+ return Object.keys(node).some((k) => FILTER_GROUP_KEYS.has(k));
695
+ }
696
+ function fieldLeaves(node) {
697
+ const leaves = [];
698
+ for (const [field, value] of Object.entries(node)) {
699
+ if (FILTER_GROUP_KEYS.has(field) || value === void 0) continue;
700
+ if (isFilterOpsObject(value)) {
701
+ for (const [op, opValue] of Object.entries(value)) {
702
+ if (opValue === void 0) continue;
703
+ leaves.push({ field, type: op, value: stringifyFilterValue(opValue) });
704
+ }
705
+ } else {
706
+ leaves.push({ field, type: "equal", value: stringifyFilterValue(value) });
707
+ }
708
+ }
709
+ return leaves;
710
+ }
711
+ function whereToBackendTree(where, defaultType = "AND") {
712
+ const obj = where;
713
+ const groupKey = Object.keys(obj).find((k) => FILTER_GROUP_KEYS.has(k));
714
+ if (groupKey) {
715
+ const children = obj[groupKey] ?? [];
716
+ const filters = [];
717
+ const filter_groups = [];
718
+ for (const child of children) {
719
+ const childObj = child;
720
+ if (isFilterGroupNode(childObj)) {
721
+ filter_groups.push(whereToBackendTree(child));
722
+ } else {
723
+ filters.push(...fieldLeaves(childObj));
724
+ }
725
+ }
726
+ return { filter_type: groupKey, filters, filter_groups };
727
+ }
728
+ return { filter_type: defaultType, filters: fieldLeaves(obj), filter_groups: [] };
729
+ }
730
+ function isFilterOpsObject(value) {
731
+ return typeof value === "object" && value !== null && !Array.isArray(value);
732
+ }
733
+ function stringifyFilterValue(value) {
734
+ if (value === null) return "";
735
+ return String(value);
736
+ }
594
737
  function makeTableClient(transport, tableSlug) {
595
738
  const root = `/db/${tableSlug}`;
596
739
  return {
@@ -643,7 +786,7 @@ function createDevClient(overrides = {}) {
643
786
  const moduleEntry = overrides.moduleSlug ? fileConfig?.modules?.[overrides.moduleSlug] : void 0;
644
787
  const runtimeToken = readEnv("DASHWISE_RUNTIME_TOKEN") ?? moduleEntry?.runtimeToken ?? null;
645
788
  const installationId = overrides.installationId ?? readEnv("DASHWISE_INSTALLATION_ID") ?? (runtimeToken !== null ? "sandbox" : "local");
646
- const snapshotToken = runtimeToken ?? readEnv("DASHWISE_API_TOKEN") ?? fileConfig?.token ?? null;
789
+ const snapshotToken = runtimeToken ?? readEnv("DASHWISE_API_KEY") ?? readEnv("DASHWISE_API_TOKEN") ?? fileConfig?.token ?? null;
647
790
  const resolvedGetToken = overrides.getToken ?? (() => snapshotToken);
648
791
  return createClient({
649
792
  ...overrides,
@@ -701,7 +844,7 @@ function isPlausibleFileShape(v) {
701
844
  }
702
845
 
703
846
  // src/index.ts
704
- var VERSION = "0.8.0" ;
847
+ var VERSION = "0.10.0" ;
705
848
 
706
849
  export { ContractViolationError, DashwiseError, DashwiseErrorCode, DependencyContractError, ForbiddenError, JoinError, NetworkError, OperationNotAllowedByProviderError, ProviderTableMissingError, RowNotFoundError, TimeoutError, UnauthenticatedError, VERSION, ValidationError, createClient, createDevClient, fromBackendEnvelope };
707
850
  //# sourceMappingURL=index.js.map