import {z} from 'zod' export type SearchOfferingsRequest = {location: Record, category: 'archive' | 'tasking', resolution_low?: number | undefined, resolution_high?: number | undefined, product_name?: string | undefined, partner_name?: string | undefined, sensor_type?: (Array<'optical' | 'aerial' | 'sar' | 'elevation' | 'basemap' | 'drone'>) | undefined}; export type CreateApiProductsOfferingsSearchArgs = {body: SearchOfferingsRequest}; export type CreateApiProductsOfferingsSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const configurationProperty = z.object({id: z.string(), name: z.string(), value: z.unknown(), type: z.string().optional(), default: z.unknown(), display: z.object({label: z.string().optional(), type: z.string().optional(), description: z.string().optional(), design_component: z.string().optional(), tooltip: z.string().optional(), placeholder: z.string().optional(), order: z.number().int().optional()}).optional()}); export const configurationParameter = z.object({id: z.string(), name: z.string(), value: z.unknown(), type: z.string().optional(), default: z.unknown(), range: z.object({min: z.number().optional(), max: z.number().optional(), step: z.number().optional()}).optional(), values: z.array(z.string()).optional(), display: z.object({label: z.string().optional(), type: z.string().optional(), tooltip: z.string().optional(), placeholder: z.string().optional(), order: z.number().int().optional()}).optional()}); export const configuration = z.object({id: z.string(), name: z.string().optional(), is_default: z.boolean().optional(), is_standard: z.boolean().optional(), properties: z.object({data: z.array(configurationProperty).optional()}).optional(), parameters: z.object({data: z.array(configurationParameter).optional()}).optional()}); export const purchaseOptions = z.record(z.string(), z.unknown()); export const offeringDetail = z.object({offering_id: z.string(), offering_name: z.string(), configurations: z.array(configuration), purchase_options: purchaseOptions.optional(), on_platform: z.boolean(), category: z.string()}); export const productProperties = z.record(z.string(), z.unknown()); export const product = z.object({product_id: z.string(), partner_name: z.string(), product_name: z.string(), resolution: z.number(), bands: z.array(z.string()), offerings: z.array(offeringDetail), properties: productProperties.optional(), subcategory: z.string().nullable().optional()}); export const createApiProductsOfferingsSearchResponse = z.array(product); 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 }; /** * Discover available offerings * * POST /products/offerings/search */ export const createApiProductsOfferingsSearch = async (args: CreateApiProductsOfferingsSearchArgs, options?: CreateApiProductsOfferingsSearchOptions) => { 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(/\/+$/, '')}/products/offerings/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(createApiProductsOfferingsSearchResponse, await res.json()) };