@carto/api-client 0.4.6 → 0.4.7-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.
Files changed (124) hide show
  1. package/CHANGELOG.md +17 -1
  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 +2365 -279
  6. package/build/api-client.cjs.map +1 -1
  7. package/build/api-client.modern.js +2274 -298
  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 +25 -3
  12. package/build/deck/get-data-filter-extension-props.d.ts +28 -0
  13. package/build/deck/index.d.ts +1 -0
  14. package/build/filters/Filter.d.ts +25 -0
  15. package/build/filters/FilterTypes.d.ts +3 -0
  16. package/build/filters/geosjonFeatures.d.ts +8 -0
  17. package/build/filters/index.d.ts +6 -0
  18. package/build/filters/tileFeatures.d.ts +20 -0
  19. package/build/filters/tileFeaturesGeometries.d.ts +13 -0
  20. package/build/filters/tileFeaturesSpatialIndex.d.ts +10 -0
  21. package/build/filters.d.ts +2 -2
  22. package/build/geo.d.ts +1 -1
  23. package/build/index.d.ts +5 -0
  24. package/build/models/common.d.ts +5 -4
  25. package/build/models/index.d.ts +1 -1
  26. package/build/models/model.d.ts +2 -2
  27. package/build/operations/aggregation.d.ts +8 -0
  28. package/build/operations/applySorting.d.ts +20 -0
  29. package/build/operations/groupBy.d.ts +15 -0
  30. package/build/operations/groupByDate.d.ts +11 -0
  31. package/build/operations/histogram.d.ts +13 -0
  32. package/build/operations/index.d.ts +6 -0
  33. package/build/operations/scatterPlot.d.ts +14 -0
  34. package/build/sources/base-source.d.ts +2 -2
  35. package/build/sources/boundary-query-source.d.ts +1 -1
  36. package/build/sources/boundary-table-source.d.ts +1 -1
  37. package/build/sources/h3-query-source.d.ts +2 -2
  38. package/build/sources/h3-table-source.d.ts +2 -2
  39. package/build/sources/h3-tileset-source.d.ts +1 -1
  40. package/build/sources/index.d.ts +26 -26
  41. package/build/sources/quadbin-query-source.d.ts +2 -2
  42. package/build/sources/quadbin-table-source.d.ts +2 -2
  43. package/build/sources/quadbin-tileset-source.d.ts +1 -1
  44. package/build/sources/raster-source.d.ts +1 -1
  45. package/build/sources/types.d.ts +3 -3
  46. package/build/sources/vector-query-source.d.ts +1 -1
  47. package/build/sources/vector-table-source.d.ts +1 -1
  48. package/build/sources/vector-tileset-source.d.ts +1 -1
  49. package/build/spatial-index.d.ts +3 -3
  50. package/build/types-internal.d.ts +9 -5
  51. package/build/types.d.ts +74 -14
  52. package/build/utils/dateUtils.d.ts +10 -0
  53. package/build/utils/getTileFormat.d.ts +3 -0
  54. package/build/utils/makeIntervalComplete.d.ts +2 -0
  55. package/build/utils/transformTileCoordsToWGS84.d.ts +8 -0
  56. package/build/utils/transformToTileCoords.d.ts +9 -0
  57. package/build/utils.d.ts +3 -3
  58. package/build/widget-sources/index.d.ts +3 -1
  59. package/build/widget-sources/types.d.ts +38 -25
  60. package/build/widget-sources/widget-query-source.d.ts +4 -3
  61. package/build/widget-sources/widget-remote-source.d.ts +18 -0
  62. package/build/widget-sources/{widget-base-source.d.ts → widget-source.d.ts} +16 -41
  63. package/build/widget-sources/widget-table-source.d.ts +4 -3
  64. package/build/widget-sources/widget-tileset-source.d.ts +75 -0
  65. package/package.json +46 -29
  66. package/src/api/carto-api-error.ts +1 -1
  67. package/src/api/query.ts +5 -5
  68. package/src/api/request-with-parameters.ts +6 -6
  69. package/src/client.ts +3 -3
  70. package/src/constants-internal.ts +5 -5
  71. package/src/constants.ts +28 -3
  72. package/src/deck/get-data-filter-extension-props.ts +164 -0
  73. package/src/deck/index.ts +1 -0
  74. package/src/filters/Filter.ts +179 -0
  75. package/src/filters/FilterTypes.ts +109 -0
  76. package/src/filters/geosjonFeatures.ts +32 -0
  77. package/src/filters/index.ts +6 -0
  78. package/src/filters/tileFeatures.ts +50 -0
  79. package/src/filters/tileFeaturesGeometries.ts +444 -0
  80. package/src/filters/tileFeaturesSpatialIndex.ts +119 -0
  81. package/src/filters.ts +4 -4
  82. package/src/geo.ts +12 -14
  83. package/src/index.ts +7 -0
  84. package/src/models/common.ts +11 -9
  85. package/src/models/index.ts +1 -1
  86. package/src/models/model.ts +3 -4
  87. package/src/operations/aggregation.ts +154 -0
  88. package/src/operations/applySorting.ts +109 -0
  89. package/src/operations/groupBy.ts +59 -0
  90. package/src/operations/groupByDate.ts +98 -0
  91. package/src/operations/histogram.ts +66 -0
  92. package/src/operations/index.ts +6 -0
  93. package/src/operations/scatterPlot.ts +50 -0
  94. package/src/sources/base-source.ts +8 -8
  95. package/src/sources/boundary-query-source.ts +2 -2
  96. package/src/sources/boundary-table-source.ts +2 -2
  97. package/src/sources/h3-query-source.ts +7 -5
  98. package/src/sources/h3-table-source.ts +7 -5
  99. package/src/sources/h3-tileset-source.ts +2 -2
  100. package/src/sources/index.ts +26 -26
  101. package/src/sources/quadbin-query-source.ts +7 -5
  102. package/src/sources/quadbin-table-source.ts +7 -5
  103. package/src/sources/quadbin-tileset-source.ts +2 -2
  104. package/src/sources/raster-source.ts +3 -2
  105. package/src/sources/types.ts +3 -3
  106. package/src/sources/vector-query-source.ts +7 -5
  107. package/src/sources/vector-table-source.ts +7 -5
  108. package/src/sources/vector-tileset-source.ts +2 -2
  109. package/src/spatial-index.ts +4 -5
  110. package/src/types-internal.ts +11 -5
  111. package/src/types.ts +73 -15
  112. package/src/utils/dateUtils.ts +28 -0
  113. package/src/utils/getTileFormat.ts +9 -0
  114. package/src/utils/makeIntervalComplete.ts +17 -0
  115. package/src/utils/transformTileCoordsToWGS84.ts +77 -0
  116. package/src/utils/transformToTileCoords.ts +85 -0
  117. package/src/utils.ts +3 -3
  118. package/src/widget-sources/index.ts +3 -1
  119. package/src/widget-sources/types.ts +39 -25
  120. package/src/widget-sources/widget-query-source.ts +12 -5
  121. package/src/widget-sources/{widget-base-source.ts → widget-remote-source.ts} +51 -171
  122. package/src/widget-sources/widget-source.ts +173 -0
  123. package/src/widget-sources/widget-table-source.ts +12 -5
  124. package/src/widget-sources/widget-tileset-source.ts +456 -0
