@carto/api-client 0.4.2-alpha.0 → 0.4.2

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.
@@ -493,43 +493,41 @@ function executeModel(props) {
493
493
  data,
494
494
  filters,
495
495
  filtersLogicalOperator = 'and',
496
- spatialDataType = 'geo',
497
- spatialFiltersMode = 'intersects',
498
- spatialFiltersResolution = 0
496
+ geoColumn = DEFAULT_GEO_COLUMN
499
497
  } = source;
498
+ const queryParameters = source.queryParameters ? JSON.stringify(source.queryParameters) : '';
500
499
  const queryParams = {
501
500
  type,
502
501
  client: clientId,
503
502
  source: data,
504
- params,
505
- queryParameters: source.queryParameters || '',
506
- filters,
503
+ params: JSON.stringify(params),
504
+ queryParameters,
505
+ filters: JSON.stringify(filters),
507
506
  filtersLogicalOperator
508
507
  };
509
- const spatialDataColumn = source.spatialDataColumn || DEFAULT_GEO_COLUMN;
510
508
  // Picking Model API requires 'spatialDataColumn'.
511
509
  if (model === 'pick') {
512
- queryParams.spatialDataColumn = spatialDataColumn;
510
+ queryParams.spatialDataColumn = geoColumn;
513
511
  }
514
- // API supports multiple filters, we apply it only to spatialDataColumn
512
+ // API supports multiple filters, we apply it only to geoColumn
515
513
  const spatialFilters = source.spatialFilter ? {
516
- [spatialDataColumn]: source.spatialFilter
514
+ [geoColumn]: source.spatialFilter
517
515
  } : undefined;
518
516
  if (spatialFilters) {
519
- queryParams.spatialFilters = spatialFilters; // JSON.stringify(spatialFilters);
520
- queryParams.spatialDataColumn = spatialDataColumn;
521
- queryParams.spatialDataType = spatialDataType;
522
- }
523
- if (spatialDataType !== 'geo') {
524
- if (spatialFiltersResolution > 0) {
525
- queryParams.spatialFiltersResolution = spatialFiltersResolution;
526
- }
527
- queryParams.spatialFiltersMode = spatialFiltersMode;
517
+ queryParams.spatialFilters = JSON.stringify(spatialFilters);
528
518
  }
529
- const urlWithSearchParams = url + '?' + objectToURLSearchParams(queryParams).toString();
519
+ const urlWithSearchParams = url + '?' + new URLSearchParams(queryParams).toString();
530
520
  const isGet = urlWithSearchParams.length <= REQUEST_GET_MAX_URL_LENGTH;
531
521
  if (isGet) {
532
522
  url = urlWithSearchParams;
523
+ } else {
524
+ // undo the JSON.stringify, @TODO find a better pattern
525
+ queryParams.params = params;
526
+ queryParams.filters = filters;
527
+ queryParams.queryParameters = source.queryParameters;
528
+ if (spatialFilters) {
529
+ queryParams.spatialFilters = spatialFilters;
530
+ }
533
531
  }
534
532
  return makeCall({
535
533
  url,
@@ -541,75 +539,15 @@ function executeModel(props) {
541
539
  })
542
540
  });
543
541
  }
544
- function objectToURLSearchParams(object) {
545
- const params = new URLSearchParams();
546
- for (const key in object) {
547
- if (isPureObject(object[key])) {
548
- params.append(key, JSON.stringify(object[key]));
549
- } else if (Array.isArray(object[key])) {
550
- params.append(key, JSON.stringify(object[key]));
551
- } else if (object[key] === null) {
552
- params.append(key, 'null');
553
- } else if (object[key] !== undefined) {
554
- params.append(key, String(object[key]));
555
- }
556
- }
557
- return params;
558
- }
559
542
 
