import {z} from 'zod' export type TileMatrixSets = 'WebMercatorQuad' | 'WorldCRS84Quad' | 'GNOSISGlobalGrid' | 'WorldMercatorWGS84Quad'; export type GetApiTileMatrixSetsTileMatrixSetIdArgs = {tileMatrixSetId: TileMatrixSets, f?: ('json' | 'html') | undefined}; export type GetApiTileMatrixSetsTileMatrixSetIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const schema2DPoint = z.array(z.number()); export const crs = z.union([z.string(), z.object({uri: z.string()}), z.object({wkt: z.object({})}), z.object({referenceSystem: z.object({})})]); export const tileMatrixSet = z.object({title: z.string().optional(), description: z.string().optional(), keywords: z.array(z.string()).optional(), id: z.string().optional(), uri: z.string().optional(), orderedAxes: z.array(z.string()).optional(), crs: z.union([z.string(), z.object({uri: z.string()}), z.object({wkt: z.object({})}), z.object({referenceSystem: z.object({})})]), wellKnownScaleSet: z.string().optional(), boundingBox: z.object({lowerLeft: schema2DPoint, upperRight: schema2DPoint, crs: crs.optional(), orderedAxes: z.array(z.string()).optional()}).optional(), tileMatrices: z.array(z.object({title: z.string().optional(), description: z.string().optional(), keywords: z.array(z.string()).optional(), id: z.string(), scaleDenominator: z.number(), cellSize: z.number(), cornerOfOrigin: z.enum(["topLeft", "bottomLeft"]).optional(), pointOfOrigin: z.array(z.number()), tileWidth: z.number().gte(1).multipleOf(1), tileHeight: z.number().gte(1).multipleOf(1), matrixHeight: z.number().gte(1).multipleOf(1), matrixWidth: z.number().gte(1).multipleOf(1), variableMatrixWidths: z.array(z.object({coalesce: z.number().gte(2).multipleOf(1), minTileRow: z.number().gte(0).multipleOf(1), maxTileRow: z.number().gte(0).multipleOf(1)})).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 }; /** * Retrieve the definition of the specified tiling scheme (tile matrix set) * * GET /tileMatrixSets/{tileMatrixSetId} */ export const getApiTileMatrixSetsTileMatrixSetId = async (args: GetApiTileMatrixSetsTileMatrixSetIdArgs, options?: GetApiTileMatrixSetsTileMatrixSetIdOptions) => { const {baseUrl = "https://maps.ecere.com/ogcapi", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.f != null) query.append("f", String(args.f)) const url = `${baseUrl.replace(/\/+$/, '')}/tileMatrixSets/${encodeURIComponent(String(args.tileMatrixSetId))}${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(tileMatrixSet, await res.json()) };