import {z} from 'zod' export type GetApiV1PlacesArgs = {offset?: number | undefined, limit?: number | undefined, id?: Array | undefined, country?: Array | undefined, location?: string | undefined, q?: string | undefined, type?: (Array<'planet' | 'continent' | 'country' | 'region' | 'county' | 'localadmin' | 'locality' | 'neighbourhood' | 'major' | 'metro' | 'local'>) | undefined}; export type GetApiV1PlacesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const baseLevel = z.enum(["planet", "continent", "country", "region", "county", "localadmin", "locality", "neighbourhood"]); export const publicGeonamesPlaceResponse = z.object({id: z.string(), type: baseLevel, name: z.string(), county: z.string().optional(), region: z.string().optional(), country: z.string().optional(), country_alpha2: z.string().optional(), country_alpha3: z.string().optional(), location: z.array(z.number())}); export const publicGeonamesPlacesResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(publicGeonamesPlaceResponse)}); 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 Places * * GET /v1/places */ export const getApiV1Places = async (args: GetApiV1PlacesArgs, options?: GetApiV1PlacesOptions) => { const {baseUrl = "https://api.predicthq.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.id != null) query.append("id", args.id.join(',')) if (args.country != null) query.append("country", args.country.join(',')) if (args.location != null) query.append("location", String(args.location)) if (args.q != null) query.append("q", String(args.q)) if (args.type != null) query.append("type", args.type.join(',')) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/places${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(publicGeonamesPlacesResponse, await res.json()) };