import {z} from 'zod' export type Bbox = Array; export type DatetimeInterval = string; export type PointGeoJSON = {type: 'Point', coordinates: Array}; export type MultipointGeoJSON = {type: 'MultiPoint', coordinates: Array>}; export type LinestringGeoJSON = {type: 'LineString', coordinates: Array>}; export type MultilinestringGeoJSON = {type: 'MultiLineString', coordinates: Array>>}; export type PolygonGeoJSON = {type: 'Polygon', coordinates: Array>>}; export type MultipolygonGeoJSON = {type: 'MultiPolygon', coordinates: Array>>>}; export type GeometrycollectionGeoJSON = {type: 'GeometryCollection', geometries: Array}; export type GeometryGeoJSON = PointGeoJSON | MultipointGeoJSON | LinestringGeoJSON | MultilinestringGeoJSON | PolygonGeoJSON | MultipolygonGeoJSON | GeometrycollectionGeoJSON; export type CollectionsArray = Array; export type Ids = Array; export type Limit = number; export type Sortby = Array<{field: string, direction: 'asc' | 'desc'}>; export type Fields = {include?: Array | undefined, exclude?: Array | undefined}; export type Query = string; export type SearchBody = {bbox?: Bbox | undefined, datetime?: DatetimeInterval | undefined, intersects?: GeometryGeoJSON | undefined, collections?: CollectionsArray | undefined, ids?: Ids | undefined, limit?: Limit | undefined, sortby?: Sortby | undefined, fields?: Fields | undefined, query?: Query | undefined}; export type CreateApiSearchArgs = {body: SearchBody}; export type CreateApiSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const stacVersion = z.string(); export const stacExtensions = z.array(z.union([z.string(), z.string()])); export const itemId = z.string(); export const bbox = z.array(z.number()); export const pointGeoJSON = z.object({type: z.literal("Point"), coordinates: z.array(z.number())}); export const multipointGeoJSON = z.object({type: z.literal("MultiPoint"), coordinates: z.array(z.array(z.number()))}); export const linestringGeoJSON = z.object({type: z.literal("LineString"), coordinates: z.array(z.array(z.number()))}); export const multilinestringGeoJSON = z.object({type: z.literal("MultiLineString"), coordinates: z.array(z.array(z.array(z.number())))}); export const polygonGeoJSON = z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number())))}); export const multipolygonGeoJSON = z.object({type: z.literal("MultiPolygon"), coordinates: z.array(z.array(z.array(z.array(z.number()))))}); export const geometrycollectionGeoJSON = z.object({type: z.literal("GeometryCollection"), geometries: z.array(z.lazy(() => geometryGeoJSON))}); export const geometryGeoJSON: z.ZodType = z.union([pointGeoJSON, multipointGeoJSON, linestringGeoJSON, multilinestringGeoJSON, polygonGeoJSON, multipolygonGeoJSON, geometrycollectionGeoJSON]); export const itemType = z.literal("Feature"); export const datetime = z.string().nullable(); export const itemProperties = z.object({datetime: datetime}).and(z.record(z.string(), z.object({}))); 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 itemAssets = z.record(z.string(), z.object({href: z.string(), title: z.string().optional(), description: z.string().optional(), type: z.string().optional(), roles: z.array(z.string()).optional()})); export const item = z.object({stac_version: stacVersion, stac_extensions: stacExtensions.optional(), id: itemId, bbox: bbox, geometry: geometryGeoJSON, type: itemType, properties: itemProperties, links: z.array(link), assets: itemAssets}); export const itemCollectionLinks = z.array(link); export const itemCollectionContext = z.object({returned: z.number().int().gte(0), limit: z.number().int().gte(0).nullable().optional(), matched: z.number().int().gte(0).optional()}); export const itemCollection = z.object({type: z.literal("FeatureCollection"), features: z.array(item), links: itemCollectionLinks.optional(), context: itemCollectionContext.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 STAC items with full-featured filtering. * * POST /search */ export const createApiSearch = async (args: CreateApiSearchArgs, options?: CreateApiSearchOptions) => { const {baseUrl = "https://geoservice.dlr.de/eoc/ogc/stac/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/geo+json") const url = `${baseUrl.replace(/\/+$/, '')}/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(itemCollection, await res.json()) };