@elastic/ems-client 8.3.3 → 8.5.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,28 +3,29 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.EMSClient = void 0;
7
-
6
+ exports.LATEST_API_URL_PATH = exports.EMSClient = void 0;
8
7
  var _lodash = _interopRequireDefault(require("lodash"));
9
-
10
8
  var _tms_service = require("./tms_service");
11
-
12
9
  var _file_layer = require("./file_layer");
13
-
14
- var _semver = _interopRequireDefault(require("semver"));
15
-
10
+ var _coerce = _interopRequireDefault(require("semver/functions/coerce"));
11
+ var _valid = _interopRequireDefault(require("semver/functions/valid"));
12
+ var _major = _interopRequireDefault(require("semver/functions/major"));
13
+ var _minor = _interopRequireDefault(require("semver/functions/minor"));
16
14
  var _url = require("url");
17
-
18
15
  var _utils = require("./utils");
19
-
20
16
  var _lruCache = _interopRequireDefault(require("lru-cache"));
21
-
22
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
25
-
26
- const DEFAULT_EMS_VERSION = '8.3';
27
-
18
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
20
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /*
21
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
22
+ * or more contributor license agreements. Licensed under the Elastic License
23
+ * 2.0; you may not use this file except in compliance with the Elastic License
24
+ * 2.0.
25
+ */
26
+ const REST_API_REGEX = /\d{4}-\d{2}-\d{2}/;
27
+ const LATEST_API_URL_PATH = 'latest';
28
+ exports.LATEST_API_URL_PATH = LATEST_API_URL_PATH;
28
29
  /**
29
30
  * plugins cannot have upstream dependencies on core/*-kibana.
30
31
  * Work-around by copy-pasting modifyUrl routine here.
@@ -32,13 +33,14 @@ const DEFAULT_EMS_VERSION = '8.3';
32
33
  * @param block
33
34
  */
