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

Combine ResourceType interface and const into a single enum #2103

Merged
merged 6 commits into from
Jan 30, 2023
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: 1 addition & 1 deletion src/source/geojson_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Event, ErrorEvent, Evented} from '../util/evented';

import {extend} from '../util/util';
import EXTENT from '../data/extent';
import {ResourceType} from '../util/ajax';
import {ResourceType} from '../util/request_manager';
import browser from '../util/browser';

import type {Source} from './source';
Expand Down
3 changes: 2 additions & 1 deletion src/source/image_source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {CanonicalTileID} from './tile_id';
import {Event, ErrorEvent, Evented} from '../util/evented';
import {getImage, ResourceType} from '../util/ajax';
import {getImage} from '../util/ajax';
import {ResourceType} from '../util/request_manager';
import EXTENT from '../data/extent';
import {RasterBoundsArray} from '../data/array_types.g';
import rasterBoundsAttributes from '../data/raster_bounds_attributes';
Expand Down
3 changes: 2 additions & 1 deletion src/source/load_tilejson.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {pick, extend} from '../util/util';

import {getJSON, ResourceType} from '../util/ajax';
import {getJSON} from '../util/ajax';
import {ResourceType} from '../util/request_manager';
import browser from '../util/browser';

import type {RequestManager} from '../util/request_manager';
Expand Down
3 changes: 2 additions & 1 deletion src/source/raster_dem_tile_source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getImage, ResourceType} from '../util/ajax';
import {getImage} from '../util/ajax';
import {ResourceType} from '../util/request_manager';
import {extend, isImageBitmap} from '../util/util';
import {Evented} from '../util/evented';
import browser from '../util/browser';
Expand Down
3 changes: 2 additions & 1 deletion src/source/raster_tile_source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {extend, pick} from '../util/util';

import {getImage, ResourceType} from '../util/ajax';
import {getImage} from '../util/ajax';
import {ResourceType} from '../util/request_manager';
import {Event, ErrorEvent, Evented} from '../util/evented';
import loadTileJSON from './load_tilejson';
import TileBounds from './tile_bounds';
Expand Down
2 changes: 1 addition & 1 deletion src/source/vector_tile_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Event, ErrorEvent, Evented} from '../util/evented';
import {extend, pick} from '../util/util';
import loadTileJSON from './load_tilejson';
import TileBounds from './tile_bounds';
import {ResourceType} from '../util/ajax';
import {ResourceType} from '../util/request_manager';

import type {Source} from './source';
import type {OverscaledTileID} from './tile_id';
Expand Down
3 changes: 2 additions & 1 deletion src/source/video_source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getVideo, ResourceType} from '../util/ajax';
import {getVideo} from '../util/ajax';
import {ResourceType} from '../util/request_manager';

import ImageSource from './image_source';
import rasterBoundsAttributes from '../data/raster_bounds_attributes';
Expand Down
3 changes: 2 additions & 1 deletion src/style/load_glyph_range.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getArrayBuffer, ResourceType} from '../util/ajax';
import {getArrayBuffer} from '../util/ajax';
import {ResourceType} from '../util/request_manager';

import parseGlyphPBF from './parse_glyph_pbf';

Expand Down
3 changes: 2 additions & 1 deletion src/style/load_sprite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {getJSON, getImage, ResourceType} from '../util/ajax';
import {getJSON, getImage} from '../util/ajax';
import {ResourceType} from '../util/request_manager';

import browser from '../util/browser';
import {RGBAImage} from '../util/image';
Expand Down
3 changes: 2 additions & 1 deletion src/style/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import Light from './light';
import LineAtlas from '../render/line_atlas';
import {pick, clone, extend, deepEqual, filterObject, mapObject} from '../util/util';
import {coerceSpriteToArray} from '../util/style';
import {getJSON, getReferrer, makeRequest, ResourceType} from '../util/ajax';
import {getJSON, getReferrer, makeRequest} from '../util/ajax';
import {ResourceType} from '../util/request_manager';
import browser from '../util/browser';
import Dispatcher from '../util/dispatcher';
import {validateStyle, emitValidationErrors as _emitValidationErrors} from './validate_style';
Expand Down
4 changes: 2 additions & 2 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {extend, bindAll, warnOnce, uniqueId, isImageBitmap} from '../util/util';
import browser from '../util/browser';
import DOM from '../util/dom';
import packageJSON from '../../package.json' assert {type: 'json'};
import {getImage, GetImageCallback, getJSON, ResourceType} from '../util/ajax';
import {RequestManager} from '../util/request_manager';
import {getImage, GetImageCallback, getJSON} from '../util/ajax';
import {RequestManager, ResourceType} from '../util/request_manager';
import Style, {StyleSwapOptions} from '../style/style';
import EvaluationParameters from '../style/evaluation_parameters';
import Painter from '../render/painter';
Expand Down
33 changes: 0 additions & 33 deletions src/util/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,6 @@ import webpSupported from './webp_supported';
import type {Callback} from '../types/callback';
import type {Cancelable} from '../types/cancelable';

export interface IResourceType {
Unknown: keyof this;
Style: keyof this;
Source: keyof this;
Tile: keyof this;
Glyphs: keyof this;
SpriteImage: keyof this;
SpriteJSON: keyof this;
Image: keyof this;
}

/**
* The type of a resource.
* @private
* @readonly
* @enum {string}
*/
const ResourceType = {
Unknown: 'Unknown',
Style: 'Style',
Source: 'Source',
Tile: 'Tile',
Glyphs: 'Glyphs',
SpriteImage: 'SpriteImage',
SpriteJSON: 'SpriteJSON',
Image: 'Image'
} as IResourceType;
export {ResourceType};

if (typeof Object.freeze == 'function') {
Object.freeze(ResourceType);
}

/**
* A `RequestParameters` object to be returned from Map.options.transformRequest callbacks.
* @typedef {Object} RequestParameters
Expand Down
21 changes: 16 additions & 5 deletions src/util/request_manager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import {IResourceType} from './ajax';

import type {RequestParameters} from './ajax';

type ResourceTypeEnum = keyof IResourceType;
export type RequestTransformFunction = (url: string, resourceType?: ResourceTypeEnum) => RequestParameters;
/**
* A type of MapLibre resource.
*/
export const enum ResourceType {
Glyphs = 'Glyphs',
Image = 'Image',
Source = 'Source',
SpriteImage = 'SpriteImage',
SpriteJSON = 'SpriteJSON',
Style = 'Style',
Tile = 'Tile',
Unknown = 'Unknown',
}

export type RequestTransformFunction = (url: string, resourceType?: ResourceType) => RequestParameters;

type UrlObject = {
protocol: string;
Expand All @@ -19,7 +30,7 @@ export class RequestManager {
this._transformRequestFn = transformRequestFn;
}

transformRequest(url: string, type: ResourceTypeEnum) {
transformRequest(url: string, type: ResourceType) {
if (this._transformRequestFn) {
return this._transformRequestFn(url, type) || {url};
}
Expand Down