From 54b4fc473c5e6f7d1295e0947b3ba4f60eb5bce4 Mon Sep 17 00:00:00 2001 From: pathmapper Date: Tue, 27 Nov 2018 13:29:47 +0100 Subject: [PATCH] Use access token for fetchSources --- src/components/App.jsx | 25 +++++++++++++++++++++++++ src/libs/style.js | 1 + 2 files changed, 26 insertions(+) diff --git a/src/components/App.jsx b/src/components/App.jsx index 6fc5ce9..88cc50c 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -49,6 +49,25 @@ function normalizeSourceURL (url, apiToken="") { } } +function setFetchAccessToken(url, mapStyle) { + const matchesTilehosting = url.match(/\.tilehosting\.com/); + const matchesThunderforest = url.match(/\.thunderforest\.com/); + if (matchesTilehosting) { + const accessToken = style.getAccessToken("openmaptiles", mapStyle, {allowFallback: true}) + if (accessToken) { + return url.replace('{key}', accessToken) + } + } + else if (matchesThunderforest) { + const accessToken = style.getAccessToken("thunderforest", mapStyle, {allowFallback: true}) + if (accessToken) { + return url.replace('{key}', accessToken) + } + } + else { + return url; + } +} function updateRootSpec(spec, fieldName, newValues) { return { @@ -398,6 +417,12 @@ export default class App extends React.Component { console.warn("Failed to normalizeSourceURL: ", err); } + try { + url = setFetchAccessToken(url, this.state.mapStyle) + } catch(err) { + console.warn("Failed to setFetchAccessToken: ", err); + } + fetch(url, { mode: 'cors', }) diff --git a/src/libs/style.js b/src/libs/style.js index 4caa357..ee2041b 100644 --- a/src/libs/style.js +++ b/src/libs/style.js @@ -119,5 +119,6 @@ export default { emptyStyle, indexOfLayer, generateId, + getAccessToken, replaceAccessTokens, }