import {z} from 'zod' export type CreateApiPostalcodeArgs = {key: unknown, body: {postalcode?: string | undefined, country?: string | undefined, at?: ({lat?: number | undefined, lng?: number | undefined}) | undefined, format?: 'geojson' | undefined}}; export type CreateApiPostalcodeOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPostalcodeResponse = z.object({warning: z.array(z.string()).optional(), places: z.object({geopoint: z.object({lat: z.number().optional(), lng: z.number().optional()}).optional(), country: z.string().optional(), countryCode: z.string().optional(), state: z.string().optional(), district: z.string().optional(), subdistrict: z.string().optional(), postalcode: z.string().optional(), address: z.string().optional(), distance: z.number().optional(), boundary: z.object({multipolygon: z.array(z.object({polygon: z.array(z.object({points: z.array(z.object({lat: z.number().optional(), lng: z.number().optional()})).optional()})).optional()})).optional(), type: z.string().optional(), geometry: z.object({type: z.string().optional(), coordinates: z.array(z.array(z.array(z.number()))).optional()}).optional(), properties: z.string().optional()}).optional()}).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 coordinates by postal code * * POST /postalcode */ export const createApiPostalcode = async (args: CreateApiPostalcodeArgs, options?: CreateApiPostalcodeOptions) => { const {baseUrl = "https://api.nextbillion.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/postalcode${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiPostalcodeResponse, await res.json()) };