Skip to content

Commit

Permalink
Improve loadTileJson types (#3423)
Browse files Browse the repository at this point in the history
* Improve `loadTileJson` response type and remove unused variable

* Added missing files...
  • Loading branch information
HarelM authored Nov 29, 2023
1 parent 8b16a2a commit 8edbac0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
40 changes: 26 additions & 14 deletions src/source/load_tilejson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,42 @@ import type {RequestManager} from '../util/request_manager';
import type {TileJSON} from '../types/tilejson';
import type {RasterDEMSourceSpecification, RasterSourceSpecification, VectorSourceSpecification} from '@maplibre/maplibre-gl-style-spec';

export type LoadTileJsonResponse = {
tiles: Array<string>;
minzoom: number;
maxzoom: number;
attribution: string;
bounds: RasterSourceSpecification['bounds'];
scheme: RasterSourceSpecification['scheme'];
tileSize: number;
encoding: RasterDEMSourceSpecification['encoding'];
vectorLayerIds?: Array<string>;
}

export async function loadTileJson(
options: RasterSourceSpecification | RasterDEMSourceSpecification | VectorSourceSpecification,
requestManager: RequestManager,
abortController: AbortController,
): Promise<TileJSON> {
let tileJSON: any = options;
): Promise<LoadTileJsonResponse | null> {
let tileJSON: TileJSON | typeof options = options;
if (options.url) {
const response = await getJSON<TileJSON>(requestManager.transformRequest(options.url, ResourceType.Source), abortController);
tileJSON = response.data;
} else {
await browser.frameAsync(abortController);
}
if (tileJSON) {
const result: TileJSON = pick(
// explicit source options take precedence over TileJSON
extend(tileJSON, options),
['tiles', 'minzoom', 'maxzoom', 'attribution', 'bounds', 'scheme', 'tileSize', 'encoding']
);

if (tileJSON.vector_layers) {
result.vectorLayers = tileJSON.vector_layers;
result.vectorLayerIds = result.vectorLayers.map((layer) => { return layer.id; });
}
if (!tileJSON) {
return null;
}
const result: LoadTileJsonResponse = pick(
// explicit source options take precedence over TileJSON
extend(tileJSON, options),
['tiles', 'minzoom', 'maxzoom', 'attribution', 'bounds', 'scheme', 'tileSize', 'encoding']
);

return result;
if ('vector_layers' in tileJSON && tileJSON.vector_layers) {
result.vectorLayerIds = tileJSON.vector_layers.map((layer) => { return layer.id; });
}

return result;
}
3 changes: 1 addition & 2 deletions src/types/tilejson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export type TileJSON = {
maxzoom?: number;
bounds?: [number, number, number, number];
center?: [number, number, number];
vectorLayers: any;
vectorLayerIds: Array<string>;
vector_layers: [{id: string}]; // this is partial but enough for what we need
};
10 changes: 8 additions & 2 deletions test/integration/render/run_render_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,14 @@ function getReportItem(test: TestData) {

function applyDebugParameter(options: RenderOptions, page: Page) {
if (options.debug) {
page.on('console', message =>
console.log(`${message.type().substr(0, 3).toUpperCase()} ${message.text()}`));
page.on('console', async (message) => {
if (message.text() !== 'JSHandle@error') {
console.log(`${message.type().substring(0, 3).toUpperCase()} ${message.text()}`);
return;
}
const messages = await Promise.all(message.args().map((arg) => arg.getProperty('message')));
console.log(`${message.type().substring(0, 3).toUpperCase()} ${messages.filter(Boolean)}`);
});

page.on('pageerror', ({message}) => console.error(message));

Expand Down

0 comments on commit 8edbac0

Please sign in to comment.