sharemap-maplib-js 0.0.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.
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "sharemap-maplib-js",
3
+ "version": "0.0.1",
4
+ "description": "A ShareMap maplib javascript library.",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "keywords": [
8
+ "sharemap",
9
+ "maplibre",
10
+ "maplibre-gl",
11
+ "javascript",
12
+ "map"
13
+ ],
14
+ "author": "SHAREMAP",
15
+ "license": "MIT",
16
+ "scripts": {
17
+ "build": "rollup -c",
18
+ "prepare": "npm run build",
19
+ "release": "npm run build && npm publish",
20
+ "publish": "npm run build && npm publish"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "src"
25
+ ],
26
+ "dependencies": {
27
+ "maplibre-gl": "^2.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "@rollup/plugin-commonjs": "^24.0.0",
31
+ "@rollup/plugin-node-resolve": "^15.0.0",
32
+ "rollup": "^3.0.0"
33
+ }
34
+ }
package/src/index.js ADDED
@@ -0,0 +1,66 @@
1
+ import maplibregl from 'maplibre-gl';
2
+
3
+ const DEFAULT_MODE_KEY = 'header';
4
+ const STYLE_URLS = {
5
+ default: 'https://tiles.sharemap.live/tiles/styles/sharemap.json',
6
+ bright: 'https://tiles.sharemap.live/tiles/styles/bright.json',
7
+ dark: 'https://tiles.sharemap.live/tiles/styles/dark.json'
8
+ };
9
+
10
+ function buildServiceRequest(url, modeKey, token) {
11
+ if (!token) {
12
+ return { url, headers: {} };
13
+ }
14
+
15
+ const headers = { 'sharemap-service-key': token };
16
+ if (modeKey === 'query') {
17
+ const separator = url.includes('?') ? '&' : '?';
18
+ return { url: `${url}${separator}sharemap-service-key=${encodeURIComponent(token)}`, headers: {} };
19
+ }
20
+
21
+ return { url, headers };
22
+ }
23
+
24
+ function mergeTransformRequest(userTransform, modeKey, token) {
25
+ return (url, resourceType) => {
26
+ const baseRequest = userTransform ? userTransform(url, resourceType) : { url, headers: {} };
27
+ const serviceRequest = buildServiceRequest(baseRequest.url, modeKey, token);
28
+ return {
29
+ ...baseRequest,
30
+ url: serviceRequest.url,
31
+ headers: {
32
+ ...baseRequest.headers,
33
+ ...serviceRequest.headers
34
+ }
35
+ };
36
+ };
37
+ }
38
+
39
+ export class ShareMapLibre extends maplibregl.Map {
40
+ constructor(options = {}) {
41
+ const {
42
+ sharemapServiceKey,
43
+ 'sharemap-service-key': sharemapServiceKeyLegacy,
44
+ modeKey = DEFAULT_MODE_KEY,
45
+ style,
46
+ transformRequest,
47
+ ...rest
48
+ } = options;
49
+
50
+ const token = sharemapServiceKey || sharemapServiceKeyLegacy;
51
+
52
+ const resolvedStyle = typeof style === 'string'
53
+ ? STYLE_URLS[style] ?? style
54
+ : style ?? rest.style ?? STYLE_URLS.default;
55
+
56
+ const finalOptions = {
57
+ ...rest,
58
+ style: resolvedStyle,
59
+ transformRequest: mergeTransformRequest(transformRequest, modeKey, token)
60
+ };
61
+
62
+ super(finalOptions);
63
+ }
64
+ }
65
+
66
+ export default ShareMapLibre;