Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

baseApiUrl w/ path & normalizeTileURL functionality #8466

Merged
merged 3 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ export class RequestManager {

const urlObject = parseUrl(tileURL);
const imageExtensionRe = /(\.(png|jpg)\d*)(?=$)/;
const tileURLAPIPrefixRe = /^.+\/v4\//;
sumarlidason marked this conversation as resolved.
Show resolved Hide resolved

// The v4 mapbox tile API supports 512x512 image tiles only when @2x
// is appended to the tile URL. If `tileSize: 512` is specified for
// a Mapbox raster source force the @2x suffix even if a non hidpi device.
const suffix = browser.devicePixelRatio >= 2 || tileSize === 512 ? '@2x' : '';
const extension = webpSupported.supported ? '.webp' : '$1';
urlObject.path = urlObject.path.replace(imageExtensionRe, `${suffix}${extension}`);
urlObject.path = urlObject.path.replace(tileURLAPIPrefixRe, '/');
urlObject.path = `/v4${urlObject.path}`;

if (config.REQUIRE_ACCESS_TOKEN && (config.ACCESS_TOKEN || this._customAccessToken) && this._skuToken) {
Expand Down Expand Up @@ -176,6 +178,7 @@ export class RequestManager {
if (accessToken[0] === 's')
throw new Error(`Use a public access token (pk.*) with Mapbox GL, not a secret access token (sk.*). ${help}`);

urlObject.params = urlObject.params.filter((d) => !d.includes('access_token'));
urlObject.params.push(`access_token=${accessToken}`);
return formatUrl(urlObject);
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/util/mapbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ test("mapbox", (t) => {
t.end();
});

t.test('.normalizeTileURL accounts for tileURLs w/ paths', (t) => {
// Add a path to the config:
config.API_URL = 'http://localhost:8080/mbx';
const input = `https://localhost:8080/mbx/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/10/184/401.vector.pbf?access_token=${config.ACCESS_TOKEN}`;
const expected = `http://localhost:8080/mbx/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/10/184/401.vector.pbf?sku=${manager._skuToken}&access_token=${config.ACCESS_TOKEN}`;
t.equal(manager.normalizeTileURL(input, 'mapbox://mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7'), expected);
t.end();
});

t.test('.normalizeTileURL ignores undefined sources', (t) => {
t.equal(manager.normalizeTileURL('http://path.png'), 'http://path.png');
t.end();
Expand Down