@carto/api-client 0.5.0-alpha.2 → 0.5.0-alpha.4

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 (110) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/api/carto-api-error.d.ts +1 -1
  3. package/build/api/query.d.ts +1 -1
  4. package/build/api/request-with-parameters.d.ts +2 -2
  5. package/build/api-client.cjs +2380 -2508
  6. package/build/api-client.cjs.map +1 -1
  7. package/build/api-client.modern.js +2220 -2349
  8. package/build/api-client.modern.js.map +1 -1
  9. package/build/client.d.ts +2 -2
  10. package/build/constants-internal.d.ts +5 -5
  11. package/build/constants.d.ts +6 -6
  12. package/build/deck/get-data-filter-extension-props.d.ts +18 -0
  13. package/build/deck/index.d.ts +1 -0
  14. package/build/filters/Filter.d.ts +15 -3
  15. package/build/filters/FilterTypes.d.ts +1 -1
  16. package/build/filters/geosjonFeatures.d.ts +2 -2
  17. package/build/filters/tileFeatures.d.ts +10 -9
  18. package/build/filters/tileFeaturesRaster.d.ts +16 -0
  19. package/build/filters.d.ts +2 -2
  20. package/build/geo.d.ts +1 -1
  21. package/build/index.d.ts +2 -0
  22. package/build/models/common.d.ts +4 -3
  23. package/build/models/model.d.ts +2 -2
  24. package/build/operations/aggregation.d.ts +5 -5
  25. package/build/operations/applySorting.d.ts +3 -3
  26. package/build/operations/groupBy.d.ts +4 -4
  27. package/build/operations/groupByDate.d.ts +1 -1
  28. package/build/operations/histogram.d.ts +3 -3
  29. package/build/operations/scatterPlot.d.ts +3 -3
  30. package/build/sources/base-source.d.ts +2 -2
  31. package/build/sources/boundary-query-source.d.ts +1 -1
  32. package/build/sources/boundary-table-source.d.ts +1 -1
  33. package/build/sources/h3-query-source.d.ts +2 -2
  34. package/build/sources/h3-table-source.d.ts +2 -2
  35. package/build/sources/h3-tileset-source.d.ts +2 -2
  36. package/build/sources/index.d.ts +26 -26
  37. package/build/sources/quadbin-query-source.d.ts +2 -2
  38. package/build/sources/quadbin-table-source.d.ts +2 -2
  39. package/build/sources/quadbin-tileset-source.d.ts +2 -2
  40. package/build/sources/raster-source.d.ts +3 -2
  41. package/build/sources/types.d.ts +4 -4
  42. package/build/sources/vector-query-source.d.ts +1 -1
  43. package/build/sources/vector-table-source.d.ts +1 -1
  44. package/build/sources/vector-tileset-source.d.ts +2 -2
  45. package/build/spatial-index.d.ts +10 -4
  46. package/build/types-internal.d.ts +5 -5
  47. package/build/types.d.ts +23 -15
  48. package/build/utils/makeIntervalComplete.d.ts +1 -1
  49. package/build/utils.d.ts +3 -3
  50. package/build/widget-sources/index.d.ts +1 -0
  51. package/build/widget-sources/types.d.ts +4 -2
  52. package/build/widget-sources/widget-query-source.d.ts +2 -1
  53. package/build/widget-sources/widget-raster-source.d.ts +11 -0
  54. package/build/widget-sources/widget-remote-source.d.ts +3 -0
  55. package/build/widget-sources/widget-source.d.ts +3 -3
  56. package/build/widget-sources/widget-table-source.d.ts +2 -1
  57. package/build/widget-sources/widget-tileset-source.d.ts +21 -22
  58. package/package.json +44 -32
  59. package/src/api/carto-api-error.ts +1 -1
  60. package/src/api/query.ts +5 -5
  61. package/src/api/request-with-parameters.ts +6 -6
  62. package/src/client.ts +3 -3
  63. package/src/constants-internal.ts +5 -5
  64. package/src/constants.ts +6 -6
  65. package/src/deck/get-data-filter-extension-props.ts +146 -0
  66. package/src/deck/index.ts +1 -0
  67. package/src/filters/Filter.ts +18 -8
  68. package/src/filters/FilterTypes.ts +2 -2
  69. package/src/filters/geosjonFeatures.ts +2 -2
  70. package/src/filters/tileFeatures.ts +33 -23
  71. package/src/filters/tileFeaturesRaster.ts +111 -0
  72. package/src/filters.ts +4 -4
  73. package/src/geo.ts +12 -14
  74. package/src/index.ts +3 -0
  75. package/src/models/common.ts +9 -7
  76. package/src/models/model.ts +3 -4
  77. package/src/operations/aggregation.ts +5 -5
  78. package/src/operations/applySorting.ts +4 -4
  79. package/src/operations/groupBy.ts +4 -4
  80. package/src/operations/groupByDate.ts +1 -1
  81. package/src/operations/histogram.ts +4 -4
  82. package/src/operations/scatterPlot.ts +4 -4
  83. package/src/sources/base-source.ts +8 -8
  84. package/src/sources/boundary-query-source.ts +2 -2
  85. package/src/sources/boundary-table-source.ts +2 -2
  86. package/src/sources/h3-query-source.ts +7 -5
  87. package/src/sources/h3-table-source.ts +7 -5
  88. package/src/sources/h3-tileset-source.ts +4 -4
  89. package/src/sources/index.ts +26 -26
  90. package/src/sources/quadbin-query-source.ts +7 -5
  91. package/src/sources/quadbin-table-source.ts +7 -5
  92. package/src/sources/quadbin-tileset-source.ts +4 -4
  93. package/src/sources/raster-source.ts +20 -7
  94. package/src/sources/types.ts +4 -4
  95. package/src/sources/vector-query-source.ts +2 -3
  96. package/src/sources/vector-table-source.ts +2 -3
  97. package/src/sources/vector-tileset-source.ts +5 -5
  98. package/src/spatial-index.ts +12 -10
  99. package/src/types-internal.ts +5 -5
  100. package/src/types.ts +21 -15
  101. package/src/utils/makeIntervalComplete.ts +1 -1
  102. package/src/utils.ts +3 -3
  103. package/src/widget-sources/index.ts +1 -0
  104. package/src/widget-sources/types.ts +5 -3
  105. package/src/widget-sources/widget-query-source.ts +6 -2
  106. package/src/widget-sources/widget-raster-source.ts +14 -0
  107. package/src/widget-sources/widget-remote-source.ts +31 -16
  108. package/src/widget-sources/widget-source.ts +13 -4
  109. package/src/widget-sources/widget-table-source.ts +6 -2
  110. package/src/widget-sources/widget-tileset-source.ts +133 -85
