@deck.gl/carto 9.0.35 → 9.1.0-beta.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.
Files changed (242) hide show
  1. package/dist/api/basemap.d.ts +2 -1
  2. package/dist/api/basemap.d.ts.map +1 -1
  3. package/dist/api/basemap.js +3 -0
  4. package/dist/api/fetch-map.d.ts.map +1 -1
  5. package/dist/api/fetch-map.js +9 -11
  6. package/dist/api/index.d.ts +1 -4
  7. package/dist/api/index.d.ts.map +1 -1
  8. package/dist/api/index.js +3 -2
  9. package/dist/api/layer-map.d.ts.map +1 -1
  10. package/dist/api/layer-map.js +5 -2
  11. package/dist/api/parse-map.d.ts.map +1 -1
  12. package/dist/api/parse-map.js +3 -0
  13. package/dist/api/types.d.ts +0 -56
  14. package/dist/api/types.d.ts.map +1 -1
  15. package/dist/api/types.js +4 -20
  16. package/dist/basemap.d.ts +2 -1
  17. package/dist/basemap.d.ts.map +1 -1
  18. package/dist/basemap.js +4 -1
  19. package/dist/cartoRasterTile-worker.js +25 -2
  20. package/dist/cartoSpatialTile-worker.js +28 -3
  21. package/dist/cartoVectorTile-worker.js +29 -3
  22. package/dist/constants.d.ts +0 -3
  23. package/dist/constants.d.ts.map +1 -1
  24. package/dist/constants.js +3 -3
  25. package/dist/dist.dev.js +4391 -3737
  26. package/dist/index.cjs +224 -531
  27. package/dist/index.cjs.map +4 -4
  28. package/dist/index.d.ts +16 -16
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +5 -2
  31. package/dist/layers/cluster-tile-layer.d.ts +1 -1
  32. package/dist/layers/cluster-tile-layer.d.ts.map +1 -1
  33. package/dist/layers/cluster-tile-layer.js +4 -0
  34. package/dist/layers/cluster-utils.d.ts.map +1 -1
  35. package/dist/layers/cluster-utils.js +3 -0
  36. package/dist/layers/h3-tile-layer.d.ts +1 -1
  37. package/dist/layers/h3-tile-layer.d.ts.map +1 -1
  38. package/dist/layers/h3-tile-layer.js +3 -0
  39. package/dist/layers/h3-tileset-2d.d.ts.map +1 -1
  40. package/dist/layers/h3-tileset-2d.js +3 -0
  41. package/dist/layers/heatmap-tile-layer.d.ts +1 -1
  42. package/dist/layers/heatmap-tile-layer.d.ts.map +1 -1
  43. package/dist/layers/heatmap-tile-layer.js +23 -7
  44. package/dist/layers/heatmap.d.ts +3 -3
  45. package/dist/layers/heatmap.d.ts.map +1 -1
  46. package/dist/layers/heatmap.js +69 -36
  47. package/dist/layers/point-label-layer.d.ts.map +1 -1
  48. package/dist/layers/point-label-layer.js +5 -2
  49. package/dist/layers/post-process-utils.d.ts.map +1 -1
  50. package/dist/layers/post-process-utils.js +9 -5
  51. package/dist/layers/quadbin-layer.d.ts.map +1 -1
  52. package/dist/layers/quadbin-layer.js +3 -0
  53. package/dist/layers/quadbin-tile-layer.d.ts +1 -1
  54. package/dist/layers/quadbin-tile-layer.d.ts.map +1 -1
  55. package/dist/layers/quadbin-tile-layer.js +3 -0
  56. package/dist/layers/quadbin-tileset-2d.d.ts.map +1 -1
  57. package/dist/layers/quadbin-tileset-2d.js +3 -0
  58. package/dist/layers/quadbin-utils.d.ts.map +1 -1
  59. package/dist/layers/quadbin-utils.js +3 -0
  60. package/dist/layers/raster-layer-vertex.glsl.d.ts +1 -1
  61. package/dist/layers/raster-layer-vertex.glsl.d.ts.map +1 -1
  62. package/dist/layers/raster-layer-vertex.glsl.js +19 -24
  63. package/dist/layers/raster-layer.d.ts +6 -6
  64. package/dist/layers/raster-layer.d.ts.map +1 -1
  65. package/dist/layers/raster-layer.js +6 -1
  66. package/dist/layers/raster-tile-layer.d.ts +1 -1
  67. package/dist/layers/raster-tile-layer.d.ts.map +1 -1
  68. package/dist/layers/raster-tile-layer.js +3 -0
  69. package/dist/layers/schema/carto-properties-tile-loader.d.ts.map +1 -1
  70. package/dist/layers/schema/carto-properties-tile-loader.js +3 -0
  71. package/dist/layers/schema/carto-properties-tile.d.ts.map +1 -1
  72. package/dist/layers/schema/carto-properties-tile.js +3 -0
  73. package/dist/layers/schema/carto-raster-tile-loader.d.ts.map +1 -1
  74. package/dist/layers/schema/carto-raster-tile-loader.js +4 -1
  75. package/dist/layers/schema/carto-raster-tile.d.ts.map +1 -1
  76. package/dist/layers/schema/carto-raster-tile.js +3 -0
  77. package/dist/layers/schema/carto-spatial-tile-loader.d.ts.map +1 -1
  78. package/dist/layers/schema/carto-spatial-tile-loader.js +4 -1
  79. package/dist/layers/schema/carto-spatial-tile.d.ts.map +1 -1
  80. package/dist/layers/schema/carto-spatial-tile.js +3 -0
  81. package/dist/layers/schema/carto-tile.d.ts.map +1 -1
  82. package/dist/layers/schema/carto-tile.js +3 -0
  83. package/dist/layers/schema/carto-vector-tile-loader.d.ts.map +1 -1
  84. package/dist/layers/schema/carto-vector-tile-loader.js +4 -1
  85. package/dist/layers/schema/fast-pbf.d.ts.map +1 -1
  86. package/dist/layers/schema/fast-pbf.js +3 -0
  87. package/dist/layers/schema/spatialjson-utils.d.ts.map +1 -1
  88. package/dist/layers/schema/spatialjson-utils.js +3 -0
  89. package/dist/layers/schema/tile-loader-utils.d.ts.map +1 -1
  90. package/dist/layers/schema/tile-loader-utils.js +3 -0
  91. package/dist/layers/spatial-index-tile-layer.d.ts.map +1 -1
  92. package/dist/layers/spatial-index-tile-layer.js +3 -0
  93. package/dist/layers/utils.d.ts +2 -2
  94. package/dist/layers/utils.d.ts.map +1 -1
  95. package/dist/layers/utils.js +3 -0
  96. package/dist/layers/vector-tile-layer.d.ts +1 -1
  97. package/dist/layers/vector-tile-layer.d.ts.map +1 -1
  98. package/dist/layers/vector-tile-layer.js +3 -0
  99. package/dist/style/color-bins-style.d.ts.map +1 -1
  100. package/dist/style/color-bins-style.js +3 -0
  101. package/dist/style/color-categories-style.d.ts.map +1 -1
  102. package/dist/style/color-categories-style.js +3 -0
  103. package/dist/style/color-continuous-style.d.ts.map +1 -1
  104. package/dist/style/color-continuous-style.js +3 -0
  105. package/dist/style/palette.d.ts.map +1 -1
  106. package/dist/style/palette.js +3 -0
  107. package/dist/style/utils.d.ts.map +1 -1
  108. package/dist/style/utils.js +3 -0
  109. package/dist/utils.d.ts.map +1 -1
  110. package/dist/utils.js +3 -0
  111. package/dist/workers/cartoRasterTile.js +3 -0
  112. package/dist/workers/cartoSpatialTile.js +3 -0
  113. package/dist/workers/cartoVectorTile.js +3 -0
  114. package/dist.min.js +113 -73
  115. package/package.json +13 -11
  116. package/src/api/basemap.ts +6 -1
  117. package/src/api/fetch-map.ts +23 -15
  118. package/src/api/index.ts +5 -13
  119. package/src/api/layer-map.ts +6 -2
  120. package/src/api/parse-map.ts +4 -0
  121. package/src/api/types.ts +4 -75
  122. package/src/basemap.ts +6 -2
  123. package/src/constants.ts +4 -4
  124. package/src/index.ts +7 -9
  125. package/src/layers/cluster-tile-layer.ts +6 -1
  126. package/src/layers/cluster-utils.ts +4 -0
  127. package/src/layers/h3-tile-layer.ts +5 -1
  128. package/src/layers/h3-tileset-2d.ts +4 -0
  129. package/src/layers/heatmap-tile-layer.ts +29 -8
  130. package/src/layers/heatmap.ts +8 -2
  131. package/src/layers/point-label-layer.ts +6 -2
  132. package/src/layers/post-process-utils.ts +10 -5
  133. package/src/layers/quadbin-layer.ts +4 -0
  134. package/src/layers/quadbin-tile-layer.ts +5 -1
  135. package/src/layers/quadbin-tileset-2d.ts +4 -0
  136. package/src/layers/quadbin-utils.ts +4 -0
  137. package/src/layers/raster-layer-vertex.glsl.ts +20 -26
  138. package/src/layers/raster-layer.ts +7 -1
  139. package/src/layers/raster-tile-layer.ts +5 -1
  140. package/src/layers/schema/carto-properties-tile-loader.ts +4 -0
  141. package/src/layers/schema/carto-properties-tile.ts +4 -0
  142. package/src/layers/schema/carto-raster-tile-loader.ts +4 -0
  143. package/src/layers/schema/carto-raster-tile.ts +4 -0
  144. package/src/layers/schema/carto-spatial-tile-loader.ts +4 -0
  145. package/src/layers/schema/carto-spatial-tile.ts +4 -0
  146. package/src/layers/schema/carto-tile.ts +4 -0
  147. package/src/layers/schema/carto-vector-tile-loader.ts +4 -0
  148. package/src/layers/schema/fast-pbf.ts +4 -0
  149. package/src/layers/schema/spatialjson-utils.ts +4 -0
  150. package/src/layers/schema/tile-loader-utils.ts +4 -0
  151. package/src/layers/spatial-index-tile-layer.ts +4 -0
  152. package/src/layers/utils.ts +5 -1
  153. package/src/layers/vector-tile-layer.ts +5 -1
  154. package/src/style/color-bins-style.ts +4 -0
  155. package/src/style/color-categories-style.ts +4 -0
  156. package/src/style/color-continuous-style.ts +4 -0
  157. package/src/style/palette.ts +4 -0
  158. package/src/style/utils.ts +4 -0
  159. package/src/utils.ts +4 -0
  160. package/src/workers/cartoRasterTile.ts +4 -0
  161. package/src/workers/cartoSpatialTile.ts +4 -0
  162. package/src/workers/cartoVectorTile.ts +4 -0
  163. package/dist/api/carto-api-error.d.ts +0 -19
  164. package/dist/api/carto-api-error.d.ts.map +0 -1
  165. package/dist/api/carto-api-error.js +0 -48
  166. package/dist/api/common.d.ts +0 -5
  167. package/dist/api/common.d.ts.map +0 -1
  168. package/dist/api/common.js +0 -5
  169. package/dist/api/endpoints.d.ts +0 -23
  170. package/dist/api/endpoints.d.ts.map +0 -1
  171. package/dist/api/endpoints.js +0 -22
  172. package/dist/api/query.d.ts +0 -4
  173. package/dist/api/query.d.ts.map +0 -1
  174. package/dist/api/query.js +0 -26
  175. package/dist/api/request-with-parameters.d.ts +0 -9
  176. package/dist/api/request-with-parameters.d.ts.map +0 -1
  177. package/dist/api/request-with-parameters.js +0 -86
  178. package/dist/sources/base-source.d.ts +0 -5
  179. package/dist/sources/base-source.d.ts.map +0 -1
  180. package/dist/sources/base-source.js +0 -60
  181. package/dist/sources/boundary-query-source.d.ts +0 -10
  182. package/dist/sources/boundary-query-source.d.ts.map +0 -1
  183. package/dist/sources/boundary-query-source.js +0 -18
  184. package/dist/sources/boundary-table-source.d.ts +0 -8
  185. package/dist/sources/boundary-table-source.d.ts.map +0 -1
  186. package/dist/sources/boundary-table-source.js +0 -15
  187. package/dist/sources/h3-query-source.d.ts +0 -4
  188. package/dist/sources/h3-query-source.d.ts.map +0 -1
  189. package/dist/sources/h3-query-source.js +0 -22
  190. package/dist/sources/h3-table-source.d.ts +0 -4
  191. package/dist/sources/h3-table-source.d.ts.map +0 -1
  192. package/dist/sources/h3-table-source.js +0 -19
  193. package/dist/sources/h3-tileset-source.d.ts +0 -4
  194. package/dist/sources/h3-tileset-source.d.ts.map +0 -1
  195. package/dist/sources/h3-tileset-source.js +0 -6
  196. package/dist/sources/index.d.ts +0 -28
  197. package/dist/sources/index.d.ts.map +0 -1
  198. package/dist/sources/index.js +0 -13
  199. package/dist/sources/quadbin-query-source.d.ts +0 -4
  200. package/dist/sources/quadbin-query-source.d.ts.map +0 -1
  201. package/dist/sources/quadbin-query-source.js +0 -22
  202. package/dist/sources/quadbin-table-source.d.ts +0 -4
  203. package/dist/sources/quadbin-table-source.d.ts.map +0 -1
  204. package/dist/sources/quadbin-table-source.js +0 -19
  205. package/dist/sources/quadbin-tileset-source.d.ts +0 -4
  206. package/dist/sources/quadbin-tileset-source.d.ts.map +0 -1
  207. package/dist/sources/quadbin-tileset-source.js +0 -6
  208. package/dist/sources/raster-source.d.ts +0 -4
  209. package/dist/sources/raster-source.d.ts.map +0 -1
  210. package/dist/sources/raster-source.js +0 -9
  211. package/dist/sources/types.d.ts +0 -219
  212. package/dist/sources/types.d.ts.map +0 -1
  213. package/dist/sources/types.js +0 -1
  214. package/dist/sources/vector-query-source.d.ts +0 -4
  215. package/dist/sources/vector-query-source.d.ts.map +0 -1
  216. package/dist/sources/vector-query-source.js +0 -22
  217. package/dist/sources/vector-table-source.d.ts +0 -4
  218. package/dist/sources/vector-table-source.d.ts.map +0 -1
  219. package/dist/sources/vector-table-source.js +0 -19
  220. package/dist/sources/vector-tileset-source.d.ts +0 -4
  221. package/dist/sources/vector-tileset-source.d.ts.map +0 -1
  222. package/dist/sources/vector-tileset-source.js +0 -6
  223. package/src/api/carto-api-error.ts +0 -68
  224. package/src/api/common.ts +0 -6
  225. package/src/api/endpoints.ts +0 -69
  226. package/src/api/query.ts +0 -42
  227. package/src/api/request-with-parameters.ts +0 -117
  228. package/src/sources/base-source.ts +0 -79
  229. package/src/sources/boundary-query-source.ts +0 -39
  230. package/src/sources/boundary-table-source.ts +0 -33
  231. package/src/sources/h3-query-source.ts +0 -55
  232. package/src/sources/h3-table-source.ts +0 -51
  233. package/src/sources/h3-tileset-source.ts +0 -14
  234. package/src/sources/index.ts +0 -45
  235. package/src/sources/quadbin-query-source.ts +0 -56
  236. package/src/sources/quadbin-table-source.ts +0 -52
  237. package/src/sources/quadbin-tileset-source.ts +0 -14
  238. package/src/sources/raster-source.ts +0 -17
  239. package/src/sources/types.ts +0 -231
  240. package/src/sources/vector-query-source.ts +0 -57
  241. package/src/sources/vector-table-source.ts +0 -51
  242. package/src/sources/vector-tileset-source.ts +0 -14
