import {z} from 'zod' export type SupportedGeojsonGeometry = {type: 'Polygon', coordinates: Array>>, bbox?: Array | undefined}; export type PipelineCalculatorRequest = {resolution: 'low' | 'medium' | 'high' | 'very_high', location: SupportedGeojsonGeometry | {ref: {uri: string}}, start_date?: string | undefined, end_date?: string | undefined, interval?: string | undefined, tasking?: boolean | undefined, return_probability_of_collection?: boolean | undefined, output?: ({id?: string | undefined}) | undefined, source?: string | undefined}; export type CreateApiPipelinesCalculateArgs = {body: PipelineCalculatorRequest}; export type CreateApiPipelinesCalculateOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pipelineCalculatorResponse = z.object({data: z.object({plan: z.enum(["test", "build", "scale"]).optional(), resolution: z.enum(["low", "medium", "high", "very_high"]).optional(), source: z.string().optional(), area_km2: z.number().gte(0).optional(), archive: z.object({price_km2: z.number().gte(0).optional(), minimum_area_km2: z.number().gte(0).optional(), discount_price_km2: z.number().gte(0).lte(100).optional(), interval_quantity: z.number().gte(0).optional(), interval_cost: z.number().gte(0).optional(), max_cost: z.number().gte(0).optional()}).optional(), tasking: z.object({price_km2: z.number().gte(0).optional(), minimum_area_km2: z.number().gte(0).optional(), discount_price_km2: z.number().gte(0).lte(100).optional(), interval_quantity: z.number().gte(0).optional(), interval_cost: z.number().gte(0).optional(), max_cost: z.number().gte(0).optional(), probability_of_collection: z.array(z.object({interval_start: z.string(), interval_end: z.string(), probability_percent: z.number().gte(0), probability_reason: z.string().min(1).optional()})).optional()}).optional(), max_cost: z.number().gte(0).optional(), prepurchase_balance: z.number().gte(0).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 }; /** * Calculate cost of area and intervals and probability of collection * * POST /pipelines/calculate */ export const createApiPipelinesCalculate = async (args: CreateApiPipelinesCalculateArgs, options?: CreateApiPipelinesCalculateOptions) => { 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(/\/+$/, '')}/pipelines/calculate${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(pipelineCalculatorResponse, await res.json()) };