100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
/*
* Copyright 2026, OpenRemote Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
export function isMapboxURL(url: string) {
return url.indexOf("mapbox:") === 0;
}
export function transformMapboxUrl(url: string, accessToken: string, resourceType?: string) {
if (url.indexOf("/styles/") > -1 && url.indexOf("/sprite") === -1)
return { url: normalizeStyleURL(url, accessToken) };
if (url.indexOf("/sprites/") > -1) return { url: normalizeSpriteURL(url, accessToken) };
if (url.indexOf("/fonts/") > -1) return { url: normalizeGlyphsURL(url, accessToken) };
if (url.indexOf("/v4/") > -1) return { url: normalizeSourceURL(url, accessToken) };
if (resourceType === "Source") return { url: normalizeSourceURL(url, accessToken) };
}
function parseUrl(url: string) {
const urlRe = /^(\w+):\/\/([^/?]*)(\/[^?]+)?\??(.+)?/;
const parts = url.match(urlRe);
if (!parts) {
throw new Error("Unable to parse URL object");
}
return {
protocol: parts[1],
authority: parts[2],
path: parts[3] || "/",
params: parts[4] ? parts[4].split("&") : [],
};
}
function formatUrl(urlObject: any, accessToken: string) {
const apiUrlObject = parseUrl("https://api.mapbox.com");
urlObject.protocol = apiUrlObject.protocol;
urlObject.authority = apiUrlObject.authority;
urlObject.params.push(`access_token=${accessToken}`);
const params = urlObject.params.length ? `?${urlObject.params.join("&")}` : "";
return `${urlObject.protocol}://${urlObject.authority}${urlObject.path}${params}`;
}
function normalizeStyleURL(url: string, accessToken: string) {
const urlObject = parseUrl(url);
urlObject.path = `/styles/v1${urlObject.path}`;
return formatUrl(urlObject, accessToken);
}
function normalizeGlyphsURL(url: string, accessToken: string) {
const urlObject = parseUrl(url);
urlObject.path = `/fonts/v1${urlObject.path}`;
return formatUrl(urlObject, accessToken);
}
function normalizeSourceURL(url: string, accessToken: string) {
const urlObject = parseUrl(url);
urlObject.path = `/v4/${urlObject.authority}.json`;
urlObject.params.push("secure");
return formatUrl(urlObject, accessToken);
}
function normalizeSpriteURL(url: string, accessToken: string) {
const urlObject = parseUrl(url);
const path = urlObject.path.split(".");
let properPath = path[0];
const extension = path[1];
let format = "";
if (properPath.indexOf("@2x")) {
properPath = properPath.split("@2x")[0];
format = "@2x";
}
urlObject.path = `/styles/v1${properPath}/sprite${format}.${extension}`;
return formatUrl(urlObject, accessToken);
}