@@ -1,19 +0,0 @@
1
- /* eslint-disable camelcase */
2
- import { DEFAULT_TILE_RESOLUTION } from "../constants.js";
3
- import { baseSource } from "./base-source.js";
4
- export const vectorTableSource = async function (options) {
5
- const { columns, filters, spatialDataColumn = 'geom', tableName, tileResolution = DEFAULT_TILE_RESOLUTION } = options;
6
- const urlParameters = {
7
- name: tableName,
8
- spatialDataColumn,
9
- spatialDataType: 'geo',
10
- tileResolution: tileResolution.toString()
11
- };
12
- if (columns) {
13
- urlParameters.columns = columns.join(',');
14
- }
15
- if (filters) {
16
- urlParameters.filters = filters;
17
- }
18
- return baseSource('table', options, urlParameters);
19
- };
@@ -1,4 +0,0 @@
1
- import type { SourceOptions, TilesetSourceOptions, TilejsonResult } from "./types.js";
2
- export type VectorTilesetSourceOptions = SourceOptions & TilesetSourceOptions;
3
- export declare const vectorTilesetSource: (options: VectorTilesetSourceOptions) => Promise<TilejsonResult>;
4
- //# sourceMappingURL=vector-tileset-source.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vector-tileset-source.d.ts","sourceRoot":"","sources":["../../src/sources/vector-tileset-source.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,aAAa,EAAE,oBAAoB,EAAE,cAAc,EAAC,mBAAgB;AAEjF,MAAM,MAAM,0BAA0B,GAAG,aAAa,GAAG,oBAAoB,CAAC;AAG9E,eAAO,MAAM,mBAAmB,YACrB,0BAA0B,KAClC,QAAQ,cAAc,CAKxB,CAAC"}
@@ -1,6 +0,0 @@
1
- import { baseSource } from "./base-source.js";
2
- export const vectorTilesetSource = async function (options) {
3
- const { tableName } = options;
4
- const urlParameters = { name: tableName };
5
- return baseSource('tileset', options, urlParameters);
6
- };
@@ -1,68 +0,0 @@
1
- import type {APIErrorContext} from './types';
2
-
3
- /**
4
- *
5
- * Custom error for reported errors in CARTO Maps API.
6
- * Provides useful debugging information in console and context for applications.
7
- *
8
- */
9
- export class CartoAPIError extends Error {
10
- /** Source error from server */
11
- error: Error;
12
-
13
- /** Context (API call & parameters) in which error occured */
14
- errorContext: APIErrorContext;
15
-
16
- /** Response from server */
17
- response?: Response;
18
-
19
- /** JSON Response from server */
20
- responseJson?: any;
21
-
22
- constructor(
23
- error: Error,
24
- errorContext: APIErrorContext,
25
- response?: Response,
26
- responseJson?: any
27
- ) {
28
- let responseString = 'Failed to connect';
29
- if (response) {
30
- responseString = 'Server returned: ';
31
- if (response.status === 400) {
32
- responseString += 'Bad request';
33
- } else if (response.status === 401 || response.status === 403) {
34
- responseString += 'Unauthorized access';
35
- } else if (response.status === 404) {
36
- responseString += 'Not found';
37
- } else {
38
- responseString += 'Error';
39
- }
40
-
41
- responseString += ` (${response.status}):`;
42
- }
43
- responseString += ` ${error.message || error}`;
44
-
45
- let message = `${errorContext.requestType} API request failed`;
46
- message += `\n${responseString}`;
47
- for (const key of Object.keys(errorContext)) {
48
- if (key === 'requestType') continue; // eslint-disable-line no-continue
49
- message += `\n${formatErrorKey(key)}: ${errorContext[key]}`;
50
- }
51
- message += '\n';
52
-
53
- super(message);
54
-
55
- this.name = 'CartoAPIError';
56
- this.response = response;
57
- this.responseJson = responseJson;
58
- this.error = error;
59
- this.errorContext = errorContext;
60
- }
61
- }
62
-
63
- /**
64
- * Converts camelCase to Camel Case
65
- */
66
- function formatErrorKey(key) {
67
- return key.replace(/([A-Z])/g, ' $1').replace(/^./, s => s.toUpperCase());
68
- }
package/src/api/common.ts DELETED
@@ -1,6 +0,0 @@
1
- export const DEFAULT_API_BASE_URL = 'https://gcp-us-east1.api.carto.com';
2
- export const DEFAULT_CLIENT = 'deck-gl-carto';
3
- export const V3_MINOR_VERSION = '3.4';
4
-
5
- // Fastly default limit is 8192; leave some padding.
6
- export const DEFAULT_MAX_LENGTH_URL = 7000;
@@ -1,69 +0,0 @@
1
- import {MapType} from './types';
2
-
3
- export type V3Endpoint = 'maps' | 'stats' | 'sql';
4
-
5
- function joinPath(...args: string[]): string {
6
- return args.map(part => (part.endsWith('/') ? part.slice(0, -1) : part)).join('/');
7
- }
8
-
9
- function buildV3Path(
10
- apiBaseUrl: string,
11
- version: 'v3',
12
- endpoint: V3Endpoint,
13
- ...rest: string[]
14
- ): string {
15
- return joinPath(apiBaseUrl, version, endpoint, ...rest);
16
- }
17
-
18
- export function buildPublicMapUrl({
19
- apiBaseUrl,
20
- cartoMapId
21
- }: {
22
- apiBaseUrl: string;
23
- cartoMapId: string;
24
- }): string {
25
- return buildV3Path(apiBaseUrl, 'v3', 'maps', 'public', cartoMapId);
26
- }
27
-
28
- export function buildStatsUrl({
29
- attribute,
30
- apiBaseUrl,
31
- connectionName,
32
- source,
33
- type
34
- }: {
35
- attribute: string;
36
- apiBaseUrl: string;
37
- connectionName: string;
38
- source: string;
39
- type: MapType;
40
- }): string {
41
- if (type === 'query') {
42
- return buildV3Path(apiBaseUrl, 'v3', 'stats', connectionName, attribute);
43
- }
44
-
45
- // type === 'table'
46
- return buildV3Path(apiBaseUrl, 'v3', 'stats', connectionName, source, attribute);
47
- }
48
-
49
- export function buildSourceUrl({
50
- apiBaseUrl,
51
- connectionName,
52
- endpoint
53
- }: {
54
- apiBaseUrl: string;
55
- connectionName: string;
56
- endpoint: MapType;
57
- }): string {
58
- return buildV3Path(apiBaseUrl, 'v3', 'maps', connectionName, endpoint);
59
- }
60
-
61
- export function buildQueryUrl({
62
- apiBaseUrl,
63
- connectionName
64
- }: {
65
- apiBaseUrl: string;
66
- connectionName: string;
67
- }): string {
68
- return buildV3Path(apiBaseUrl, 'v3', 'sql', connectionName, 'query');
69
- }
package/src/api/query.ts DELETED
@@ -1,42 +0,0 @@
1
- import {SOURCE_DEFAULTS} from '../sources/index';
2
- import type {SourceOptions, QuerySourceOptions, QueryResult} from '../sources/types';
3
- import {buildQueryUrl} from './endpoints';
4
- import {requestWithParameters} from './request-with-parameters';
5
- import {APIErrorContext} from './types';
6
-
7
- export type QueryOptions = SourceOptions & Omit<QuerySourceOptions, 'spatialDataColumn'>;
8
- type UrlParameters = {q: string; queryParameters?: string};
9
-
10
- export const query = async function (options: QueryOptions): Promise<QueryResult> {
11
- const {
12
- apiBaseUrl = SOURCE_DEFAULTS.apiBaseUrl,
13
- clientId = SOURCE_DEFAULTS.clientId,
14
- maxLengthURL = SOURCE_DEFAULTS.maxLengthURL,
15
- connectionName,
16
- sqlQuery,
17
- queryParameters
18
- } = options;
19
- const urlParameters: UrlParameters = {q: sqlQuery};
20
-
21
- if (queryParameters) {
22
- urlParameters.queryParameters = JSON.stringify(queryParameters);
23
- }
24
-
25
- const baseUrl = buildQueryUrl({apiBaseUrl, connectionName});
26
- const headers = {Authorization: `Bearer ${options.accessToken}`, ...options.headers};
27
- const parameters = {client: clientId, ...urlParameters};
28
-
29
- const errorContext: APIErrorContext = {
30
- requestType: 'SQL',
31
- connection: options.connectionName,
32
- type: 'query',
33
- source: JSON.stringify(parameters, undefined, 2)
34
- };
35
- return await requestWithParameters<QueryResult>({
36
- baseUrl,
37
- parameters,
38
- headers,
39
- errorContext,
40
- maxLengthURL
41
- });
42
- };
@@ -1,117 +0,0 @@
1
- import {VERSION} from '@deck.gl/core';
2
- import {isPureObject} from '../utils';
3
- import {CartoAPIError} from './carto-api-error';
4
- import {DEFAULT_MAX_LENGTH_URL, V3_MINOR_VERSION} from './common';
5
- import type {APIErrorContext} from './types';
6
-
7
- /**
8
- * Parameters added to all requests issued with `requestWithParameters()`.
9
- * These parameters override parameters already in the base URL, but not
10
- * user-provided parameters.
11
- */
12
- const DEFAULT_PARAMETERS = {
13
- v: V3_MINOR_VERSION,
14
- deckglVersion: VERSION
15
- };
16
-
17
- const DEFAULT_HEADERS = {
18
- Accept: 'application/json',
19
- 'Content-Type': 'application/json'
20
- };
21
-
22
- const REQUEST_CACHE = new Map<string, Promise<unknown>>();
23
-
24
- export async function requestWithParameters<T = any>({
25
- baseUrl,
26
- parameters = {},
27
- headers: customHeaders = {},
28
- errorContext,
29
- maxLengthURL = DEFAULT_MAX_LENGTH_URL
30
- }: {
31
- baseUrl: string;
32
- parameters?: Record<string, unknown>;
33
- headers?: Record<string, string>;
34
- errorContext: APIErrorContext;
35
- maxLengthURL?: number;
36
- }): Promise<T> {
37
- parameters = {...DEFAULT_PARAMETERS, ...parameters};
38
- baseUrl = excludeURLParameters(baseUrl, Object.keys(parameters));
39
- const key = createCacheKey(baseUrl, parameters, customHeaders);
40
- if (REQUEST_CACHE.has(key)) {
41
- return REQUEST_CACHE.get(key) as Promise<T>;
42
- }
43
-
44
- const url = createURLWithParameters(baseUrl, parameters);
45
- const headers = {...DEFAULT_HEADERS, ...customHeaders};
46
-
47
- /* global fetch */
48
- const fetchPromise =
49
- url.length > maxLengthURL
50
- ? fetch(baseUrl, {method: 'POST', body: JSON.stringify(parameters), headers})
51
- : fetch(url, {headers});
52
-
53
- let response: Response | undefined;
54
- let responseJson;
55
- const jsonPromise: Promise<T> = fetchPromise
56
- .then((_response: Response) => {
57
- response = _response;
58
- return response.json();
59
- })
60
- .then((json: any) => {
61
- responseJson = json;
62
- if (!response || !response.ok) {
63
- throw new Error(json.error);
64
- }
65
- return json;
66
- })
67
- .catch((error: Error) => {
68
- REQUEST_CACHE.delete(key);
69
- throw new CartoAPIError(error, errorContext, response, responseJson);
70
- });
71
-
72
- REQUEST_CACHE.set(key, jsonPromise);
73
- return jsonPromise;
74
- }
75
-
76
- function createCacheKey(
77
- baseUrl: string,
78
- parameters: Record<string, unknown>,
79
- headers: Record<string, string>
80
- ): string {
81
- const parameterEntries = Object.entries(parameters).sort(([a], [b]) => (a > b ? 1 : -1));
82
- const headerEntries = Object.entries(headers).sort(([a], [b]) => (a > b ? 1 : -1));
83
- return JSON.stringify({baseUrl, parameters: parameterEntries, headers: headerEntries});
84
- }
85
-
86
- /**
87
- * Appends query string parameters to a URL. Existing URL parameters are kept,
88
- * unless there is a conflict, in which case the new parameters override
89
- * those already in the URL.
90
- */
91
- function createURLWithParameters(
92
- baseUrlString: string,
93
- parameters: Record<string, unknown>
94
- ): string {
95
- const baseUrl = new URL(baseUrlString);
96
- for (const [key, value] of Object.entries(parameters)) {
97
- if (isPureObject(value) || Array.isArray(value)) {
98
- baseUrl.searchParams.set(key, JSON.stringify(value));
99
- } else {
100
- baseUrl.searchParams.set(key, (value as string | boolean | number).toString());
101
- }
102
- }
103
- return baseUrl.toString();
104
- }
105
-
106
- /**
107
- * Deletes query string parameters from a URL.
108
- */
109
- function excludeURLParameters(baseUrlString: string, parameters: string[]) {
110
- const baseUrl = new URL(baseUrlString);
111
- for (const param of parameters) {
112
- if (baseUrl.searchParams.has(param)) {
113
- baseUrl.searchParams.delete(param);
114
- }
115
- }
116
- return baseUrl.toString();
117
- }
@@ -1,79 +0,0 @@
1
- /* eslint-disable camelcase */
2
- import {DEFAULT_API_BASE_URL, DEFAULT_CLIENT, DEFAULT_MAX_LENGTH_URL} from '../api/common';
3
- import {buildSourceUrl} from '../api/endpoints';
4
- import {requestWithParameters} from '../api/request-with-parameters';
5
- import type {APIErrorContext, MapType} from '../api/types';
6
- import type {
7
- GeojsonResult,
8
- JsonResult,
9
- SourceOptionalOptions,
10
- SourceRequiredOptions,
11
- TilejsonMapInstantiation,
12
- TilejsonResult
13
- } from './types';
14
-
15
- export const SOURCE_DEFAULTS: SourceOptionalOptions = {
16
- apiBaseUrl: DEFAULT_API_BASE_URL,
17
- clientId: DEFAULT_CLIENT,
18
- format: 'tilejson',
19
- headers: {},
20
- maxLengthURL: DEFAULT_MAX_LENGTH_URL
21
- };
22
-
23
- export async function baseSource<UrlParameters extends Record<string, unknown>>(
24
- endpoint: MapType,
25
- options: Partial<SourceOptionalOptions> & SourceRequiredOptions,
26
- urlParameters: UrlParameters
27
- ): Promise<TilejsonResult | GeojsonResult | JsonResult> {
28
- const {accessToken, connectionName, cache, ...optionalOptions} = options;
29
- const mergedOptions = {...SOURCE_DEFAULTS, accessToken, connectionName, endpoint};
30
- for (const key in optionalOptions) {
31
- if (optionalOptions[key]) {
32
- mergedOptions[key] = optionalOptions[key];
33
- }
34
- }
35
- const baseUrl = buildSourceUrl(mergedOptions);
36
- const {clientId, maxLengthURL, format} = mergedOptions;
37
- const headers = {Authorization: `Bearer ${options.accessToken}`, ...options.headers};
38
- const parameters = {client: clientId, ...urlParameters};
39
-
40
- const errorContext: APIErrorContext = {
41
- requestType: 'Map instantiation',
42
- connection: options.connectionName,
43
- type: endpoint,
44
- source: JSON.stringify(parameters, undefined, 2)
45
- };
46
- const mapInstantiation = await requestWithParameters<TilejsonMapInstantiation>({
47
- baseUrl,
48
- parameters,
49
- headers,
50
- errorContext,
51
- maxLengthURL
52
- });
53
-
54
- const dataUrl = mapInstantiation[format].url[0];
55
- if (cache) {
56
- cache.value = parseInt(new URL(dataUrl).searchParams.get('cache') || '', 10);
57
- }
58
- errorContext.requestType = 'Map data';
59
-
60
- if (format === 'tilejson') {
61
- const json = await requestWithParameters<TilejsonResult>({
62
- baseUrl: dataUrl,
63
- headers,
64
- errorContext,
65
- maxLengthURL
66
- });
67
- if (accessToken) {
68
- json.accessToken = accessToken;
69
- }
70
- return json;
71
- }
72
-
73
- return await requestWithParameters<GeojsonResult | JsonResult>({
74
- baseUrl: dataUrl,
75
- headers,
76
- errorContext,
77
- maxLengthURL
78
- });
79
- }
@@ -1,39 +0,0 @@
1
- import {QueryParameters} from '../api/index';
2
- import {baseSource} from './base-source';
3
- import type {FilterOptions, SourceOptions, TilejsonResult} from './types';
4
-
5
- export type BoundaryQuerySourceOptions = SourceOptions &
6
- FilterOptions & {
7
- columns?: string[];
8
- tilesetTableName: string;
9
- propertiesSqlQuery: string;
10
- queryParameters?: QueryParameters;
11
- };
12
- type UrlParameters = {
13
- columns?: string;
14
- filters?: Record<string, unknown>;
15
- tilesetTableName: string;
16
- propertiesSqlQuery: string;
17
- queryParameters?: Record<string, unknown> | unknown[];
18
- };
19
-
20
- export const boundaryQuerySource = async function (
21
- options: BoundaryQuerySourceOptions
22
- ): Promise<TilejsonResult> {
23
- const {columns, filters, tilesetTableName, propertiesSqlQuery, queryParameters} = options;
24
- const urlParameters: UrlParameters = {
25
- tilesetTableName,
26
- propertiesSqlQuery
27
- };
28
-
29
- if (columns) {
30
- urlParameters.columns = columns.join(',');
31
- }
32
- if (filters) {
33
- urlParameters.filters = filters;
34
- }
35
- if (queryParameters) {
36
- urlParameters.queryParameters = queryParameters;
37
- }
38
- return baseSource<UrlParameters>('boundary', options, urlParameters) as Promise<TilejsonResult>;
39
- };
@@ -1,33 +0,0 @@
1
- import {baseSource} from './base-source';
2
- import type {FilterOptions, SourceOptions, TilejsonResult} from './types';
3
-
4
- export type BoundaryTableSourceOptions = SourceOptions &
5
- FilterOptions & {
6
- tilesetTableName: string;
7
- columns?: string[];
8
- propertiesTableName: string;
9
- };
10
- type UrlParameters = {
11
- filters?: Record<string, unknown>;
12
- tilesetTableName: string;
13
- columns?: string;
14
- propertiesTableName: string;
15
- };
16
-
17
- export const boundaryTableSource = async function (
18
- options: BoundaryTableSourceOptions
19
- ): Promise<TilejsonResult> {
20
- const {filters, tilesetTableName, columns, propertiesTableName} = options;
21
- const urlParameters: UrlParameters = {
22
- tilesetTableName,
23
- propertiesTableName
24
- };
25
-
26
- if (columns) {
27
- urlParameters.columns = columns.join(',');
28
- }
29
- if (filters) {
30
- urlParameters.filters = filters;
31
- }
32
- return baseSource<UrlParameters>('boundary', options, urlParameters) as Promise<TilejsonResult>;
33
- };
@@ -1,55 +0,0 @@
1
- /* eslint-disable camelcase */
2
- import {DEFAULT_AGGREGATION_RES_LEVEL_H3} from '../constants';
3
- import {baseSource} from './base-source';
4
- import type {
5
- AggregationOptions,
6
- FilterOptions,
7
- QuerySourceOptions,
8
- SourceOptions,
9
- SpatialDataType,
10
- TilejsonResult
11
- } from './types';
12
-
13
- export type H3QuerySourceOptions = SourceOptions &
14
- QuerySourceOptions &
15
- AggregationOptions &
16
- FilterOptions;
17
- type UrlParameters = {
18
- aggregationExp: string;
19
- aggregationResLevel?: string;
20
- spatialDataType: SpatialDataType;
21
- spatialDataColumn?: string;
22
- q: string;
23
- queryParameters?: Record<string, unknown> | unknown[];
24
- filters?: Record<string, unknown>;
25
- };
26
-
27
- export const h3QuerySource = async function (
28
- options: H3QuerySourceOptions
29
- ): Promise<TilejsonResult> {
30
- const {
31
- aggregationExp,
32
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_H3,
33
- sqlQuery,
34
- spatialDataColumn = 'h3',
35
- queryParameters,
36
- filters
37
- } = options;
38
- const urlParameters: UrlParameters = {
39
- aggregationExp,
40
- spatialDataColumn,
41
- spatialDataType: 'h3',
42
- q: sqlQuery
43
- };
44
-
45
- if (aggregationResLevel) {
46
- urlParameters.aggregationResLevel = String(aggregationResLevel);
47
- }
48
- if (queryParameters) {
49
- urlParameters.queryParameters = queryParameters;
50
- }
51
- if (filters) {
52
- urlParameters.filters = filters;
53
- }
54
- return baseSource<UrlParameters>('query', options, urlParameters) as Promise<TilejsonResult>;
55
- };
@@ -1,51 +0,0 @@
1
- /* eslint-disable camelcase */
2
- import {DEFAULT_AGGREGATION_RES_LEVEL_H3} from '../constants';
3
- import {baseSource} from './base-source';
4
- import type {
5
- AggregationOptions,
6
- FilterOptions,
7
- SourceOptions,
8
- SpatialDataType,
9
- TableSourceOptions,
10
- TilejsonResult
11
- } from './types';
12
-
13
- export type H3TableSourceOptions = SourceOptions &
14
- TableSourceOptions &
15
- AggregationOptions &
16
- FilterOptions;
17
-
18
- type UrlParameters = {
19
- aggregationExp: string;
20
- aggregationResLevel?: string;
21
- spatialDataType: SpatialDataType;
22
- spatialDataColumn?: string;
23
- name: string;
24
- filters?: Record<string, unknown>;
25
- };
26
-
27
- export const h3TableSource = async function (
28
- options: H3TableSourceOptions
29
- ): Promise<TilejsonResult> {
30
- const {
31
- aggregationExp,
32
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_H3,
33
- spatialDataColumn = 'h3',
34
- tableName,
35
- filters
36
- } = options;
37
- const urlParameters: UrlParameters = {
38
- aggregationExp,
39
- name: tableName,
40
- spatialDataColumn,
41
- spatialDataType: 'h3'
42
- };
43
-
44
- if (aggregationResLevel) {
45
- urlParameters.aggregationResLevel = String(aggregationResLevel);
46
- }
47
- if (filters) {
48
- urlParameters.filters = filters;
49
- }
50
- return baseSource<UrlParameters>('table', options, urlParameters) as Promise<TilejsonResult>;
51
- };
@@ -1,14 +0,0 @@
1
- import {baseSource} from './base-source';
2
- import type {SourceOptions, TilejsonResult, TilesetSourceOptions} from './types';
3
-
4
- export type H3TilesetSourceOptions = SourceOptions & TilesetSourceOptions;
5
- type UrlParameters = {name: string};
6
-
7
- export const h3TilesetSource = async function (
8
- options: H3TilesetSourceOptions
9
- ): Promise<TilejsonResult> {
10
- const {tableName} = options;
11
- const urlParameters: UrlParameters = {name: tableName};
12
-
13
- return baseSource<UrlParameters>('tileset', options, urlParameters) as Promise<TilejsonResult>;
14
- };
@@ -1,45 +0,0 @@
1
- export {SOURCE_DEFAULTS} from './base-source';
2
- export type {TilejsonResult, GeojsonResult, JsonResult} from './types';
3
-
4
- export {boundaryQuerySource} from './boundary-query-source';
5
- export type {BoundaryQuerySourceOptions} from './boundary-query-source';
6
-
7
- export {boundaryTableSource} from './boundary-table-source';
8
- export type {BoundaryTableSourceOptions} from './boundary-table-source';
9
-
10
- export {h3QuerySource} from './h3-query-source';
11
- export type {H3QuerySourceOptions} from './h3-query-source';
12
-
13
- export {h3TableSource} from './h3-table-source';
14
- export type {H3TableSourceOptions} from './h3-table-source';
15
-
16
- export {h3TilesetSource} from './h3-tileset-source';
17
- export type {H3TilesetSourceOptions} from './h3-tileset-source';
18
-
19
- export {rasterSource} from './raster-source';
20
- export type {RasterSourceOptions} from './raster-source';
21
-
22
- export {quadbinQuerySource} from './quadbin-query-source';
23
- export type {QuadbinQuerySourceOptions} from './quadbin-query-source';
24
-
25
- export {quadbinTableSource} from './quadbin-table-source';
26
- export type {QuadbinTableSourceOptions} from './quadbin-table-source';
27
-
28
- export {quadbinTilesetSource} from './quadbin-tileset-source';
29
- export type {QuadbinTilesetSourceOptions} from './quadbin-tileset-source';
30
-
31
- export {vectorQuerySource} from './vector-query-source';
32
- export type {VectorQuerySourceOptions} from './vector-query-source';
33
-
34
- export {vectorTableSource} from './vector-table-source';
35
- export type {VectorTableSourceOptions} from './vector-table-source';
36
-
37
- export {vectorTilesetSource} from './vector-tileset-source';
38
- export type {VectorTilesetSourceOptions} from './vector-tileset-source';
39
-
40
- export type {
41
- SourceOptions,
42
- QuerySourceOptions,
43
- TableSourceOptions,
44
- TilesetSourceOptions
45
- } from './types';