import {z} from 'zod' export type GetMapRequest = {mapId: string}; export type CreateApiV1MapGetArgs = {body: GetMapRequest}; export type CreateApiV1MapGetOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pose = z.object({xMeters: z.number().optional(), yMeters: z.number().optional(), headingRadians: z.number().optional()}); export const destination = z.object({destinationId: z.string().optional(), displayName: z.string().optional(), pose: pose.optional()}); export const annotation = z.object({annotationId: z.string().optional(), displayName: z.string().optional(), createdTime: z.string().optional(), destinations: z.record(z.string(), destination).optional()}); export const mapImageFileInfo = z.object({checksum: z.number().int().optional(), size: z.string().optional(), md5Checksum: z.string().optional()}); export const signedURL = z.object({url: z.string().optional(), expiresAt: z.string().optional()}); export const mapImageDownloadInfo = z.object({fileInfo: mapImageFileInfo.optional(), downloadUrl: signedURL.optional()}); export const origin = z.object({xM: z.number().optional(), yM: z.number().optional(), yawRadians: z.number().optional()}); export const map = z.object({mapId: z.string().optional(), createdTime: z.string().optional(), modifiedTime: z.string().optional(), displayName: z.string().optional(), annotation: annotation.optional(), imageDownloadInfo: mapImageDownloadInfo.optional(), origin: origin.optional(), resolution: z.number().optional()}); export const getMapResponse = z.object({map: map.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 a saved map by map_id from the cloud. * * POST /v1/map/get */ export const createApiV1MapGet = async (args: CreateApiV1MapGetArgs, options?: CreateApiV1MapGetOptions) => { const {baseUrl = "https://api.bearrobotics.ai", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/map/get${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(getMapResponse, await res.json()) };