@maptiler/sdk 1.0.12 → 1.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maptiler/sdk",
3
- "version": "1.0.12",
3
+ "version": "1.1.0",
4
4
  "description": "The Javascript & TypeScript map SDK tailored for MapTiler Cloud",
5
5
  "module": "dist/maptiler-sdk.mjs",
6
6
  "types": "dist/maptiler-sdk.d.ts",
@@ -60,10 +60,10 @@
60
60
  "typescript": "^5.0.4"
61
61
  },
62
62
  "dependencies": {
63
- "@maptiler/client": "^1.3.0",
63
+ "@maptiler/client": "^1.5.0",
64
64
  "events": "^3.3.0",
65
65
  "js-base64": "^3.7.4",
66
- "maplibre-gl": "3.0.0-pre.4",
66
+ "maplibre-gl": "3.0.1",
67
67
  "uuid": "^9.0.0"
68
68
  }
69
69
  }
package/src/Map.ts CHANGED
@@ -8,15 +8,13 @@ import type {
8
8
  MapDataEvent,
9
9
  Tile,
10
10
  RasterDEMSourceSpecification,
11
- TerrainSpecification,
12
- MapTerrainEvent,
11
+ RequestTransformFunction,
13
12
  } from "maplibre-gl";
14
- import { v4 as uuidv4 } from "uuid";
15
13
  import { ReferenceMapStyle, MapStyleVariant } from "@maptiler/client";
16
- import { config, SdkConfig } from "./config";
14
+ import { config, MAPTILER_SESSION_ID, SdkConfig } from "./config";
17
15
  import { defaults } from "./defaults";
18
16
  import { MaptilerLogoControl } from "./MaptilerLogoControl";
