@carto/api-client 0.2.2-alpha.1 → 0.3.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.
@@ -3,6 +3,7 @@ import bboxPolygon from '@turf/bbox-polygon';
3
3
  import union from '@turf/union';
4
4
  import { getType } from '@turf/invariant';
5
5
  import { featureCollection, feature, polygon, multiPolygon } from '@turf/helpers';
6
+ import { vectorTableSource as vectorTableSource$1, vectorQuerySource as vectorQuerySource$1, h3TableSource as h3TableSource$1, h3QuerySource as h3QuerySource$1, quadbinTableSource as quadbinTableSource$1, quadbinQuerySource as quadbinQuerySource$1 } from '@deck.gl/carto';
6
7
 
7
8
  /**
8
9
  * @internal
@@ -28,6 +29,8 @@ function setClient(c) {
28
29
  client = c;
29
30
  }
30
31
 
32
+ /** Current version of @carto/api-client. */
33
+ const API_CLIENT_VERSION = "0.3.0";
31
34
  /**
32
35
  * Defines a comparator used when matching a column's values against given filter values.
33
36
  *
@@ -361,7 +364,7 @@ function _objectWithoutPropertiesLoose(r, e) {
361
364
  * @internalRemarks Source: @carto/constants
362
365
  * @internal
363
366
  */