@@ -16,112 +16,35 @@ import {
16
16
  TableResponse,
17
17
  TimeSeriesRequestOptions,
18
18
  TimeSeriesResponse,
19
- ViewState,
20
19
  } from './types.js';
21
- import {FilterLogicalOperator, Filter, SpatialFilter} from '../types.js';
22
- import {getApplicableFilters, normalizeObjectKeys} from '../utils.js';
23
- import {getClient} from '../client.js';
24
- import {ModelSource} from '../models/model.js';
25
- import {SourceOptions} from '../sources/index.js';
26
- import {ApiVersion, DEFAULT_API_BASE_URL} from '../constants.js';
20
+ import {normalizeObjectKeys} from '../utils.js';
27
21
  import {DEFAULT_TILE_RESOLUTION} from '../constants-internal.js';
28
- import {getSpatialFiltersResolution} from '../spatial-index.js';
29
- import {AggregationOptions} from '../sources/types.js';
22
+ import {WidgetSource, WidgetSourceProps} from './widget-source.js';
30
23
 
31
- export interface WidgetBaseSourceProps extends Omit<SourceOptions, 'filters'> {
32
- apiVersion?: ApiVersion;
33
- filters?: Record<string, Filter>;
34
- filtersLogicalOperator?: FilterLogicalOperator;
35
- }
36
-
37
- export type WidgetSource = WidgetBaseSource<WidgetBaseSourceProps>;
24
+ export type WidgetRemoteSourceProps = WidgetSourceProps;
38
25
 
