import {z} from 'zod' export type GetApiEarthcacheAoiMonitorsMonitorIdResultsArgs = {monitor_id: string, limit?: number | undefined, cursor?: string | undefined}; export type GetApiEarthcacheAoiMonitorsMonitorIdResultsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const aOIMonitorLocation = z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number()))), bbox: z.array(z.number()).optional()}); export const aOIMonitorResult = z.object({id: z.string(), monitor_id: z.string(), location: aOIMonitorLocation, intersection: aOIMonitorLocation, source: z.string(), product_name: z.string(), resolution: z.number(), start_time: z.string(), end_time: z.string(), preview_uri: z.string(), thumbnail_uri: z.string(), location_coverage_percentage: z.number().gte(0).lte(100), area_sq_km: z.number(), price_per_sq_km: z.number().nullable().optional(), minimum_area_sq_km: z.number().nullable().optional(), cost: z.number().nullable().optional(), result_cloud_cover_percentage: z.number().gte(0).lte(100), sensor_type: z.string(), off_nadir_angle: z.number().nullable().optional(), product: z.object({}).nullable().optional(), properties: z.object({}).nullable().optional()}); export const paginatedAOIMonitorResults = z.object({data: z.array(aOIMonitorResult), limit: z.number().int(), next_cursor: z.string().nullable().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 }; /** * List AOI Monitor Results * * GET /earthcache/aoi-monitors/{monitor_id}/results */ export const getApiEarthcacheAoiMonitorsMonitorIdResults = async (args: GetApiEarthcacheAoiMonitorsMonitorIdResultsArgs, options?: GetApiEarthcacheAoiMonitorsMonitorIdResultsOptions) => { const {baseUrl = "https://api.skywatch.co/earthcache", fetch: fetchFn = fetch, ApiKey} = 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 (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/earthcache/aoi-monitors/${encodeURIComponent(String(args.monitor_id))}/results${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(paginatedAOIMonitorResults, await res.json()) };