@carto/api-client 0.4.5-alpha.0 → 0.4.6-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 (79) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/build/api-client.cjs +2179 -216
  3. package/build/api-client.cjs.map +1 -1
  4. package/build/api-client.modern.js +2061 -226
  5. package/build/api-client.modern.js.map +1 -1
  6. package/build/constants.d.ts +22 -0
  7. package/build/filters/Filter.d.ts +13 -0
  8. package/build/filters/FilterTypes.d.ts +3 -0
  9. package/build/filters/geosjonFeatures.d.ts +8 -0
  10. package/build/filters/index.d.ts +6 -0
  11. package/build/filters/tileFeatures.d.ts +20 -0
  12. package/build/filters/tileFeaturesGeometries.d.ts +13 -0
  13. package/build/filters/tileFeaturesSpatialIndex.d.ts +10 -0
  14. package/build/index.d.ts +4 -0
  15. package/build/models/index.d.ts +1 -1
  16. package/build/operations/aggregation.d.ts +8 -0
  17. package/build/operations/applySorting.d.ts +20 -0
  18. package/build/operations/groupBy.d.ts +15 -0
  19. package/build/operations/groupByDate.d.ts +11 -0
  20. package/build/operations/histogram.d.ts +13 -0
  21. package/build/operations/index.d.ts +6 -0
  22. package/build/operations/scatterPlot.d.ts +14 -0
  23. package/build/sources/h3-tileset-source.d.ts +2 -1
  24. package/build/sources/index.d.ts +2 -2
  25. package/build/sources/quadbin-tileset-source.d.ts +2 -1
  26. package/build/sources/vector-tileset-source.d.ts +2 -1
  27. package/build/types-internal.d.ts +4 -0
  28. package/build/types.d.ts +61 -1
  29. package/build/utils/dateUtils.d.ts +10 -0
  30. package/build/utils/getTileFormat.d.ts +3 -0
  31. package/build/utils/makeIntervalComplete.d.ts +2 -0
  32. package/build/utils/transformTileCoordsToWGS84.d.ts +8 -0
  33. package/build/utils/transformToTileCoords.d.ts +9 -0
  34. package/build/widget-sources/index.d.ts +3 -1
  35. package/build/widget-sources/types.d.ts +31 -22
  36. package/build/widget-sources/widget-query-source.d.ts +2 -2
  37. package/build/widget-sources/widget-remote-source.d.ts +18 -0
  38. package/build/widget-sources/{widget-base-source.d.ts → widget-source.d.ts} +13 -38
  39. package/build/widget-sources/widget-table-source.d.ts +2 -2
  40. package/build/widget-sources/widget-tileset-source.d.ts +76 -0
  41. package/package.json +9 -3
  42. package/src/constants.ts +25 -0
  43. package/src/filters/Filter.ts +169 -0
  44. package/src/filters/FilterTypes.ts +109 -0
  45. package/src/filters/geosjonFeatures.ts +32 -0
  46. package/src/filters/index.ts +6 -0
  47. package/src/filters/tileFeatures.ts +56 -0
  48. package/src/filters/tileFeaturesGeometries.ts +444 -0
  49. package/src/filters/tileFeaturesSpatialIndex.ts +119 -0
  50. package/src/index.ts +6 -0
  51. package/src/models/index.ts +1 -1
  52. package/src/operations/aggregation.ts +154 -0
  53. package/src/operations/applySorting.ts +109 -0
  54. package/src/operations/groupBy.ts +59 -0
  55. package/src/operations/groupByDate.ts +98 -0
  56. package/src/operations/histogram.ts +66 -0
  57. package/src/operations/index.ts +6 -0
  58. package/src/operations/scatterPlot.ts +50 -0
  59. package/src/sources/h3-tileset-source.ts +18 -6
  60. package/src/sources/index.ts +3 -1
  61. package/src/sources/quadbin-tileset-source.ts +18 -6
  62. package/src/sources/raster-source.ts +1 -0
  63. package/src/sources/vector-query-source.ts +5 -2
  64. package/src/sources/vector-table-source.ts +5 -2
  65. package/src/sources/vector-tileset-source.ts +19 -6
  66. package/src/types-internal.ts +6 -0
  67. package/src/types.ts +60 -2
  68. package/src/utils/dateUtils.ts +28 -0
  69. package/src/utils/getTileFormat.ts +9 -0
  70. package/src/utils/makeIntervalComplete.ts +17 -0
  71. package/src/utils/transformTileCoordsToWGS84.ts +77 -0
  72. package/src/utils/transformToTileCoords.ts +85 -0
  73. package/src/widget-sources/index.ts +3 -1
  74. package/src/widget-sources/types.ts +32 -22
  75. package/src/widget-sources/widget-query-source.ts +6 -3
  76. package/src/widget-sources/{widget-base-source.ts → widget-remote-source.ts} +12 -147
  77. package/src/widget-sources/widget-source.ts +160 -0
  78. package/src/widget-sources/widget-table-source.ts +6 -3
  79. package/src/widget-sources/widget-tileset-source.ts +407 -0
