56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import type { MapProviderId } from "./contracts.js";
|
|
import { MapRuntimeError } from "./runtime.js";
|
|
|
|
export type ProviderEndpoint = {
|
|
assetId: string;
|
|
type: "IMAGERY" | "TERRAIN" | "3DTILES";
|
|
externalType?: "BING";
|
|
credentialMode: "gateway";
|
|
url?: string;
|
|
options?: {
|
|
url?: string;
|
|
mapStyle?: string;
|
|
};
|
|
attributions: readonly {
|
|
html?: string;
|
|
collapsible?: boolean;
|
|
}[];
|
|
};
|
|
|
|
export function parseProviderEndpoint(
|
|
document: Record<string, unknown>,
|
|
expectedAssetId: number,
|
|
providerId: MapProviderId,
|
|
): ProviderEndpoint {
|
|
const expectedType =
|
|
providerId === "imagery"
|
|
? "IMAGERY"
|
|
: providerId === "terrain"
|
|
? "TERRAIN"
|
|
: "3DTILES";
|
|
if (
|
|
document.assetId !== String(expectedAssetId) ||
|
|
document.type !== expectedType ||
|
|
document.credentialMode !== "gateway" ||
|
|
!Array.isArray(document.attributions)
|
|
) {
|
|
throw new MapRuntimeError("map_provider_contract_mismatch");
|
|
}
|
|
if (
|
|
(providerId === "imagery" &&
|
|
(document.externalType !== "BING" ||
|
|
!isEndpointOptions(document.options) ||
|
|
typeof document.options.url !== "string")) ||
|
|
(providerId !== "imagery" && typeof document.url !== "string")
|
|
) {
|
|
throw new MapRuntimeError("map_provider_contract_mismatch");
|
|
}
|
|
return document as ProviderEndpoint;
|
|
}
|
|
|
|
function isEndpointOptions(
|
|
value: unknown,
|
|
): value is NonNullable<ProviderEndpoint["options"]> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|