@deck.gl/carto 8.6.2 → 8.7.0-alpha.2

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 (65) hide show
  1. package/dist/dist.dev.js +24009 -380
  2. package/dist/es5/api/index.js +14 -8
  3. package/dist/es5/api/index.js.map +1 -1
  4. package/dist/es5/api/layer-map.js +244 -0
  5. package/dist/es5/api/layer-map.js.map +1 -0
  6. package/dist/es5/api/maps-api-common.js +9 -13
  7. package/dist/es5/api/maps-api-common.js.map +1 -1
  8. package/dist/es5/api/maps-client.js +98 -157
  9. package/dist/es5/api/maps-client.js.map +1 -1
  10. package/dist/es5/api/maps-v3-client.js +325 -304
  11. package/dist/es5/api/maps-v3-client.js.map +1 -1
  12. package/dist/es5/api/parseMap.js +210 -0
  13. package/dist/es5/api/parseMap.js.map +1 -0
  14. package/dist/es5/basemap.js +1 -1
  15. package/dist/es5/basemap.js.map +1 -1
  16. package/dist/es5/config.js +23 -39
  17. package/dist/es5/config.js.map +1 -1
  18. package/dist/es5/index.js +22 -16
  19. package/dist/es5/index.js.map +1 -1
  20. package/dist/es5/layers/carto-bqtiler-layer.js +6 -39
  21. package/dist/es5/layers/carto-bqtiler-layer.js.map +1 -1
  22. package/dist/es5/layers/carto-layer.js +130 -211
  23. package/dist/es5/layers/carto-layer.js.map +1 -1
  24. package/dist/es5/layers/carto-sql-layer.js +6 -39
  25. package/dist/es5/layers/carto-sql-layer.js.map +1 -1
  26. package/dist/es5/style/color-bins-style.js +14 -13
  27. package/dist/es5/style/color-bins-style.js.map +1 -1
  28. package/dist/es5/style/color-categories-style.js +16 -39
  29. package/dist/es5/style/color-categories-style.js.map +1 -1
  30. package/dist/es5/style/color-continuous-style.js +14 -13
  31. package/dist/es5/style/color-continuous-style.js.map +1 -1
  32. package/dist/es5/style/palette.js +15 -21
  33. package/dist/es5/style/palette.js.map +1 -1
  34. package/dist/es5/style/utils.js +6 -13
  35. package/dist/es5/style/utils.js.map +1 -1
  36. package/dist/esm/api/index.js +1 -1
  37. package/dist/esm/api/index.js.map +1 -1
  38. package/dist/esm/api/layer-map.js +219 -0
  39. package/dist/esm/api/layer-map.js.map +1 -0
  40. package/dist/esm/api/maps-client.js +1 -1
  41. package/dist/esm/api/maps-client.js.map +1 -1
  42. package/dist/esm/api/maps-v3-client.js +145 -16
  43. package/dist/esm/api/maps-v3-client.js.map +1 -1
  44. package/dist/esm/api/parseMap.js +194 -0
  45. package/dist/esm/api/parseMap.js.map +1 -0
  46. package/dist/esm/config.js +11 -26
  47. package/dist/esm/config.js.map +1 -1
  48. package/dist/esm/index.js +1 -1
  49. package/dist/esm/index.js.map +1 -1
  50. package/dist/esm/layers/carto-layer.js +7 -9
  51. package/dist/esm/layers/carto-layer.js.map +1 -1
  52. package/dist/esm/style/color-bins-style.js.map +1 -1
  53. package/dist/esm/style/color-continuous-style.js.map +1 -1
  54. package/dist.min.js +278 -1
  55. package/package.json +7 -3
  56. package/src/api/index.js +1 -1
  57. package/src/api/layer-map.js +194 -0
  58. package/src/api/maps-client.js +3 -7
  59. package/src/api/maps-v3-client.js +114 -11
  60. package/src/api/parseMap.js +182 -0
  61. package/src/config.js +8 -39
  62. package/src/index.js +1 -0
  63. package/src/layers/carto-layer.js +3 -5
  64. package/src/style/color-bins-style.js +1 -3
  65. package/src/style/color-continuous-style.js +1 -3
@@ -1,112 +1,73 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
6
  exports.mapInstantiation = mapInstantiation;
9
7
  exports.fetchLayerData = fetchLayerData;
10
8
  exports.getData = getData;
