import {z} from 'zod' export type Polygon = {type: 'Polygon', coordinates: Array>>, bbox?: Array | undefined}; export type SearchResource = {location?: (Polygon | {ref: {uri: string}}) | undefined, start_date?: string | undefined, end_date?: string | undefined, resolution?: (Array<'low' | 'medium' | 'high' | 'very_high'> | 'low' | 'medium' | 'high' | 'very_high') | undefined, coverage?: number | undefined, interval_length?: number | undefined, order_by?: (Array<'resolution' | 'cost' | 'coverage' | 'date' | '-resolution' | '-cost' | '-coverage' | '-date'> | 'resolution' | 'cost' | 'coverage' | 'date' | '-resolution' | '-cost' | '-coverage' | '-date') | undefined, off_nadir_angle?: Array | undefined}; export type CreateApiArchiveSearchArgs = {body: SearchResource}; export type CreateApiArchiveSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const polygon = z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number()))), bbox: z.array(z.number()).optional()}); export const searchPostResponse = z.object({data: z.object({id: z.string().optional(), location: z.union([polygon, z.object({ref: z.object({uri: z.string().min(1)})})]).optional(), start_date: z.string().optional(), end_date: z.string().optional(), resolution: z.union([z.array(z.enum(["low", "medium", "high"])), z.enum(["low", "medium", "high"])]).optional(), coverage: z.number().gte(0).optional(), interval_length: z.number().gte(0).optional(), order_by: z.union([z.array(z.enum(["resolution", "cost", "coverage", "date", "-resolution", "-cost", "-coverage", "-date"])), z.enum(["resolution", "cost", "coverage", "date", "-resolution", "-cost", "-coverage", "-date"])]).optional(), created_at: z.string().optional(), off_nadir_angle: z.array(z.number().gte(-90).lte(90)).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 }; /** * Search * * POST /archive/search */ export const createApiArchiveSearch = async (args: CreateApiArchiveSearchArgs, options?: CreateApiArchiveSearchOptions) => { const {baseUrl = "https://api.skywatch.co/earthcache", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/archive/search${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(searchPostResponse, await res.json()) };