import {z} from 'zod' export type GetApiV1AssetsArgs = {limit?: number | undefined, cursor?: (string | null) | undefined, location_id?: (Array | null) | undefined}; export type GetApiV1AssetsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const assetTypeExternalSchema = z.object({slug: z.string()}); export const assetImageExternalSchema = z.object({image_id: z.string(), visit_id: z.string().nullable()}); 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 assetExternalSchema = z.object({id: z.string(), location_id: z.string(), room_id: z.string().nullable(), asset_type: assetTypeExternalSchema, name: z.string().nullable(), description: z.string().nullable(), first_observed_visit_id: z.string().nullable(), last_observed_visit_id: z.string().nullable(), last_observed_at: z.string().nullable(), images: z.array(assetImageExternalSchema), attributes: z.array(attributeValueExternalSchema)}); export const listAssetsResponse = z.object({assets: z.array(assetExternalSchema), 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 assets * * GET /v1/assets */ export const getApiV1Assets = async (args: GetApiV1AssetsArgs, options?: GetApiV1AssetsOptions) => { 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)) for (const item of args.location_id ?? []) query.append("location_id", String(item)) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/assets${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(listAssetsResponse, await res.json()) };