import {z} from 'zod' export type GetApiCoordinatesSearchQueryJsonArgs = {query: string, limit?: number | undefined, offset?: number | undefined, transformations?: boolean | undefined, exports?: boolean | undefined}; export type GetApiCoordinatesSearchQueryJsonOptions = {baseUrl?: string | undefined, key?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.object({authority: z.string(), code: z.number().int()}); export const exportItem = z.object({proj4: z.string().optional(), wkt: z.string().optional()}); export const getApiCoordinatesSearchQueryJsonResponse = z.object({results: z.array(z.object({accuracy: z.number().optional(), area: z.string().optional(), bbox: z.array(z.number()).optional(), default_transformation: id.optional(), deprecated: z.boolean(), exports: exportItem.optional(), id: id, kind: z.string(), name: z.string(), transformations: z.array(z.union([z.object({accuracy: z.number().optional(), area: z.string().optional(), bbox: z.array(z.number()).optional(), deprecated: z.boolean(), exports: exportItem.optional(), grids: z.array(z.object({path: z.string()})), id: id, name: z.string(), reversible: z.boolean(), target_crs: id.optional(), unit: z.string().optional(), usable: z.boolean()}), z.number().int()])).optional(), unit: z.string().optional()})), total: z.number().int()}); 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 }; /** * Search coordinate systems * * GET /coordinates/search/{query}.json */ export const getApiCoordinatesSearchQueryJson = async (args: GetApiCoordinatesSearchQueryJsonArgs, options?: GetApiCoordinatesSearchQueryJsonOptions) => { const {baseUrl = "https://api.maptiler.com", fetch: fetchFn = fetch, key} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.transformations != null) query.append("transformations", String(args.transformations)) if (args.exports != null) query.append("exports", String(args.exports)) if (key !== undefined) query.set("key", key) const url = `${baseUrl.replace(/\/+$/, '')}/coordinates/search/${encodeURIComponent(String(args.query))}.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(getApiCoordinatesSearchQueryJsonResponse, await res.json()) };