39
26
  /**
40
- * Source for Widget API requests on a data source defined by a SQL query.
27
+ * Source for Widget API requests.
41
28
  *
42
29
  * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
43
30
  */
44
- export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
45
- readonly props: Props;
46
-
47
- static defaultProps: Partial<WidgetBaseSourceProps> = {
48
- apiVersion: ApiVersion.V3,
49
- apiBaseUrl: DEFAULT_API_BASE_URL,
50
- clientId: getClient(),
51
- filters: {},
52
- filtersLogicalOperator: 'and',
53
- };
54
-
55
- constructor(props: Props) {
56
- this.props = {...WidgetBaseSource.defaultProps, ...props};
57
- }
58
-
59
- /**
60
- * Subclasses of {@link WidgetBaseSource} must implement this method, calling
61
- * {@link WidgetBaseSource.prototype._getModelSource} for common source
62
- * properties, and adding additional required properties including 'type' and
63
- * 'data'.
64
- */
65
- protected abstract getModelSource(owner: string | undefined): ModelSource;
66
-
67
- protected _getModelSource(
68
- owner?: string
69
- ): Omit<ModelSource, 'type' | 'data'> {
70
- const props = this.props;
71
- return {
72
- apiVersion: props.apiVersion as ApiVersion,
73
- apiBaseUrl: props.apiBaseUrl as string,
74
- clientId: props.clientId as string,
75
- accessToken: props.accessToken,
76
- connectionName: props.connectionName,
77
- filters: getApplicableFilters(owner, props.filters),
78
- filtersLogicalOperator: props.filtersLogicalOperator,
79
- spatialDataType: props.spatialDataType,
80
- spatialDataColumn: props.spatialDataColumn,
81
- dataResolution: (props as Partial<AggregationOptions>).dataResolution,
82
- };
83
- }
84
-
85
- protected _getSpatialFiltersResolution(
86
- source: Omit<ModelSource, 'type' | 'data'>,
87
- spatialFilter?: SpatialFilter,
88
- referenceViewState?: ViewState
89
- ): number | undefined {
90
- // spatialFiltersResolution applies only to spatial index sources.
91
- if (!spatialFilter || source.spatialDataType === 'geo') {
92
- return;
93
- }
94
-
95
- if (!referenceViewState) {
96
- throw new Error(
97
- 'Missing required option, "spatialIndexReferenceViewState".'
98
- );
99
- }
100
-
101
- return getSpatialFiltersResolution(source, referenceViewState);
102
- }
103
-
104
- /****************************************************************************
105
- * CATEGORIES
106
- */
107
-
108
- /**
109
- * Returns a list of labeled datapoints for categorical data. Suitable for
110
- * charts including grouped bar charts, pie charts, and tree charts.
111
- */
31
+ export abstract class WidgetRemoteSource<
32
+ Props extends WidgetRemoteSourceProps,
33
+ > extends WidgetSource<Props> {
112
34
  async getCategories(
113
35
  options: CategoryRequestOptions
114
36
  ): Promise<CategoryResponse> {
115
37
  const {
38
+ signal,
39
+ filters = this.props.filters,
116
40
  filterOwner,
117
41
  spatialFilter,
118
42
  spatialFiltersMode,
119
43
  spatialIndexReferenceViewState,
120
- abortController,
121
44
  ...params
122
45
  } = options;
123
46
  const {column, operation, operationColumn} = params;
124
- const source = this.getModelSource(filterOwner);
47
+ const source = this.getModelSource(filters, filterOwner);
125
48
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
126
49
  source,
127
50
  spatialFilter,
@@ -143,35 +66,25 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
143
66
  operation,
144
67
  operationColumn: operationColumn || column,
145
68
  },
146
- opts: {abortController},
69
+ opts: {signal, headers: this.props.headers},
147
70
  }).then((res: CategoriesModelResponse) => normalizeObjectKeys(res.rows));
