import {z} from 'zod' export type TileMatrixSetNames = 'CanadianNAD83_LCC' | 'EPSG3413' | 'EPSG6933' | 'EuropeanETRS89_LAEAQuad' | 'LINZAntarticaMapTilegrid' | 'NZTM2000' | 'NZTM2000Quad' | 'UPSAntarcticWGS84Quad' | 'UPSArcticWGS84Quad' | 'UTM31WGS84Quad' | 'WebMercatorQuad' | 'WorldCRS84Quad' | 'WorldMercatorWGS84Quad'; export type GetApiApiV1LayersTilesTileMatrixSetIdLayerTilejsonJsonArgs = {layer: string, TileMatrixSetId: TileMatrixSetNames, minzoom?: number | undefined, maxzoom?: number | undefined}; export type GetApiApiV1LayersTilesTileMatrixSetIdLayerTilejsonJsonOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const schemeEnum = z.enum(["xyz", "tms"]); export const tileJSON = z.object({tilejson: z.string().optional(), name: z.string().optional(), description: z.string().optional(), version: z.string().optional(), attribution: z.string().optional(), template: z.string().optional(), legend: z.string().optional(), scheme: schemeEnum.optional(), tiles: z.array(z.string()), grids: z.array(z.string()).optional(), data: z.array(z.string()).optional(), minzoom: z.number().int().gte(0).lte(30).optional(), maxzoom: z.number().int().gte(0).lte(30).optional(), bounds: z.array(z.number()).optional(), center: z.array(z.unknown()).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Tilejson * * GET /api/v1/layers/tiles/{TileMatrixSetId}/{layer}/tilejson.json */ export const getApiApiV1LayersTilesTileMatrixSetIdLayerTilejsonJson = async (args: GetApiApiV1LayersTilesTileMatrixSetIdLayerTilejsonJsonArgs, options: GetApiApiV1LayersTilesTileMatrixSetIdLayerTilejsonJsonOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.minzoom != null) query.append("minzoom", String(args.minzoom)) if (args.maxzoom != null) query.append("maxzoom", String(args.maxzoom)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/layers/tiles/${encodeURIComponent(String(args.TileMatrixSetId))}/${encodeURIComponent(String(args.layer))}/tilejson.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tileJSON, await res.json()) };