@@ -16,99 +16,21 @@ 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> {
@@ -147,18 +69,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
147
69
  }).then((res: CategoriesModelResponse) => normalizeObjectKeys(res.rows));
148
70
  }
149
71
 
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
72
  async getFeatures(
163
73
  options: FeaturesRequestOptions
164
74
  ): Promise<FeaturesResponse> {
@@ -201,14 +111,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
201
111
  }).then(({rows}: FeaturesModelResponse) => ({rows}));
202
112
  }
203
113
 
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
114
  async getFormula(options: FormulaRequestOptions): Promise<FormulaResponse> {
213
115
  const {
214
116
  filterOwner,
@@ -237,19 +139,15 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
237
139
  spatialFiltersMode,
238
140
  spatialFilter,
239
141
  },
240
- params: {column: column ?? '*', operation, operationExp},
142
+ params: {
143
+ column: column ?? '*',
144
+ operation: operation ?? 'count',
145
+ operationExp,
146
+ },
241
147
  opts: {abortController},
242
148
  }).then((res: FormulaModelResponse) => normalizeObjectKeys(res.rows[0]));
243
149
  }
244
150
 
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
151
  async getHistogram(
254
152
  options: HistogramRequestOptions
255
153
  ): Promise<HistogramResponse> {
@@ -296,15 +194,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
296
194
  return [];
297
195
  }
298
196
 
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
197
  async getRange(options: RangeRequestOptions): Promise<RangeResponse> {
309
198
  const {
310
199
  filterOwner,
@@ -337,14 +226,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
337
226
  }).then((res: RangeModelResponse) => normalizeObjectKeys(res.rows[0]));
338
227
  }
339
228
 
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
229
  async getScatter(options: ScatterRequestOptions): Promise<ScatterResponse> {
349
230
  const {
350
231
  filterOwner,
@@ -390,14 +271,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
390
271
  .then((res) => res.map(({x, y}: {x: number; y: number}) => [x, y]));
391
272
  }
392
273
 
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
274
  async getTable(options: TableRequestOptions): Promise<TableResponse> {
402
275
  const {
403
276
  filterOwner,
@@ -443,14 +316,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
443
316
  }));
444
317
  }