11
-
12
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
-
14
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
15
-
16
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
9
+ exports.fetchMap = fetchMap;
17
10
 
18
11
  var _config = require("../config");
19
12
 
20
13
  var _mapsApiCommon = require("./maps-api-common");
21
14
 
15
+ var _parseMap = require("./parseMap");
16
+
22
17
  var _core = require("@deck.gl/core");
23
18
 
24
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
19
+ const MAX_GET_LENGTH = 2048;
20
+
21
+ async function request({
22
+ method,
23
+ url,
24
+ format,
25
+ accessToken,
26
+ body
27
+ }) {
28
+ let response;
29
+ const headers = {
30
+ Accept: 'application/json'
31
+ };
32
+
33
+ if (accessToken) {
34
+ headers.Authorization = "Bearer ".concat(accessToken);
35
+ }
25
36
 
26
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
37
+ if (method === 'POST') {
38
+ headers['Content-Type'] = 'application/json';
39
+ }
27
40
 
28
- var MAX_GET_LENGTH = 2048;
41
+ try {
42
+ response = await fetch(url, {
43
+ method,
44
+ headers,
45
+ body
46
+ });
47
+ } catch (error) {
48
+ throw new Error("Failed to connect to Maps API: ".concat(error));
49
+ }
29
50
 
30
- function request(_x) {
31
- return _request.apply(this, arguments);
32
- }
51
+ if (format === _mapsApiCommon.FORMATS.NDJSON) {
52
+ return response;
53
+ }
33
54
 
34
- function _request() {
35
- _request = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee(_ref) {
36
- var method, url, format, accessToken, body, response, headers, json;
37
- return _regenerator.default.wrap(function _callee$(_context) {
38
- while (1) {
39
- switch (_context.prev = _context.next) {
40
- case 0:
41
- method = _ref.method, url = _ref.url, format = _ref.format, accessToken = _ref.accessToken, body = _ref.body;
42
- headers = {
43
- Accept: 'application/json'
44
- };
45
-
46
- if (accessToken) {
47
- headers.Authorization = "Bearer ".concat(accessToken);
48
- }
49
-
50
- if (method === 'POST') {
51
- headers['Content-Type'] = 'application/json';
52
- }
53
-
54
- _context.prev = 4;
55
- _context.next = 7;
56
- return fetch(url, {
57
- method: method,
58
- headers: headers,
59
- body: body
60
- });
61
-
62
- case 7:
63
- response = _context.sent;
64
- _context.next = 13;
65
- break;
66
-
67
- case 10:
68
- _context.prev = 10;
69
- _context.t0 = _context["catch"](4);
70
- throw new Error("Failed to connect to Maps API: ".concat(_context.t0));
71
-
72
- case 13:
73
- if (!(format === _mapsApiCommon.FORMATS.NDJSON)) {
74
- _context.next = 15;
75
- break;
76
- }
77
-
78
- return _context.abrupt("return", response);
79
-
80
- case 15:
81
- _context.next = 17;
82
- return response.json();
83
-
84
- case 17:
85
- json = _context.sent;
86
-
87
- if (!response.ok) {
88
- dealWithError({
89
- response: response,
90
- error: json.error
91
- });
92
- }
93
-
94
- return _context.abrupt("return", json.rows ? json.rows : json);
95
-
96
- case 20:
97
- case "end":
98
- return _context.stop();
99
- }
100
- }
101
- }, _callee, null, [[4, 10]]);
102
- }));
103
- return _request.apply(this, arguments);
104
- }
55
+ const json = await response.json();
105
56
 