34
35
  function modifyUrlLocal(url, block) {
35
- const parsed = (0, _url.parse)(url, true); // copy over the most specific version of each
36
+ const parsed = (0, _url.parse)(url, true);
37
+
38
+ // copy over the most specific version of each
36
39
  // property. By default, the parsed url includes
37
40
  // several conflicting properties (like path and
38
41
  // pathname + search, or search and query) and keeping
39
42
  // track of which property is actually used when they
40
43
  // are formatted is harder than necessary
41
-
42
44
  const meaningfulParts = {
43
45
  protocol: parsed.protocol,
44
46
  slashes: parsed.slashes,
@@ -48,10 +50,12 @@ function modifyUrlLocal(url, block) {
48
50
  pathname: parsed.pathname,
49
51
  query: parsed.query || {},
50
52
  hash: parsed.hash
51
- }; // the block modifies the meaningfulParts object, or returns a new one
53
+ };
52
54
 
53
- const modifiedParts = block(meaningfulParts) || meaningfulParts; // format the modified/replaced meaningfulParts back into a url
55
+ // the block modifies the meaningfulParts object, or returns a new one
56
+ const modifiedParts = block(meaningfulParts) || meaningfulParts;
54
57
 
58
+ // format the modified/replaced meaningfulParts back into a url
55
59
  return (0, _url.format)({
56
60
  protocol: modifiedParts.protocol,
57
61
  slashes: modifiedParts.slashes,
@@ -63,65 +67,43 @@ function modifyUrlLocal(url, block) {
63
67
  hash: modifiedParts.hash
64
68
  });
65
69
  }
66
-
67
70
  const extendUrl = (url, props) => modifyUrlLocal(url, parsed => _lodash.default.merge(parsed, props));
71
+
68
72
  /**
69
73
  * Unescape a url template that was escaped by encodeURI() so leaflet
70
74
  * will be able to correctly locate the variables in the template
71
75
  * @param {String} url
72
76
  * @return {String}
73
77
  */
74
-
75
-
76
78
  const unescapeTemplateVars = url => {
77
79
  const ENCODED_TEMPLATE_VARS_RE = /%7B(\w+?)%7D/g;
78
80
  return url.replace(ENCODED_TEMPLATE_VARS_RE, (total, varName) => `{${varName}}`);
79
81
  };
80
-
81
82
  //this is not the default locale from Kibana, but the default locale supported by the Elastic Maps Service
82
83
  const DEFAULT_LANGUAGE = 'en';
83
-
84
84
  class EMSClient {
85
- /**
86
- * these methods are assigned outside the constructor
87
- */
88
85
  constructor(config) {
89
86
  _defineProperty(this, "EMS_LOAD_TIMEOUT", 32000);
90
-
91
87
  _defineProperty(this, "_queryParams", void 0);
92
-
93
88
  _defineProperty(this, "_appVersion", void 0);
94
-
95
89
  _defineProperty(this, "_fetchFunction", void 0);
96
-
97
90
  _defineProperty(this, "_sanitizer", void 0);
98
-
99
- _defineProperty(this, "_manifestServiceUrl", void 0);
100
-
101
91
  _defineProperty(this, "_fileApiUrl", void 0);
102
-
103
92
  _defineProperty(this, "_tileApiUrl", void 0);
104
-
105
93
  _defineProperty(this, "_emsVersion", void 0);
106
-
107
94
  _defineProperty(this, "_emsLandingPageUrl", void 0);
108
-
109
95
  _defineProperty(this, "_language", void 0);
110
-
111
96
  _defineProperty(this, "_proxyPath", void 0);
112
-
113
97
  _defineProperty(this, "_cache", void 0);
114
-
98
+ _defineProperty(this, "_isRestApi", false);
99
+ /**
100
+ * these methods are assigned outside the constructor
101
+ */
115
102
  _defineProperty(this, "_getMainCatalog", void 0);
116
-
117
103
  _defineProperty(this, "_getDefaultTMSCatalog", void 0);
118
-
119
104
  _defineProperty(this, "_getDefaultFileCatalog", void 0);
120
-
121
105
  _defineProperty(this, "_loadTMSServices", void 0);
122
-
123
106
  _defineProperty(this, "_loadFileLayers", void 0);
124
-
125
107
  // Remove kbnVersion in 8.0
126
108
  if ('kbnVersion' in config) {
127
109
  console.warn('The "kbnVersion" parameter for ems-client is deprecated. Please use "appVersion" instead.');
@@ -129,17 +111,16 @@ class EMSClient {
129
111
  } else {
130
112
  this._appVersion = config.appVersion;
131
113
  }
132
-
133
114
  this._queryParams = {
134
115
  elastic_tile_service_tos: 'agree',
135
116
  my_app_name: config.appName || 'kibana',
136
117
  my_app_version: this._appVersion
137
118
  };
138
119
  this._sanitizer = config.htmlSanitizer ? config.htmlSanitizer : x => x;
139
- this._manifestServiceUrl = config.manifestServiceUrl;
140
120
  this._tileApiUrl = config.tileApiUrl;
141
121
  this._fileApiUrl = config.fileApiUrl;
142
122
  this._emsVersion = this._getEmsVersion(config.emsVersion);
123
+ this._isRestApi = this._emsVersion == config.emsVersion;
143
124
  this._emsLandingPageUrl = config.landingPageUrl || '';
144
125
  this._language = config.language || DEFAULT_LANGUAGE;
145
126
  this._fetchFunction = config.fetchFunction;
@@ -147,41 +128,33 @@ class EMSClient {
147
128
  this._cache = new _lruCache.default({
148
129
  max: config.cacheSize || 10
149
130
  });
150
-
151
131
  this._invalidateSettings();
152
132
  }
153
-
154
133
  getDefaultLocale() {
155
134
  return DEFAULT_LANGUAGE;
156
135
  }
157
-
158
136
  getLocale() {
159
137
  return this._language;
160
138
  }
161
-
162
139
  getValueInLanguage(i18nObject) {
163
140
  if (!i18nObject) {
164
141
  return '';
165
142
  }
166
-
167
143
  return i18nObject[this._language] ? i18nObject[this._language] : i18nObject[DEFAULT_LANGUAGE];
168
144
  }
145
+
169
146
  /**
170
147
  * this internal method is overridden by the tests to simulate custom manifest.
171
148
  */
172
-
173
-
174
149
  async getManifest(endpointUrl) {
175
150
  try {
176
151
  const url = extendUrl(endpointUrl, {
177
152
  query: this._queryParams
178
153
  });
179
154
  const response = await this._fetchWithTimeout(url);
180
-
181
155
  if (!response.ok) {
182
156
  throw new Error(response.statusText);
183
157
  }
184
-
185
158
  return response ? await response.json() : null;
186
159
  } catch (e) {
187
160
  if (e instanceof Error) {
@@ -191,116 +164,93 @@ class EMSClient {
191
164
  }
192
165
  }
193
166
  }
167
+
194
168
  /**
195
169
  * Add optional query-parameters to all requests
196
170
  *
197
171
  * @param additionalQueryParams
198
172
  */
199
-
200
-
201
173
  addQueryParams(additionalQueryParams) {
202
174
  for (const key in additionalQueryParams) {
203
175
  if (additionalQueryParams.hasOwnProperty(key)) {
204
176
  if (additionalQueryParams[key] !== this._queryParams[key]) {
205
177
  //changes detected.
206
178
  this._queryParams = _lodash.default.assign({}, this._queryParams, additionalQueryParams);
207
-
208
179
  this._invalidateSettings();
209
-
210
180
  break;
211
181
  }
212
182
  }
213
183
  }
214
184
  }
215
-
216
185
  async getMainManifest() {
217
186
  return await this._getMainCatalog();
218
187
  }
219
-
220
188
  async getDefaultFileManifest() {
221
189
  return await this._getDefaultFileCatalog();
222
190
  }
223
-
224
191
  async getDefaultTMSManifest() {
225
192
  return await this._getDefaultTMSCatalog();
226
193
  }
227
-
228
194
  async getFileLayers() {
229
195
  return await this._loadFileLayers();
230
196
  }
231
-
232
197
  async getTMSServices() {
233
198
  return await this._loadTMSServices();
234
199
  }
235
-
236
200
  cacheGeoJson(layerId, geoJson) {
237
201
  this._cache.set(layerId, geoJson);
238
-
239
202
  return;
240
203
  }
241
-
242
204
  getCachedGeoJson(layerId) {
243
205
  return this._cache.get(layerId);
244
206
  }
245
-
246
207
  getTileApiUrl() {
247
208
  return this._tileApiUrl;
248
209
  }
249
-
250
210
  getFileApiUrl() {
251
211
  return this._fileApiUrl;
252
212
  }
253
-
254
213
  getLandingPageUrl() {
255
214
  return this._emsLandingPageUrl;
256
215
  }
257
-
258
216
  sanitizeHtml(html) {
259
217
  return this._sanitizer(html);
260
218
  }
261
-
262
219
  extendUrlWithParams(url) {
263
220
  return unescapeTemplateVars(extendUrl(url, {
264
221
  query: this._queryParams
265
222
  }));
266
223
  }
267
-
268
224
  async findFileLayerById(id) {
269
225
  const fileLayers = await this.getFileLayers();
270
-
271
226
  for (let i = 0; i < fileLayers.length; i++) {
272
227
  if (fileLayers[i].hasId(id)) {
273
228
  return fileLayers[i];
274
229
  }
275
230
  }
276
231
  }
277
-
278
232
  async findTMSServiceById(id) {
279
233
  const tmsServices = await this.getTMSServices();
280
-
281
234
  for (let i = 0; i < tmsServices.length; i++) {
282
235
  if (tmsServices[i].hasId(id)) {
283
236
  return tmsServices[i];
284
237
  }
285
238
  }
286
239
  }
287
-
288
240
  _getEmsVersion(version) {
289
- const userVersion = _semver.default.valid(_semver.default.coerce(version));
290
-
291
- const semverVersion = userVersion ? userVersion : _semver.default.coerce(DEFAULT_EMS_VERSION);
292
-
241
+ if (version?.match(REST_API_REGEX)) {
242
+ return version;
243
+ }
244
+ const semverVersion = (0, _valid.default)((0, _coerce.default)(version));
293
245
  if (semverVersion) {
294
- return `v${_semver.default.major(semverVersion)}.${_semver.default.minor(semverVersion)}`;
246
+ return `v${(0, _major.default)(semverVersion)}.${(0, _minor.default)(semverVersion)}`;
295
247
  } else {
296
248
  throw new Error(`Invalid version: ${version}`);
297
249
  }
298
250
  }
299
-
300
251
  _fetchWithTimeout(url) {
301
252
  return new Promise((resolve, reject) => {
302
253
  const timer = setTimeout(() => reject(new Error(`Request to ${url} timed out`)), this.EMS_LOAD_TIMEOUT);
303
-
304
254
  this._fetchFunction(url).then(response => {
305
255
  clearTimeout(timer);
306
256
  resolve(response);
@@ -310,70 +260,52 @@ class EMSClient {
310
260
  });
311
261
  });
312
262
  }
313
-
314
- async _getManifestWithParams(url) {
315
- const extendedUrl = this.extendUrlWithParams(url);
316
- return await this.getManifest(extendedUrl);
317
- }
318
-
319
263
  _invalidateSettings() {
320
264
  this._cache.reset();
321
-
322
265
  this._getMainCatalog = _lodash.default.once(async () => {
323
- // Preserve manifestServiceUrl parameter for backwards compatibility with EMS v7.2
324
- if (this._manifestServiceUrl) {
325
- console.warn(`The "manifestServiceUrl" parameter is deprecated in v7.6.0.
326
- Consider using "tileApiUrl" and "fileApiUrl" instead.`);
327
- return await this._getManifestWithParams(this._manifestServiceUrl);
328
- } else {
329
- const services = [];
330
-
331
- if (this._tileApiUrl) {
332
- services.push({
333
- type: 'tms',
334
- manifest: (0, _utils.toAbsoluteUrl)(this._tileApiUrl, `${this._emsVersion}/manifest`)
335
- });
336
- }
337
-
338
- if (this._fileApiUrl) {
339
- services.push({
340
- type: 'file',
341
- manifest: (0, _utils.toAbsoluteUrl)(this._fileApiUrl, `${this._emsVersion}/manifest`)
342
- });
343
- }
344
-
345
- return {
346
- services: services
347
- };
266
+ const services = [];
267
+ if (this._tileApiUrl) {
268
+ const version = this._isRestApi ? LATEST_API_URL_PATH : this._emsVersion;
269
+ services.push({
270
+ type: 'tms',
271
+ manifest: (0, _utils.toAbsoluteUrl)(this._tileApiUrl, `${version}/manifest`)
272
+ });
273
+ }
274
+ if (this._fileApiUrl) {
275
+ const version = this._isRestApi ? LATEST_API_URL_PATH : this._emsVersion;
276
+ services.push({
277
+ type: 'file',
278
+ manifest: (0, _utils.toAbsoluteUrl)(this._fileApiUrl, `${version}/manifest`)
279
+ });
348
280
  }
281
+ return {
282
+ services: services
283
+ };
349
284
  });
350
285
  this._getDefaultTMSCatalog = _lodash.default.once(async () => {
351
286
  const catalogue = await this._getMainCatalog();
352
287
  const firstService = catalogue.services.find(service => service.type === 'tms');
353
-
354
288
  if (!firstService) {
355
289
  return {
356
290
  services: []
357
291
  };
358
292
  }
359
-
360
293
  const url = this._proxyPath + firstService.manifest;
361
294
  return await this.getManifest(url);
362
295
  });
363
296
  this._getDefaultFileCatalog = _lodash.default.once(async () => {
364
297
  const catalogue = await this._getMainCatalog();
365
298
  const firstService = catalogue.services.find(service => service.type === 'file');
366
-
367
299
  if (!firstService) {
368
300
  return {
369
301
  layers: []
370
302
  };
371
303
  }
372
-
373
304
  const url = this._proxyPath + firstService.manifest;
374
305
  return await this.getManifest(url);
375
- }); //Cache the actual instances of TMSService as these in turn cache sub-manifests for the style-files
306
+ });
376
307
 
308
+ //Cache the actual instances of TMSService as these in turn cache sub-manifests for the style-files
377
309
  this._loadTMSServices = _lodash.default.once(async () => {
378
310
  const tmsManifest = await this._getDefaultTMSCatalog();
379
311
  return tmsManifest.services.map(serviceConfig => new _tms_service.TMSService(serviceConfig, this, this._proxyPath));
@@ -383,7 +315,5 @@ class EMSClient {
383
315
  return fileManifest.layers.map(layerConfig => new _file_layer.FileLayer(layerConfig, this, this._proxyPath));
384
316
  });
385
317
  }
386
-
387
318
  }
388
-
389
319
  exports.EMSClient = EMSClient;
@@ -4,19 +4,23 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.AbstractEmsService = void 0;
7
-
8
7
  var _utils = require("./utils");
9
-
10
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
11
-
8
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
10
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /*
11
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
12
+ * or more contributor license agreements. Licensed under the Elastic License
13
+ * 2.0; you may not use this file except in compliance with the Elastic License
14
+ * 2.0.
15
+ */
12
16
  class AbstractEmsService {
13
17
  constructor(config, emsClient, proxyPath) {
14
18
  _defineProperty(this, "_emsClient", void 0);
15
-
16
19
  _defineProperty(this, "_config", void 0);
17
-
18
20
  _defineProperty(this, "_proxyPath", void 0);
19
-
21
+ /**
22
+ * Checks if url is absolute. If not, prepend the basePath.
23
+ */
20
24
  _defineProperty(this, "_getAbsoluteUrl", url => {
21
25
  if (/^https?:\/\//.test(url)) {
22
26
  return url;
@@ -24,41 +28,27 @@ class AbstractEmsService {
24
28
  return (0, _utils.toAbsoluteUrl)(this.getApiUrl(), url);
25
29
  }
26
30
  });
27
-
28
31
  this._config = config;
29
32
  this._emsClient = emsClient;
30
33
  this._proxyPath = proxyPath;
31
34
  }
32
-
33
35
  getAttributions() {
34
36
  return this._config.attribution.map(attribution => {
35
37
  const url = this._emsClient.getValueInLanguage(attribution.url);
36
-
37
38
  const label = this._emsClient.getValueInLanguage(attribution.label);
38
-
39
39
  return {
40
40
  url: url,
41
41
  label: label
42
42
  };
43
43
  });
44
44
  }
45
-
46
45
  getMarkdownAttribution() {
47
46
  const attributions = this._config.attribution.map(attribution => {
48
47
  const url = this._emsClient.getValueInLanguage(attribution.url);
49
-
50
48
  const label = this._emsClient.getValueInLanguage(attribution.label);
51
-
52
49
  return `[${label}](${url})`;
53
50
  });
54
-
55
51
  return attributions.join('|');
56
52
  }
57
- /**
58
- * Checks if url is absolute. If not, prepend the basePath.
59
- */
60
-
61
-
62
53
  }
63
-
64
54
  exports.AbstractEmsService = AbstractEmsService;