@carto/api-client 0.4.1-alpha.1 → 0.4.2-alpha.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.
@@ -1,4 +1,4 @@
1
- import {TileResolution} from '../sources/types';
1
+ import {SpatialFilterPolyfillMode, TileResolution} from '../sources/types';
2
2
  import {
3
3
  GroupDateType,
4
4
  SortColumnType,
@@ -10,11 +10,19 @@ import {
10
10
  * WIDGET API REQUESTS
11
11
  */
12
12
 
13
+ export interface ViewState {
14
+ zoom: number;
15
+ latitude: number;
16
+ longitude: number;
17
+ }
18
+
13
19
  /** Common options for {@link WidgetBaseSource} requests. */
14
20
  interface BaseRequestOptions {
15
21
  spatialFilter?: SpatialFilter;
22
+ spatialFiltersMode?: SpatialFilterPolyfillMode;
16
23
  abortController?: AbortController;
17
24
  filterOwner?: string;
25
+ viewState?: ViewState;
18
26
  }
19
27
 
20
28
  /** Options for {@link WidgetBaseSource#getCategories}. */
@@ -16,9 +16,10 @@ import {
16
16
  TableResponse,
17
17
  TimeSeriesRequestOptions,
18
18
  TimeSeriesResponse,
19
+ ViewState,
19
20
  } from './types.js';
20
21
  import {FilterLogicalOperator, Filter} from '../types.js';
21
- import {getApplicableFilters, normalizeObjectKeys} from '../utils.js';
22
+ import {assert, getApplicableFilters, normalizeObjectKeys} from '../utils.js';
22
23
  import {getClient} from '../client.js';
23
24
  import {ModelSource} from '../models/model.js';
24
25
  import {SourceOptions} from '../sources/index.js';
@@ -27,10 +28,11 @@ import {
27
28
  DEFAULT_GEO_COLUMN,
28
29
  DEFAULT_TILE_RESOLUTION,
29
30
  } from '../constants-internal.js';
31
+ import {getSpatialFiltersResolution} from '../spatial-index.js';
32
+ import {AggregationOptions} from '../sources/types.js';
30
33
 
31
34
  export interface WidgetBaseSourceProps extends Omit<SourceOptions, 'filters'> {
32
35
  apiVersion?: ApiVersion;
33
- geoColumn?: string;
34
36
  filters?: Record<string, Filter>;
35
37
  filtersLogicalOperator?: FilterLogicalOperator;
36
38
  }
@@ -51,7 +53,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
51
53
  clientId: getClient(),
52
54
  filters: {},
53
55
  filtersLogicalOperator: 'and',
54
- geoColumn: DEFAULT_GEO_COLUMN,
55
56
  };
56
57
 
57
58
  constructor(props: Props) {
@@ -78,7 +79,9 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
78
79
  connectionName: props.connectionName,
79
80
  filters: getApplicableFilters(owner, props.filters),
80
81
  filtersLogicalOperator: props.filtersLogicalOperator,
81
- geoColumn: props.geoColumn,
82
+ spatialDataType: props.spatialDataType,
83
+ spatialDataColumn: props.spatialDataColumn,
84
+ dataResolution: (props as Partial<AggregationOptions>).dataResolution,
82
85
  };
83
86
  }
84
87
 
