@carto/api-client 0.4.1-alpha.0 → 0.4.1-alpha.1
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.
- package/CHANGELOG.md +4 -0
- package/build/api/query.d.ts +1 -1
- package/build/api/request-with-parameters.d.ts +3 -1
- package/build/api-client.cjs +71 -231
- package/build/api-client.cjs.map +1 -1
- package/build/api-client.modern.js +88 -239
- package/build/api-client.modern.js.map +1 -1
- package/build/models/model.d.ts +1 -7
- package/build/sources/index.d.ts +1 -1
- package/build/sources/types.d.ts +175 -34
- package/build/utils.d.ts +1 -1
- package/build/widget-sources/types.d.ts +1 -8
- package/build/widget-sources/widget-base-source.d.ts +1 -0
- package/package.json +1 -1
- package/src/api/query.ts +4 -1
- package/src/api/request-with-parameters.ts +34 -4
- package/src/models/model.ts +24 -47
- package/src/sources/base-source.ts +4 -1
- package/src/sources/h3-query-source.ts +1 -7
- package/src/sources/h3-table-source.ts +1 -6
- package/src/sources/index.ts +5 -0
- package/src/sources/quadbin-query-source.ts +1 -6
- package/src/sources/quadbin-table-source.ts +1 -6
- package/src/sources/types.ts +200 -36
- package/src/sources/vector-query-source.ts +1 -4
- package/src/sources/vector-table-source.ts +1 -5
- package/src/utils.ts +1 -1
- package/src/widget-sources/types.ts +1 -9
- package/src/widget-sources/widget-base-source.ts +19 -185
- package/build/spatial-index.d.ts +0 -11
- package/src/spatial-index.ts +0 -119
package/build/models/model.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Filter, FilterLogicalOperator, MapType, QueryParameters, SpatialFilter } from '../types.js';
|
|
2
2
|
import { ModelRequestOptions } from './common.js';
|
|
3
3
|
import { ApiVersion } from '../constants.js';
|
|
4
|
-
import { SpatialDataType, SpatialFilterPolyfillMode } from '../sources/types.js';
|
|
5
4
|
/** @internalRemarks Source: @carto/react-api */
|
|
6
5
|
declare const AVAILABLE_MODELS: readonly ["category", "histogram", "formula", "pick", "timeseries", "range", "scatterplot", "table"];
|
|
7
6
|
export type Model = (typeof AVAILABLE_MODELS)[number];
|
|
@@ -15,14 +14,9 @@ export interface ModelSource {
|
|
|
15
14
|
data: string;
|
|
16
15
|
filters?: Record<string, Filter>;
|
|
17
16
|
filtersLogicalOperator?: FilterLogicalOperator;
|
|
17
|
+
geoColumn?: string;
|
|
18
18
|
spatialFilter?: SpatialFilter;
|
|
19
19
|
queryParameters?: QueryParameters;
|
|
20
|
-
spatialDataColumn?: string;
|
|
21
|
-
spatialDataType?: SpatialDataType;
|
|
22
|
-
spatialFiltersResolution?: number;
|
|
23
|
-
spatialFiltersMode?: SpatialFilterPolyfillMode;
|
|
24
|
-
/** original resolution of the spatial index data as stored in the DW */
|
|
25
|
-
dataResolution?: number;
|
|
26
20
|
}
|
|
27
21
|
/**
|
|
28
22
|
* Execute a SQL model request.
|
package/build/sources/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { SOURCE_DEFAULTS } from './base-source';
|
|
2
|
-
export type { TilejsonResult, GeojsonResult, JsonResult, QueryResult, } from './types';
|
|
2
|
+
export type { VectorLayer, RasterMetadataBandStats, RasterBandColorinterp, RasterMetadataBand, RasterMetadata, TilejsonResult, GeojsonResult, JsonResult, QueryResult, } from './types';
|
|
3
3
|
export { boundaryQuerySource } from './boundary-query-source';
|
|
4
4
|
export type { BoundaryQuerySourceOptions } from './boundary-query-source';
|
|
5
5
|
export { boundaryTableSource } from './boundary-table-source';
|
package/build/sources/types.d.ts
CHANGED
|
@@ -39,29 +39,23 @@ export type SourceOptionalOptions = {
|
|
|
39
39
|
*/
|
|
40
40
|
maxLengthURL?: number;
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* If not present, defaults to `'geom'` for generic queries, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
|
|
42
|
+
* By default, local in-memory caching is enabled.
|
|
45
43
|
*/
|
|
46
|
-
|
|
44
|
+
localCache?: LocalCacheOptions;
|
|
45
|
+
};
|
|
46
|
+
export type LocalCacheOptions = {
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* Map that stores requests and their responses.
|
|
49
49
|
*/
|
|
50
|
-
|
|
50
|
+
cache?: Map<string, Promise<unknown>>;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* Supported `tileResolution` values, with corresponding grid sizes:
|
|
52
|
+
* Cache control
|
|
53
|
+
* * `no-cache`: If present, the source will always fetch from original source.
|
|
54
|
+
* * `no-store`: If present, source will not store result in cache (for later reuse).
|
|
57
55
|
*
|
|
58
|
-
*
|
|
59
|
-
* - 0.5: 512x512
|
|
60
|
-
* - 1: 1024x1024
|
|
61
|
-
* - 2: 2048x2048
|
|
62
|
-
* - 4: 4096x4096
|
|
56
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#directives
|
|
63
57
|
*/
|
|
64
|
-
|
|
58
|
+
cacheControl?: ('no-cache' | 'no-store')[];
|
|
65
59
|
};
|
|
66
60
|
export type SourceOptions = SourceRequiredOptions & Partial<SourceOptionalOptions>;
|
|
67
61
|
export type AggregationOptions = {
|
|
@@ -79,10 +73,6 @@ export type AggregationOptions = {
|
|
|
79
73
|
* @default 6 for quadbin and 4 for h3 sources
|
|
80
74
|
*/
|
|
81
75
|
aggregationResLevel?: number;
|
|
82
|
-
/**
|
|
83
|
-
* Original resolution of the spatial index data as stored in the DW
|
|
84
|
-
*/
|
|
85
|
-
dataResolution?: number;
|
|
86
76
|
};
|
|
87
77
|
export type FilterOptions = {
|
|
88
78
|
/**
|
|
@@ -91,8 +81,28 @@ export type FilterOptions = {
|
|
|
91
81
|
filters?: Filters;
|
|
92
82
|
};
|
|
93
83
|
export type QuerySourceOptions = {
|
|
94
|
-
/**
|
|
84
|
+
/**
|
|
85
|
+
* The column name and the type of geospatial support.
|
|
86
|
+
*
|
|
87
|
+
* If not present, defaults to `'geom'` for generic queries, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
|
|
88
|
+
*/
|
|
89
|
+
spatialDataColumn?: string;
|
|
90
|
+
/** SQL query. */
|
|
95
91
|
sqlQuery: string;
|
|
92
|
+
/**
|
|
93
|
+
* Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
|
|
94
|
+
* quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
|
|
95
|
+
* the quantization grid proportionately.
|
|
96
|
+
*
|
|
97
|
+
* Supported `tileResolution` values, with corresponding grid sizes:
|
|
98
|
+
*
|
|
99
|
+
* - 0.25: 256x256
|
|
100
|
+
* - 0.5: 512x512
|
|
101
|
+
* - 1: 1024x1024
|
|
102
|
+
* - 2: 2048x2048
|
|
103
|
+
* - 4: 4096x4096
|
|
104
|
+
*/
|
|
105
|
+
tileResolution?: TileResolution;
|
|
96
106
|
/**
|
|
97
107
|
* Values for named or positional paramteres in the query.
|
|
98
108
|
*
|
|
@@ -124,6 +134,26 @@ export type TableSourceOptions = {
|
|
|
124
134
|
* Fully qualified name of table.
|
|
125
135
|
*/
|
|
126
136
|
tableName: string;
|
|
137
|
+
/**
|
|
138
|
+
* The column name and the type of geospatial support.
|
|
139
|
+
*
|
|
140
|
+
* If not present, defaults to `'geom'` for generic tables, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
|
|
141
|
+
*/
|
|
142
|
+
spatialDataColumn?: string;
|
|
143
|
+
/**
|
|
144
|
+
* Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
|
|
145
|
+
* quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
|
|
146
|
+
* the quantization grid proportionately.
|
|
147
|
+
*
|
|
148
|
+
* Supported `tileResolution` values, with corresponding grid sizes:
|
|
149
|
+
*
|
|
150
|
+
* - 0.25: 256x256
|
|
151
|
+
* - 0.5: 512x512
|
|
152
|
+
* - 1: 1024x1024
|
|
153
|
+
* - 2: 2048x2048
|
|
154
|
+
* - 4: 4096x4096
|
|
155
|
+
*/
|
|
156
|
+
tileResolution?: TileResolution;
|
|
127
157
|
};
|
|
128
158
|
export type TilesetSourceOptions = {
|
|
129
159
|
/**
|
|
@@ -140,13 +170,6 @@ export type ColumnsOption = {
|
|
|
140
170
|
columns?: string[];
|
|
141
171
|
};
|
|
142
172
|
export type SpatialDataType = 'geo' | 'h3' | 'quadbin';
|
|
143
|
-
/**
|
|
144
|
-
* Strategy used for covering spatial filter geometry with spatial indexes.
|
|
145
|
-
* See https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/sql-reference/quadbin#quadbin_polyfill_mode
|
|
146
|
-
* or https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/sql-reference/h3#h3_polyfill_mode for more information.
|
|
147
|
-
* @internalRemarks Source: cloud-native maps-api
|
|
148
|
-
* */
|
|
149
|
-
export type SpatialFilterPolyfillMode = 'center' | 'intersects' | 'contains';
|
|
150
173
|
export type TilejsonMapInstantiation = MapInstantiation & {
|
|
151
174
|
tilejson: {
|
|
152
175
|
url: string[];
|
|
@@ -161,16 +184,42 @@ export interface Tilejson {
|
|
|
161
184
|
attribution: string;
|
|
162
185
|
scheme: string;
|
|
163
186
|
tiles: string[];
|
|
164
|
-
properties_tiles: string[];
|
|
165
|
-
minresolution: number;
|
|
166
|
-
maxresolution: number;
|
|
167
187
|
minzoom: number;
|
|
168
188
|
maxzoom: number;
|
|
169
|
-
bounds: [number, number, number, number];
|
|
170
|
-
center: [number, number, number];
|
|
189
|
+
bounds: [left: number, bottom: number, right: number, top: number];
|
|
190
|
+
center: [longitute: number, latitude: number, zoom: number];
|
|
171
191
|
vector_layers: VectorLayer[];
|
|
192
|
+
minresolution: number;
|
|
193
|
+
maxresolution: number;
|
|
194
|
+
properties_tiles: string[];
|
|
172
195
|
tilestats: Tilestats;
|
|
173
196
|
tileResolution?: TileResolution;
|
|
197
|
+
/**
|
|
198
|
+
* Resolution of data in spatial-index dataset (e.g. H3, Quadbin).
|
|
199
|
+
*
|
|
200
|
+
* @internal
|
|
201
|
+
*/
|
|
202
|
+
dataresolution?: number;
|
|
203
|
+
/**
|
|
204
|
+
* Array of ratios of dropped features per zoom level.
|
|
205
|
+
*
|
|
206
|
+
* Example: `[0,0,0.5]` - means that 50% of features are dropped at zoom 2 and bigger.
|
|
207
|
+
*
|
|
208
|
+
* @internal
|
|
209
|
+
*/
|
|
210
|
+
fraction_dropped_per_zoom?: number[];
|
|
211
|
+
/**
|
|
212
|
+
* Names of bands - rasters only.
|
|
213
|
+
*
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
216
|
+
raster_bands?: string[];
|
|
217
|
+
/**
|
|
218
|
+
* Raster metadata - rasters only.
|
|
219
|
+
*
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
raster_metadata?: RasterMetadata;
|
|
174
223
|
}
|
|
175
224
|
export interface Tilestats {
|
|
176
225
|
layerCount: number;
|
|
@@ -191,7 +240,99 @@ export interface VectorLayer {
|
|
|
191
240
|
minzoom: number;
|
|
192
241
|
maxzoom: number;
|
|
193
242
|
fields: Record<string, string>;
|
|
243
|
+
geometry_type?: string;
|
|
194
244
|
}
|
|
245
|
+
export type RasterMetadataBandStats = {
|
|
246
|
+
approximated_stats?: boolean;
|
|
247
|
+
min: number;
|
|
248
|
+
max: number;
|
|
249
|
+
mean: number;
|
|
250
|
+
stddev: number;
|
|
251
|
+
sum: number;
|
|
252
|
+
sum_squares: number;
|
|
253
|
+
count: number;
|
|
254
|
+
/**
|
|
255
|
+
* Quantiles by number of buckets.
|
|
256
|
+
*
|
|
257
|
+
* Example:
|
|
258
|
+
* ```ts
|
|
259
|
+
* {
|
|
260
|
+
* // for 3 buckets, first 1/3 of items lies in range [min, 20], second 1/3 is in [20, 40], and last 1/3 is in [40, max]
|
|
261
|
+
* 3: [20, 40],
|
|
262
|
+
* 4: [20, 30, 50], for 4 buckets ...
|
|
263
|
+
* }
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
quantiles?: Record<number, number[]>;
|
|
267
|
+
/**
|
|
268
|
+
* Top values by number of values.
|
|
269
|
+
*
|
|
270
|
+
* Key of dictionary is value, value is count.
|
|
271
|
+
* Key order is random.
|
|
272
|
+
*
|
|
273
|
+
* Example:
|
|
274
|
+
* ```
|
|
275
|
+
* {
|
|
276
|
+
* 3: 5, // means there are 5 pixels with value 3
|
|
277
|
+
* 11: 222,
|
|
278
|
+
* 12: 333, // means that 12 is most common value with count 333
|
|
279
|
+
* ... // (assuming 333 was largest value in dict)
|
|
280
|
+
* }
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
top_values?: Record<number, number>;
|
|
284
|
+
/**
|
|
285
|
+
* Raster loader version.
|
|
286
|
+
*/
|
|
287
|
+
version?: string;
|
|
288
|
+
};
|
|
289
|
+
export declare enum RasterBandColorinterp {
|
|
290
|
+
Gray = "gray",
|
|
291
|
+
Red = "red",
|
|
292
|
+
Green = "green",
|
|
293
|
+
Blue = "blue",
|
|
294
|
+
Alpha = "alpha",
|
|
295
|
+
Palette = "palette"
|
|
296
|
+
}
|
|
297
|
+
export type RasterMetadataBand = {
|
|
298
|
+
type: string;
|
|
299
|
+
name: string;
|
|
300
|
+
stats: RasterMetadataBandStats;
|
|
301
|
+
/**
|
|
302
|
+
* Known values:
|
|
303
|
+
* * `palette`: use unique value and `colortable` ad default mapping
|
|
304
|
+
* * `red`, `green`, `blue`: use the band as color channel
|
|
305
|
+
* * `gray`: use the band as grayscale
|
|
306
|
+
*/
|
|
307
|
+
colorinterp?: string | RasterBandColorinterp;
|
|
308
|
+
/**
|
|
309
|
+
* Default color mapping for unique values (or if coloprinterp is `palette`)
|
|
310
|
+
*/
|
|
311
|
+
colortable?: Record<string, number[]>;
|
|
312
|
+
/**
|
|
313
|
+
* No value representation.
|
|
314
|
+
* Observed values:
|
|
315
|
+
* * `'nan'` for `NaN`
|
|
316
|
+
* * `number`: both as string as number, so parsing is needed
|
|
317
|
+
*/
|
|
318
|
+
nodata: string | number;
|
|
319
|
+
};
|
|
320
|
+
export type RasterMetadata = {
|
|
321
|
+
block_resolution: number;
|
|
322
|
+
minresolution: number;
|
|
323
|
+
maxresolution: number;
|
|
324
|
+
nodata: number | string;
|
|
325
|
+
bands: RasterMetadataBand[];
|
|
326
|
+
bounds: [left: number, bottom: number, right: number, top: number];
|
|
327
|
+
center: [longitute: number, latitude: number, zoom: number];
|
|
328
|
+
width: number;
|
|
329
|
+
height: number;
|
|
330
|
+
block_width: number;
|
|
331
|
+
block_height: number;
|
|
332
|
+
num_blocks: number;
|
|
333
|
+
num_pixels: number;
|
|
334
|
+
pixel_resolution: number;
|
|
335
|
+
};
|
|
195
336
|
export type TilejsonResult = Tilejson & {
|
|
196
337
|
accessToken: string;
|
|
197
338
|
};
|
package/build/utils.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ type Row<T> = Record<string, T> | Record<string, T>[] | T[] | T;
|
|
|
14
14
|
*/
|
|
15
15
|
export declare function normalizeObjectKeys<T, R extends Row<T>>(el: R): R;
|
|
16
16
|
/** @internalRemarks Source: @carto/react-core */
|
|
17
|
-
export declare function assert(condition: unknown, message: string):
|
|
17
|
+
export declare function assert(condition: unknown, message: string): void;
|
|
18
18
|
/**
|
|
19
19
|
* @internalRemarks Source: @carto/react-core
|
|
20
20
|
* @internal
|
|
@@ -1,20 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TileResolution } from '../sources/types';
|
|
2
2
|
import { GroupDateType, SortColumnType, SortDirection, SpatialFilter } from '../types';
|
|
3
3
|
/******************************************************************************
|
|
4
4
|
* WIDGET API REQUESTS
|
|
5
5
|
*/
|
|
6
|
-
export interface ViewState {
|
|
7
|
-
zoom: number;
|
|
8
|
-
latitude: number;
|
|
9
|
-
longitude: number;
|
|
10
|
-
}
|
|
11
6
|
/** Common options for {@link WidgetBaseSource} requests. */
|
|
12
7
|
interface BaseRequestOptions {
|
|
13
8
|
spatialFilter?: SpatialFilter;
|
|
14
|
-
spatialFiltersMode?: SpatialFilterPolyfillMode;
|
|
15
9
|
abortController?: AbortController;
|
|
16
10
|
filterOwner?: string;
|
|
17
|
-
viewState?: ViewState;
|
|
18
11
|
}
|
|
19
12
|
/** Options for {@link WidgetBaseSource#getCategories}. */
|
|
20
13
|
export interface CategoryRequestOptions extends BaseRequestOptions {
|
|
@@ -5,6 +5,7 @@ import { SourceOptions } from '../sources/index.js';
|
|
|
5
5
|
import { ApiVersion } from '../constants.js';
|
|
6
6
|
export interface WidgetBaseSourceProps extends Omit<SourceOptions, 'filters'> {
|
|
7
7
|
apiVersion?: ApiVersion;
|
|
8
|
+
geoColumn?: string;
|
|
8
9
|
filters?: Record<string, Filter>;
|
|
9
10
|
filtersLogicalOperator?: FilterLogicalOperator;
|
|
10
11
|
}
|
package/package.json
CHANGED
package/src/api/query.ts
CHANGED
|
@@ -12,7 +12,8 @@ import {buildQueryUrl} from './endpoints';
|
|
|
12
12
|
import {requestWithParameters} from './request-with-parameters';
|
|
13
13
|
import {APIErrorContext} from './carto-api-error';
|
|
14
14
|
|
|
15
|
-
export type QueryOptions = SourceOptions &
|
|
15
|
+
export type QueryOptions = SourceOptions &
|
|
16
|
+
Omit<QuerySourceOptions, 'spatialDataColumn'>;
|
|
16
17
|
type UrlParameters = {q: string; queryParameters?: string};
|
|
17
18
|
|
|
18
19
|
export const query = async function (
|
|
@@ -22,6 +23,7 @@ export const query = async function (
|
|
|
22
23
|
apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl,
|
|
23
24
|
clientId = SOURCE_DEFAULTS.clientId,
|
|
24
25
|
maxLengthURL = SOURCE_DEFAULTS.maxLengthURL,
|
|
26
|
+
localCache,
|
|
25
27
|
connectionName,
|
|
26
28
|
sqlQuery,
|
|
27
29
|
queryParameters,
|
|
@@ -51,5 +53,6 @@ export const query = async function (
|
|
|
51
53
|
headers,
|
|
52
54
|
errorContext,
|
|
53
55
|
maxLengthURL,
|
|
56
|
+
localCache,
|
|
54
57
|
});
|
|
55
58
|
};
|
|
@@ -7,13 +7,14 @@ import {CartoAPIError, APIErrorContext} from './carto-api-error';
|
|
|
7
7
|
import {V3_MINOR_VERSION} from '../constants-internal';
|
|
8
8
|
import {DEFAULT_MAX_LENGTH_URL} from '../constants-internal';
|
|
9
9
|
import {getClient} from '../client';
|
|
10
|
+
import {LocalCacheOptions} from '../sources/types';
|
|
10
11
|
|
|
11
12
|
const DEFAULT_HEADERS = {
|
|
12
13
|
Accept: 'application/json',
|
|
13
14
|
'Content-Type': 'application/json',
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
const
|
|
17
|
+
const DEFAULT_REQUEST_CACHE = new Map<string, Promise<unknown>>();
|
|
17
18
|
|
|
18
19
|
export async function requestWithParameters<T = any>({
|
|
19
20
|
baseUrl,
|
|
@@ -21,12 +22,14 @@ export async function requestWithParameters<T = any>({
|
|
|
21
22
|
headers: customHeaders = {},
|
|
22
23
|
errorContext,
|
|
23
24
|
maxLengthURL = DEFAULT_MAX_LENGTH_URL,
|
|
25
|
+
localCache,
|
|
24
26
|
}: {
|
|
25
27
|
baseUrl: string;
|
|
26
28
|
parameters?: Record<string, unknown>;
|
|
27
29
|
headers?: Record<string, string>;
|
|
28
30
|
errorContext: APIErrorContext;
|
|
29
31
|
maxLengthURL?: number;
|
|
32
|
+
localCache?: LocalCacheOptions;
|
|
30
33
|
}): Promise<T> {
|
|
31
34
|
// Parameters added to all requests issued with `requestWithParameters()`.
|
|
32
35
|
// These parameters override parameters already in the base URL, but not
|
|
@@ -41,7 +44,14 @@ export async function requestWithParameters<T = any>({
|
|
|
41
44
|
|
|
42
45
|
baseUrl = excludeURLParameters(baseUrl, Object.keys(parameters));
|
|
43
46
|
const key = createCacheKey(baseUrl, parameters, customHeaders);
|
|
44
|
-
|
|
47
|
+
|
|
48
|
+
const {
|
|
49
|
+
cache: REQUEST_CACHE,
|
|
50
|
+
canReadCache,
|
|
51
|
+
canStoreInCache,
|
|
52
|
+
} = getCacheSettings(localCache);
|
|
53
|
+
|
|
54
|
+
if (canReadCache && REQUEST_CACHE.has(key)) {
|
|
45
55
|
return REQUEST_CACHE.get(key) as Promise<T>;
|
|
46
56
|
}
|
|
47
57
|
|
|
@@ -73,14 +83,34 @@ export async function requestWithParameters<T = any>({
|
|
|
73
83
|
return json;
|
|
74
84
|
})
|
|
75
85
|
.catch((error: Error) => {
|
|
76
|
-
|
|
86
|
+
if (canStoreInCache) {
|
|
87
|
+
REQUEST_CACHE.delete(key);
|
|
88
|
+
}
|
|
77
89
|
throw new CartoAPIError(error, errorContext, response, responseJson);
|
|
78
90
|
});
|
|
79
91
|
|
|
80
|
-
|
|
92
|
+
if (canStoreInCache) {
|
|
93
|
+
REQUEST_CACHE.set(key, jsonPromise);
|
|
94
|
+
}
|
|
81
95
|
return jsonPromise;
|
|
82
96
|
}
|
|
83
97
|
|
|
98
|
+
function getCacheSettings(localCache: LocalCacheOptions | undefined) {
|
|
99
|
+
const canReadCache = localCache?.cacheControl?.includes('no-cache')
|
|
100
|
+
? false
|
|
101
|
+
: true;
|
|
102
|
+
const canStoreInCache = localCache?.cacheControl?.includes('no-store')
|
|
103
|
+
? false
|
|
104
|
+
: true;
|
|
105
|
+
const cache = localCache?.cache || DEFAULT_REQUEST_CACHE;
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
cache,
|
|
109
|
+
canReadCache,
|
|
110
|
+
canStoreInCache,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
84
114
|
function createCacheKey(
|
|
85
115
|
baseUrl: string,
|
|
86
116
|
parameters: Record<string, unknown>,
|
package/src/models/model.ts
CHANGED
|
@@ -7,10 +7,9 @@ import {
|
|
|
7
7
|
SpatialFilter,
|
|
8
8
|
} from '../types.js';
|
|
9
9
|
import {$TODO} from '../types-internal.js';
|
|
10
|
-
import {assert
|
|
10
|
+
import {assert} from '../utils.js';
|
|
11
11
|
import {ModelRequestOptions, makeCall} from './common.js';
|
|
12
12
|
import {ApiVersion} from '../constants.js';
|
|
13
|
-
import {SpatialDataType, SpatialFilterPolyfillMode} from '../sources/types.js';
|
|
14
13
|
|
|
15
14
|
/** @internalRemarks Source: @carto/react-api */
|
|
16
15
|
const AVAILABLE_MODELS = [
|
|
@@ -36,14 +35,9 @@ export interface ModelSource {
|
|
|
36
35
|
data: string;
|
|
37
36
|
filters?: Record<string, Filter>;
|
|
38
37
|
filtersLogicalOperator?: FilterLogicalOperator;
|
|
38
|
+
geoColumn?: string;
|
|
39
39
|
spatialFilter?: SpatialFilter;
|
|
40
40
|
queryParameters?: QueryParameters;
|
|
41
|
-
spatialDataColumn?: string;
|
|
42
|
-
spatialDataType?: SpatialDataType;
|
|
43
|
-
spatialFiltersResolution?: number;
|
|
44
|
-
spatialFiltersMode?: SpatialFilterPolyfillMode;
|
|
45
|
-
/** original resolution of the spatial index data as stored in the DW */
|
|
46
|
-
dataResolution?: number;
|
|
47
41
|
}
|
|
48
42
|
|
|
49
43
|
const {V3} = ApiVersion;
|
|
@@ -85,51 +79,50 @@ export function executeModel(props: {
|
|
|
85
79
|
data,
|
|
86
80
|
filters,
|
|
87
81
|
filtersLogicalOperator = 'and',
|
|
88
|
-
|
|
89
|
-
spatialFiltersMode = 'intersects',
|
|
90
|
-
spatialFiltersResolution = 0,
|
|
82
|
+
geoColumn = DEFAULT_GEO_COLUMN,
|
|
91
83
|
} = source;
|
|
92
84
|
|
|
93
|
-
const
|
|
85
|
+
const queryParameters = source.queryParameters
|
|
86
|
+
? JSON.stringify(source.queryParameters)
|
|
87
|
+
: '';
|
|
88
|
+
|
|
89
|
+
const queryParams: Record<string, string> = {
|
|
94
90
|
type,
|
|
95
91
|
client: clientId,
|
|
96
92
|
source: data,
|
|
97
|
-
params,
|
|
98
|
-
queryParameters
|
|
99
|
-
filters,
|
|
93
|
+
params: JSON.stringify(params),
|
|
94
|
+
queryParameters,
|
|
95
|
+
filters: JSON.stringify(filters),
|
|
100
96
|
filtersLogicalOperator,
|
|
101
97
|
};
|
|
102
98
|
|
|
103
|
-
const spatialDataColumn = source.spatialDataColumn || DEFAULT_GEO_COLUMN;
|
|
104
|
-
|
|
105
99
|
// Picking Model API requires 'spatialDataColumn'.
|
|
106
100
|
if (model === 'pick') {
|
|
107
|
-
queryParams.spatialDataColumn =
|
|
101
|
+
queryParams.spatialDataColumn = geoColumn;
|
|
108
102
|
}
|
|
109
103
|
|
|
110
|
-
// API supports multiple filters, we apply it only to
|
|
104
|
+
// API supports multiple filters, we apply it only to geoColumn
|
|
111
105
|
const spatialFilters = source.spatialFilter
|
|
112
|
-
? {[
|
|
106
|
+
? {[geoColumn]: source.spatialFilter}
|
|
113
107
|
: undefined;
|
|
114
108
|
|
|
115
109
|
if (spatialFilters) {
|
|
116
|
-
queryParams.spatialFilters =
|
|
117
|
-
queryParams.spatialDataColumn = spatialDataColumn;
|
|
118
|
-
queryParams.spatialDataType = spatialDataType;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (spatialDataType !== 'geo') {
|
|
122
|
-
if (spatialFiltersResolution > 0) {
|
|
123
|
-
queryParams.spatialFiltersResolution = spatialFiltersResolution;
|
|
124
|
-
}
|
|
125
|
-
queryParams.spatialFiltersMode = spatialFiltersMode;
|
|
110
|
+
queryParams.spatialFilters = JSON.stringify(spatialFilters);
|
|
126
111
|
}
|
|
127
112
|
|
|
128
113
|
const urlWithSearchParams =
|
|
129
|
-
url + '?' +
|
|
114
|
+
url + '?' + new URLSearchParams(queryParams).toString();
|
|
130
115
|
const isGet = urlWithSearchParams.length <= REQUEST_GET_MAX_URL_LENGTH;
|
|
131
116
|
if (isGet) {
|
|
132
117
|
url = urlWithSearchParams;
|
|
118
|
+
} else {
|
|
119
|
+
// undo the JSON.stringify, @TODO find a better pattern
|
|
120
|
+
queryParams.params = params as $TODO;
|
|
121
|
+
queryParams.filters = filters as $TODO;
|
|
122
|
+
queryParams.queryParameters = source.queryParameters as $TODO;
|
|
123
|
+
if (spatialFilters) {
|
|
124
|
+
queryParams.spatialFilters = spatialFilters as $TODO;
|
|
125
|
+
}
|
|
133
126
|
}
|
|
134
127
|
return makeCall({
|
|
135
128
|
url,
|
|
@@ -141,19 +134,3 @@ export function executeModel(props: {
|
|
|
141
134
|
},
|
|
142
135
|
});
|
|
143
136
|
}
|
|
144
|
-
|
|
145
|
-
function objectToURLSearchParams(object: Record<string, unknown>) {
|
|
146
|
-
const params = new URLSearchParams();
|
|
147
|
-
for (const key in object) {
|
|
148
|
-
if (isPureObject(object[key])) {
|
|
149
|
-
params.append(key, JSON.stringify(object[key]));
|
|
150
|
-
} else if (Array.isArray(object[key])) {
|
|
151
|
-
params.append(key, JSON.stringify(object[key]));
|
|
152
|
-
} else if (object[key] === null) {
|
|
153
|
-
params.append(key, 'null');
|
|
154
|
-
} else if (object[key] !== undefined) {
|
|
155
|
-
params.append(key, String(object[key]));
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return params;
|
|
159
|
-
}
|
|
@@ -45,7 +45,7 @@ export async function baseSource<UrlParameters extends Record<string, unknown>>(
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
const baseUrl = buildSourceUrl(mergedOptions);
|
|
48
|
-
const {clientId, maxLengthURL, format} = mergedOptions;
|
|
48
|
+
const {clientId, maxLengthURL, format, localCache} = mergedOptions;
|
|
49
49
|
const headers = {
|
|
50
50
|
Authorization: `Bearer ${options.accessToken}`,
|
|
51
51
|
...options.headers,
|
|
@@ -65,6 +65,7 @@ export async function baseSource<UrlParameters extends Record<string, unknown>>(
|
|
|
65
65
|
headers,
|
|
66
66
|
errorContext,
|
|
67
67
|
maxLengthURL,
|
|
68
|
+
localCache,
|
|
68
69
|
});
|
|
69
70
|
|
|
70
71
|
const dataUrl = mapInstantiation[format].url[0];
|
|
@@ -82,6 +83,7 @@ export async function baseSource<UrlParameters extends Record<string, unknown>>(
|
|
|
82
83
|
headers,
|
|
83
84
|
errorContext,
|
|
84
85
|
maxLengthURL,
|
|
86
|
+
localCache,
|
|
85
87
|
});
|
|
86
88
|
if (accessToken) {
|
|
87
89
|
json.accessToken = accessToken;
|
|
@@ -94,5 +96,6 @@ export async function baseSource<UrlParameters extends Record<string, unknown>>(
|
|
|
94
96
|
headers,
|
|
95
97
|
errorContext,
|
|
96
98
|
maxLengthURL,
|
|
99
|
+
localCache,
|
|
97
100
|
});
|
|
98
101
|
}
|
|
@@ -19,7 +19,6 @@ export type H3QuerySourceOptions = SourceOptions &
|
|
|
19
19
|
QuerySourceOptions &
|
|
20
20
|
AggregationOptions &
|
|
21
21
|
FilterOptions;
|
|
22
|
-
|
|
23
22
|
type UrlParameters = {
|
|
24
23
|
aggregationExp: string;
|
|
25
24
|
aggregationResLevel?: string;
|
|
@@ -60,12 +59,7 @@ export const h3QuerySource = async function (
|
|
|
60
59
|
return baseSource<UrlParameters>('query', options, urlParameters).then(
|
|
61
60
|
(result) => ({
|
|
62
61
|
...(result as TilejsonResult),
|
|
63
|
-
widgetSource: new WidgetQuerySource(
|
|
64
|
-
...options,
|
|
65
|
-
// NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
|
|
66
|
-
spatialDataColumn,
|
|
67
|
-
spatialDataType: 'h3',
|
|
68
|
-
}),
|
|
62
|
+
widgetSource: new WidgetQuerySource(options),
|
|
69
63
|
})
|
|
70
64
|
);
|
|
71
65
|
};
|
|
@@ -55,12 +55,7 @@ export const h3TableSource = async function (
|
|
|
55
55
|
return baseSource<UrlParameters>('table', options, urlParameters).then(
|
|
56
56
|
(result) => ({
|
|
57
57
|
...(result as TilejsonResult),
|
|
58
|
-
widgetSource: new WidgetTableSource(
|
|
59
|
-
...options,
|
|
60
|
-
// NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
|
|
61
|
-
spatialDataColumn,
|
|
62
|
-
spatialDataType: 'h3',
|
|
63
|
-
}),
|
|
58
|
+
widgetSource: new WidgetTableSource(options),
|
|
64
59
|
})
|
|
65
60
|
);
|
|
66
61
|
};
|
package/src/sources/index.ts
CHANGED
|
@@ -60,12 +60,7 @@ export const quadbinQuerySource = async function (
|
|
|
60
60
|
return baseSource<UrlParameters>('query', options, urlParameters).then(
|
|
61
61
|
(result) => ({
|
|
62
62
|
...(result as TilejsonResult),
|
|
63
|
-
widgetSource: new WidgetQuerySource(
|
|
64
|
-
...options,
|
|
65
|
-
// NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
|
|
66
|
-
spatialDataColumn,
|
|
67
|
-
spatialDataType: 'quadbin',
|
|
68
|
-
}),
|
|
63
|
+
widgetSource: new WidgetQuerySource(options),
|
|
69
64
|
})
|
|
70
65
|
);
|
|
71
66
|
};
|