import {z} from 'zod' export type GetApiV1LocationArgs = {limit?: number | undefined, cursor?: (string | null) | undefined, reference_key?: (string | null) | undefined, market_id?: (string | null) | undefined}; export type GetApiV1LocationOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonValue = z.unknown(); export const imageEvidenceExternalSchema = z.object({type: z.literal("image").optional(), image_id: z.string(), reference_key: z.string().nullable().optional(), visit_id: z.string().nullable().optional(), is_selected: z.boolean().optional(), is_starred: z.boolean().optional()}); export const attributeValueExternalSchema = z.object({attribute_id: z.string(), attribute_slug: z.string(), value_type: z.string(), value: z.union([z.boolean(), z.number().int(), z.number(), z.string(), z.array(jsonValue), z.record(z.string(), jsonValue)]).nullable(), unit: z.string().nullable(), reasoning: z.string().nullable(), evidence: z.array(imageEvidenceExternalSchema).optional(), recorded_by_visit_id: z.string().nullable(), recorded_at: z.string().nullable()}); export const locationWithAttributesExternalSchema = z.object({id: z.string(), address_1: z.string(), address_2: z.string().nullable(), city: z.string(), state: z.string(), zip_code: z.string(), lat: z.number().nullable(), lon: z.number().nullable(), reference_key: z.string().nullable(), market_id: z.string().nullable(), market_name: z.string().nullable(), custom_fields: z.object({}).nullable(), attributes: z.array(attributeValueExternalSchema).optional()}); export const listLocationsResponse = z.object({locations: z.array(locationWithAttributesExternalSchema), next_cursor: z.string().nullable()}); 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 }; /** * List locations * * GET /v1/location */ export const getApiV1Location = async (args: GetApiV1LocationArgs, options?: GetApiV1LocationOptions) => { const {baseUrl = "https://api.navigateai.co", fetch: fetchFn = fetch, ApiKeyAuth} = 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.reference_key != null) query.append("reference_key", String(args.reference_key)) if (args.market_id != null) query.append("market_id", String(args.market_id)) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/location${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(listLocationsResponse, await res.json()) };