445
318
 
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
319
  async getTimeSeries(
455
320
  options: TimeSeriesRequestOptions
456
321
  ): Promise<TimeSeriesResponse> {
@@ -0,0 +1,160 @@
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 {FilterLogicalOperator, Filter, SpatialFilter} from '../types.js';
21
+ import {getApplicableFilters} from '../utils.js';
22
+ import {getClient} from '../client.js';
23
+ import {ModelSource} from '../models/model.js';
24
+ import {SourceOptions} from '../sources/index.js';
25
+ import {ApiVersion, DEFAULT_API_BASE_URL} from '../constants.js';
26
+ import {getSpatialFiltersResolution} from '../spatial-index.js';
27
+ import {AggregationOptions} from '../sources/types.js';
28
+
29
+ export interface WidgetSourceProps extends Omit<SourceOptions, 'filters'> {
30
+ apiVersion?: ApiVersion;
31
+ filters?: Record<string, Filter>;
32
+ filtersLogicalOperator?: FilterLogicalOperator;
33
+ }
34
+
35
+ /**
36
+ * Source for Widget API requests on a data source defined by a SQL query.
37
+ *
38
+ * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
39
+ */
40
+ export abstract class WidgetSource<Props extends WidgetSourceProps> {
41
+ readonly props: Props;
42
+
43
+ static defaultProps: Partial<WidgetSourceProps> = {
44
+ apiVersion: ApiVersion.V3,
45
+ apiBaseUrl: DEFAULT_API_BASE_URL,
46
+ clientId: getClient(),
47
+ filters: {},
48
+ filtersLogicalOperator: 'and',
49
+ };
50
+
51
+ constructor(props: Props) {
52
+ this.props = {...WidgetSource.defaultProps, ...props};
53
+ }
54
+
55
+ /**
56
+ * Subclasses of {@link WidgetRemoteSource} must implement this method, calling
57
+ * {@link WidgetRemoteSource.prototype._getModelSource} for common source
58
+ * properties, and adding additional required properties including 'type' and
59
+ * 'data'.
60
+ */
61
+ protected abstract getModelSource(owner: string | undefined): ModelSource;
62
+
63
+ protected _getModelSource(
64
+ owner?: string
65
+ ): Omit<ModelSource, 'type' | 'data'> {
66
+ const props = this.props;
67
+ return {
68
+ apiVersion: props.apiVersion as ApiVersion,
69
+ apiBaseUrl: props.apiBaseUrl as string,
70
+ clientId: props.clientId as string,
71
+ accessToken: props.accessToken,
72
+ connectionName: props.connectionName,
73
+ filters: getApplicableFilters(owner, props.filters),
74
+ filtersLogicalOperator: props.filtersLogicalOperator,
75
+ spatialDataType: props.spatialDataType,
76
+ spatialDataColumn: props.spatialDataColumn,
77
+ dataResolution: (props as Partial<AggregationOptions>).dataResolution,
78
+ };
79
+ }
80
+
81
+ protected _getSpatialFiltersResolution(
82
+ source: Omit<ModelSource, 'type' | 'data'>,
83
+ spatialFilter?: SpatialFilter,
84
+ referenceViewState?: ViewState
85
+ ): number | undefined {
86
+ // spatialFiltersResolution applies only to spatial index sources.
87
+ if (!spatialFilter || source.spatialDataType === 'geo') {
88
+ return;
89
+ }
90
+
91
+ if (!referenceViewState) {
92
+ throw new Error(
93
+ 'Missing required option, "spatialIndexReferenceViewState".'
94
+ );
95
+ }
96
+
97
+ return getSpatialFiltersResolution(source, referenceViewState);
98
+ }
99
+
100
+ /**
101
+ * Returns a list of labeled datapoints for categorical data. Suitable for
102
+ * charts including grouped bar charts, pie charts, and tree charts.
103
+ */
104
+ abstract getCategories(
105
+ options: CategoryRequestOptions
106
+ ): Promise<CategoryResponse>;
107
+
108
+ /**
109
+ * Given a list of feature IDs (as found in `_carto_feature_id`) returns all
110
+ * matching features. In datasets containing features with duplicate geometries,
111
+ * feature IDs may be duplicated (IDs are a hash of geometry) and so more
112
+ * results may be returned than IDs in the request.
113
+ * @internal
114
+ * @experimental
115
+ */
116
+ abstract getFeatures(
117
+ options: FeaturesRequestOptions
118
+ ): Promise<FeaturesResponse>;
119
+
120
+ /**
121
+ * Returns a scalar numerical statistic over all matching data. Suitable
122
+ * for 'headline' or 'scorecard' figures such as counts and sums.
123
+ */
124
+ abstract getFormula(options: FormulaRequestOptions): Promise<FormulaResponse>;
125
+
126
+ /**
127
+ * Returns a list of labeled datapoints for 'bins' of data defined as ticks
128
+ * over a numerical range. Suitable for histogram charts.
129
+ */
130
+ abstract getHistogram(
131
+ options: HistogramRequestOptions
132
+ ): Promise<HistogramResponse>;
133
+
134
+ /**
135
+ * Returns a range (min and max) for a numerical column of matching rows.
136
+ * Suitable for displaying certain 'headline' or 'scorecard' statistics,
137
+ * or rendering a range slider UI for filtering.
138
+ */
139
+ abstract getRange(options: RangeRequestOptions): Promise<RangeResponse>;
140
+
141
+ /**
142
+ * Returns a list of bivariate datapoints defined as numerical 'x' and 'y'
143
+ * values. Suitable for rendering scatter plots.
144
+ */
145
+ abstract getScatter(options: ScatterRequestOptions): Promise<ScatterResponse>;
146
+
147
+ /**
148
+ * Returns a list of arbitrary data rows, with support for pagination and
149
+ * sorting. Suitable for displaying tables and lists.
150
+ */
151
+ abstract getTable(options: TableRequestOptions): Promise<TableResponse>;
152
+
153
+ /**
154
+ * Returns a series of labeled numerical values, grouped into equally-sized
155
+ * time intervals. Suitable for rendering time series charts.
156
+ */
157
+ abstract getTimeSeries(
158
+ options: TimeSeriesRequestOptions
159
+ ): Promise<TimeSeriesResponse>;
160
+ }
@@ -3,7 +3,10 @@ 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';
8
11
 
9
12
  type LayerTableSourceOptions =
@@ -35,8 +38,8 @@ export type WidgetTableSourceResult = {widgetSource: WidgetTableSource};
35
38
  * const { widgetSource } = await data;
36
39
  * ```
37
40
  */
38
- export class WidgetTableSource extends WidgetBaseSource<
39
- LayerTableSourceOptions & WidgetBaseSourceProps
41
+ export class WidgetTableSource extends WidgetRemoteSource<
42
+ LayerTableSourceOptions & WidgetRemoteSourceProps
40
43
  > {
41
44
  protected override getModelSource(owner: string): ModelSource {
42
45
  return {