import {z} from 'zod' export type Latitude = number; export type Longitude = number; export type GetApiV1PoiArgs = {lat?: Latitude | undefined, lng?: Longitude | undefined, query?: string | undefined, radius?: number | undefined, category?: string | undefined, subCategory?: string | undefined, groupCategory?: string | undefined, entityType?: (Array<'venue' | 'complex' | 'chain'>) | undefined, limit?: number | undefined, skip?: number | undefined, includeFlaggedEntities?: boolean | undefined}; export type GetApiV1PoiOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const searchResponse = z.object({requestId: requestId}); 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 for Placer Entities * * GET /v1/poi */ export const getApiV1Poi = async (args: GetApiV1PoiArgs, options?: GetApiV1PoiOptions) => { const {baseUrl = "https://papi.placer.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.lat != null) query.append("lat", String(args.lat)) if (args.lng != null) query.append("lng", String(args.lng)) if (args.query != null) query.append("query", String(args.query)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.category != null) query.append("category", String(args.category)) if (args.subCategory != null) query.append("subCategory", String(args.subCategory)) if (args.groupCategory != null) query.append("groupCategory", String(args.groupCategory)) if (args.entityType != null) query.append("entityType", args.entityType.join(',')) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.includeFlaggedEntities != null) query.append("includeFlaggedEntities", String(args.includeFlaggedEntities)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/poi${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(searchResponse, await res.json()) };