148
71
  }
149
72
 
150
- /****************************************************************************
151
- * FEATURES
152
- */
153
-
154
- /**
155
- * Given a list of feature IDs (as found in `_carto_feature_id`) returns all
156
- * matching features. In datasets containing features with duplicate geometries,
157
- * feature IDs may be duplicated (IDs are a hash of geometry) and so more
158
- * results may be returned than IDs in the request.
159
- * @internal
160
- * @experimental
161
- */
162
73
  async getFeatures(
163
74
  options: FeaturesRequestOptions
164
75
  ): Promise<FeaturesResponse> {
165
76
  const {
77
+ abortController,
78
+ signal = abortController?.signal,
79
+ filters = this.props.filters,
166
80
  filterOwner,
167
81
  spatialFilter,
168
82
  spatialFiltersMode,
169
83
  spatialIndexReferenceViewState,
170
- abortController,
171
84
  ...params
172
85
  } = options;
173
86
  const {columns, dataType, featureIds, z, limit, tileResolution} = params;
174
- const source = this.getModelSource(filterOwner);
87
+ const source = this.getModelSource(filters, filterOwner);
175
88
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
176
89
  source,
177
90
  spatialFilter,
@@ -196,31 +109,25 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
196
109
  limit: limit || 1000,
197
110
  tileResolution: tileResolution || DEFAULT_TILE_RESOLUTION,
198
111
  },
199
- opts: {abortController},
112
+ opts: {signal, headers: this.props.headers},
200
113
  // Avoid `normalizeObjectKeys()`, which changes column names.
201
114
  }).then(({rows}: FeaturesModelResponse) => ({rows}));
202
115
  }
203
116
 
204
- /****************************************************************************
205
- * FORMULA
206
- */
207
-
208
- /**
209
- * Returns a scalar numerical statistic over all matching data. Suitable
210
- * for 'headline' or 'scorecard' figures such as counts and sums.
211
- */
212
117
  async getFormula(options: FormulaRequestOptions): Promise<FormulaResponse> {
213
118
  const {
119
+ abortController,
120
+ signal = abortController?.signal,
121
+ filters = this.props.filters,
214
122
  filterOwner,
215
123
  spatialFilter,
216
124
  spatialFiltersMode,
217
125
  spatialIndexReferenceViewState,
218
- abortController,
219
126
  operationExp,
220
127
  ...params
221
128
  } = options;
222
129
  const {column, operation} = params;
223
- const source = this.getModelSource(filterOwner);
130
+ const source = this.getModelSource(filters, filterOwner);
224
131
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
225
132
  source,
226
133
  spatialFilter,
@@ -237,32 +144,30 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
237
144
  spatialFiltersMode,
238
145
  spatialFilter,
239
146
  },
240
- params: {column: column ?? '*', operation, operationExp},
241
- opts: {abortController},
147
+ params: {
148
+ column: column ?? '*',
149
+ operation: operation ?? 'count',
150
+ operationExp,
151
+ },
152
+ opts: {signal, headers: this.props.headers},
242
153
  }).then((res: FormulaModelResponse) => normalizeObjectKeys(res.rows[0]));