@@ -93,14 +96,35 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
93
96
  async getCategories(
94
97
  options: CategoryRequestOptions
95
98
  ): Promise<CategoryResponse> {
96
- const {filterOwner, spatialFilter, abortController, ...params} = options;
99
+ const {
100
+ filterOwner,
101
+ spatialFilter,
102
+ spatialFiltersMode,
103
+ abortController,
104
+ viewState,
105
+ ...params
106
+ } = options;
97
107
  const {column, operation, operationColumn} = params;
108
+ const source = this.getModelSource(filterOwner);
109
+
110
+ let spatialFiltersResolution: number | undefined;
111
+ if (spatialFilter && source.spatialDataType !== 'geo') {
112
+ spatialFiltersResolution = getSpatialFiltersResolution({
113
+ source,
114
+ viewState,
115
+ });
116
+ }
98
117
 
99
118
  type CategoriesModelResponse = {rows: {name: string; value: number}[]};
100
119
 
101
120
  return executeModel({
102
121
  model: 'category',
103
- source: {...this.getModelSource(filterOwner), spatialFilter},
122
+ source: {
123
+ ...source,
124
+ spatialFiltersResolution,
125
+ spatialFiltersMode,
126
+ spatialFilter,
127
+ },
104
128
  params: {
105
129
  column,
106
130
  operation,
@@ -125,14 +149,35 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
125
149
  async getFeatures(
126
150
  options: FeaturesRequestOptions
127
151
  ): Promise<FeaturesResponse> {
128
- const {filterOwner, spatialFilter, abortController, ...params} = options;
152
+ const {
153
+ filterOwner,
154
+ spatialFilter,
155
+ spatialFiltersMode,
156
+ abortController,
157
+ viewState,
158
+ ...params
159
+ } = options;
129
160
  const {columns, dataType, featureIds, z, limit, tileResolution} = params;
161
+ const source = this.getModelSource(filterOwner);
162
+
163
+ let spatialFiltersResolution: number | undefined;
164
+ if (spatialFilter && source.spatialDataType !== 'geo') {
165
+ spatialFiltersResolution = getSpatialFiltersResolution({
166
+ source,
167
+ viewState,
168
+ });
169
+ }
130
170
 
131
171
  type FeaturesModelResponse = {rows: Record<string, unknown>[]};
132
172
 
133
173
  return executeModel({
134
174
  model: 'pick',
135
- source: {...this.getModelSource(filterOwner), spatialFilter},
175
+ source: {
176
+ ...source,
177
+ spatialFiltersResolution,
178
+ spatialFiltersMode,
179
+ spatialFilter,
180
+ },
136
181
  params: {
137
182
  columns,
138
183
  dataType,
@@ -159,17 +204,33 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
159
204
  const {
160
205
  filterOwner,
161
206
  spatialFilter,
207
+ spatialFiltersMode,
162
208
  abortController,
163
209
  operationExp,
210
+ viewState,
164
211
  ...params
165
212
  } = options;
166
213
  const {column, operation} = params;
214
+ const source = this.getModelSource(filterOwner);
215
+
216
+ let spatialFiltersResolution: number | undefined;
217
+ if (spatialFilter && source.spatialDataType !== 'geo') {
218
+ spatialFiltersResolution = getSpatialFiltersResolution({
219
+ source,
220
+ viewState,
221
+ });
222
+ }
167
223
 
168
224
  type FormulaModelResponse = {rows: {value: number}[]};
169
225
 
170
226
  return executeModel({
171
227
  model: 'formula',
172
- source: {...this.getModelSource(filterOwner), spatialFilter},
228
+ source: {
229
+ ...source,
230
+ spatialFiltersResolution,
231
+ spatialFiltersMode,
232
+ spatialFilter,
233
+ },
173
234
  params: {column: column ?? '*', operation, operationExp},
174
235
  opts: {abortController},
175
236
  }).then((res: FormulaModelResponse) => normalizeObjectKeys(res.rows[0]));
@@ -186,14 +247,35 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
186
247
  async getHistogram(
187
248
  options: HistogramRequestOptions
188
249
  ): Promise<HistogramResponse> {
189
- const {filterOwner, spatialFilter, abortController, ...params} = options;
250
+ const {
251
+ filterOwner,
252
+ spatialFilter,
253
+ spatialFiltersMode,
254
+ abortController,
255
+ viewState,
256
+ ...params
257
+ } = options;
190
258
  const {column, operation, ticks} = params;
259
+ const source = this.getModelSource(filterOwner);
260
+
261
+ let spatialFiltersResolution: number | undefined;
262
+ if (spatialFilter && source.spatialDataType !== 'geo') {
263
+ spatialFiltersResolution = getSpatialFiltersResolution({
264
+ source,
265
+ viewState,
266
+ });
267
+ }
191
268
 
192
269
  type HistogramModelResponse = {rows: {tick: number; value: number}[]};
193
270
 
194
271
  const data = await executeModel({
195
272
  model: 'histogram',
196
- source: {...this.getModelSource(filterOwner), spatialFilter},
273
+ source: {
274
+ ...source,
275
+ spatialFiltersResolution,
276
+ spatialFiltersMode,
277
+ spatialFilter,
278
+ },
197
279
  params: {column, operation, ticks},
198
280
  opts: {abortController},
199
281
  }).then((res: HistogramModelResponse) => normalizeObjectKeys(res.rows));
@@ -221,14 +303,35 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
221
303
  * or rendering a range slider UI for filtering.
222
304
  */
223
305
  async getRange(options: RangeRequestOptions): Promise<RangeResponse> {
224
- const {filterOwner, spatialFilter, abortController, ...params} = options;
306
+ const {
307
+ filterOwner,
308
+ spatialFilter,
309
+ spatialFiltersMode,
310
+ abortController,
311
+ viewState,
312
+ ...params
313
+ } = options;
225
314
  const {column} = params;
315
+ const source = this.getModelSource(filterOwner);
316
+
317
+ let spatialFiltersResolution: number | undefined;
318
+ if (spatialFilter && source.spatialDataType !== 'geo') {
319
+ spatialFiltersResolution = getSpatialFiltersResolution({
320
+ source,
321
+ viewState,
322
+ });
323
+ }
226
324
 
227
325
  type RangeModelResponse = {rows: {min: number; max: number}[]};
228
326
 
229
327
  return executeModel({
230
328
  model: 'range',
231
- source: {...this.getModelSource(filterOwner), spatialFilter},
329
+ source: {
330
+ ...source,
331
+ spatialFiltersResolution,
332
+ spatialFiltersMode,
333
+ spatialFilter,
334
+ },
232
335
  params: {column},
233
336
  opts: {abortController},
234
337
  }).then((res: RangeModelResponse) => normalizeObjectKeys(res.rows[0]));
@@ -243,10 +346,27 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
243
346
  * values. Suitable for rendering scatter plots.
244
347
  */
245
348
  async getScatter(options: ScatterRequestOptions): Promise<ScatterResponse> {
246
- const {filterOwner, spatialFilter, abortController, ...params} = options;
349
+ const {
350
+ filterOwner,
351
+ spatialFilter,
352
+ spatialFiltersMode,
353
+ abortController,
354
+ viewState,
355
+ ...params
356
+ } = options;
247
357
  const {xAxisColumn, xAxisJoinOperation, yAxisColumn, yAxisJoinOperation} =
248
358
  params;
249
359
 
360
+ const source = this.getModelSource(filterOwner);
361
+
362
+ let spatialFiltersResolution: number | undefined;
363
+ if (spatialFilter && source.spatialDataType !== 'geo') {
364
+ spatialFiltersResolution = getSpatialFiltersResolution({
365
+ source,
366
+ viewState,
367
+ });
368
+ }
369
+
250
370
  // Make sure this is sync with the same constant in cloud-native/maps-api
251
371
  const HARD_LIMIT = 500;
252
372
 
@@ -254,7 +374,12 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
254
374
 
255
375
  return executeModel({
256
376
  model: 'scatterplot',
257
- source: {...this.getModelSource(filterOwner), spatialFilter},
377
+ source: {
378
+ ...source,
379
+ spatialFiltersResolution,
380
+ spatialFiltersMode,
381
+ spatialFilter,
382
+ },
258
383
  params: {
259
384
  xAxisColumn,
260
385
  xAxisJoinOperation,
@@ -277,8 +402,24 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
277
402
  * sorting. Suitable for displaying tables and lists.
278
403
  */
279
404
  async getTable(options: TableRequestOptions): Promise<TableResponse> {
280
- const {filterOwner, spatialFilter, abortController, ...params} = options;
405
+ const {
406
+ filterOwner,
407
+ spatialFilter,
408
+ spatialFiltersMode,
409
+ abortController,
410
+ viewState,
411
+ ...params
412
+ } = options;
281
413
  const {columns, sortBy, sortDirection, offset = 0, limit = 10} = params;
414
+ const source = this.getModelSource(filterOwner);
415
+
416
+ let spatialFiltersResolution: number | undefined;
417
+ if (spatialFilter && source.spatialDataType !== 'geo') {
418
+ spatialFiltersResolution = getSpatialFiltersResolution({
419
+ source,
420
+ viewState,
421
+ });
422
+ }
282
423
 
283
424
  type TableModelResponse = {
284
425
  rows: Record<string, number | string>[];
@@ -287,7 +428,12 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
287
428
 
288
429
  return executeModel({
289
430
  model: 'table',
290
- source: {...this.getModelSource(filterOwner), spatialFilter},
431
+ source: {
432
+ ...source,
433
+ spatialFiltersResolution,
434
+ spatialFiltersMode,
435
+ spatialFilter,
436
+ },
291
437
  params: {
292
438
  column: columns,
293
439
  sortBy,
@@ -314,7 +460,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
314
460
  async getTimeSeries(
315
461
  options: TimeSeriesRequestOptions
316
462
  ): Promise<TimeSeriesResponse> {
317
- const {filterOwner, abortController, spatialFilter, ...params} = options;
463
+ const {
464
+ filterOwner,
465
+ abortController,
466
+ spatialFilter,
467
+ spatialFiltersMode,
468
+ viewState,
469
+ ...params
470
+ } = options;
318
471
  const {
319
472
  column,
320
473
  operationColumn,
@@ -327,6 +480,16 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
327
480
  splitByCategoryValues,
328
481
  } = params;
329
482
 
483
+ const source = this.getModelSource(filterOwner);
484
+
485
+ let spatialFiltersResolution: number | undefined;
486
+ if (spatialFilter && source.spatialDataType !== 'geo') {
487
+ spatialFiltersResolution = getSpatialFiltersResolution({
488
+ source,
489
+ viewState,
490
+ });
491
+ }
492
+
330
493
  type TimeSeriesModelResponse = {
331
494
  rows: {name: string; value: number}[];
332
495
  metadata: {categories: string[]};
@@ -334,7 +497,12 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
334
497
 
335
498
  return executeModel({
336
499
  model: 'timeseries',
337
- source: {...this.getModelSource(filterOwner), spatialFilter},
500
+ source: {
501
+ ...source,
502
+ spatialFiltersResolution,
503
+ spatialFiltersMode,
504
+ spatialFilter,
505
+ },
338
506
  params: {
339
507
  column,
340
508
  stepSize,