@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.
package/CHANGELOG.md CHANGED
@@ -1,7 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.3
4
+
5
+ ### 0.3.0
6
+
7
+ - deps: Add `@deck.gl/carto` as dependency
8
+
3
9
  ## 0.2
4
10
 
5
11
  ### 0.2.0
6
12
 
7
- - Initial release
13
+ - feat: Initial release
@@ -3,6 +3,7 @@ var bboxPolygon = require('@turf/bbox-polygon');
3
3
  var union = require('@turf/union');
4
4
  var invariant = require('@turf/invariant');
5
5
  var helpers = require('@turf/helpers');
6
+ var carto = require('@deck.gl/carto');
6
7
 
7
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
9
 
@@ -34,6 +35,8 @@ function setClient(c) {
34
35
  client = c;
35
36
  }
36
37
 
38
+ /** Current version of @carto/api-client. */
39
+ const API_CLIENT_VERSION = "0.3.0";
37
40
  /**
38
41
  * Defines a comparator used when matching a column's values against given filter values.
39
42
  *
@@ -349,7 +352,7 @@ function _isMultiPolygon(geometry) {
349
352
  * @internalRemarks Source: @carto/constants
350
353
  * @internal
351
354
  */
352
- const DEFAULT_API_BASE_URL$1 = 'https://gcp-us-east1.api.carto.com';
355
+ const DEFAULT_API_BASE_URL = 'https://gcp-us-east1.api.carto.com';
353
356
  /**
354
357
  * @internalRemarks Source: @carto/react-api
355
358
  * @internal
@@ -905,7 +908,7 @@ class WidgetBaseSource {
905
908
  }
906
909
  WidgetBaseSource.defaultProps = {
907
910
  apiVersion: ApiVersion.V3,
908
- apiBaseUrl: DEFAULT_API_BASE_URL$1,
911
+ apiBaseUrl: DEFAULT_API_BASE_URL,
909
912
  clientId: getClient(),
910
913
  filters: {},
911
914
  filtersLogicalOperator: 'and',
@@ -977,521 +980,13 @@ class WidgetTableSource extends WidgetBaseSource {
977
980
  }
978
981
  }
979
982
 
980
- const DEFAULT_TILE_RESOLUTION = 0.5;
981
- const DEFAULT_AGGREGATION_RES_LEVEL_H3 = 4;
982
- const DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN = 6;
983
-
984
- const isObject = x => x !== null && typeof x === 'object';
985
- const isPureObject = x => isObject(x) && x.constructor === {}.constructor;
986
-
987
- /**
988
- *
989
- * Custom error for reported errors in CARTO Maps API.
990
- * Provides useful debugging information in console and context for applications.
991
- *
992
- */
993
- class CartoAPIError extends Error {
994
- constructor(error, errorContext, response, responseJson) {
995
- let responseString = 'Failed to connect';
996
- if (response) {
997
- responseString = 'Server returned: ';
998
- if (response.status === 400) {
999
- responseString += 'Bad request';
1000
- } else if (response.status === 401 || response.status === 403) {
1001
- responseString += 'Unauthorized access';
1002
- } else if (response.status === 404) {
1003
- responseString += 'Not found';
1004
- } else {
1005
- responseString += 'Error';
1006
- }
1007
- responseString += ` (${response.status}):`;
1008
- }
1009
- responseString += ` ${error.message || error}`;
1010
- let message = `${errorContext.requestType} API request failed`;
1011
- message += `\n${responseString}`;
1012
- for (const key of Object.keys(errorContext)) {
1013
- if (key === 'requestType') continue; // eslint-disable-line no-continue
1014
- message += `\n${formatErrorKey(key)}: ${errorContext[key]}`;
1015
- }
1016
- message += '\n';
1017
- super(message);
1018
- this.name = 'CartoAPIError';
1019
- this.response = response;
1020
- this.responseJson = responseJson;
1021
- this.error = error;
1022
- this.errorContext = errorContext;
1023
- }
1024
- }
1025
- /**
1026
- * Converts camelCase to Camel Case
1027
- */
1028
- function formatErrorKey(key) {
1029
- return key.replace(/([A-Z])/g, ' $1').replace(/^./, s => s.toUpperCase());
1030
- }
1031
-
1032
- const DEFAULT_API_BASE_URL = 'https://gcp-us-east1.api.carto.com';
1033
- const DEFAULT_CLIENT = 'deck-gl-carto';
1034
- const V3_MINOR_VERSION = '3.4';
1035
- // Fastly default limit is 8192; leave some padding.
1036
- const DEFAULT_MAX_LENGTH_URL = 7000;
1037
-
1038
- function joinPath(...args) {
1039
- return args.map(part => part.endsWith('/') ? part.slice(0, -1) : part).join('/');
1040
- }
1041
- function buildV3Path(apiBaseUrl, version, endpoint, ...rest) {
1042
- return joinPath(apiBaseUrl, version, endpoint, ...rest);
1043
- }
1044
- function buildSourceUrl({
1045
- apiBaseUrl,
1046
- connectionName,
1047
- endpoint
1048
- }) {
1049
- return buildV3Path(apiBaseUrl, 'v3', 'maps', connectionName, endpoint);
1050
- }
1051
-
1052
- /**
1053
- * Parameters added to all requests issued with `requestWithParameters()`.
1054
- * These parameters override parameters already in the base URL, but not
1055
- * user-provided parameters.
1056
- */
1057
- const DEFAULT_PARAMETERS = {
1058
- v: V3_MINOR_VERSION,
1059
- deckglVersion: "0.2.2-alpha.1"
1060
- };
1061
- const DEFAULT_HEADERS = {
1062
- Accept: 'application/json',
1063
- 'Content-Type': 'application/json'
1064
- };
1065
- const REQUEST_CACHE = new Map();
1066
- async function requestWithParameters({
1067
- baseUrl,
1068
- parameters = {},
1069
- headers: customHeaders = {},
1070
- errorContext,
1071
- maxLengthURL = DEFAULT_MAX_LENGTH_URL
1072
- }) {
1073
- parameters = {
1074
- ...DEFAULT_PARAMETERS,
1075
- ...parameters
1076
- };
1077
- baseUrl = excludeURLParameters(baseUrl, Object.keys(parameters));
1078
- const key = createCacheKey(baseUrl, parameters, customHeaders);
1079
- if (REQUEST_CACHE.has(key)) {
1080
- return REQUEST_CACHE.get(key);
1081
- }
1082
- const url = createURLWithParameters(baseUrl, parameters);
1083
- const headers = {
1084
- ...DEFAULT_HEADERS,
1085
- ...customHeaders
1086
- };
1087
- /* global fetch */
1088
- const fetchPromise = url.length > maxLengthURL ? fetch(baseUrl, {
1089
- method: 'POST',
1090
- body: JSON.stringify(parameters),
1091
- headers
1092
- }) : fetch(url, {
1093
- headers
1094
- });
1095
- let response;
1096
- let responseJson;
1097
- const jsonPromise = fetchPromise.then(_response => {
1098
- response = _response;
1099
- return response.json();
1100
- }).then(json => {
1101
- responseJson = json;
1102
- if (!response || !response.ok) {
1103
- throw new Error(json.error);
1104
- }
1105
- return json;
1106
- }).catch(error => {
1107
- REQUEST_CACHE.delete(key);
1108
- throw new CartoAPIError(error, errorContext, response, responseJson);
1109
- });
1110
- REQUEST_CACHE.set(key, jsonPromise);
1111
- return jsonPromise;
1112
- }
1113
- function createCacheKey(baseUrl, parameters, headers) {
1114
- const parameterEntries = Object.entries(parameters).sort(([a], [b]) => a > b ? 1 : -1);
1115
- const headerEntries = Object.entries(headers).sort(([a], [b]) => a > b ? 1 : -1);
1116
- return JSON.stringify({
1117
- baseUrl,
1118
- parameters: parameterEntries,
1119
- headers: headerEntries
1120
- });
1121
- }
1122
- /**
1123
- * Appends query string parameters to a URL. Existing URL parameters are kept,
1124
- * unless there is a conflict, in which case the new parameters override
1125
- * those already in the URL.
1126
- */
1127
- function createURLWithParameters(baseUrlString, parameters) {
1128
- const baseUrl = new URL(baseUrlString);
1129
- for (const [key, value] of Object.entries(parameters)) {
1130
- if (isPureObject(value) || Array.isArray(value)) {
1131
- baseUrl.searchParams.set(key, JSON.stringify(value));
1132
- } else {
1133
- baseUrl.searchParams.set(key, value.toString());
1134
- }
1135
- }
1136
- return baseUrl.toString();
1137
- }
1138
- /**
1139
- * Deletes query string parameters from a URL.
1140
- */
1141
- function excludeURLParameters(baseUrlString, parameters) {
1142
- const baseUrl = new URL(baseUrlString);
1143
- for (const param of parameters) {
1144
- if (baseUrl.searchParams.has(param)) {
1145
- baseUrl.searchParams.delete(param);
1146
- }
1147
- }
1148
- return baseUrl.toString();
1149
- }
1150
-
1151
- /* eslint-disable camelcase */
1152
- const SOURCE_DEFAULTS = {
1153
- apiBaseUrl: DEFAULT_API_BASE_URL,
1154
- clientId: DEFAULT_CLIENT,
1155
- format: 'tilejson',
1156
- headers: {},
1157
- maxLengthURL: DEFAULT_MAX_LENGTH_URL
1158
- };
1159
- async function baseSource(endpoint, options, urlParameters) {
1160
- const {
1161
- accessToken,
1162
- connectionName,
1163
- cache,
1164
- ...optionalOptions
1165
- } = options;
1166
- const mergedOptions = {
1167
- ...SOURCE_DEFAULTS,
1168
- accessToken,
1169
- connectionName,
1170
- endpoint
1171
- };
1172
- for (const key in optionalOptions) {
1173
- if (optionalOptions[key]) {
1174
- mergedOptions[key] = optionalOptions[key];
1175
- }
1176
- }
1177
- const baseUrl = buildSourceUrl(mergedOptions);
1178
- const {
1179
- clientId,
1180
- maxLengthURL,
1181
- format
1182
- } = mergedOptions;
1183
- const headers = {
1184
- Authorization: `Bearer ${options.accessToken}`,
1185
- ...options.headers
1186
- };
1187
- const parameters = {
1188
- client: clientId,
1189
- ...urlParameters
1190
- };
1191
- const errorContext = {
1192
- requestType: 'Map instantiation',
1193
- connection: options.connectionName,
1194
- type: endpoint,
1195
- source: JSON.stringify(parameters, undefined, 2)
1196
- };
1197
- const mapInstantiation = await requestWithParameters({
1198
- baseUrl,
1199
- parameters,
1200
- headers,
1201
- errorContext,
1202
- maxLengthURL
1203
- });
1204
- const dataUrl = mapInstantiation[format].url[0];
1205
- if (cache) {
1206
- cache.value = parseInt(new URL(dataUrl).searchParams.get('cache') || '', 10);
1207
- }
1208
- errorContext.requestType = 'Map data';
1209
- if (format === 'tilejson') {
1210
- const json = await requestWithParameters({
1211
- baseUrl: dataUrl,
1212
- headers,
1213
- errorContext,
1214
- maxLengthURL
1215
- });
1216
- if (accessToken) {
1217
- json.accessToken = accessToken;
1218
- }
1219
- return json;
1220
- }
1221
- return await requestWithParameters({
1222
- baseUrl: dataUrl,
1223
- headers,
1224
- errorContext,
1225
- maxLengthURL
1226
- });
1227
- }
1228
-
1229
- const boundaryQuerySource = async function (options) {
1230
- const {
1231
- columns,
1232
- filters,
1233
- tilesetTableName,
1234
- propertiesSqlQuery,
1235
- queryParameters
1236
- } = options;
1237
- const urlParameters = {
1238
- tilesetTableName,
1239
- propertiesSqlQuery
1240
- };
1241
- if (columns) {
1242
- urlParameters.columns = columns.join(',');
1243
- }
1244
- if (filters) {
1245
- urlParameters.filters = filters;
1246
- }
1247
- if (queryParameters) {
1248
- urlParameters.queryParameters = queryParameters;
1249
- }
1250
- return baseSource('boundary', options, urlParameters);
1251
- };
1252
-
1253
- const boundaryTableSource = async function (options) {
1254
- const {
1255
- filters,
1256
- tilesetTableName,
1257
- columns,
1258
- propertiesTableName
1259
- } = options;
1260
- const urlParameters = {
1261
- tilesetTableName,
1262
- propertiesTableName
1263
- };
1264
- if (columns) {
1265
- urlParameters.columns = columns.join(',');
1266
- }
1267
- if (filters) {
1268
- urlParameters.filters = filters;
1269
- }
1270
- return baseSource('boundary', options, urlParameters);
1271
- };
1272
-
1273
- /* eslint-disable camelcase */
1274
- const h3QuerySource$1 = async function (options) {
1275
- const {
1276
- aggregationExp,
1277
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_H3,
1278
- sqlQuery,
1279
- spatialDataColumn = 'h3',
1280
- queryParameters,
1281
- filters
1282
- } = options;
1283
- const urlParameters = {
1284
- aggregationExp,
1285
- spatialDataColumn,
1286
- spatialDataType: 'h3',
1287
- q: sqlQuery
1288
- };
1289
- if (aggregationResLevel) {
1290
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1291
- }
1292
- if (queryParameters) {
1293
- urlParameters.queryParameters = queryParameters;
1294
- }
1295
- if (filters) {
1296
- urlParameters.filters = filters;
1297
- }
1298
- return baseSource('query', options, urlParameters);
1299
- };
1300
-
1301
- /* eslint-disable camelcase */
1302
- const h3TableSource$1 = async function (options) {
1303
- const {
1304
- aggregationExp,
1305
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_H3,
1306
- spatialDataColumn = 'h3',
1307
- tableName,
1308
- filters
1309
- } = options;
1310
- const urlParameters = {
1311
- aggregationExp,
1312
- name: tableName,
1313
- spatialDataColumn,
1314
- spatialDataType: 'h3'
1315
- };
1316
- if (aggregationResLevel) {
1317
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1318
- }
1319
- if (filters) {
1320
- urlParameters.filters = filters;
1321
- }
1322
- return baseSource('table', options, urlParameters);
1323
- };
1324
-
1325
- const h3TilesetSource = async function (options) {
1326
- const {
1327
- tableName
1328
- } = options;
1329
- const urlParameters = {
1330
- name: tableName
1331
- };
1332
- return baseSource('tileset', options, urlParameters);
1333
- };
1334
-
1335
- const rasterSource = async function (options) {
1336
- const {
1337
- tableName,
1338
- filters
1339
- } = options;
1340
- const urlParameters = {
1341
- name: tableName
1342
- };
1343
- if (filters) {
1344
- urlParameters.filters = filters;
1345
- }
1346
- return baseSource('raster', options, urlParameters);
1347
- };
1348
-
1349
- /* eslint-disable camelcase */
1350
- const quadbinQuerySource$1 = async function (options) {
1351
- const {
1352
- aggregationExp,
1353
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
1354
- sqlQuery,
1355
- spatialDataColumn = 'quadbin',
1356
- queryParameters,
1357
- filters
1358
- } = options;
1359
- const urlParameters = {
1360
- aggregationExp,
1361
- q: sqlQuery,
1362
- spatialDataColumn,
1363
- spatialDataType: 'quadbin'
1364
- };
1365
- if (aggregationResLevel) {
1366
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1367
- }
1368
- if (queryParameters) {
1369
- urlParameters.queryParameters = queryParameters;
1370
- }
1371
- if (filters) {
1372
- urlParameters.filters = filters;
1373
- }
1374
- return baseSource('query', options, urlParameters);
1375
- };
1376
-
1377
- /* eslint-disable camelcase */
1378
- const quadbinTableSource$1 = async function (options) {
1379
- const {
1380
- aggregationExp,
1381
- aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
1382
- spatialDataColumn = 'quadbin',
1383
- tableName,
1384
- filters
1385
- } = options;
1386
- const urlParameters = {
1387
- aggregationExp,
1388
- name: tableName,
1389
- spatialDataColumn,
1390
- spatialDataType: 'quadbin'
1391
- };
1392
- if (aggregationResLevel) {
1393
- urlParameters.aggregationResLevel = String(aggregationResLevel);
1394
- }
1395
- if (filters) {
1396
- urlParameters.filters = filters;
1397
- }
1398
- return baseSource('table', options, urlParameters);
1399
- };
1400
-
1401
- const quadbinTilesetSource = 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
- /* eslint-disable camelcase */
1412
- const vectorQuerySource$1 = async function (options) {
1413
- const {
1414
- columns,
1415
- filters,
1416
- spatialDataColumn = 'geom',
1417
- sqlQuery,
1418
- tileResolution = DEFAULT_TILE_RESOLUTION,
1419
- queryParameters
1420
- } = options;
1421
- const urlParameters = {
1422
- spatialDataColumn,
1423
- spatialDataType: 'geo',
1424
- tileResolution: tileResolution.toString(),
1425
- q: sqlQuery
1426
- };
1427
- if (columns) {
1428
- urlParameters.columns = columns.join(',');
1429
- }
1430
- if (filters) {
1431
- urlParameters.filters = filters;
1432
- }
1433
- if (queryParameters) {
1434
- urlParameters.queryParameters = queryParameters;
1435
- }
1436
- return baseSource('query', options, urlParameters);
1437
- };
1438
-
1439
- /* eslint-disable camelcase */
1440
- const vectorTableSource$1 = async function (options) {
1441
- const {
1442
- columns,
1443
- filters,
1444
- spatialDataColumn = 'geom',
1445
- tableName,
1446
- tileResolution = DEFAULT_TILE_RESOLUTION
1447
- } = options;
1448
- const urlParameters = {
1449
- name: tableName,
1450
- spatialDataColumn,
1451
- spatialDataType: 'geo',
1452
- tileResolution: tileResolution.toString()
1453
- };
1454
- if (columns) {
1455
- urlParameters.columns = columns.join(',');
1456
- }
1457
- if (filters) {
1458
- urlParameters.filters = filters;
1459
- }
1460
- return baseSource('table', options, urlParameters);
1461
- };
1462
-
1463
- const vectorTilesetSource = async function (options) {
1464
- const {
1465
- tableName
1466
- } = options;
1467
- const urlParameters = {
1468
- name: tableName
1469
- };
1470
- return baseSource('tileset', options, urlParameters);
1471
- };
1472
-
1473
- ({
1474
- boundaryQuerySource,
1475
- boundaryTableSource,
1476
- h3QuerySource: h3QuerySource$1,
1477
- h3TableSource: h3TableSource$1,
1478
- h3TilesetSource,
1479
- rasterSource,
1480
- quadbinQuerySource: quadbinQuerySource$1,
1481
- quadbinTableSource: quadbinTableSource$1,
1482
- quadbinTilesetSource,
1483
- vectorQuerySource: vectorQuerySource$1,
1484
- vectorTableSource: vectorTableSource$1,
1485
- vectorTilesetSource
1486
- });
1487
-
1488
983
  /** Wrapper adding Widget API support to [vectorTableSource](https://deck.gl/docs/api-reference/carto/data-sources). */