243
154
  }
244
155
 
245
- /****************************************************************************
246
- * HISTOGRAM
247
- */
248
-
249
- /**
250
- * Returns a list of labeled datapoints for 'bins' of data defined as ticks
251
- * over a numerical range. Suitable for histogram charts.
252
- */
253
156
  async getHistogram(
254
157
  options: HistogramRequestOptions
255
158
  ): Promise<HistogramResponse> {
256
159
  const {
160
+ abortController,
161
+ signal = abortController?.signal,
162
+ filters = this.props.filters,
257
163
  filterOwner,
258
164
  spatialFilter,
259
165
  spatialFiltersMode,
260
166
  spatialIndexReferenceViewState,
261
- abortController,
262
167
  ...params
263
168
  } = options;
264
169
  const {column, operation, ticks} = params;
265
- const source = this.getModelSource(filterOwner);
170
+ const source = this.getModelSource(filters, filterOwner);
266
171
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
267
172
  source,
268
173
  spatialFilter,
@@ -280,7 +185,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
280
185
  spatialFilter,
281
186
  },
282
187
  params: {column, operation, ticks},
283
- opts: {abortController},
188
+ opts: {signal, headers: this.props.headers},
284
189
  }).then((res: HistogramModelResponse) => normalizeObjectKeys(res.rows));
285
190
 
286
191
  if (data.length) {
@@ -296,26 +201,19 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
296
201
  return [];
297
202
  }
298
203
 
299
- /****************************************************************************
300
- * RANGE
301
- */
302
-
303
- /**
304
- * Returns a range (min and max) for a numerical column of matching rows.
305
- * Suitable for displaying certain 'headline' or 'scorecard' statistics,
306
- * or rendering a range slider UI for filtering.
307
- */
308
204
  async getRange(options: RangeRequestOptions): Promise<RangeResponse> {
309
205
  const {
206
+ abortController,
207
+ signal = abortController?.signal,
208
+ filters = this.props.filters,
310
209
  filterOwner,
311
210
  spatialFilter,
312
211
  spatialFiltersMode,
313
212
  spatialIndexReferenceViewState,
314
- abortController,
315
213
  ...params
316
214
  } = options;
317
215
  const {column} = params;
318
- const source = this.getModelSource(filterOwner);
216
+ const source = this.getModelSource(filters, filterOwner);
319
217
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
320
218
  source,
321
219
  spatialFilter,
@@ -333,31 +231,25 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
333
231
  spatialFilter,
334
232
  },
335
233
  params: {column},
336
- opts: {abortController},
234
+ opts: {signal, headers: this.props.headers},
337
235
  }).then((res: RangeModelResponse) => normalizeObjectKeys(res.rows[0]));
338
236
  }
339
237
 
340
- /****************************************************************************
341
- * SCATTER
342
- */
343
-
344
- /**
345
- * Returns a list of bivariate datapoints defined as numerical 'x' and 'y'
346
- * values. Suitable for rendering scatter plots.
347
- */
348
238
  async getScatter(options: ScatterRequestOptions): Promise<ScatterResponse> {
349
239
  const {
240
+ abortController,
241
+ signal = abortController?.signal,
242
+ filters = this.props.filters,
350
243
  filterOwner,
351
244
  spatialFilter,
352
245
  spatialFiltersMode,
353
246
  spatialIndexReferenceViewState,
354
- abortController,
355
247
  ...params
356
248
  } = options;
357
249
  const {xAxisColumn, xAxisJoinOperation, yAxisColumn, yAxisJoinOperation} =
358
250
  params;
359
251
 
360
- const source = this.getModelSource(filterOwner);
252
+ const source = this.getModelSource(filters, filterOwner);
361
253
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
362
254
  source,
363
255
  spatialFilter,
@@ -384,31 +276,25 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
384
276
  yAxisJoinOperation,
385
277
  limit: HARD_LIMIT,
386
278
  },
