import {z} from 'zod' export type GetApiGeocodingIdJsonOptions = {baseUrl?: string | undefined, key?: string | undefined, fetch?: typeof fetch | undefined}; export const featureProperties = z.object({ref: z.string(), country_code: z.string(), kind: z.enum(["road", "road_relation", "admin_area", "place", "street", "virtual_street"]).optional(), categories: z.array(z.string()).optional(), feature_tags: z.record(z.string(), z.string()).optional(), place_designation: z.enum(["allotments", "borough", "city", "city_block", "farm", "hamlet", "isolated_dwelling", "neighbourhood", "plot", "quarter", "suburb", "town", "village"]).optional()}).and(z.record(z.string(), z.unknown())); export const coordinates = z.array(z.unknown()); export const bbox = z.array(z.unknown()); export const placeType = z.enum(["continental_marine", "country", "major_landform", "region", "subregion", "county", "joint_municipality", "joint_submunicipality", "municipality", "municipal_district", "locality", "neighbourhood", "place", "postal_code", "address", "road", "poi"]); export const result = z.object({type: z.literal("FeatureCollection"), features: z.array(z.object({id: z.string(), text: z.string(), language: z.string().optional(), type: z.literal("Feature"), properties: featureProperties, geometry: z.union([z.object({type: z.literal("Point"), coordinates: coordinates}), z.object({type: z.literal("LineString"), coordinates: z.array(coordinates)}), z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(coordinates))}), z.object({type: z.literal("MultiPoint"), coordinates: z.array(coordinates)}), z.object({type: z.literal("MultiLineString"), coordinates: z.array(z.array(coordinates))}), z.object({type: z.literal("MultiPolygon"), coordinates: z.array(z.array(z.array(coordinates)))}), z.object({type: z.literal("GeometryCollection"), geometries: z.array(z.union([z.object({type: z.literal("Point"), coordinates: coordinates}), z.object({type: z.literal("LineString"), coordinates: z.array(coordinates)}), z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(coordinates))}), z.object({type: z.literal("MultiPoint"), coordinates: z.array(coordinates)}), z.object({type: z.literal("MultiLineString"), coordinates: z.array(z.array(coordinates))}), z.object({type: z.literal("MultiPolygon"), coordinates: z.array(z.array(z.array(coordinates)))})]))})]), bbox: bbox, center: coordinates, place_name: z.string(), matching_place_name: z.string().optional(), matching_text: z.string().optional(), place_type: z.array(placeType), place_type_name: z.array(z.string()), relevance: z.number(), context: z.array(z.object({ref: z.string(), country_code: z.string(), kind: z.enum(["road", "road_relation", "admin_area", "place", "street", "virtual_street"]).optional(), categories: z.array(z.string()).optional(), feature_tags: z.record(z.string(), z.string()).optional(), place_designation: z.enum(["allotments", "borough", "city", "city_block", "farm", "hamlet", "isolated_dwelling", "neighbourhood", "plot", "quarter", "suburb", "town", "village"]).optional(), id: z.string(), text: z.string(), language: z.string().optional()}).and(z.record(z.string(), z.unknown()))).optional(), address: z.string().optional()}).and(z.record(z.string(), z.unknown()))), query: z.array(z.string()), attribution: z.string()}); 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 by feature ID * * GET /geocoding/{id}.json */ export const getApiGeocodingIdJson = async (options?: GetApiGeocodingIdJsonOptions) => { 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 (key !== undefined) query.set("key", key) const url = `${baseUrl.replace(/\/+$/, '')}/geocoding/${encodeURIComponent(String(args.id))}.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(result, await res.json()) };