106
- function dealWithError(_ref2) {
107
- var response = _ref2.response,
108
- error = _ref2.error;
57
+ if (!response.ok) {
58
+ dealWithError({
59
+ response,
60
+ error: json.error
61
+ });
62
+ }
109
63
 
64
+ return json.rows ? json.rows : json;
65
+ }
66
+
67
+ function dealWithError({
68
+ response,
69
+ error
70
+ }) {
110
71
  switch (response.status) {
111
72
  case 400:
112
73
  throw new Error("Bad request. ".concat(error));
@@ -120,19 +81,20 @@ function dealWithError(_ref2) {
120
81
  }
121
82
  }
122
83
 
123
- function getParameters(_ref3) {
124
- var type = _ref3.type,
125
- source = _ref3.source,
126
- geoColumn = _ref3.geoColumn,
127
- columns = _ref3.columns,
128
- schema = _ref3.schema;
129
- var parameters = [(0, _mapsApiCommon.encodeParameter)('client', 'deck-gl-carto')];
84
+ function getParameters({
85
+ type,
86
+ source,
87
+ geoColumn,
88
+ columns,
89
+ schema
90
+ }) {
91
+ const parameters = [(0, _mapsApiCommon.encodeParameter)('client', 'deck-gl-carto')];
130
92
 
131
93
  if (schema) {
132
94
  parameters.push((0, _mapsApiCommon.encodeParameter)('schema', true));
133
95
  }
134
96
 
135
- var sourceName = type === _mapsApiCommon.MAP_TYPES.QUERY ? 'q' : 'name';
97
+ const sourceName = type === _mapsApiCommon.MAP_TYPES.QUERY ? 'q' : 'name';
136
98
  parameters.push((0, _mapsApiCommon.encodeParameter)(sourceName, source));
137
99
 
138
100
  if (type === _mapsApiCommon.MAP_TYPES.TABLE) {
@@ -148,73 +110,51 @@ function getParameters(_ref3) {
148
110
  return parameters.join('&');
149
111
  }
150
112
 
151
- function mapInstantiation(_x2) {
152
- return _mapInstantiation.apply(this, arguments);
153
- }
154
-
155
- function _mapInstantiation() {
156
- _mapInstantiation = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee2(_ref4) {
157
- var type, source, connection, credentials, geoColumn, columns, schema, baseUrl, url, accessToken, format, body;
158
- return _regenerator.default.wrap(function _callee2$(_context2) {
159
- while (1) {
160
- switch (_context2.prev = _context2.next) {
161
- case 0:
162
- type = _ref4.type, source = _ref4.source, connection = _ref4.connection, credentials = _ref4.credentials, geoColumn = _ref4.geoColumn, columns = _ref4.columns, schema = _ref4.schema;
163
- baseUrl = "".concat(credentials.mapsUrl, "/").concat(connection, "/").concat(type);
164
- url = "".concat(baseUrl, "?").concat(getParameters({
165
- type: type,
166
- source: source,
167
- geoColumn: geoColumn,
168
- columns: columns,
169
- schema: schema
170
- }));
171
- accessToken = credentials.accessToken;
172
- format = 'json';
173
-
174
- if (!(url.length > MAX_GET_LENGTH && type === _mapsApiCommon.MAP_TYPES.QUERY)) {
175
- _context2.next = 10;
176
- break;
177
- }
178
-
179
- body = JSON.stringify({
180
- q: source,
181
- client: 'deck-gl-carto'
182
- });
183
- _context2.next = 9;
184
- return request({
185
- method: 'POST',
186
- url: baseUrl,
187
- format: format,
188
- accessToken: accessToken,
189
- body: body
190
- });
191
-
192
- case 9:
193
- return _context2.abrupt("return", _context2.sent);
194
-
195
- case 10:
196
- _context2.next = 12;
197
- return request({
198
- url: url,
199
- format: format,
200
- accessToken: accessToken
201
- });
202
-
203
- case 12:
204
- return _context2.abrupt("return", _context2.sent);
205
-
206
- case 13:
207
- case "end":
208
- return _context2.stop();
209
- }
210
- }
211
- }, _callee2);
113
+ async function mapInstantiation({
114
+ type,
115
+ source,
116
+ connection,
117
+ credentials,
118
+ geoColumn,
119
+ columns,
120
+ schema
121
+ }) {
122
+ const baseUrl = "".concat(credentials.mapsUrl, "/").concat(connection, "/").concat(type);
123
+ const url = "".concat(baseUrl, "?").concat(getParameters({
124
+ type,
125
+ source,
126
+ geoColumn,
127
+ columns,
128
+ schema
212
129
  }));
213
- return _mapInstantiation.apply(this, arguments);
130
+ const {
131
+ accessToken
132
+ } = credentials;
133
+ const format = 'json';
134
+
135
+ if (url.length > MAX_GET_LENGTH && type === _mapsApiCommon.MAP_TYPES.QUERY) {
136
+ const body = JSON.stringify({
137
+ q: source,
138
+ client: 'deck-gl-carto'
139
+ });
140
+ return await request({
141
+ method: 'POST',
142
+ url: baseUrl,
143
+ format,
144
+ accessToken,
145
+ body
146
+ });
147
+ }
148
+
149
+ return await request({
150
+ url,
151
+ format,
152
+ accessToken
153
+ });
214
154
  }
215
155
 
216
156
  function getUrlFromMetadata(metadata, format) {
217
- var m = metadata[format];
157
+ const m = metadata[format];
218
158
 
219
159
  if (m && !m.error && m.url) {
220
160
  return m.url[0];
@@ -223,12 +163,12 @@ function getUrlFromMetadata(metadata, format) {
223
163
  return null;
224
164
  }
225
165
 
226
- function checkGetLayerDataParameters(_ref5) {
227
- var type = _ref5.type,
228
- source = _ref5.source,
229
- connection = _ref5.connection,
230
- localCreds = _ref5.localCreds;
231
-
166
+ function checkFetchLayerDataParameters({
167
+ type,
168
+ source,
169
+ connection,
170
+ localCreds
171
+ }) {
232
172
  _core.log.assert(connection, 'Must define connection');
233
173
 
234
174
  _core.log.assert(type, 'Must define a type');
@@ -242,152 +182,233 @@ function checkGetLayerDataParameters(_ref5) {
242
182
  _core.log.assert(localCreds.accessToken, 'Must define an accessToken');
243
183
  }
244
184
 
245
- function fetchLayerData(_x3) {
246
- return _fetchLayerData.apply(this, arguments);
185
+ async function fetchLayerData({
186
+ type,
187
+ source,
188
+ connection,
189
+ credentials,
190
+ geoColumn,
191
+ columns,
192
+ format,
193
+ schema
194
+ }) {
195
+ const {
196
+ url,
197
+ accessToken,
198
+ mapFormat,
199
+ metadata
200
+ } = await _fetchDataUrl({
201
+ type,
202
+ source,
203
+ connection,
204
+ credentials,
205
+ geoColumn,
206
+ columns,
207
+ format,
208
+ schema
209
+ });
210
+ const data = await request({
211
+ url,
212
+ format: mapFormat,
213
+ accessToken
214
+ });
215
+ const result = {
216
+ data,
217
+ format: mapFormat
218
+ };
219
+
220
+ if (schema) {
221
+ result.schema = metadata.schema;
222
+ }
223
+
224
+ return result;
247
225
  }
248
226
 
249
- function _fetchLayerData() {
250
- _fetchLayerData = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee3(_ref6) {
251
- var type, source, connection, credentials, geoColumn, columns, format, schema, defaultCredentials, localCreds, metadata, url, mapFormat, prioritizedFormats, _i, _prioritizedFormats, f, accessToken, data, result;
252
-
253
- return _regenerator.default.wrap(function _callee3$(_context3) {
254
- while (1) {
255
- switch (_context3.prev = _context3.next) {
256
- case 0:
257
- type = _ref6.type, source = _ref6.source, connection = _ref6.connection, credentials = _ref6.credentials, geoColumn = _ref6.geoColumn, columns = _ref6.columns, format = _ref6.format, schema = _ref6.schema;
258
- defaultCredentials = (0, _config.getDefaultCredentials)();
259
- localCreds = _objectSpread(_objectSpread({}, defaultCredentials.apiVersion === _mapsApiCommon.API_VERSIONS.V3 && defaultCredentials), credentials);
260
- checkGetLayerDataParameters({
261
- type: type,
262
- source: source,
263
- connection: connection,
264
- localCreds: localCreds
265
- });
266
-
267
- if (!localCreds.mapsUrl) {
268
- localCreds.mapsUrl = (0, _config.buildMapsUrlFromBase)(localCreds.apiBaseUrl);
269
- }
270
-
271
- _context3.next = 7;
272
- return mapInstantiation({
273
- type: type,
274
- source: source,
275
- connection: connection,
276
- credentials: localCreds,
277
- geoColumn: geoColumn,
278
- columns: columns,
279
- schema: schema
280
- });
281
-
282
- case 7:
283
- metadata = _context3.sent;
284
-
285
- if (!format) {
286
- _context3.next = 14;
287
- break;
288
- }
289
-
290
- mapFormat = format;
291
- url = getUrlFromMetadata(metadata, format);
292
-
293
- _core.log.assert(url, "Format ".concat(format, " not available"));
294
-
295
- _context3.next = 25;
296
- break;
297
-
298
- case 14:
299
- prioritizedFormats = [_mapsApiCommon.FORMATS.GEOJSON, _mapsApiCommon.FORMATS.NDJSON, _mapsApiCommon.FORMATS.TILEJSON];
300
- _i = 0, _prioritizedFormats = prioritizedFormats;
301
-
302
- case 16:
303
- if (!(_i < _prioritizedFormats.length)) {
304
- _context3.next = 25;
305
- break;
306
- }
307
-
308
- f = _prioritizedFormats[_i];
309
- url = getUrlFromMetadata(metadata, f);
310
-
311
- if (!url) {
312
- _context3.next = 22;
313
- break;
314
- }
315
-
316
- mapFormat = f;
317
- return _context3.abrupt("break", 25);
318
-
319
- case 22:
320
- _i++;
321
- _context3.next = 16;
322
- break;
323
-
324
- case 25:
325
- accessToken = localCreds.accessToken;
326
- _context3.next = 28;
327
- return request({
328
- url: url,
329
- format: mapFormat,
330
- accessToken: accessToken
331
- });
332
-
333
- case 28:
334
- data = _context3.sent;
335
- result = {
336
- data: data,
337
- format: mapFormat
338
- };
339
-
340
- if (schema) {
341
- result.schema = metadata.schema;
342
- }
343
-
344
- return _context3.abrupt("return", result);
345
-
346
- case 32:
347
- case "end":
348
- return _context3.stop();
349
- }
227
+ async function _fetchDataUrl({
228
+ type,
229
+ source,
230
+ connection,
231
+ credentials,
232
+ geoColumn,
233
+ columns,
234
+ format,
235
+ schema
236
+ }) {
237
+ const defaultCredentials = (0, _config.getDefaultCredentials)();
238
+ const localCreds = { ...(defaultCredentials.apiVersion === _mapsApiCommon.API_VERSIONS.V3 && defaultCredentials),
239
+ ...credentials
240
+ };
241
+ checkFetchLayerDataParameters({
242
+ type,
243
+ source,
244
+ connection,
245
+ localCreds
246
+ });
247
+
248
+ if (!localCreds.mapsUrl) {
249
+ localCreds.mapsUrl = (0, _config.buildMapsUrlFromBase)(localCreds.apiBaseUrl);
250
+ }
251
+
252
+ const metadata = await mapInstantiation({
253
+ type,
254
+ source,
255
+ connection,
256
+ credentials: localCreds,
257
+ geoColumn,
258
+ columns,
259
+ schema
260
+ });
261
+ let url;
262
+ let mapFormat;
263
+
264
+ if (format) {
265
+ mapFormat = format;
266
+ url = getUrlFromMetadata(metadata, format);
267
+
268
+ _core.log.assert(url, "Format ".concat(format, " not available"));
269
+ } else {
270
+ const prioritizedFormats = [_mapsApiCommon.FORMATS.GEOJSON, _mapsApiCommon.FORMATS.JSON, _mapsApiCommon.FORMATS.NDJSON, _mapsApiCommon.FORMATS.TILEJSON];
271
+
272
+ for (const f of prioritizedFormats) {
273
+ url = getUrlFromMetadata(metadata, f);
274
+
275
+ if (url) {
276
+ mapFormat = f;
277
+ break;
350
278
  }
351
- }, _callee3);
352
- }));
353
- return _fetchLayerData.apply(this, arguments);
279
+ }
280
+ }
281
+
282
+ const {
283
+ accessToken
284
+ } = localCreds;
285
+ return {
286
+ url,
287
+ accessToken,
288
+ mapFormat,
289
+ metadata
290
+ };
291
+ }
292
+
293
+ async function getData({
294
+ type,
295
+ source,
296
+ connection,
297
+ credentials,
298
+ geoColumn,
299
+ columns,
300
+ format
301
+ }) {
302
+ _core.log.deprecated('getData', 'fetchLayerData')();
303
+
304
+ const layerData = await fetchLayerData({
305
+ type,
306
+ source,
307
+ connection,
308
+ credentials,
309
+ geoColumn,
310
+ columns,
311
+ format,
312
+ schema: false
313
+ });
314
+ return layerData.data;
315
+ }
316
+
317
+ async function _fetchMapDataset(dataset, accessToken) {
318
+ const {
319
+ connectionName: connection,
320
+ source,
321
+ type
322
+ } = dataset;
323
+ const {
324
+ url,
325
+ mapFormat
326
+ } = await _fetchDataUrl({
327
+ credentials: {
328
+ accessToken
329
+ },
330
+ connection,
331
+ source,
332
+ type
333
+ });
334
+ const cache = parseInt(new URLSearchParams(url).get('cache'), 10);
335
+
336
+ if (cache && dataset.cache === cache) {
337
+ return false;
338
+ }
339
+
340
+ dataset.cache = cache;
341
+ const data = await request({
342
+ url,
343
+ format: mapFormat,
344
+ accessToken
345
+ });
346
+ dataset.data = data;
347
+ return true;
354
348
  }
355
349
 
356
- function getData(_x4) {
357
- return _getData.apply(this, arguments);
350
+ async function fillInMapDatasets({
351
+ datasets,
352
+ publicToken
353
+ }) {
354
+ const promises = datasets.map(dataset => _fetchMapDataset(dataset, publicToken));
355
+ return await Promise.all(promises);
358
356
  }
359
357
 
360
- function _getData() {
361
- _getData = (0, _asyncToGenerator2.default)(_regenerator.default.mark(function _callee4(_ref7) {
362
- var type, source, connection, credentials, geoColumn, columns, format, layerData;
363
- return _regenerator.default.wrap(function _callee4$(_context4) {
364
- while (1) {
365
- switch (_context4.prev = _context4.next) {
366
- case 0:
367
- type = _ref7.type, source = _ref7.source, connection = _ref7.connection, credentials = _ref7.credentials, geoColumn = _ref7.geoColumn, columns = _ref7.columns, format = _ref7.format;
368
- _context4.next = 3;
369
- return fetchLayerData({
370
- type: type,
371
- source: source,
372
- connection: connection,
373
- credentials: credentials,
374
- geoColumn: geoColumn,
375
- columns: columns,
376
- format: format,
377
- schema: false
378
- });
379
-
380
- case 3:
381
- layerData = _context4.sent;
382
- return _context4.abrupt("return", layerData.data);
383
-
384
- case 5:
385
- case "end":
386
- return _context4.stop();
387
- }
358
+ async function fetchMap({
359
+ cartoMapId,
360
+ credentials,
361
+ autoRefresh,
362
+ onNewData
363
+ }) {
364
+ const defaultCredentials = (0, _config.getDefaultCredentials)();
365
+ const localCreds = { ...(defaultCredentials.apiVersion === _mapsApiCommon.API_VERSIONS.V3 && defaultCredentials),
366
+ ...credentials
367
+ };
368
+
369
+ _core.log.assert(cartoMapId, "Must define CARTO map id: fetchMap({cartoMapId: 'XXXX-XXXX-XXXX'})");
370
+
371
+ _core.log.assert(localCreds.apiVersion === _mapsApiCommon.API_VERSIONS.V3, 'Method only available for v3');
372
+
373
+ _core.log.assert(localCreds.apiBaseUrl, 'Must define apiBaseUrl');
374
+
375
+ if (!localCreds.mapsUrl) {
376
+ localCreds.mapsUrl = (0, _config.buildMapsUrlFromBase)(localCreds.apiBaseUrl);
377
+ }
378
+
379
+ if (autoRefresh || onNewData) {
380
+ _core.log.assert(onNewData, 'Must define `onNewData` when using autoRefresh');
381
+
382
+ _core.log.assert(typeof onNewData === 'function', '`onNewData` must be a function');
383
+
384
+ _core.log.assert(autoRefresh > 0, '`autoRefresh` must be a positive number');
385
+ }
386
+
387
+ const url = "".concat(localCreds.mapsUrl, "/public/").concat(cartoMapId);
388
+ const map = await request({
389
+ url
390
+ });
391
+ let stopAutoRefresh;
392
+
393
+ if (autoRefresh) {
394
+ const intervalId = setInterval(async () => {
395
+ const changed = await fillInMapDatasets(map);
396
+
397
+ if (changed.some(v => v === true)) {
398
+ onNewData((0, _parseMap.parseMap)(map));
388
399
  }
389
- }, _callee4);
390
- }));
391
- return _getData.apply(this, arguments);
400
+ }, autoRefresh * 1000);
401
+
402
+ stopAutoRefresh = () => {
403
+ clearInterval(intervalId);
404
+ };
405
+ }
406
+
407
+ await fillInMapDatasets(map);
408
+ return { ...(0, _parseMap.parseMap)(map),
409
+ ...{
410
+ stopAutoRefresh
411
+ }
412
+ };
392
413
  }
393
414
  //# sourceMappingURL=maps-v3-client.js.map