387
- opts: {abortController},
279
+ opts: {signal, headers: this.props.headers},
388
280
  })
389
281
  .then((res: ScatterModelResponse) => normalizeObjectKeys(res.rows))
390
282
  .then((res) => res.map(({x, y}: {x: number; y: number}) => [x, y]));
391
283
  }
392
284
 
393
- /****************************************************************************
394
- * TABLE
395
- */
396
-
397
- /**
398
- * Returns a list of arbitrary data rows, with support for pagination and
399
- * sorting. Suitable for displaying tables and lists.
400
- */
401
285
  async getTable(options: TableRequestOptions): Promise<TableResponse> {
402
286
  const {
287
+ abortController,
288
+ signal = abortController?.signal,
289
+ filters = this.props.filters,
403
290
  filterOwner,
404
291
  spatialFilter,
405
292
  spatialFiltersMode,
406
293
  spatialIndexReferenceViewState,
407
- abortController,
408
294
  ...params
409
295
  } = options;
410
296
  const {columns, sortBy, sortDirection, offset = 0, limit = 10} = params;
411
- const source = this.getModelSource(filterOwner);
297
+ const source = this.getModelSource(filters, filterOwner);
412
298
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
413
299
  source,
414
300
  spatialFilter,
@@ -435,7 +321,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
435
321
  limit,
436
322
  offset,
437
323
  },
438
- opts: {abortController},
324
+ opts: {signal, headers: this.props.headers},
439
325
  }).then((res: TableModelResponse) => ({
440
326
  // Avoid `normalizeObjectKeys()`, which changes column names.
441
327
  rows: res.rows ?? (res as any).ROWS,
@@ -443,20 +329,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
443
329
  }));
444
330
  }
445
331
 