1489
984
 
1490
985
  /** Wrapper adding Widget API support to [quadbinQuerySource](https://deck.gl/docs/api-reference/carto/data-sources). */
1491
986
  const quadbinQuerySource = function (props) {
1492
987
  try {
1493
988
  assignDefaultProps(props);
1494
- return Promise.resolve(quadbinQuerySource$1(props)).then(function (response) {
989
+ return Promise.resolve(carto.quadbinQuerySource(props)).then(function (response) {
1495
990
  return {
1496
991
  ...response,
1497
992
  widgetSource: new WidgetQuerySource(props)
@@ -1505,7 +1000,7 @@ const quadbinQuerySource = function (props) {
1505
1000
  const quadbinTableSource = function (props) {
1506
1001
  try {
1507
1002
  assignDefaultProps(props);
1508
- return Promise.resolve(quadbinTableSource$1(props)).then(function (response) {
1003
+ return Promise.resolve(carto.quadbinTableSource(props)).then(function (response) {
1509
1004
  return {
1510
1005
  ...response,
1511
1006
  widgetSource: new WidgetTableSource(props)
@@ -1519,7 +1014,7 @@ const quadbinTableSource = function (props) {
1519
1014
  const h3QuerySource = function (props) {
1520
1015
  try {
1521
1016
  assignDefaultProps(props);
1522
- return Promise.resolve(h3QuerySource$1(props)).then(function (response) {
1017
+ return Promise.resolve(carto.h3QuerySource(props)).then(function (response) {
1523
1018
  return {
1524
1019
  ...response,
1525
1020
  widgetSource: new WidgetQuerySource(props)
@@ -1533,7 +1028,7 @@ const h3QuerySource = function (props) {
1533
1028
  const h3TableSource = function (props) {
1534
1029
  try {
1535
1030
  assignDefaultProps(props);
1536
- return Promise.resolve(h3TableSource$1(props)).then(function (response) {
1031
+ return Promise.resolve(carto.h3TableSource(props)).then(function (response) {
1537
1032
  return {
1538
1033
  ...response,
1539
1034
  widgetSource: new WidgetTableSource(props)
@@ -1547,7 +1042,7 @@ const h3TableSource = function (props) {
1547
1042
  const vectorQuerySource = function (props) {
1548
1043
  try {
1549
1044
  assignDefaultProps(props);
1550
- return Promise.resolve(vectorQuerySource$1(props)).then(function (response) {
1045
+ return Promise.resolve(carto.vectorQuerySource(props)).then(function (response) {
1551
1046
  return {
1552
1047
  ...response,
1553
1048
  widgetSource: new WidgetQuerySource(props)
@@ -1560,7 +1055,7 @@ const vectorQuerySource = function (props) {
1560
1055
  const vectorTableSource = function (props) {
1561
1056
  try {
1562
1057
  assignDefaultProps(props);
1563
- return Promise.resolve(vectorTableSource$1(props)).then(function (response) {
1058
+ return Promise.resolve(carto.vectorTableSource(props)).then(function (response) {
1564
1059
  return {
1565
1060
  ...response,
1566
1061
  widgetSource: new WidgetTableSource(props)
@@ -1579,6 +1074,7 @@ function assignDefaultProps(props) {
1579
1074
  }
1580
1075
  }
1581
1076
 
1077
+ exports.API_CLIENT_VERSION = API_CLIENT_VERSION;
1582
1078
  exports.WidgetBaseSource = WidgetBaseSource;
1583
1079
  exports.WidgetQuerySource = WidgetQuerySource;
1584
1080
  exports.WidgetTableSource = WidgetTableSource;