Skip to content

Commit

Permalink
Refactor based on code review suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthetechie committed Aug 28, 2024
1 parent 25fc594 commit 91600f8
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/libs/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,13 @@ function replaceSourceAccessToken(mapStyle: StyleSpecification, sourceName: stri
if(!("url" in source) || !source.url) return mapStyle

let authSourceName = sourceName
let setAccessToken = (url: string) => url.replace('{key}', accessToken)
if(sourceName === "thunderforest_transport" || sourceName === "thunderforest_outdoors") {
authSourceName = "thunderforest"
}
else if (("url" in source) && source.url?.match(/\.stadiamaps\.com/)) {
// A few weird things here worth commenting on...
//
// 1. The code currently assumes openmaptiles == MapTiler,
// so we need to check the source URL.
// 2. Stadia Maps does not always require an API key,
// so there is no placeholder in our styles.
// We append it at the end during exporting if necessary.
// The code currently usually assumes openmaptiles == MapTiler,
// so we need to check the source URL.
authSourceName = "stadia"
setAccessToken = (url: string) => `${url}?api_key=${accessToken}`
}

const accessToken = getAccessToken(authSourceName, mapStyle, opts)
Expand All @@ -94,11 +87,21 @@ function replaceSourceAccessToken(mapStyle: StyleSpecification, sourceName: stri
return mapStyle;
}

let sourceUrl: string
if (authSourceName == "stadia") {
// Stadia Maps does not always require an API key,
// so there is no placeholder in our styles.
// We append it at the end of the URL when exporting if necessary.
sourceUrl = `${source.url}?api_key=${accessToken}`
} else {
sourceUrl = source.url.replace('{key}', accessToken)
}

const changedSources = {
...mapStyle.sources,
[sourceName]: {
...source,
url: setAccessToken(source.url)
url: sourceUrl
}
}
const changedStyle = {
Expand Down

0 comments on commit 91600f8

Please sign in to comment.