Use access token for fetchSources

This commit is contained in:
pathmapper 2018-11-27 13:29:47 +01:00
parent fe8595cdc9
commit 54b4fc473c
2 changed files with 26 additions and 0 deletions

View file

@ -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',
})

View file

@ -119,5 +119,6 @@ export default {
emptyStyle,
indexOfLayer,
generateId,
getAccessToken,
replaceAccessTokens,
}