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

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 (94) hide show
  1. package/build/api-client.cjs +2794 -2698
  2. package/build/api-client.d.cts +1373 -0
  3. package/build/api-client.d.ts +1373 -0
  4. package/build/api-client.js +1712 -0
  5. package/build/chunk-V3E7BKVR.js +2063 -0
  6. package/build/worker.d.ts +2 -0
  7. package/build/worker.js +25 -0
  8. package/package.json +18 -18
  9. package/src/constants-internal.ts +0 -6
  10. package/src/filters/tileFeatures.ts +10 -26
  11. package/src/global.d.ts +3 -8
  12. package/src/sources/h3-tileset-source.ts +8 -1
  13. package/src/sources/quadbin-tileset-source.ts +8 -1
  14. package/src/sources/raster-source.ts +5 -18
  15. package/src/sources/types.ts +7 -1
  16. package/src/sources/vector-tileset-source.ts +8 -1
  17. package/src/types.ts +0 -6
  18. package/src/widget-sources/index.ts +1 -1
  19. package/src/widget-sources/widget-remote-source.ts +35 -2
  20. package/src/widget-sources/widget-source.ts +1 -38
  21. package/src/widget-sources/widget-tileset-source.ts +7 -18
  22. package/src/widget-sources/widget-tileset-worker-source.ts +238 -0
  23. package/src/workers/constants.ts +13 -0
  24. package/src/workers/types.ts +20 -0
  25. package/src/workers/utils.ts +33 -0
  26. package/src/workers/widget-tileset-worker.ts +36 -0
  27. package/build/api/carto-api-error.d.ts +0 -26
  28. package/build/api/endpoints.d.ts +0 -24
  29. package/build/api/index.d.ts +0 -5
  30. package/build/api/query.d.ts +0 -3
  31. package/build/api/request-with-parameters.d.ts +0 -10
  32. package/build/api-client.cjs.map +0 -1
  33. package/build/api-client.modern.js +0 -3445
  34. package/build/api-client.modern.js.map +0 -1
  35. package/build/client.d.ts +0 -14
  36. package/build/constants-internal.d.ts +0 -26
  37. package/build/constants.d.ts +0 -53
  38. package/build/deck/get-data-filter-extension-props.d.ts +0 -18
  39. package/build/deck/index.d.ts +0 -1
  40. package/build/filters/Filter.d.ts +0 -25
  41. package/build/filters/FilterTypes.d.ts +0 -3
  42. package/build/filters/geosjonFeatures.d.ts +0 -8
  43. package/build/filters/index.d.ts +0 -6
  44. package/build/filters/tileFeatures.d.ts +0 -21
  45. package/build/filters/tileFeaturesGeometries.d.ts +0 -13
  46. package/build/filters/tileFeaturesRaster.d.ts +0 -16
  47. package/build/filters/tileFeaturesSpatialIndex.d.ts +0 -10
  48. package/build/filters.d.ts +0 -39
  49. package/build/geo.d.ts +0 -19
  50. package/build/index.d.ts +0 -16
  51. package/build/models/common.d.ts +0 -28
  52. package/build/models/index.d.ts +0 -3
  53. package/build/models/model.d.ts +0 -37
  54. package/build/operations/aggregation.d.ts +0 -8
  55. package/build/operations/applySorting.d.ts +0 -20
  56. package/build/operations/groupBy.d.ts +0 -15
  57. package/build/operations/groupByDate.d.ts +0 -11
  58. package/build/operations/histogram.d.ts +0 -13
  59. package/build/operations/index.d.ts +0 -6
  60. package/build/operations/scatterPlot.d.ts +0 -14
  61. package/build/sources/base-source.d.ts +0 -4
  62. package/build/sources/boundary-query-source.d.ts +0 -10
  63. package/build/sources/boundary-table-source.d.ts +0 -8
  64. package/build/sources/h3-query-source.d.ts +0 -5
  65. package/build/sources/h3-table-source.d.ts +0 -5
  66. package/build/sources/h3-tileset-source.d.ts +0 -5
  67. package/build/sources/index.d.ts +0 -26
  68. package/build/sources/quadbin-query-source.d.ts +0 -5
  69. package/build/sources/quadbin-table-source.d.ts +0 -5
  70. package/build/sources/quadbin-tileset-source.d.ts +0 -5
  71. package/build/sources/raster-source.d.ts +0 -5
  72. package/build/sources/types.d.ts +0 -366
  73. package/build/sources/vector-query-source.d.ts +0 -5
  74. package/build/sources/vector-table-source.d.ts +0 -5
  75. package/build/sources/vector-tileset-source.d.ts +0 -5
  76. package/build/spatial-index.d.ts +0 -14
  77. package/build/types-internal.d.ts +0 -56
  78. package/build/types.d.ts +0 -148
  79. package/build/utils/dateUtils.d.ts +0 -10
  80. package/build/utils/getTileFormat.d.ts +0 -3
  81. package/build/utils/makeIntervalComplete.d.ts +0 -2
  82. package/build/utils/transformTileCoordsToWGS84.d.ts +0 -8
  83. package/build/utils/transformToTileCoords.d.ts +0 -9
  84. package/build/utils.d.ts +0 -32
  85. package/build/widget-sources/index.d.ts +0 -7
  86. package/build/widget-sources/types.d.ts +0 -160
  87. package/build/widget-sources/widget-query-source.d.ts +0 -34
  88. package/build/widget-sources/widget-raster-source.d.ts +0 -11
  89. package/build/widget-sources/widget-remote-source.d.ts +0 -21
  90. package/build/widget-sources/widget-source.d.ts +0 -74
  91. package/build/widget-sources/widget-table-source.d.ts +0 -34
  92. package/build/widget-sources/widget-tileset-source.d.ts +0 -75
  93. package/src/filters/tileFeaturesRaster.ts +0 -111
  94. package/src/widget-sources/widget-raster-source.ts +0 -14
