@maptiler/client 3.1.0-rc.5 → 4.0.0-rc.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.
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const DEFAULT_API_HOST = "api.maptiler.com";
4
+ const EU_API_HOST = "api.maptiler.eu";
3
5
  function tryGettingFetch() {
4
6
  if (typeof self !== "undefined") {
5
7
  return fetch.bind(self);
@@ -20,6 +22,10 @@ class ClientConfig {
20
22
  * will be automatically resolved.
21
23
  */
22
24
  this._fetch = tryGettingFetch();
25
+ /**
26
+ * Whether to use the EU-based `api.maptiler.eu` host instead of the default `api.maptiler.com`
27
+ */
28
+ this._useEuEndpoints = false;
23
29
  /**
24
30
  * Number of tiles to keep in cache
25
31
  */
@@ -49,6 +55,32 @@ class ClientConfig {
49
55
  get fetch() {
50
56
  return this._fetch;
51
57
  }
58
+ /**
59
+ * Switch between the default `api.maptiler.com` host and the EU-based `api.maptiler.eu` host.
60
+ * Set to `false` to switch back to the default.
61
+ */
62
+ set useEuEndpoints(value) {
63
+ this._useEuEndpoints = value;
64
+ }
65
+ /**
66
+ * Whether the EU-based `api.maptiler.eu` host is currently in use instead of the default `api.maptiler.com`
67
+ */
68
+ get useEuEndpoints() {
69
+ return this._useEuEndpoints;
70
+ }
71
+ /**
72
+ * The host currently used for the MapTiler API requests (`api.maptiler.com` or `api.maptiler.eu`)
73
+ */
74
+ get apiHost() {
75
+ return this._useEuEndpoints ? EU_API_HOST : DEFAULT_API_HOST;
76
+ }
77
+ /**
78
+ * The base URL currently used for the MapTiler API requests
79
+ * (`https://api.maptiler.com/` or `https://api.maptiler.eu/`)
80
+ */
81
+ get apiURL() {
82
+ return `https://${this.apiHost}/`;
83
+ }
52
84
  }
53
85
  const config = new ClientConfig();
54
86
 
@@ -1018,12 +1050,6 @@ async function callFetch(resource, options = {}) {
1018
1050
  return config.fetch(resource, options);
1019
1051
  }
1020
1052
 
1021
- const defaults = {
1022
- maptilerApiURL: "https://api.maptiler.com/",
1023
- mapStyle: "streets-v2"
1024
- };
1025
- Object.freeze(defaults);
1026
-
1027
1053
  class ServiceError extends Error {
1028
1054
  constructor(res, customMessage = "") {
1029
1055
  super(
@@ -1096,7 +1122,7 @@ async function forward(query, options = {}) {
1096
1122
  }
1097
1123
  const endpoint = new URL(
1098
1124
  `geocoding/${encodeURIComponent(query)}.json`,
1099
- defaults.maptilerApiURL
1125
+ config.apiURL
1100
1126
  );
1101
1127
  addBaseGeocodingOptions(endpoint.searchParams, options);
1102
1128
  addLanguageGeocodingOptions(endpoint.searchParams, options);
@@ -1114,7 +1140,7 @@ async function reverse(position, options = {}) {
1114
1140
  }
1115
1141
  const endpoint = new URL(
1116
1142
  `geocoding/${position[0]},${position[1]}.json`,
1117
- defaults.maptilerApiURL
1143
+ config.apiURL
1118
1144
  );
1119
1145
  addBaseGeocodingOptions(endpoint.searchParams, options);
1120
1146
  addLanguageGeocodingOptions(endpoint.searchParams, options);
@@ -1126,7 +1152,7 @@ async function reverse(position, options = {}) {
1126
1152
  return await res.json();
1127
1153
  }
1128
1154
  async function byId(id, options = {}) {
1129
- const endpoint = new URL(`geocoding/${id}.json`, defaults.maptilerApiURL);
1155
+ const endpoint = new URL(`geocoding/${id}.json`, config.apiURL);
1130
1156
  addBaseGeocodingOptions(endpoint.searchParams, options);
1131
1157
  addLanguageGeocodingOptions(endpoint.searchParams, options);
1132
1158
  const res = await callFetch(endpoint.toString());
@@ -1140,10 +1166,7 @@ async function batch$1(queries, options = {}) {
1140
1166
  return [];
1141
1167
  }
1142
1168
  const joinedQuery = queries.map((query) => encodeURIComponent(query)).join(";");
1143
- const endpoint = new URL(
1144
- `geocoding/${joinedQuery}.json`,
1145
- defaults.maptilerApiURL
1146
- );
1169
+ const endpoint = new URL(`geocoding/${joinedQuery}.json`, config.apiURL);
1147
1170
  addBaseGeocodingOptions(endpoint.searchParams, options);
1148
1171
  addLanguageGeocodingOptions(endpoint.searchParams, options);
1149
1172
  addCommonForwardAndReverseGeocodingOptions(endpoint.searchParams, options);
@@ -1166,7 +1189,7 @@ const customMessages$3 = {
1166
1189
  403: "Key is missing, invalid or restricted"
1167
1190
  };
1168
1191
  async function info(options = {}) {
1169
- const endpoint = new URL(`geolocation/ip.json`, defaults.maptilerApiURL);
1192
+ const endpoint = new URL(`geolocation/ip.json`, config.apiURL);
1170
1193
  endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
1171
1194
  if ("elevation" in options) {
1172
1195
  endpoint.searchParams.set(
@@ -1347,7 +1370,7 @@ async function processDirectionsResponse(res) {
1347
1370
  }
1348
1371
  async function directionsPost(body, options = {}) {
1349
1372
  const { fetchExtras } = options;
1350
- const url = new URL("routing/v1/directions", defaults.maptilerApiURL);
1373
+ const url = new URL("routing/v1/directions", config.apiURL);
1351
1374
  addRoutingOptions(url.searchParams, options);
1352
1375
  const headers = new Headers(fetchExtras?.headers);
1353
1376
  headers.set("content-type", "application/json");
@@ -1361,7 +1384,7 @@ async function directionsPost(body, options = {}) {
1361
1384
  }
1362
1385
  async function directionsGet(req, options = {}) {
1363
1386
  const { fetchExtras } = options;
1364
- const url = new URL("routing/v1/directions", defaults.maptilerApiURL);
1387
+ const url = new URL("routing/v1/directions", config.apiURL);
1365
1388
  const search = url.searchParams;
1366
1389
  search.set("profile", req.profile);
1367
1390
  if (req.id) search.set("id", req.id);
@@ -1396,10 +1419,7 @@ async function search(query, options = {}) {
1396
1419
  if (typeof query !== "string" || query.trim().length === 0) {
1397
1420
  throw new Error("The query must be a non-empty string");
1398
1421
  }
1399
- const endpoint = new URL(
1400
- `coordinates/search/${query}.json`,
1401
- defaults.maptilerApiURL
1402
- );
1422
+ const endpoint = new URL(`coordinates/search/${query}.json`, config.apiURL);
1403
1423
  endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
1404
1424
  if ("limit" in options) {
1405
1425
  endpoint.searchParams.set("limit", options.limit.toString());
@@ -1428,7 +1448,7 @@ async function transform(positions, options = {}) {
1428
1448
  const coordinatesStr = (Array.isArray(positions[0]) ? positions : [positions]).map((coord) => `${coord[0]},${coord[1]}`).join(";");
1429
1449
  const endpoint = new URL(
1430
1450
  `coordinates/transform/${coordinatesStr}.json`,
1431
- defaults.maptilerApiURL
1451
+ config.apiURL
1432
1452
  );
1433
1453
  endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
1434
1454
  if ("sourceCrs" in options) {
@@ -1468,7 +1488,7 @@ async function get(dataId, options = {}) {
1468
1488
  }
1469
1489
  const endpoint = new URL(
1470
1490
  `data/${encodeURIComponent(dataId)}/features.json`,
1471
- defaults.maptilerApiURL
1491
+ config.apiURL
1472
1492
  );
1473
1493
  endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
1474
1494
  const urlWithParams = endpoint.toString();
@@ -1494,9 +1514,9 @@ function expandMapStyle(style) {
1494
1514
  if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
1495
1515
  expandedStyle = trimmed;
1496
1516
  } else if ((match = maptilerDomainRegex.exec(trimmed)) !== null) {
1497
- expandedStyle = `https://api.maptiler.com/maps/${match[1]}/style.json`;
1517
+ expandedStyle = `${config.apiURL}maps/${match[1]}/style.json`;
1498
1518
  } else {
1499
- expandedStyle = `https://api.maptiler.com/maps/${trimmed}/style.json`;
1519
+ expandedStyle = `${config.apiURL}maps/${trimmed}/style.json`;
1500
1520
  }
1501
1521
  return expandedStyle;
1502
1522
  }
@@ -2718,7 +2738,7 @@ function centered(center, zoom, options = {}) {
2718
2738
  }
2719
2739
  const endpoint = new URL(
2720
2740
  `maps/${encodeURIComponent(style)}/static/${center[0]},${center[1]},${zoom}/${width}x${height}${scale}.${format}`,
2721
- defaults.maptilerApiURL
2741
+ config.apiURL
2722
2742
  );
2723
2743
  if ("attribution" in options) {
2724
2744
  endpoint.searchParams.set("attribution", options.attribution.toString());
@@ -2767,7 +2787,7 @@ function bounded(boundingBox, options = {}) {
2767
2787
  }
2768
2788
  const endpoint = new URL(
2769
2789
  `maps/${encodeURIComponent(style)}/static/${boundingBox[0]},${boundingBox[1]},${boundingBox[2]},${boundingBox[3]}/${width}x${height}${scale}.${format}`,
2770
- defaults.maptilerApiURL
2790
+ config.apiURL
2771
2791
  );
2772
2792
  if ("attribution" in options) {
2773
2793
  endpoint.searchParams.set("attribution", options.attribution.toString());
@@ -2826,7 +2846,7 @@ function automatic(options = {}) {
2826
2846
  `maps/${encodeURIComponent(
2827
2847
  style
2828
2848
  )}/static/auto/${width}x${height}${scale}.${format}`,
2829
- defaults.maptilerApiURL
2849
+ config.apiURL
2830
2850
  );
2831
2851
  if ("attribution" in options) {
2832
2852
  endpoint.searchParams.set("attribution", options.attribution.toString());
@@ -3317,6 +3337,7 @@ const TERRAIN_TILESET = "terrain-rgb-v2";
3317
3337
  const API_BATCH_SIZE = 50;
3318
3338
  const API_WARN_SIZE = 1e3;
3319
3339
  let terrainTileJson = null;
3340
+ let terrainTileJsonHost = null;
3320
3341
  const customMessages = {
3321
3342
  403: "Key is missing, invalid or restricted"
3322
3343
  };
@@ -3332,10 +3353,7 @@ async function computeOnServer(positions, apiKey) {
3332
3353
  const startPos = part * API_BATCH_SIZE;
3333
3354
  const batch2 = positions.slice(startPos, startPos + API_BATCH_SIZE);
3334
3355
  const batchEncoded = batch2.map((pos) => pos.join(",")).join(";");
3335
- const endpoint = new URL(
3336
- `elevation/${batchEncoded}.json`,
3337
- defaults.maptilerApiURL
3338
- );
3356
+ const endpoint = new URL(`elevation/${batchEncoded}.json`, config.apiURL);
3339
3357
  endpoint.searchParams.set("key", apiKey);
3340
3358
  return callFetch(endpoint.toString());
3341
3359
  }
@@ -3359,16 +3377,17 @@ async function computeOnServer(positions, apiKey) {
3359
3377
  return jsons.flat();
3360
3378
  }
3361
3379
  async function computeOnClient(positions, apiKey, zoom) {
3362
- if (!terrainTileJson) {
3380
+ if (!terrainTileJson || terrainTileJsonHost !== config.apiHost) {
3363
3381
  const endpoint = new URL(
3364
3382
  `tiles/${TERRAIN_TILESET}/tiles.json`,
3365
- defaults.maptilerApiURL
3383
+ config.apiURL
3366
3384
  );
3367
3385
  endpoint.searchParams.set("key", apiKey);
3368
3386
  const urlWithParams = endpoint.toString();
3369
3387
  const res = await callFetch(urlWithParams);
3370
3388
  if (res.ok) {
3371
3389
  terrainTileJson = await res.json();
3390
+ terrainTileJsonHost = config.apiHost;
3372
3391
  } else {
3373
3392
  throw new ServiceError(res, customMessages[res.status] ?? "");
3374
3393
  }