19
- import { enableRTL } from "./tools";
17
+ import { combineTransformRequest, enableRTL } from "./tools";
20
18
  import {
21
19
  getBrowserLanguage,
22
20
  isLanguageSupported,
@@ -56,8 +54,6 @@ export type StyleSwapOptions = {
56
54
  transformStyle?: TransformStyleFunction;
57
55
  };
58
56
 
59
- const MAPTILER_SESSION_ID = uuidv4();
60
-
61
57
  export const GeolocationType: {
62
58
  POINT: "POINT";
63
59
  COUNTRY: "COUNTRY";
@@ -197,37 +193,7 @@ export class Map extends maplibregl.Map {
197
193
  ...options,
198
194
  style,
199
195
  maplibreLogo: false,
200
-
201
- transformRequest: (url: string) => {
202
- let reqUrl = null;
203
-
204
- try {
205
- // The URL is expected to be absolute.
206
- // Yet, if it's local we just return it without assuming a 'base' url (in the URL constructor)
207
- // and we let the URL be locally resolved with a potential base path.
208
- reqUrl = new URL(url);
209
- } catch (e) {
210
- return {
211
- url,
212
- headers: {},
213
- };
214
- }
215
-
216
- if (reqUrl.host === defaults.maptilerApiHost) {
217
- if (!reqUrl.searchParams.has("key")) {
218
- reqUrl.searchParams.append("key", config.apiKey);
219
- }
220
-
221
- if (config.session) {
222
- reqUrl.searchParams.append("mtsid", MAPTILER_SESSION_ID);
223
- }
224
- }
225
-
226
- return {
227
- url: reqUrl.href,
228
- headers: {},
229
- };
230
- },
196
+ transformRequest: combineTransformRequest(options.transformRequest),
231
197
  });
232
198
 
233
199
  this.primaryLanguage = options.language ?? config.primaryLanguage;
@@ -384,7 +350,11 @@ export class Map extends maplibregl.Map {
384
350
 
385
351
  // if attribution in option is `false` but the the logo shows up in the tileJson, then the attribution must show anyways
386
352
  if (options.attributionControl === false) {
387
- this.addControl(new AttributionControl(options));
353
+ this.addControl(
354
+ new AttributionControl({
355
+ customAttribution: options.customAttribution,
356
+ })
357
+ );
388
358
  }
389
359
  } else if (options.maptilerLogo) {
390
360
  this.addControl(new MaptilerLogoControl(), options.logoPosition);
@@ -1207,4 +1177,20 @@ export class Map extends maplibregl.Map {
1207
1177
  getMaptilerSessionId(): string {
1208
1178
  return MAPTILER_SESSION_ID;
1209
1179
  }
1180
+
1181
+ /**
1182
+ * Updates the requestManager's transform request with a new function.
1183
+ *
1184
+ * @param transformRequest A callback run before the Map makes a request for an external URL. The callback can be used to modify the url, set headers, or set the credentials property for cross-origin requests.
1185
+ * Expected to return an object with a `url` property and optionally `headers` and `credentials` properties
1186
+ *
1187
+ * @returns {Map} `this`
1188
+ *
1189
+ * @example
1190
+ * map.setTransformRequest((url: string, resourceType: string) => {});
1191
+ */
1192
+ setTransformRequest(transformRequest: RequestTransformFunction) {
1193
+ super.setTransformRequest(combineTransformRequest(transformRequest));
1194
+ return this;
1195
+ }
1210
1196
  }
@@ -5,6 +5,7 @@ import { DOMcreate } from "./tools";
5
5
 
6
6
  const Marker = maplibregl.Marker;
7
7
  const LngLat = maplibregl.LngLat;
8
+ const LngLatBounds = maplibregl.LngLatBounds;
8
9
 
9
10
  /**
10
11
  * The MaptilerGeolocateControl is an extension of the original GeolocateControl
@@ -40,7 +41,7 @@ export class MaptilerGeolocateControl extends GeolocateControl {
40
41
  options.zoom = currentMapZoom;
41
42
  }
42
43
 
43
- this._map.fitBounds(center.toBounds(radius), options, {
44
+ this._map.fitBounds(LngLatBounds.fromLngLat(center, radius), options, {
44
45
  geolocateSource: true, // tag this camera change so it won't cause the control to change to background state
45
46
  });
46
47
 
@@ -43,13 +43,10 @@ export class MaptilerLogoControl extends LogoControl {
43
43
  anchor.style.height = "30px";
44
44
 
45
45
  anchor.target = "_blank";
46
- anchor.rel = "noopener nofollow";
46
+ anchor.rel = "noopener";
47
47
  anchor.href = this.linkURL;
48
- anchor.setAttribute(
49
- "aria-label",
50
- this._map._getUIString("LogoControl.Title")
51
- );
52
- anchor.setAttribute("rel", "noopener nofollow");
48
+ anchor.setAttribute("aria-label", "MapTiler logo");
49
+ anchor.setAttribute("rel", "noopener");
53
50
  this._container.appendChild(anchor);
54
51
  this._container.style.display = "block";
55
52
 
package/src/config.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import EventEmitter from "events";
2
2
  import { Language, LanguageString } from "./language";
3
3
  import { config as clientConfig, FetchFunction } from "@maptiler/client";
4
+ import { v4 as uuidv4 } from "uuid";
4
5
  import { Unit } from "./unit";
5
6
 
7
+ export const MAPTILER_SESSION_ID = uuidv4();
8
+
6
9
  /**
7
10
  * Configuration class for the SDK
8
11
  */
package/src/index.ts CHANGED
@@ -11,7 +11,7 @@ export * from "maplibre-gl";
11
11
  import maplibregl from "maplibre-gl";
12
12
 
13
13
  const {
14
- supported,
14
+ // supported,
15
15
  setRTLTextPlugin,
16
16
  getRTLTextPluginStatus,
17
17
  LngLat,
@@ -50,7 +50,7 @@ const FullscreenControlMLGL = maplibregl.FullscreenControl;
50
50
  const TerrainControlMLGL = maplibregl.TerrainControl;
51
51
 
52
52
  export {
53
- supported,
53
+ // supported,
54
54
  setRTLTextPlugin,
55
55
  getRTLTextPluginStatus,
56
56
  PopupMLGL,
package/src/tools.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import maplibregl from "maplibre-gl";
2
+ import type {
3
+ RequestParameters,
4
+ ResourceType,
5
+ RequestTransformFunction,
6
+ } from "maplibre-gl";
2
7
  import { defaults } from "./defaults";
8
+ import { config } from "./config";
9
+ import { MAPTILER_SESSION_ID } from "./config";
3
10
 
4
11
  export function enableRTL() {
5
12
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -43,3 +50,70 @@ export function DOMremove(node: HTMLElement) {
43
50
  node.parentNode.removeChild(node);
44
51
  }
45
52
  }
53
+
54
+ /**
55
+ * This function is meant to be used as transformRequest by any Map instance created.
56
+ * It adds the session ID as well as the MapTiler Cloud key from the config to all the requests
57
+ * performed on MapTiler Cloud servers.
58
+ * @param url
59
+ * @param resourceType
60
+ * @returns
61
+ */
62
+ export function maptilerCloudTransformRequest(
63
+ url: string,
64
+ resourceType?: ResourceType
65
+ ): RequestParameters {
66
+ let reqUrl = null;
67
+
68
+ try {
69
+ // The URL is expected to be absolute.
70
+ // Yet, if it's local we just return it without assuming a 'base' url (in the URL constructor)
71
+ // and we let the URL be locally resolved with a potential base path.
72
+ reqUrl = new URL(url);
73
+ } catch (e) {
74
+ return {
75
+ url,
76
+ };
77
+ }
78
+
79
+ if (reqUrl.host === defaults.maptilerApiHost) {
80
+ if (!reqUrl.searchParams.has("key")) {
81
+ reqUrl.searchParams.append("key", config.apiKey);
82
+ }
83
+
84
+ if (config.session) {
85
+ reqUrl.searchParams.append("mtsid", MAPTILER_SESSION_ID);
86
+ }
87
+ }
88
+
89
+ return {
90
+ url: reqUrl.href,
91
+ };
92
+ }
93
+
94
+ /**
95
+ * This combines a user-defined tranformRequest function (optionnal)
96
+ * with the MapTiler Cloud-specific one: maptilerCloudTransformRequest
97
+ * @param userDefinedRTF
98
+ * @returns
99
+ */
100
+ export function combineTransformRequest(
101
+ userDefinedRTF: RequestTransformFunction = null
102
+ ): RequestTransformFunction {
103
+ return function (
104
+ url: string,
105
+ resourceType?: ResourceType
106
+ ): RequestParameters {
107
+ if (userDefinedRTF) {
108
+ const rp = userDefinedRTF(url, resourceType);
109
+ const rp2 = maptilerCloudTransformRequest(rp.url);
110
+
111
+ return {
112
+ ...rp,
113
+ ...rp2,
114
+ };
115
+ } else {
116
+ return maptilerCloudTransformRequest(url);
117
+ }
118
+ };
119
+ }