@@ -0,0 +1,238 @@
1
+ import {
2
+ CategoryRequestOptions,
3
+ CategoryResponse,
4
+ FeaturesResponse,
5
+ FormulaRequestOptions,
6
+ FormulaResponse,
7
+ HistogramRequestOptions,
8
+ HistogramResponse,
9
+ RangeRequestOptions,
10
+ RangeResponse,
11
+ ScatterRequestOptions,
12
+ ScatterResponse,
13
+ TableRequestOptions,
14
+ TableResponse,
15
+ TimeSeriesRequestOptions,
16
+ TimeSeriesResponse,
17
+ } from './types.js';
18
+ import {SpatialFilter, Tile} from '../types.js';
19
+ import {TileFeatureExtractOptions} from '../filters/index.js';
20
+ import {FeatureCollection} from 'geojson';
21
+ import {WidgetSource} from './widget-source.js';
22
+ import {WidgetTilesetSourceProps} from './widget-tileset-source.js';
23
+ import {Method} from '../workers/constants.js';
24
+ import {WorkerRequest, WorkerResponse} from '../workers/types.js';
25
+
26
+ /**
27
+ * TODO
28
+ */
29
+ export class WidgetTilesetWorkerSource extends WidgetSource<WidgetTilesetSourceProps> {
30
+ constructor(props: WidgetTilesetSourceProps) {
31
+ super(props);
32
+
33
+ WidgetTilesetWorkerSource.init();
34
+ WidgetTilesetWorkerSource.WORKER.postMessage({
35
+ tableName: this.props.tableName,
36
+ method: Method.INIT,
37
+ params: [this.props],
38
+ } as WorkerRequest);
39
+ }
40
+
41
+ /**
42
+ * Loads features as a list of tiles (typically provided by deck.gl).
43
+ * After tiles are loaded, {@link extractTileFeatures} must be called
44
+ * before computing statistics on the tiles.
45
+ */
46
+ loadTiles(tiles: unknown[]) {
47
+ tiles = (tiles as Tile[]).map(({id, bbox, data}) => ({
48
+ id,
49
+ bbox,
50
+ data,
51
+ }));
52
+
53
+ WidgetTilesetWorkerSource.WORKER.postMessage({
54
+ tableName: this.props.tableName,
55
+ method: Method.LOAD_TILES,
56
+ params: [tiles],
57
+ } as WorkerRequest);
58
+ }
59
+
60
+ /** Configures options used to extract features from tiles. */
61
+ setTileFeatureExtractOptions(options: TileFeatureExtractOptions) {
62
+ WidgetTilesetWorkerSource.WORKER.postMessage({
63
+ tableName: this.props.tableName,
64
+ type: Method.SET_TILE_FEATURE_EXTRACT_OPTIONS,
65
+ params: [options],
66
+ });
67
+ }
68
+
69
+ /**
70
+ * Loads features as GeoJSON (used for testing).
71
+ * @experimental
72
+ * @internal Not for public use. Spatial filters in other method calls will be ignored.
73
+ */
74
+ loadGeoJSON({
75
+ geojson,
76
+ spatialFilter,
77
+ }: {
78
+ geojson: FeatureCollection;
79
+ spatialFilter: SpatialFilter;
80
+ }) {
81
+ WidgetTilesetWorkerSource.WORKER.postMessage({
82
+ tableName: this.props.tableName,
83
+ method: Method.LOAD_GEOJSON,
84
+ params: [{geojson, spatialFilter}],
85
+ } as WorkerRequest);
86
+ }
87
+
88
+ // eslint-disable-next-line @typescript-eslint/require-await
89
+ override async getFeatures(): Promise<FeaturesResponse> {
90
+ throw new Error('getFeatures not supported for tilesets');
91
+ }
92
+
93
+ async getFormula({
94
+ abortController,
95
+ ...options
96
+ }: FormulaRequestOptions): Promise<FormulaResponse> {
97
+ return this._executeWorkerMethod(
98
+ this.props.tableName,
99
+ Method.GET_FORMULA,
100
+ [options],
101
+ abortController?.signal
102
+ );
103
+ }
104
+
105
+ override async getHistogram({
106
+ abortController,
107
+ ...options
108
+ }: HistogramRequestOptions): Promise<HistogramResponse> {
109
+ return this._executeWorkerMethod(
110
+ this.props.tableName,
111
+ Method.GET_HISTOGRAM,
112
+ [options],
113
+ abortController?.signal
114
+ );
115
+ }
116
+
117
+ override async getCategories({
118
+ abortController,
119
+ ...options
120
+ }: CategoryRequestOptions): Promise<CategoryResponse> {
121
+ return this._executeWorkerMethod(
122
+ this.props.tableName,
123
+ Method.GET_CATEGORIES,
124
+ [options],
125
+ abortController?.signal
126
+ );
127
+ }
128
+
129
+ override async getScatter({
130
+ abortController,
131
+ ...options
132
+ }: ScatterRequestOptions): Promise<ScatterResponse> {
133
+ return this._executeWorkerMethod(
134
+ this.props.tableName,
135
+ Method.GET_SCATTER,
136
+ [options],
137
+ abortController?.signal
138
+ );
139
+ }
140
+
141
+ override async getTable({
142
+ abortController,
143
+ ...options
144
+ }: TableRequestOptions): Promise<TableResponse> {
145
+ return this._executeWorkerMethod(
146
+ this.props.tableName,
147
+ Method.GET_TABLE,
148
+ [options],
149
+ abortController?.signal
150
+ );
151
+ }
152
+
153
+ override async getTimeSeries({
154
+ abortController,
155
+ ...options
156
+ }: TimeSeriesRequestOptions): Promise<TimeSeriesResponse> {
157
+ return this._executeWorkerMethod(
158
+ this.props.tableName,
159
+ Method.GET_TIME_SERIES,
160
+ [options],
161
+ abortController?.signal
162
+ );
163
+ }
164
+
165
+ override async getRange({
166
+ abortController,
167
+ ...options
168
+ }: RangeRequestOptions): Promise<RangeResponse> {
169
+ return this._executeWorkerMethod(
170
+ this.props.tableName,
171
+ Method.GET_RANGE,
172
+ [options],
173
+ abortController?.signal
174
+ );
175
+ }
176
+
177
+ /////////////////////////////////////////////////////////////////////////////
178
+ // WEB WORKER MANAGEMENT
179
+
180
+ // TODO: Singleton? Pool shared among datasets? One per dataset?
181
+ protected static WORKER: Worker;
182
+ protected static _nextRequestID = 1;
183
+
184
+ static init() {
185
+ WidgetTilesetWorkerSource.WORKER = new Worker(
186
+ new URL('@carto/api-client/worker', import.meta.url),
187
+ {
188
+ type: 'module',
189
+ name: 'cartowidgettileset',
190
+ }
191
+ );
192
+ }
193
+
194
+ _executeWorkerMethod<T>(
195
+ tableName: string,
196
+ method: Method,
197
+ params: unknown[],
198
+ signal?: AbortSignal
199
+ ): Promise<T> {
200
+ const worker = WidgetTilesetWorkerSource.WORKER;
201
+ const requestId = WidgetTilesetWorkerSource._nextRequestID++;
202
+
203
+ // TODO: ViewState may contain non-serializable data, which we do not need.
204
+ // Remove this sanitization after sc-469614 is fixed.
205
+ const options = params[0] as any;
206
+ if (options?.spatialIndexReferenceViewState) {
207
+ const {zoom, latitude, longitude} =
208
+ options.spatialIndexReferenceViewState;
209
+ options.spatialIndexReferenceViewState = {zoom, latitude, longitude};
210
+ }
211
+
212
+ worker.postMessage({
213
+ requestId,
214
+ tableName,
215
+ method,
216
+ params,
217
+ } as WorkerRequest);
218
+
219
+ return new Promise((resolve, reject) => {
220
+ function listener(e: MessageEvent) {
221
+ const response = e.data as WorkerResponse;
222
+ if (response.requestId !== requestId) return;
223
+
224
+ worker.removeEventListener('message', listener);
225
+
226
+ if (signal?.aborted) {
227
+ reject(new Error(signal.reason));
228
+ } else if (response.ok) {
229
+ resolve(response.result as T);
230
+ } else {
231
+ reject(new Error(response.error));
232
+ }
233
+ }
234
+
235
+ worker.addEventListener('message', listener);
236
+ });
237
+ }
238
+ }
@@ -0,0 +1,13 @@
1
+ export enum Method {
2
+ INIT = 'init',
3
+ LOAD_TILES = 'loadTiles',
4
+ SET_TILE_FEATURE_EXTRACT_OPTIONS = 'setTileFeatureExtractOptions',
5
+ LOAD_GEOJSON = 'loadGeoJSON',
6
+ GET_FORMULA = 'getFormula',
7
+ GET_HISTOGRAM = 'getHistogram',
8
+ GET_CATEGORIES = 'getCategories',
9
+ GET_SCATTER = 'getScatter',
10
+ GET_TABLE = 'getTable',
11
+ GET_TIME_SERIES = 'getTimeSeries',
12
+ GET_RANGE = 'getRange',
13
+ }
@@ -0,0 +1,20 @@
1
+ import type {Method} from './constants.js';
2
+
3
+ export type WorkerRequest = {
4
+ requestId?: number;
5
+ tableName: string; // TODO: Table name is not a unique identifier.
6
+ method: Method;
7
+ params: unknown[];
8
+ };
9
+
10
+ export type WorkerResponse =
11
+ | {
12
+ requestId: number;
13
+ ok: true;
14
+ result: unknown;
15
+ }
16
+ | {
17
+ requestId: number;
18
+ ok: false;
19
+ error: string;
20
+ };
@@ -0,0 +1,33 @@
1
+ let _isModuleWorkerSupported: boolean | null =
2
+ TSUP_FORMAT === 'cjs' ? false : null;
3
+
4
+ /**
5
+ * Checks whether current environment supports ES Module Workers, by way of a
6
+ * two-part check:
7
+ * (1) If this is a CJS build, return false.
8
+ * (2) If this is an ESM build, return true if the runtime supports ESM Workers.
9
+ */
10
+ export function isModuleWorkerSupported(): boolean {
11
+ if (_isModuleWorkerSupported !== null) {
12
+ return _isModuleWorkerSupported;
13
+ }
14
+
15
+ try {
16
+ // https://stackoverflow.com/a/62963963
17
+ new Worker('blob://', {
18
+ get type() {
19
+ _isModuleWorkerSupported = true;
20
+ return 'module' as const;
21
+ },
22
+ });
23
+ } catch {
24
+ // Do nothing. 'blob://' should always throw an error to prevent the
25
+ // browser from creating an (expensive) worker. We care whether the
26
+ // 'type' getter was called, not about the error.
27
+ } finally {
28
+ // If 'type' getter wasn't called, modules are unsupported.
29
+ _isModuleWorkerSupported ||= false;
30
+ }
31
+
32
+ return _isModuleWorkerSupported;
33
+ }
@@ -0,0 +1,36 @@
1
+ import {
2
+ WidgetTilesetSource,
3
+ type WidgetTilesetSourceProps,
4
+ } from '../widget-sources/widget-tileset-source.js';
5
+ import {Method} from './constants.js';
6
+ import type {WorkerRequest, WorkerResponse} from './types.js';
7
+
8
+ // TODO: Cannot rely on tableName as unique ID.
9
+ const SOURCES_BY_NAME = new Map<string, WidgetTilesetSource>();
10
+
11
+ addEventListener('message', (e) => {
12
+ const {tableName, method, params, requestId} = e.data as WorkerRequest;
13
+
14
+ if (method === Method.INIT) {
15
+ const props = params[0] as WidgetTilesetSourceProps;
16
+ SOURCES_BY_NAME.set(tableName, new WidgetTilesetSource(props));
17
+ return;
18
+ }
19
+
20
+ const source = SOURCES_BY_NAME.get(tableName);
21
+
22
+ if (!source) {
23
+ const error = `Unknown dataset: ${tableName}`;
24
+ postMessage({ok: false, error, requestId} as WorkerResponse);
25
+ return;
26
+ }
27
+
28
+ // @ts-expect-error No type-checking dynamic method name.
29
+ Promise.resolve(source[method](...params))
30
+ .then((result) => {
31
+ postMessage({ok: true, result, requestId} as WorkerResponse);
32
+ })
33
+ .catch((error) => {
34
+ postMessage({ok: false, error, requestId} as WorkerResponse);
35
+ });
36
+ });
@@ -1,26 +0,0 @@
1
- import { MapType } from '../types.js';
2
- export type APIRequestType = 'Map data' | 'Map instantiation' | 'Public map' | 'Tile stats' | 'SQL' | 'Basemap style';
3
- export type APIErrorContext = {
4
- requestType: APIRequestType;
5
- mapId?: string;
6
- connection?: string;
7
- source?: string;
8
- type?: MapType;
9
- };
10
- /**
11
- *
12
- * Custom error for reported errors in CARTO Maps API.
13
- * Provides useful debugging information in console and context for applications.
14
- *
15
- */
16
- export declare class CartoAPIError extends Error {
17
- /** Source error from server */
18
- error: Error;
19
- /** Context (API call & parameters) in which error occured */
20
- errorContext: APIErrorContext;
21
- /** Response from server */
22
- response?: Response;
23
- /** JSON Response from server */
24
- responseJson?: any;
25
- constructor(error: Error, errorContext: APIErrorContext, response?: Response, responseJson?: any);
26
- }
@@ -1,24 +0,0 @@
1
- import { MapType } from '../types.js';
2
- export type V3Endpoint = 'maps' | 'stats' | 'sql';
3
- /** @internal Required by fetchMap(). */
4
- export declare function buildPublicMapUrl({ apiBaseUrl, cartoMapId, }: {
5
- apiBaseUrl: string;
6
- cartoMapId: string;
7
- }): string;
8
- /** @internal Required by fetchMap(). */
9
- export declare function buildStatsUrl({ attribute, apiBaseUrl, connectionName, source, type, }: {
10
- attribute: string;
11
- apiBaseUrl: string;
12
- connectionName: string;
13
- source: string;
14
- type: MapType;
15
- }): string;
16
- export declare function buildSourceUrl({ apiBaseUrl, connectionName, endpoint, }: {
17
- apiBaseUrl: string;
18
- connectionName: string;
19
- endpoint: MapType;
20
- }): string;
21
- export declare function buildQueryUrl({ apiBaseUrl, connectionName, }: {
22
- apiBaseUrl: string;
23
- connectionName: string;
24
- }): string;
@@ -1,5 +0,0 @@
1
- export { CartoAPIError, APIErrorContext, APIRequestType, } from './carto-api-error.js';
2
- export { buildPublicMapUrl, buildStatsUrl } from './endpoints.js';
3
- export { query } from './query.js';
4
- export type { QueryOptions } from './query.js';
5
- export { requestWithParameters } from './request-with-parameters.js';
@@ -1,3 +0,0 @@
1
- import type { SourceOptions, QuerySourceOptions, QueryResult } from '../sources/types.js';
2
- export type QueryOptions = SourceOptions & QuerySourceOptions;
3
- export declare const query: (options: QueryOptions) => Promise<QueryResult>;
@@ -1,10 +0,0 @@
1
- import { APIErrorContext } from './carto-api-error.js';
2
- import { LocalCacheOptions } from '../sources/types.js';
3
- export declare function requestWithParameters<T = any>({ baseUrl, parameters, headers: customHeaders, errorContext, maxLengthURL, localCache, }: {
4
- baseUrl: string;
5
- parameters?: Record<string, unknown>;
6
- headers?: Record<string, string>;
7
- errorContext: APIErrorContext;
8
- maxLengthURL?: number;
9
- localCache?: LocalCacheOptions;
10
- }): Promise<T>;