560
- const DEFAULT_TILE_SIZE = 512;
561
- const QUADBIN_ZOOM_MAX_OFFSET = 4;
562
- function getSpatialFiltersResolution({
563
- source,
564
- viewState
565
- }) {
566
- var _source$dataResolutio, _source$aggregationRe;
567
- assert(viewState, 'viewState prop is required to compute automatic spatialFiltersResolution when using spatialFilter with spatial indexes. Either pass a `spatialFiltersResolution` prop or a `viewState` prop to avoid this error');
568
- const dataResolution = (_source$dataResolutio = source.dataResolution) != null ? _source$dataResolutio : Number.MAX_VALUE;
569
- const aggregationResLevel = (_source$aggregationRe = source.aggregationResLevel) != null ? _source$aggregationRe : source.spatialDataType === 'h3' ? DEFAULT_AGGREGATION_RES_LEVEL_H3 : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN;
570
- const aggregationResLevelOffset = Math.max(0, Math.floor(aggregationResLevel));
571
- const currentZoomInt = Math.ceil(viewState.zoom);
572
- if (source.spatialDataType === 'h3') {
573
- var _maxH3SpatialFiltersR, _maxH3SpatialFiltersR2;
574
- const tileSize = DEFAULT_TILE_SIZE;
575
- const maxResolutionForZoom = (_maxH3SpatialFiltersR = (_maxH3SpatialFiltersR2 = maxH3SpatialFiltersResolutions.find(([zoom]) => zoom === currentZoomInt)) == null ? void 0 : _maxH3SpatialFiltersR2[1]) != null ? _maxH3SpatialFiltersR : Math.max(0, currentZoomInt - 3);
576
- const maxSpatialFiltersResolution = maxResolutionForZoom ? Math.min(dataResolution, maxResolutionForZoom) : dataResolution;
577
- const hexagonResolution = getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
578
- return Math.min(hexagonResolution, maxSpatialFiltersResolution);
579
- }
580
- if (source.spatialDataType === 'quadbin') {
581
- const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
582
- const maxSpatialFiltersResolution = Math.min(dataResolution, maxResolutionForZoom);
583
- const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
584
- return Math.min(quadsResolution, maxSpatialFiltersResolution);
585
- }
586
- return undefined;
587
- }
588
- const maxH3SpatialFiltersResolutions = [[20, 14], [19, 13], [18, 12], [17, 11], [16, 10], [15, 9], [14, 8], [13, 7], [12, 7], [11, 7], [10, 6], [9, 6], [8, 5], [7, 4], [6, 4], [5, 3], [4, 2], [3, 1], [2, 1], [1, 0]];
589
- // stolen from https://github.com/visgl/deck.gl/blob/master/modules/carto/src/layers/h3-tileset-2d.ts
590
- // Relative scale factor (0 = no biasing, 2 = a few hexagons cover view)
591
- const BIAS = 2;
592
- // Resolution conversion function. Takes a WebMercatorViewport and returns
593
- // a H3 resolution such that the screen space size of the hexagons is
594
- // similar
595
- function getHexagonResolution(viewport, tileSize) {
596
- // Difference in given tile size compared to deck's internal 512px tile size,
597
- // expressed as an offset to the viewport zoom.
598
- const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
599
- const hexagonScaleFactor = 2 / 3 * (viewport.zoom - zoomOffset);
600
- const latitudeScaleFactor = Math.log(1 / Math.cos(Math.PI * viewport.latitude / 180));
601
- // Clip and bias
602
- return Math.max(0, Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS));
603
- }
604
-
605
- const _excluded$1 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "abortController", "viewState"],
606
- _excluded2 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "abortController", "viewState"],
607
- _excluded3 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "abortController", "operationExp", "viewState"],
608
- _excluded4 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "abortController", "viewState"],
609
- _excluded5 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "abortController", "viewState"],
610
- _excluded6 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "abortController", "viewState"],
611
- _excluded7 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "abortController", "viewState"],
612
- _excluded8 = ["filterOwner", "abortController", "spatialFilter", "spatialFiltersMode", "viewState"];
543
+ const _excluded$1 = ["filterOwner", "spatialFilter", "abortController"],
544
+ _excluded2 = ["filterOwner", "spatialFilter", "abortController"],
545
+ _excluded3 = ["filterOwner", "spatialFilter", "abortController", "operationExp"],
546
+ _excluded4 = ["filterOwner", "spatialFilter", "abortController"],
547
+ _excluded5 = ["filterOwner", "spatialFilter", "abortController"],
548
+ _excluded6 = ["filterOwner", "spatialFilter", "abortController"],
549
+ _excluded7 = ["filterOwner", "spatialFilter", "abortController"],
550
+ _excluded8 = ["filterOwner", "abortController", "spatialFilter"];
613
551
  /**
614
552
  * Source for Widget API requests on a data source defined by a SQL query.
615
553
  *
@@ -630,9 +568,7 @@ class WidgetBaseSource {
630
568
  connectionName: props.connectionName,
631
569
  filters: getApplicableFilters(owner, props.filters),
632
570
  filtersLogicalOperator: props.filtersLogicalOperator,
633
- spatialDataType: props.spatialDataType,
634
- spatialDataColumn: props.spatialDataColumn,
635
- dataResolution: props.dataResolution
571
+ geoColumn: props.geoColumn
636
572
  };
637
573
  }
638
574
  /****************************************************************************
@@ -646,9 +582,7 @@ class WidgetBaseSource {
646
582
  const {
647
583
  filterOwner,
648
584
  spatialFilter,
649
- spatialFiltersMode,
650
- abortController,
651
- viewState
585
+ abortController
652
586
  } = options,
653
587
  params = _objectWithoutPropertiesLoose(options, _excluded$1);
654
588
  const {
@@ -656,19 +590,9 @@ class WidgetBaseSource {
656
590
  operation,
657
591
  operationColumn
658
592
  } = params;
659
- const source = this.getModelSource(filterOwner);
660
- let spatialFiltersResolution;
661
- if (spatialFilter && source.spatialDataType !== 'geo') {
662
- spatialFiltersResolution = getSpatialFiltersResolution({
663
- source,
664
- viewState
665
- });
666
- }
667
593
  return executeModel({
668
594
  model: 'category',
669
- source: _extends({}, source, {
670
- spatialFiltersResolution,
671
- spatialFiltersMode,
595
+ source: _extends({}, this.getModelSource(filterOwner), {
672
596
  spatialFilter
673
597
  }),
674
598
  params: {
@@ -696,9 +620,7 @@ class WidgetBaseSource {
696
620
  const {
697
621
  filterOwner,
698
622
  spatialFilter,
699
- spatialFiltersMode,
700
- abortController,
701
- viewState
623
+ abortController
702
624
  } = options,
703
625
  params = _objectWithoutPropertiesLoose(options, _excluded2);
704
626
  const {
@@ -709,19 +631,9 @@ class WidgetBaseSource {
709
631
  limit,
710
632
  tileResolution
711
633
  } = params;
712
- const source = this.getModelSource(filterOwner);
713
- let spatialFiltersResolution;
714
- if (spatialFilter && source.spatialDataType !== 'geo') {
715
- spatialFiltersResolution = getSpatialFiltersResolution({
716
- source,
717
- viewState
718
- });
719
- }
720
634
  return executeModel({
721
635
  model: 'pick',
722
- source: _extends({}, source, {
723
- spatialFiltersResolution,
724
- spatialFiltersMode,
636
+ source: _extends({}, this.getModelSource(filterOwner), {
725
637
  spatialFilter
726
638
  }),
727
639
  params: {
@@ -735,8 +647,11 @@ class WidgetBaseSource {
735
647
  opts: {
736
648
  abortController
737
649
  }
738
- }).then(res => ({
739
- rows: normalizeObjectKeys(res.rows)
650
+ // Avoid `normalizeObjectKeys()`, which changes column names.
651
+ }).then(({
652
+ rows
653
+ }) => ({
654
+ rows
740
655
  }));
741
656
  }
742
657
  /****************************************************************************
@@ -750,29 +665,17 @@ class WidgetBaseSource {
750
665
  const {
751
666
  filterOwner,
752
667
  spatialFilter,
753
- spatialFiltersMode,
754
668
  abortController,
755
- operationExp,
756
- viewState
669
+ operationExp
757
670
  } = options,
758
671
  params = _objectWithoutPropertiesLoose(options, _excluded3);
759
672
  const {
760
673
  column,
761
674
  operation
762
675
  } = params;
763
- const source = this.getModelSource(filterOwner);
764
- let spatialFiltersResolution;
765
- if (spatialFilter && source.spatialDataType !== 'geo') {
766
- spatialFiltersResolution = getSpatialFiltersResolution({
767
- source,
768
- viewState
769
- });
770
- }
771
676
  return executeModel({
772
677
  model: 'formula',
773
- source: _extends({}, source, {
774
- spatialFiltersResolution,
775
- spatialFiltersMode,
678
+ source: _extends({}, this.getModelSource(filterOwner), {
776
679
  spatialFilter
777
680
  }),
778
681
  params: {
@@ -796,9 +699,7 @@ class WidgetBaseSource {
796
699
  const {
797
700
  filterOwner,
798
701
  spatialFilter,
799
- spatialFiltersMode,
800
- abortController,
801
- viewState
702
+ abortController
802
703
  } = options,
803
704
  params = _objectWithoutPropertiesLoose(options, _excluded4);
804
705
  const {
@@ -806,19 +707,9 @@ class WidgetBaseSource {
806
707
  operation,
807
708
  ticks
808
709
  } = params;
809
- const source = this.getModelSource(filterOwner);
810
- let spatialFiltersResolution;
811
- if (spatialFilter && source.spatialDataType !== 'geo') {
812
- spatialFiltersResolution = getSpatialFiltersResolution({
813
- source,
814
- viewState
815
- });
816
- }
817
710
  const data = await executeModel({
818
711
  model: 'histogram',
819
- source: _extends({}, source, {
820
- spatialFiltersResolution,
821
- spatialFiltersMode,
712
+ source: _extends({}, this.getModelSource(filterOwner), {
822
713
  spatialFilter
823
714
  }),
824
715
  params: {
@@ -854,27 +745,15 @@ class WidgetBaseSource {
854
745
  const {
855
746
  filterOwner,
856
747
  spatialFilter,
857
- spatialFiltersMode,
858
- abortController,
859
- viewState
748
+ abortController
860
749
  } = options,
861
750
  params = _objectWithoutPropertiesLoose(options, _excluded5);
862
751
  const {
863
752
  column
864
753
  } = params;
865
- const source = this.getModelSource(filterOwner);
866
- let spatialFiltersResolution;
867
- if (spatialFilter && source.spatialDataType !== 'geo') {
868
- spatialFiltersResolution = getSpatialFiltersResolution({
869
- source,
870
- viewState
871
- });
872
- }
873
754
  return executeModel({
874
755
  model: 'range',
875
- source: _extends({}, source, {
876
- spatialFiltersResolution,
877
- spatialFiltersMode,
756
+ source: _extends({}, this.getModelSource(filterOwner), {
878
757
  spatialFilter
879
758
  }),
880
759
  params: {
@@ -896,9 +775,7 @@ class WidgetBaseSource {
896
775
  const {
897
776
  filterOwner,
898
777
  spatialFilter,
899
- spatialFiltersMode,
900
- abortController,
901
- viewState
778
+ abortController
902
779
  } = options,
903
780
  params = _objectWithoutPropertiesLoose(options, _excluded6);
904
781
  const {
@@ -907,21 +784,11 @@ class WidgetBaseSource {
907
784
  yAxisColumn,
908
785
  yAxisJoinOperation
909
786
  } = params;
910
- const source = this.getModelSource(filterOwner);
911
- let spatialFiltersResolution;
912
- if (spatialFilter && source.spatialDataType !== 'geo') {
913
- spatialFiltersResolution = getSpatialFiltersResolution({
914
- source,
915
- viewState
916
- });
917
- }
918
787
  // Make sure this is sync with the same constant in cloud-native/maps-api
919
788
  const HARD_LIMIT = 500;
920
789
  return executeModel({
921
790
  model: 'scatterplot',
922
- source: _extends({}, source, {
923
- spatialFiltersResolution,
924
- spatialFiltersMode,
791
+ source: _extends({}, this.getModelSource(filterOwner), {
925
792
  spatialFilter
926
793
  }),
927
794
  params: {
@@ -950,9 +817,7 @@ class WidgetBaseSource {
950
817
  const {
951
818
  filterOwner,
952
819
  spatialFilter,
953
- spatialFiltersMode,
954
- abortController,
955
- viewState
820
+ abortController
956
821
  } = options,
957
822
  params = _objectWithoutPropertiesLoose(options, _excluded7);
958
823
  const {
@@ -962,19 +827,9 @@ class WidgetBaseSource {
962
827
  offset = 0,
963
828
  limit = 10
964
829
  } = params;
965
- const source = this.getModelSource(filterOwner);
966
- let spatialFiltersResolution;
967
- if (spatialFilter && source.spatialDataType !== 'geo') {
968
- spatialFiltersResolution = getSpatialFiltersResolution({
969
- source,
970
- viewState
971
- });
972
- }
973
830
  return executeModel({
974
831
  model: 'table',
975
- source: _extends({}, source, {
976
- spatialFiltersResolution,
977
- spatialFiltersMode,
832
+ source: _extends({}, this.getModelSource(filterOwner), {
978
833
  spatialFilter
979
834
  }),
980
835
  params: {
@@ -1007,9 +862,7 @@ class WidgetBaseSource {
1007
862
  const {
1008
863
  filterOwner,
1009
864
  abortController,
1010
- spatialFilter,
1011
- spatialFiltersMode,
1012
- viewState
865
+ spatialFilter
1013
866
  } = options,
1014
867
  params = _objectWithoutPropertiesLoose(options, _excluded8);
1015
868
  const {
@@ -1023,19 +876,9 @@ class WidgetBaseSource {
1023
876
  splitByCategoryLimit,
1024
877
  splitByCategoryValues
1025
878
  } = params;
1026
- const source = this.getModelSource(filterOwner);
1027
- let spatialFiltersResolution;
1028
- if (spatialFilter && source.spatialDataType !== 'geo') {
1029
- spatialFiltersResolution = getSpatialFiltersResolution({
1030
- source,
1031
- viewState
1032
- });
1033
- }
1034
879
  return executeModel({
1035
880
  model: 'timeseries',
1036
- source: _extends({}, source, {
1037
- spatialFiltersResolution,
1038
- spatialFiltersMode,
881
+ source: _extends({}, this.getModelSource(filterOwner), {
1039
882
  spatialFilter
1040
883
  }),
1041
884
  params: {
@@ -1066,7 +909,8 @@ WidgetBaseSource.defaultProps = {
1066
909
  apiBaseUrl: DEFAULT_API_BASE_URL,
1067
910
  clientId: getClient(),
1068
911
  filters: {},
1069
- filtersLogicalOperator: 'and'
912
+ filtersLogicalOperator: 'and',
913
+ geoColumn: DEFAULT_GEO_COLUMN
1070
914
  };
1071
915
 
1072
916
  /**
@@ -1495,11 +1339,7 @@ const h3QuerySource = async function h3QuerySource(options) {
1495
1339
  urlParameters.filters = filters;
1496
1340
  }
1497
1341
  return baseSource('query', options, urlParameters).then(result => _extends({}, result, {
1498
- widgetSource: new WidgetQuerySource(_extends({}, options, {
1499
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
1500
- spatialDataColumn,
1501
- spatialDataType: 'h3'
1502
- }))
1342
+ widgetSource: new WidgetQuerySource(options)
1503
1343
  }));
1504
1344
  };
1505
1345
 
@@ -1524,11 +1364,7 @@ const h3TableSource = async function h3TableSource(options) {
1524
1364
  urlParameters.filters = filters;
1525
1365
  }
1526
1366
  return baseSource('table', options, urlParameters).then(result => _extends({}, result, {
1527
- widgetSource: new WidgetTableSource(_extends({}, options, {
1528
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
1529
- spatialDataColumn,
1530
- spatialDataType: 'h3'
1531
- }))
1367
+ widgetSource: new WidgetTableSource(options)
1532
1368
  }));
1533
1369
  };
1534
1370
 
@@ -1583,11 +1419,7 @@ const quadbinQuerySource = async function quadbinQuerySource(options) {
1583
1419
  urlParameters.filters = filters;
1584
1420
  }
1585
1421
  return baseSource('query', options, urlParameters).then(result => _extends({}, result, {
1586
- widgetSource: new WidgetQuerySource(_extends({}, options, {
1587
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
1588
- spatialDataColumn,
1589
- spatialDataType: 'quadbin'
1590
- }))
1422
+ widgetSource: new WidgetQuerySource(options)
1591
1423
  }));
1592
1424
  };
1593
1425
 
@@ -1612,11 +1444,7 @@ const quadbinTableSource = async function quadbinTableSource(options) {
1612
1444
  urlParameters.filters = filters;
1613
1445
  }
1614
1446
  return baseSource('table', options, urlParameters).then(result => _extends({}, result, {
1615
- widgetSource: new WidgetTableSource(_extends({}, options, {
1616
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
1617
- spatialDataColumn,
1618
- spatialDataType: 'quadbin'
1619
- }))
1447
+ widgetSource: new WidgetTableSource(options)
1620
1448
  }));
1621
1449
  };
1622
1450
 
@@ -1656,9 +1484,7 @@ const vectorQuerySource = async function vectorQuerySource(options) {
1656
1484
  urlParameters.queryParameters = queryParameters;
1657
1485
  }
1658
1486
  return baseSource('query', options, urlParameters).then(result => _extends({}, result, {
1659
- widgetSource: new WidgetQuerySource(_extends({}, options, {
1660
- spatialDataType: 'geo'
1661
- }))
1487
+ widgetSource: new WidgetQuerySource(options)
1662
1488
  }));
1663
1489
  };
1664
1490
 
@@ -1683,9 +1509,7 @@ const vectorTableSource = async function vectorTableSource(options) {
1683
1509
  urlParameters.filters = filters;
1684
1510
  }
1685
1511
  return baseSource('table', options, urlParameters).then(result => _extends({}, result, {
1686
- widgetSource: new WidgetTableSource(_extends({}, options, {
1687
- spatialDataType: 'geo'
1688
- }))
1512
+ widgetSource: new WidgetTableSource(options)
1689
1513
  }));
1690
1514
  };
1691
1515