446
- /****************************************************************************
447
- * TIME SERIES
448
- */
449
-
450
- /**
451
- * Returns a series of labeled numerical values, grouped into equally-sized
452
- * time intervals. Suitable for rendering time series charts.
453
- */
454
332
  async getTimeSeries(
455
333
  options: TimeSeriesRequestOptions
456
334
  ): Promise<TimeSeriesResponse> {
457
335
  const {
458
- filterOwner,
459
336
  abortController,
337
+ signal = abortController?.signal,
338
+ filters = this.props.filters,
339
+ filterOwner,
460
340
  spatialFilter,
461
341
  spatialFiltersMode,
462
342
  spatialIndexReferenceViewState,
@@ -474,7 +354,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
474
354
  splitByCategoryValues,
475
355
  } = params;
476
356
 
477
- const source = this.getModelSource(filterOwner);
357
+ const source = this.getModelSource(filters, filterOwner);
478
358
  const spatialFiltersResolution = this._getSpatialFiltersResolution(
479
359
  source,
480
360
  spatialFilter,
@@ -505,7 +385,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
505
385
  splitByCategoryLimit,
506
386
  splitByCategoryValues,
507
387
  },
508
- opts: {abortController},
388
+ opts: {signal, headers: this.props.headers},
509
389
  }).then((res: TimeSeriesModelResponse) => ({
510
390
  rows: normalizeObjectKeys(res.rows),
511
391
  categories: res.metadata?.categories,
@@ -0,0 +1,173 @@
1
+ import {
2
+ CategoryRequestOptions,
3
+ CategoryResponse,
4
+ FeaturesRequestOptions,
5
+ FeaturesResponse,
6
+ FormulaRequestOptions,
7
+ FormulaResponse,
8
+ HistogramRequestOptions,
9
+ HistogramResponse,
10
+ RangeRequestOptions,
11
+ RangeResponse,
12
+ ScatterRequestOptions,
13
+ ScatterResponse,
14
+ TableRequestOptions,
15
+ TableResponse,
16
+ TimeSeriesRequestOptions,
17
+ TimeSeriesResponse,
18
+ ViewState,
19
+ } from './types.js';
20
+ import {
21
+ FilterLogicalOperator,
22
+ Filter,
23
+ SpatialFilter,
24
+ Filters,
25
+ } from '../types.js';
26
+ import {getApplicableFilters} from '../utils.js';
27
+ import {getClient} from '../client.js';
28
+ import {ModelSource} from '../models/model.js';
29
+ import {SourceOptions} from '../sources/index.js';
30
+ import {ApiVersion, DEFAULT_API_BASE_URL} from '../constants.js';
31
+ import {getSpatialFiltersResolution} from '../spatial-index.js';
32
+ import {AggregationOptions} from '../sources/types.js';
33
+
34
+ export interface WidgetSourceProps extends Omit<SourceOptions, 'filters'> {
35
+ apiVersion?: ApiVersion;
36
+ filters?: Record<string, Filter>;
37
+ filtersLogicalOperator?: FilterLogicalOperator;
38
+ }
39
+
40
+ /**
41
+ * Source for Widget API requests on a data source defined by a SQL query.
42
+ *
43
+ * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
44
+ */
45
+ export abstract class WidgetSource<Props extends WidgetSourceProps> {
46
+ readonly props: Props;
47
+
48
+ static defaultProps: Partial<WidgetSourceProps> = {
49
+ apiVersion: ApiVersion.V3,
50
+ apiBaseUrl: DEFAULT_API_BASE_URL,
51
+ clientId: getClient(),
52
+ filters: {},
53
+ filtersLogicalOperator: 'and',
54
+ };
55
+
56
+ constructor(props: Props) {
57
+ this.props = {
58
+ ...WidgetSource.defaultProps,
59
+ clientId: getClient(), // Refresh clientId, default may have changed.
60
+ ...props,
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Subclasses of {@link WidgetRemoteSource} must implement this method, calling
66
+ * {@link WidgetRemoteSource.prototype._getModelSource} for common source
67
+ * properties, and adding additional required properties including 'type' and
68
+ * 'data'.
69
+ */
70
+ protected abstract getModelSource(
71
+ filters: Filters | undefined,
72
+ filterOwner?: string
73
+ ): ModelSource;
74
+
75
+ protected _getModelSource(
76
+ filters: Filters | undefined,
77
+ filterOwner?: string
78
+ ): Omit<ModelSource, 'type' | 'data'> {
79
+ const props = this.props;
80
+ return {
81
+ apiVersion: props.apiVersion as ApiVersion,
82
+ apiBaseUrl: props.apiBaseUrl as string,
83
+ clientId: props.clientId as string,
84
+ accessToken: props.accessToken,
85
+ connectionName: props.connectionName,
86
+ filters: getApplicableFilters(filterOwner, filters || props.filters),
87
+ filtersLogicalOperator: props.filtersLogicalOperator,
88
+ spatialDataType: props.spatialDataType,
89
+ spatialDataColumn: props.spatialDataColumn,
90
+ dataResolution: (props as Partial<AggregationOptions>).dataResolution,
91
+ };
92
+ }
93
+
94
+ protected _getSpatialFiltersResolution(
95
+ source: Omit<ModelSource, 'type' | 'data'>,
96
+ spatialFilter?: SpatialFilter,
97
+ referenceViewState?: ViewState
98
+ ): number | undefined {
99
+ // spatialFiltersResolution applies only to spatial index sources.
100
+ if (!spatialFilter || source.spatialDataType === 'geo') {
101
+ return;
102
+ }
103
+
104
+ if (!referenceViewState) {
105
+ throw new Error(
106
+ 'Missing required option, "spatialIndexReferenceViewState".'
107
+ );
108
+ }
109
+
110
+ return getSpatialFiltersResolution(source, referenceViewState);
111
+ }
112
+
113
+ /**
114
+ * Returns a list of labeled datapoints for categorical data. Suitable for
115
+ * charts including grouped bar charts, pie charts, and tree charts.
116
+ */
117
+ abstract getCategories(
118
+ options: CategoryRequestOptions
119
+ ): Promise<CategoryResponse>;
120
+
121
+ /**
122
+ * Given a list of feature IDs (as found in `_carto_feature_id`) returns all
123
+ * matching features. In datasets containing features with duplicate geometries,
124
+ * feature IDs may be duplicated (IDs are a hash of geometry) and so more
125
+ * results may be returned than IDs in the request.
126
+ * @internal
127
+ * @experimental
128
+ */
129
+ abstract getFeatures(
130
+ options: FeaturesRequestOptions
131
+ ): Promise<FeaturesResponse>;
132
+
133
+ /**
134
+ * Returns a scalar numerical statistic over all matching data. Suitable
135
+ * for 'headline' or 'scorecard' figures such as counts and sums.
136
+ */
137
+ abstract getFormula(options: FormulaRequestOptions): Promise<FormulaResponse>;
138
+
139
+ /**
140
+ * Returns a list of labeled datapoints for 'bins' of data defined as ticks
141
+ * over a numerical range. Suitable for histogram charts.
142
+ */
143
+ abstract getHistogram(
144
+ options: HistogramRequestOptions
145
+ ): Promise<HistogramResponse>;
146
+
147
+ /**
148
+ * Returns a range (min and max) for a numerical column of matching rows.
149
+ * Suitable for displaying certain 'headline' or 'scorecard' statistics,
150
+ * or rendering a range slider UI for filtering.
151
+ */
152
+ abstract getRange(options: RangeRequestOptions): Promise<RangeResponse>;
153
+
154
+ /**
155
+ * Returns a list of bivariate datapoints defined as numerical 'x' and 'y'
156
+ * values. Suitable for rendering scatter plots.
157
+ */
158
+ abstract getScatter(options: ScatterRequestOptions): Promise<ScatterResponse>;
159
+
160
+ /**
161
+ * Returns a list of arbitrary data rows, with support for pagination and
162
+ * sorting. Suitable for displaying tables and lists.
163
+ */
164
+ abstract getTable(options: TableRequestOptions): Promise<TableResponse>;
165
+
166
+ /**
167
+ * Returns a series of labeled numerical values, grouped into equally-sized
168
+ * time intervals. Suitable for rendering time series charts.
169
+ */
170
+ abstract getTimeSeries(
171
+ options: TimeSeriesRequestOptions
172
+ ): Promise<TimeSeriesResponse>;
173
+ }
@@ -3,8 +3,12 @@ import {
3
3
  QuadbinTableSourceOptions,
4
4
  VectorTableSourceOptions,
5
5
  } from '../sources/index.js';
6
- import {WidgetBaseSource, WidgetBaseSourceProps} from './widget-base-source.js';
6
+ import {
7
+ WidgetRemoteSource,
8
+ WidgetRemoteSourceProps,
9
+ } from './widget-remote-source.js';
7
10
  import {ModelSource} from '../models/model.js';
11
+ import {Filters} from '../types.js';
8
12
 
9
13
  type LayerTableSourceOptions =
10
14
  | Omit<VectorTableSourceOptions, 'filters'>
@@ -35,12 +39,15 @@ export type WidgetTableSourceResult = {widgetSource: WidgetTableSource};
35
39
  * const { widgetSource } = await data;
36
40
  * ```
37
41
  */
38
- export class WidgetTableSource extends WidgetBaseSource<
39
- LayerTableSourceOptions & WidgetBaseSourceProps
42
+ export class WidgetTableSource extends WidgetRemoteSource<
43
+ LayerTableSourceOptions & WidgetRemoteSourceProps
40
44
  > {
41
- protected override getModelSource(owner: string): ModelSource {
45
+ protected override getModelSource(
46
+ filters: Filters | undefined,
47
+ filterOwner?: string
48
+ ): ModelSource {
42
49
  return {
43
- ...super._getModelSource(owner),
50
+ ...super._getModelSource(filters, filterOwner),
44
51
  type: 'table',
45
52
  data: this.props.tableName,
46
53
  };