@@ -1,9 +1,9 @@
1
+ /* eslint-disable @typescript-eslint/require-await */
1
2
  import {TilesetSourceOptions} from '../sources/index.js';
2
3
  import type {ModelSource} from '../models/index.js';
3
4
  import {
4
5
  CategoryRequestOptions,
5
6
  CategoryResponse,
6
- FeaturesRequestOptions,
7
7
  FeaturesResponse,
8
8
  FormulaRequestOptions,
9
9
  FormulaResponse,
@@ -18,9 +18,9 @@ import {
18
18
  TimeSeriesRequestOptions,
19
19
  TimeSeriesResponse,
20
20
  } from './types.js';
21
- import {InvalidColumnError, getApplicableFilters} from '../utils.js';
21
+ import {InvalidColumnError, assert, getApplicableFilters} from '../utils.js';
22
22
  import {TileFormat} from '../constants.js';
23
- import {SpatialFilter, Tile} from '../types.js';
23
+ import {Filter, Filters, SpatialFilter, Tile} from '../types.js';
24
24
  import {
25
25
  TileFeatureExtractOptions,
26
26
  applyFilters,
@@ -39,6 +39,7 @@ import {FeatureData} from '../types-internal.js';
39
39
  import {FeatureCollection} from 'geojson';
40
40
  import {SpatialDataType} from '../sources/types.js';
41
41
  import {WidgetSource, WidgetSourceProps} from './widget-source.js';
42
+ import {booleanEqual} from '@turf/boolean-equal';
42
43
 
43
44
  // TODO(cleanup): Parameter defaults in source functions and widget API calls are
44
45
  // currently duplicated and possibly inconsistent. Consider consolidating and
@@ -75,13 +76,21 @@ export type WidgetTilesetSourceResult = {widgetSource: WidgetTilesetSource};
75
76
  * const { widgetSource } = await data;
76
77
  * ```
77
78
  */
78
- export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps> {
79
+ export class WidgetTilesetSource<
80
+ Props extends WidgetTilesetSourceProps = WidgetTilesetSourceProps,
81
+ > extends WidgetSource<Props> {
79
82
  private _tiles: Tile[] = [];
80
83
  private _features: FeatureData[] = [];
81
-
82
- protected override getModelSource(owner: string): ModelSource {
84
+ private _tileFeatureExtractOptions: TileFeatureExtractOptions = {};
85
+ private _tileFeatureExtractPreviousInputs: {spatialFilter?: SpatialFilter} =
86
+ {};
87
+
88
+ protected override getModelSource(
89
+ filters: Filters | undefined,
90
+ filterOwner: string
91
+ ): ModelSource {
83
92
  return {
84
- ...super._getModelSource(owner),
93
+ ...super._getModelSource(filters, filterOwner),
85
94
  type: 'tileset',
86
95
  data: this.props.tableName,
87
96
  };
@@ -94,55 +103,58 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
94
103
  */
95
104
  loadTiles(tiles: unknown[]) {
96
105
  this._tiles = tiles as Tile[];
106
+ this._features.length = 0;
97
107
  }
98
108
 
99
- /**
100
- * Extracts feature data from tiles previously loaded with {@link loadTiles}.
101
- * Must be called before computing statistics on tiles.
102
- */
103
- extractTileFeatures({
104
- spatialFilter,
105
- uniqueIdProperty,
106
- options,
107
- }: {
108
- spatialFilter: SpatialFilter;
109
- // TODO(cleanup): As an optional property, 'uniqueIdProperty' will be easy to forget.
110
- // Would it be better to configure it on the source function, rather than separately
111
- // on the layer and in 'loadTiles()'?
112
- uniqueIdProperty?: string;
113
- options?: TileFeatureExtractOptions;
114
- }) {
109
+ /** Configures options used to extract features from tiles. */
110
+ setTileFeatureExtractOptions(options: TileFeatureExtractOptions) {
111
+ this._tileFeatureExtractOptions = options;
112
+ this._features.length = 0;
113
+ }
114
+
115
+ protected _extractTileFeatures(spatialFilter: SpatialFilter) {
116
+ // When spatial filter has not changed, don't redo extraction. If tiles or
117
+ // tile extract options change, features will have been cleared already.
118
+ const prevInputs = this._tileFeatureExtractPreviousInputs;
119
+ if (
120
+ this._features.length &&
121
+ prevInputs.spatialFilter &&
122
+ booleanEqual(prevInputs.spatialFilter, spatialFilter)
123
+ ) {
124
+ return;
125
+ }
126
+
115
127
  this._features = tileFeatures({
128
+ ...this.props,
129
+ ...this._tileFeatureExtractOptions,
116
130
  tiles: this._tiles,
117
- options,
118
131
  spatialFilter,
119
- uniqueIdProperty,
120
- tileFormat: this.props.tileFormat,
121
- spatialDataColumn: this.props.spatialDataColumn,
122
- spatialDataType: this.props.spatialDataType,
123
132
  });
133
+
134
+ prevInputs.spatialFilter = spatialFilter;
124
135
  }
125
136
 
126
- /** Loads features as GeoJSON (used for testing). */
137
+ /**
138
+ * Loads features as GeoJSON (used for testing).
139
+ * @experimental
140
+ * @internal Not for public use. Spatial filters in other method calls will be ignored.
141
+ */
127
142
  loadGeoJSON({
128
143
  geojson,
129
144
  spatialFilter,
130
- uniqueIdProperty,
131
145
  }: {
132
146
  geojson: FeatureCollection;
133
147
  spatialFilter: SpatialFilter;
134
- uniqueIdProperty?: string;
135
148
  }) {
136
149
  this._features = geojsonFeatures({
137
150
  geojson,
138
151
  spatialFilter,
139
- uniqueIdProperty,
152
+ ...this._tileFeatureExtractOptions,
140
153
  });
154
+ this._tileFeatureExtractPreviousInputs.spatialFilter = spatialFilter;
141
155
  }
142
156
 
143
- override async getFeatures(
144
- options: FeaturesRequestOptions
145
- ): Promise<FeaturesResponse> {
157
+ override async getFeatures(): Promise<FeaturesResponse> {
146
158
  throw new Error('getFeatures not supported for tilesets');
147
159
  }
148
160
 
@@ -150,22 +162,24 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
150
162
  column = '*',
151
163
  operation = 'count',
152
164
  joinOperation,
165
+ filters,
153
166
  filterOwner,
167
+ spatialFilter,
154
168
  }: FormulaRequestOptions): Promise<FormulaResponse> {
155
169
  if (operation === 'custom') {
156
170
  throw new Error('Custom aggregation not supported for tilesets');
157
171
  }
158
172
 
159
- if (!this._features.length) {
160
- return {value: null};
161
- }
162
-
163
173
  // Column is required except when operation is 'count'.
164
174
  if ((column && column !== '*') || operation !== 'count') {
165
175
  assertColumn(this._features, column);
166
176
  }
167
177
 
168
- const filteredFeatures = this._getFilteredFeatures(filterOwner);
178
+ const filteredFeatures = this._getFilteredFeatures(
179
+ spatialFilter,
180
+ filters,
181
+ filterOwner
182
+ );
169
183
 
170
184
  if (filteredFeatures.length === 0 && operation !== 'count') {
171
185
  return {value: null};
@@ -173,11 +187,7 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
173
187
 
174
188
  const targetOperation = aggregationFunctions[operation];
175
189
  return {
176
- value: targetOperation(
177
- filteredFeatures as FeatureData[],
178
- column,
179
- joinOperation
180
- ),
190
+ value: targetOperation(filteredFeatures, column, joinOperation),
181
191
  };
182
192
  }
183
193
 
@@ -186,16 +196,22 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
186
196
  ticks,
187
197
  column,
188
198
  joinOperation,
199
+ filters,
189
200
  filterOwner,
201
+ spatialFilter,
190
202
  }: HistogramRequestOptions): Promise<HistogramResponse> {
203
+ const filteredFeatures = this._getFilteredFeatures(
204
+ spatialFilter,
205
+ filters,
206
+ filterOwner
207
+ );
208
+
209
+ assertColumn(this._features, column);
210
+
191
211
  if (!this._features.length) {
192
212
  return [];
193
213
  }
194
214
 
195
- const filteredFeatures = this._getFilteredFeatures(filterOwner);
196
-
197
- assertColumn(this._features, column);
198
-
199
215
  return histogram({
200
216
  data: filteredFeatures,
201
217
  valuesColumns: normalizeColumns(column),
@@ -210,15 +226,21 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
210
226
  operation = 'count',
211
227
  operationColumn,
212
228
  joinOperation,
229
+ filters,
213
230
  filterOwner,
231
+ spatialFilter,
214
232
  }: CategoryRequestOptions): Promise<CategoryResponse> {
215
- if (!this._features.length) {
233
+ const filteredFeatures = this._getFilteredFeatures(
234
+ spatialFilter,
235
+ filters,
236
+ filterOwner
237
+ );
238
+
239
+ if (!filteredFeatures.length) {
216
240
  return [];
217
241
  }
218
242
 
219
- const filteredFeatures = this._getFilteredFeatures(filterOwner);
220
-
221
- assertColumn(this._features, column as string, operationColumn as string);
243
+ assertColumn(this._features, column, operationColumn as string);
222
244
 
223
245
  const groups = groupValuesByColumn({
224
246
  data: filteredFeatures,
@@ -236,14 +258,20 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
236
258
  yAxisColumn,
237
259
  xAxisJoinOperation,
238
260
  yAxisJoinOperation,
261
+ filters,
239
262
  filterOwner,
263
+ spatialFilter,
240
264
  }: ScatterRequestOptions): Promise<ScatterResponse> {
241
- if (!this._features.length) {
265
+ const filteredFeatures = this._getFilteredFeatures(
266
+ spatialFilter,
267
+ filters,
268
+ filterOwner
269
+ );
270
+
271
+ if (!filteredFeatures.length) {
242
272
  return [];
243
273
  }
244
274
 
245
- const filteredFeatures = this._getFilteredFeatures(filterOwner);
246
-
247
275
  assertColumn(this._features, xAxisColumn, yAxisColumn);
248
276
 
249
277
  return scatterPlot({
@@ -255,35 +283,36 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
255
283
  });
256
284
  }
257
285
 
258
- override async getTable(
259
- options: TableRequestOptions
260
- ): Promise<TableResponse> {
261
- const {filterOwner, spatialFilter, abortController, ...params} = options;
262
- const {
263
- columns,
264
- searchFilterColumn,
265
- searchFilterText,
266
- sortBy,
267
- sortDirection,
268
- sortByColumnType,
269
- offset = 0,
270
- limit = 10,
271
- } = params;
286
+ override async getTable({
287
+ columns,
288
+ searchFilterColumn,
289
+ searchFilterText,
290
+ sortBy,
291
+ sortDirection,
292
+ sortByColumnType,
293
+ offset = 0,
294
+ limit = 10,
295
+ filters,
296
+ filterOwner,
297
+ spatialFilter,
298
+ }: TableRequestOptions): Promise<TableResponse> {
299
+ // Filter.
300
+ let filteredFeatures = this._getFilteredFeatures(
301
+ spatialFilter,
302
+ filters,
303
+ filterOwner
304
+ );
272
305
 
273
- if (!this._features.length) {
306
+ if (!filteredFeatures.length) {
274
307
  return {rows: [], totalCount: 0};
275
308
  }
276
309
 
277
- // Filter.
278
- let filteredFeatures = this._getFilteredFeatures(filterOwner);
279
-
280
310
  // Search.
281
- // TODO: Could we get the same behavior by applying filters in loadTiles()?
282
311
  if (searchFilterColumn && searchFilterText) {
283
312
  filteredFeatures = filteredFeatures.filter(
284
313
  (row) =>
285
314
  row[searchFilterColumn] &&
286
- String(row[searchFilterColumn])
315
+ String(row[searchFilterColumn] as unknown)
287
316
  .toLowerCase()
288
317
  .includes(String(searchFilterText).toLowerCase())
289
318
  );
@@ -321,15 +350,21 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
321
350
  operation,
322
351
  operationColumn,
323
352
  joinOperation,
353
+ filters,
324
354
  filterOwner,
355
+ spatialFilter,
325
356
  }: TimeSeriesRequestOptions): Promise<TimeSeriesResponse> {
326
- if (!this._features.length) {
357
+ const filteredFeatures = this._getFilteredFeatures(
358
+ spatialFilter,
359
+ filters,
360
+ filterOwner
361
+ );
362
+
363
+ if (!filteredFeatures.length) {
327
364
  return {rows: []};
328
365
  }
329
366
 
330
- const filteredFeatures = this._getFilteredFeatures(filterOwner);
331
-
332
- assertColumn(this._features, column as string, operationColumn as string);
367
+ assertColumn(this._features, column, operationColumn as string);
333
368
 
334
369
  const rows =
335
370
  groupValuesByDateColumn({
@@ -346,17 +381,24 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
346
381
 
347
382
  override async getRange({
348
383
  column,
384
+ filters,
349
385
  filterOwner,
386
+ spatialFilter,
350
387
  }: RangeRequestOptions): Promise<RangeResponse> {
388
+ assertColumn(this._features, column);
389
+
390
+ const filteredFeatures = this._getFilteredFeatures(
391
+ spatialFilter,
392
+ filters,
393
+ filterOwner
394
+ );
395
+
351
396
  if (!this._features.length) {
352
397
  // TODO: Is this the only nullable response in the Widgets API? If so,
353
398
  // can we do something more consistent?
354
399
  return null;
355
400
  }
356
401
 
357
- assertColumn(this._features, column);
358
-
359
- const filteredFeatures = this._getFilteredFeatures(filterOwner);
360
402
  return {
361
403
  min: aggregationFunctions.min(filteredFeatures, column),
362
404
  max: aggregationFunctions.max(filteredFeatures, column),
@@ -367,10 +409,16 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
367
409
  * INTERNAL
368
410
  */
369
411
 
370
- private _getFilteredFeatures(filterOwner: string | undefined): FeatureData[] {
412
+ private _getFilteredFeatures(
413
+ spatialFilter?: SpatialFilter,
414
+ filters?: Record<string, Filter>,
415
+ filterOwner?: string
416
+ ): FeatureData[] {
417
+ assert(spatialFilter, 'spatialFilter required for tilesets');
418
+ this._extractTileFeatures(spatialFilter);
371
419
  return applyFilters(
372
420
  this._features,
373
- getApplicableFilters(filterOwner, this.props.filters),
421
+ getApplicableFilters(filterOwner, filters || this.props.filters),
374
422
  this.props.filtersLogicalOperator || 'and'
375
423
  );
376
424
  }