364
- const DEFAULT_API_BASE_URL$1 = 'https://gcp-us-east1.api.carto.com';
367
+ const DEFAULT_API_BASE_URL = 'https://gcp-us-east1.api.carto.com';
365
368
  /**
366
369
  * @internalRemarks Source: @carto/react-api
367
370
  * @internal
@@ -845,7 +848,7 @@ class WidgetBaseSource {
845
848
  }
846
849
  WidgetBaseSource.defaultProps = {
847
850
  apiVersion: ApiVersion.V3,
848
- apiBaseUrl: DEFAULT_API_BASE_URL$1,
851
+ apiBaseUrl: DEFAULT_API_BASE_URL,
849
852
  clientId: getClient(),
850
853
  filters: {},
851
854
  filtersLogicalOperator: 'and',
@@ -915,514 +918,6 @@ class WidgetTableSource extends WidgetBaseSource {
915
918
  }
916
919
  }
917
920
 
918
- const DEFAULT_TILE_RESOLUTION = 0.5;
919
- const DEFAULT_AGGREGATION_RES_LEVEL_H3 = 4;
920
- const DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN = 6;
921
-
922
- const isObject = x => x !== null && typeof x === 'object';
923
- const isPureObject = x => isObject(x) && x.constructor === {}.constructor;
924
-
925
- /**
926
- *
927
- * Custom error for reported errors in CARTO Maps API.
928
- * Provides useful debugging information in console and context for applications.
929
- *
930
- */
931
- class CartoAPIError extends Error {
932
- constructor(error, errorContext, response, responseJson) {
933
- let responseString = 'Failed to connect';
934
- if (response) {
935
- responseString = 'Server returned: ';
936
- if (response.status === 400) {
937
- responseString += 'Bad request';
938
- } else if (response.status === 401 || response.status === 403) {
939
- responseString += 'Unauthorized access';
940
- } else if (response.status === 404) {
941
- responseString += 'Not found';
942
- } else {
943
- responseString += 'Error';
944
- }
945
- responseString += ` (${response.status}):`;
946
- }
947
- responseString += ` ${error.message || error}`;
948
- let message = `${errorContext.requestType} API request failed`;
949
- message += `\n${responseString}`;
950
- for (const key of Object.keys(errorContext)) {
951
- if (key === 'requestType') continue; // eslint-disable-line no-continue
952
- message += `\n${formatErrorKey(key)}: ${errorContext[key]}`;
953
- }
954
- message += '\n';
955
- super(message);
956
- this.name = 'CartoAPIError';
957
- this.response = response;
958
- this.responseJson = responseJson;
959
- this.error = error;
960
- this.errorContext = errorContext;
961
- }
962
- }
963
- /**
964
- * Converts camelCase to Camel Case
965
- */
966
- function formatErrorKey(key) {
967
- return key.replace(/([A-Z])/g, ' $1').replace(/^./, s => s.toUpperCase());
968
- }
969
-
970
- const DEFAULT_API_BASE_URL = 'https://gcp-us-east1.api.carto.com';
971
- const DEFAULT_CLIENT = 'deck-gl-carto';
972
- const V3_MINOR_VERSION = '3.4';
973
- // Fastly default limit is 8192; leave some padding.
974
- const DEFAULT_MAX_LENGTH_URL = 7000;
975
-
976
- function joinPath(...args) {
977
- return args.map(part => part.endsWith('/') ? part.slice(0, -1) : part).join('/');
978
- }
979
- function buildV3Path(apiBaseUrl, version, endpoint, ...rest) {
980
- return joinPath(apiBaseUrl, version, endpoint, ...rest);
981
- }
982
- function buildSourceUrl({
983
- apiBaseUrl,
984
- connectionName,
985
- endpoint
986
- }) {
987
- return buildV3Path(apiBaseUrl, 'v3', 'maps', connectionName, endpoint);
988
- }
989
-
990
- /**
991
- * Parameters added to all requests issued with `requestWithParameters()`.
992
- * These parameters override parameters already in the base URL, but not
993
- * user-provided parameters.
994
- */
995
- const DEFAULT_PARAMETERS = {
996
- v: V3_MINOR_VERSION,
997
- deckglVersion: "0.2.2-alpha.1"
998
- };
999
- const DEFAULT_HEADERS = {
1000
- Accept: 'application/json',
1001
- 'Content-Type': 'application/json'
1002
- };
1003
- const REQUEST_CACHE = new Map();
1004
- async function requestWithParameters({
1005
- baseUrl,
1006
- parameters = {},
1007
- headers: customHeaders = {},
1008
- errorContext,
1009
- maxLengthURL = DEFAULT_MAX_LENGTH_URL
1010
- }) {
1011
- parameters = {
1012
- ...DEFAULT_PARAMETERS,
1013
- ...parameters
1014
- };
1015
- baseUrl = excludeURLParameters(baseUrl, Object.keys(parameters));
1016
- const key = createCacheKey(baseUrl, parameters, customHeaders);
1017
- if (REQUEST_CACHE.has(key)) {
1018
- return REQUEST_CACHE.get(key);
1019
- }
1020
- const url = createURLWithParameters(baseUrl, parameters);
1021
- const headers = {
1022
- ...DEFAULT_HEADERS,
1023
- ...customHeaders
1024
- };
1025
- /* global fetch */
1026
- const fetchPromise = url.length > maxLengthURL ? fetch(baseUrl, {
1027
- method: 'POST',
1028
- body: JSON.stringify(parameters),
1029
- headers
1030
- }) : fetch(url, {
1031
- headers
1032
- });
1033
- let response;
1034
- let responseJson;
1035
- const jsonPromise = fetchPromise.then(_response => {
1036
- response = _response;
1037
- return response.json();
1038
- }).then(json => {
1039
- responseJson = json;
1040
- if (!response || !response.ok) {
1041
- throw new Error(json.error);
1042
- }
1043
- return json;
1044
- }).catch(error => {
1045
- REQUEST_CACHE.delete(key);
1046
- throw new CartoAPIError(error, errorContext, response, responseJson);
1047
- });
1048
- REQUEST_CACHE.set(key, jsonPromise);
1049
- return jsonPromise;
1050
- }
1051
- function createCacheKey(baseUrl, parameters, headers) {
1052
- const parameterEntries = Object.entries(parameters).sort(([a], [b]) => a > b ? 1 : -1);
1053
- const headerEntries = Object.entries(headers).sort(([a], [b]) => a > b ? 1 : -1);
1054
- return JSON.stringify({
1055
- baseUrl,
1056
- parameters: parameterEntries,
1057
- headers: headerEntries
1058
- });
1059
- }
1060
- /**
1061
- * Appends query string parameters to a URL. Existing URL parameters are kept,
1062
- * unless there is a conflict, in which case the new parameters override
1063
- * those already in the URL.
1064
- */
1065
- function createURLWithParameters(baseUrlString, parameters) {
1066
- const baseUrl = new URL(baseUrlString);
1067
- for (const [key, value] of Object.entries(parameters)) {
1068
- if (isPureObject(value) || Array.isArray(value)) {
1069
- baseUrl.searchParams.set(key, JSON.stringify(value));
1070
- } else {
1071
- baseUrl.searchParams.set(key, value.toString());
1072
- }
1073
- }
1074
- return baseUrl.toString();
1075
- }
1076
- /**
1077
- * Deletes query string parameters from a URL.
1078
- */
1079
- function excludeURLParameters(baseUrlString, parameters) {
1080
- const baseUrl = new URL(baseUrlString);
1081
- for (const param of parameters) {
1082
- if (baseUrl.searchParams.has(param)) {
1083
- baseUrl.searchParams.delete(param);
1084
- }
1085
- }
1086
- return baseUrl.toString();
1087
- }
1088
-
1089
- /* eslint-disable camelcase */
1090
- const SOURCE_DEFAULTS = {
1091
- apiBaseUrl: DEFAULT_API_BASE_URL,
1092
- clientId: DEFAULT_CLIENT,
1093
- format: 'tilejson',
1094
- headers: {},
1095
- maxLengthURL: DEFAULT_MAX_LENGTH_URL
1096
- };
1097
- async function baseSource(endpoint, options, urlParameters) {
1098
- const {
1099
- accessToken,
1100
- connectionName,
1101
- cache,
1102
- ...optionalOptions
1103
- } = options;
1104
- const mergedOptions = {
1105
- ...SOURCE_DEFAULTS,
1106
- accessToken,
1107
- connectionName,
1108
- endpoint
1109
- };
1110
- for (const key in optionalOptions) {
1111
- if (optionalOptions[key]) {
1112
- mergedOptions[key] = optionalOptions[key];
1113
- }
1114
- }
1115
- const baseUrl = buildSourceUrl(mergedOptions);
1116
- const {
1117
- clientId,
1118
- maxLengthURL,
1119
- format
1120
- } = mergedOptions;
1121
- const headers = {
1122
- Authorization: `Bearer ${options.accessToken}`,
1123
- ...options.headers
1124
- };
1125
- const parameters = {
1126
- client: clientId,
1127
- ...urlParameters
1128
- };
1129
- const errorContext = {
1130
- requestType: 'Map instantiation',
1131
- connection: options.connectionName,
1132
- type: endpoint,
1133
- source: JSON.stringify(parameters, undefined, 2)
1134
- };
1135
- const mapInstantiation = await requestWithParameters({
1136
- baseUrl,
1137
- parameters,
1138
- headers,
1139
- errorContext,
1140
- maxLengthURL
1141
- });
1142
- const dataUrl = mapInstantiation[format].url[0];
1143
- if (cache) {
1144
- cache.value = parseInt(new URL(dataUrl).searchParams.get('cache') || '', 10);
1145
- }
1146
- errorContext.requestType = 'Map data';
1147
- if (format === 'tilejson') {
1148
- const json = await requestWithParameters({
1149
- baseUrl: dataUrl,
1150
- headers,
1151
- errorContext,
1152
- maxLengthURL
1153
- });
1154
- if (accessToken) {
1155
- json.accessToken = accessToken;
1156
- }
1157
- return json;
1158
- }
1159
- return await requestWithParameters({
1160
- baseUrl: dataUrl,
1161
- headers,
1162
- errorContext,
1163
- maxLengthURL
1164
- });
1165
- }
1166
-
1167
- const boundaryQuerySource = async function (options) {
1168
- const {
1169
- columns,
1170
- filters,
1171
- tilesetTableName,
1172
- propertiesSqlQuery,
1173
- queryParameters
1174
- } = options;
1175
- const urlParameters = {
1176
- tilesetTableName,
1177
- propertiesSqlQuery
1178
- };
1179
- if (columns) {
1180
- urlParameters.columns = columns.join(',');
1181
- }
1182
- if (filters) {
1183
- urlParameters.filters = filters;
1184
- }
1185
- if (queryParameters) {
1186
- urlParameters.queryParameters = queryParameters;
1187
- }
1188
- return baseSource('boundary', options, urlParameters);
1189
- };
1190
-
1191
- const boundaryTableSource = async function (options) {
1192
- const {
1193
- filters,
1194
- tilesetTableName,
1195
- columns,
1196
- propertiesTableName
1197
- } = options;
1198
- const urlParameters = {
1199
- tilesetTableName,
1200
- propertiesTableName
1201
- };
1202
- if (columns) {
1203
- urlParameters.columns = columns.join(',');
1204
- }
1205
- if (filters) {
1206
- urlParameters.filters = filters;
1207
- }
1208
- return baseSource('boundary', options, urlParameters);
1209
- };
1210
-
1211
- /* eslint-disable camelcase */
1212
- const h3QuerySource$1 = async function (options) {
1213
- const {
1214
- aggregationExp,
1215
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_H3,
1216
- sqlQuery,
1217
- spatialDataColumn = 'h3',
1218
- queryParameters,
1219
- filters
1220
- } = options;
1221
- const urlParameters = {
1222
- aggregationExp,
1223
- spatialDataColumn,
1224
- spatialDataType: 'h3',
1225
- q: sqlQuery
1226
- };
1227
- if (aggregationResLevel) {
1228
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1229
- }
1230
- if (queryParameters) {
1231
- urlParameters.queryParameters = queryParameters;
1232
- }
1233
- if (filters) {
1234
- urlParameters.filters = filters;
1235
- }
1236
- return baseSource('query', options, urlParameters);
1237
- };
1238
-
1239
- /* eslint-disable camelcase */
1240
- const h3TableSource$1 = async function (options) {
1241
- const {
1242
- aggregationExp,
1243
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_H3,
1244
- spatialDataColumn = 'h3',
1245
- tableName,
1246
- filters
1247
- } = options;
1248
- const urlParameters = {
1249
- aggregationExp,
1250
- name: tableName,
1251
- spatialDataColumn,
1252
- spatialDataType: 'h3'
1253
- };
1254
- if (aggregationResLevel) {
1255
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1256
- }
1257
- if (filters) {
1258
- urlParameters.filters = filters;
1259
- }
1260
- return baseSource('table', options, urlParameters);
1261
- };
1262
-
1263
- const h3TilesetSource = async function (options) {
1264
- const {
1265
- tableName
1266
- } = options;
1267
- const urlParameters = {
1268
- name: tableName
1269
- };
1270
- return baseSource('tileset', options, urlParameters);
1271
- };
1272
-
1273
- const rasterSource = async function (options) {
1274
- const {
1275
- tableName,
1276
- filters
1277
- } = options;
1278
- const urlParameters = {
1279
- name: tableName
1280
- };
1281
- if (filters) {
1282
- urlParameters.filters = filters;
1283
- }
1284
- return baseSource('raster', options, urlParameters);
1285
- };
1286
-
1287
- /* eslint-disable camelcase */
1288
- const quadbinQuerySource$1 = async function (options) {
1289
- const {
1290
- aggregationExp,
1291
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
1292
- sqlQuery,
1293
- spatialDataColumn = 'quadbin',
1294
- queryParameters,
1295
- filters
1296
- } = options;
1297
- const urlParameters = {
1298
- aggregationExp,
1299
- q: sqlQuery,
1300
- spatialDataColumn,
1301
- spatialDataType: 'quadbin'
1302
- };
1303
- if (aggregationResLevel) {
1304
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1305
- }
1306
- if (queryParameters) {
1307
- urlParameters.queryParameters = queryParameters;
1308
- }
1309
- if (filters) {
1310
- urlParameters.filters = filters;
1311
- }
1312
- return baseSource('query', options, urlParameters);
1313
- };
1314
-
1315
- /* eslint-disable camelcase */
1316
- const quadbinTableSource$1 = async function (options) {
1317
- const {
1318
- aggregationExp,
1319
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
1320
- spatialDataColumn = 'quadbin',
1321
- tableName,
1322
- filters
1323
- } = options;
1324
- const urlParameters = {
1325
- aggregationExp,
1326
- name: tableName,
1327
- spatialDataColumn,
1328
- spatialDataType: 'quadbin'
1329
- };
1330
- if (aggregationResLevel) {
1331
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1332
- }
1333
- if (filters) {
1334
- urlParameters.filters = filters;
1335
- }
1336
- return baseSource('table', options, urlParameters);
1337
- };
1338
-
1339
- const quadbinTilesetSource = async function (options) {
1340
- const {
1341
- tableName
1342
- } = options;
1343
- const urlParameters = {
1344
- name: tableName
1345
- };
1346
- return baseSource('tileset', options, urlParameters);
1347
- };
1348
-
1349
- /* eslint-disable camelcase */
1350
- const vectorQuerySource$1 = async function (options) {
1351
- const {
1352
- columns,
1353
- filters,
1354
- spatialDataColumn = 'geom',
1355
- sqlQuery,
1356
- tileResolution = DEFAULT_TILE_RESOLUTION,
1357
- queryParameters
1358
- } = options;
1359
- const urlParameters = {
1360
- spatialDataColumn,
1361
- spatialDataType: 'geo',
1362
- tileResolution: tileResolution.toString(),
1363
- q: sqlQuery
1364
- };
1365
- if (columns) {
1366
- urlParameters.columns = columns.join(',');
1367
- }
1368
- if (filters) {
1369
- urlParameters.filters = filters;
1370
- }
1371
- if (queryParameters) {
1372
- urlParameters.queryParameters = queryParameters;
1373
- }
1374
- return baseSource('query', options, urlParameters);
1375
- };
1376
-
1377
- /* eslint-disable camelcase */
1378
- const vectorTableSource$1 = async function (options) {
1379
- const {
1380
- columns,
1381
- filters,
1382
- spatialDataColumn = 'geom',
1383
- tableName,
1384
- tileResolution = DEFAULT_TILE_RESOLUTION
1385
- } = options;
1386
- const urlParameters = {
1387
- name: tableName,
1388
- spatialDataColumn,
1389
- spatialDataType: 'geo',
1390
- tileResolution: tileResolution.toString()
1391
- };
1392
- if (columns) {
1393
- urlParameters.columns = columns.join(',');
1394
- }
1395
- if (filters) {
1396
- urlParameters.filters = filters;
1397
- }
1398
- return baseSource('table', options, urlParameters);
1399
- };
1400
-
1401
- const vectorTilesetSource = async function (options) {
1402
- const {
1403
- tableName
1404
- } = options;
1405
- const urlParameters = {
1406
- name: tableName
1407
- };
1408
- return baseSource('tileset', options, urlParameters);
1409
- };
1410
-
1411
- ({
1412
- boundaryQuerySource,
1413
- boundaryTableSource,
1414
- h3QuerySource: h3QuerySource$1,
1415
- h3TableSource: h3TableSource$1,
1416
- h3TilesetSource,
1417
- rasterSource,
1418
- quadbinQuerySource: quadbinQuerySource$1,
1419
- quadbinTableSource: quadbinTableSource$1,
1420
- quadbinTilesetSource,
1421
- vectorQuerySource: vectorQuerySource$1,
1422
- vectorTableSource: vectorTableSource$1,
1423
- vectorTilesetSource
1424
- });
1425
-
1426
921
  /** Wrapper adding Widget API support to [vectorTableSource](https://deck.gl/docs/api-reference/carto/data-sources). */
1427
922
  async function vectorTableSource(props) {
1428
923
  assignDefaultProps(props);
@@ -1480,5 +975,5 @@ function assignDefaultProps(props) {
1480
975
  }
1481
976
  }
1482
977
 
1483
- export { FilterType, WidgetBaseSource, WidgetQuerySource, WidgetTableSource, addFilter, clearFilters, createPolygonSpatialFilter, createViewportSpatialFilter, getClient, getFilter, h3QuerySource, h3TableSource, hasFilter, quadbinQuerySource, quadbinTableSource, removeFilter, setClient, vectorQuerySource, vectorTableSource };
978
+ export { API_CLIENT_VERSION, FilterType, WidgetBaseSource, WidgetQuerySource, WidgetTableSource, addFilter, clearFilters, createPolygonSpatialFilter, createViewportSpatialFilter, getClient, getFilter, h3QuerySource, h3TableSource, hasFilter, quadbinQuerySource, quadbinTableSource, removeFilter, setClient, vectorQuerySource, vectorTableSource };
1484
979
  //# sourceMappingURL=api-client.modern.js.map