import {z} from 'zod' export type GetApiMediaArgs = {missionId: string, siteId?: string | undefined, droneId?: string | undefined, mediaType?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiMediaOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mediaType = z.enum(["image", "video"]); export const captureType = z.enum(["rgb", "thermal"]).nullable(); export const gimbalOrientation = z.object({yaw: z.number(), pitch: z.number(), roll: z.number()}); export const resolution = z.object({width: z.number().int(), height: z.number().int()}); export const geoLocation = z.object({lat: z.number(), lng: z.number(), altitude: z.number().optional()}); export const geoPathPoint = z.object({timestampSec: z.number(), lat: z.number(), lng: z.number(), altitude: z.number().optional()}); export const missionMedia = z.object({id: z.string(), organizationId: z.string(), missionId: z.string(), droneId: z.string().nullable(), siteId: z.string().optional(), mediaType: mediaType, captureType: captureType, url: z.string(), thumbnailUrl: z.string(), capturedAt: z.number(), captureDevice: z.string().optional(), orientation: gimbalOrientation.optional(), focalLength: z.number().optional(), digitalZoom: z.number().optional(), fov: z.number().optional(), resolution: resolution.optional(), geoLocation: geoLocation.nullable(), duration: z.number().optional(), geoPath: z.array(geoPathPoint).optional(), deletedAt: z.number().nullable(), createdAt: z.number(), updatedAt: z.number(), commentCount: z.number().int().optional(), thermalRange: z.object({minTempF: z.number().nullable().optional(), maxTempF: z.number().nullable().optional()}).nullable().optional(), thermalDisplayRamp: z.array(z.string()).nullable().optional()}); export const paginationMeta = z.object({cursor: z.string().nullable(), total: z.number().int().optional()}); export const getApiMediaResponse = z.object({data: z.array(missionMedia), meta: paginationMeta}); 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 media for a mission * * GET /media * * Also accepts auth: ApiKeyAuth */ export const getApiMedia = async (args: GetApiMediaArgs, options: GetApiMediaOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.missionId != null) query.append("missionId", String(args.missionId)) if (args.siteId != null) query.append("siteId", String(args.siteId)) if (args.droneId != null) query.append("droneId", String(args.droneId)) if (args.mediaType != null) query.append("mediaType", String(args.mediaType)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/media${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(getApiMediaResponse, await res.json()) };