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

added setTransformRequest to map to allow dynamically setting the transformRequestFn #159

Merged
merged 3 commits into from
Jun 5, 2021
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
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ declare namespace mapboxgl {

setStyle(style: mapboxgl.Style | string, options?: { diff?: boolean; localIdeographFontFamily?: string }): this;

setTransformRequest(transformRequest: RequestTransformFunction): void;

getStyle(): mapboxgl.Style;

isStyleLoaded(): boolean;
Expand Down
16 changes: 16 additions & 0 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,22 @@ class Map extends Camera {
}
}

/**
* Updates the requestManager's transform request with a new function
*
* @param transformRequest A callback run before the Map makes a request for an external URL. The callback can be used to modify the url, set headers, or set the credentials property for cross-origin requests.
* Expected to return an object with a `url` property and optionally `headers` and `credentials` properties
*
* @returns {Map} `this`
*
* @example
* map.setTransformRequest((url: string, resourceType: string) => {});
*/
setTransformRequest(transformRequest: RequestTransformFunction) {
this._requestManager.setTransformRequest(transformRequest);
return this;
}

_getUIString(key: string) {
const str = this._locale[key];
if (str == null) {
Expand Down
4 changes: 4 additions & 0 deletions src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ export class RequestManager {
urlObject.params.push(`access_token=${accessToken}`);
return formatUrl(urlObject);
}

setTransformRequest(transformRequest: RequestTransformFunction) {
this._transformRequestFn = transformRequest;
}
}

function isMapboxURL(url: string) {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,29 @@ test('Map', (t) => {
t.end();
});

t.test('#setTransformRequest', (t) => {
t.test('returns self', (t) => {
t.stub(Map.prototype, '_detectMissingCSS');
const transformRequest = () => { };
const map = new Map({container: window.document.createElement('div')});
t.equal(map.setTransformRequest(transformRequest), map);
t.equal(map._requestManager._transformRequestFn, transformRequest);
t.end();
});

t.test('can be called more than once', (t) => {
const map = createMap(t);

const transformRequest = () => { };
map.setTransformRequest(transformRequest);
map.setTransformRequest(transformRequest);

t.end();
});

t.end();
});

t.test('#is_Loaded', (t) => {

t.test('Map#isSourceLoaded', (t) => {
Expand Down