import {z} from 'zod' export type GetApiCollectionsArgs = {otherParameters?: {[key: string]: unknown} | undefined}; export type GetApiCollectionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string(), rel: z.string().optional(), type: z.string().optional(), hreflang: z.string().optional(), title: z.string().optional(), length: z.number().int().optional()}); export const extent = z.object({spatial: z.object({bbox: z.array(z.array(z.number())).optional(), crs: z.literal("http://www.opengis.net/def/crs/OGC/1.3/CRS84").optional()}).optional(), temporal: z.object({interval: z.array(z.array(z.string().nullable())).optional(), trs: z.literal("http://www.opengis.net/def/uom/ISO-8601/0/Gregorian").optional()}).optional()}); export const collection = z.object({id: z.string(), title: z.string().optional(), description: z.string().optional(), links: z.array(link), extent: extent.optional(), itemType: z.string().optional(), crs: z.array(z.string()).optional()}); export const collections = z.object({links: z.array(link), collections: z.array(collection), crs: z.array(z.string()).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 }; /** * the feature collections in the dataset * * GET /collections */ export const getApiCollections = async (args: GetApiCollectionsArgs, options?: GetApiCollectionsOptions) => { const {baseUrl = "https://geoservice.dlr.de/eoc/ogc/stac/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.otherParameters != null) query.append("otherParameters", JSON.stringify(args.otherParameters)) const url = `${baseUrl.replace(/\/+$/, '')}/collections${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(collections, await res.json()) };