import {z} from 'zod' export type GetApiCollectionsCollectionIdArgs = {collectionId: string, 'If-Match'?: string | undefined, 'If-None-Match'?: string | undefined}; export type GetApiCollectionsCollectionIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const extent = z.object({spatial: z.object({bbox: z.array(z.array(z.number()))}), temporal: z.object({interval: z.array(z.array(z.string()))})}); export const license = z.string(); export const providers = z.array(z.object({description: z.string().optional(), name: z.string(), roles: z.array(z.enum(["producer", "licensor", "processor", "host"])).optional(), url: z.string().optional()})); export const stacVersion = z.string(); export const created = z.string(); export const updated = z.string(); export const link = z.object({href: z.string(), rel: z.string(), title: z.string().optional(), type: z.string().optional(), method: z.enum(["GET", "POST"]).optional()}); export const collection = z.object({crs: z.array(z.string()).optional(), description: z.string(), extent: extent, id: z.string(), itemType: z.string().optional(), license: license, providers: providers.optional(), stac_version: stacVersion, summaries: z.record(z.string(), z.union([z.array(z.unknown()), z.object({max: z.union([z.string(), z.number()]), min: z.union([z.string(), z.number()])})])).optional(), title: z.string().optional(), created: created, updated: updated, links: z.array(link)}); 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 }; /** * Fetch a single collection * * GET /collections/{collectionId} */ export const getApiCollectionsCollectionId = async (args: GetApiCollectionsCollectionIdArgs, options?: GetApiCollectionsCollectionIdOptions) => { const {baseUrl = "http://data.geo.admin.ch/api/stac/v0.9", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) const url = `${baseUrl.replace(/\/+$/, '')}/collections/${encodeURIComponent(String(args.collectionId))}${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(collection, await res.json()) };