import {z} from 'zod' export type GetApiCollectionsCollectionIdItemsArgs = {collectionId: string, limit?: number | undefined, bbox?: Array | undefined, datetime?: string | undefined}; export type GetApiCollectionsCollectionIdItemsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 featureGeoJSON = z.object({type: z.literal("Feature"), geometry: geometryGeoJSON, properties: z.object({}).nullable(), id: z.union([z.string(), z.number().int()]).optional(), links: z.array(link).optional()}); export const timeStamp = z.string(); export const numberMatched = z.number().int().gte(0); export const numberReturned = z.number().int().gte(0); export const featureCollectionGeoJSON = z.object({type: z.literal("FeatureCollection"), features: z.array(featureGeoJSON), links: z.array(link).optional(), timeStamp: timeStamp.optional(), numberMatched: numberMatched.optional(), numberReturned: numberReturned.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 }; /** * fetch features * * GET /collections/{collectionId}/items */ export const getApiCollectionsCollectionIdItems = async (args: GetApiCollectionsCollectionIdItemsArgs, options?: GetApiCollectionsCollectionIdItemsOptions) => { const {baseUrl = "http://bbox:8080", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/geo+json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.bbox != null) query.append("bbox", args.bbox.join(',')) if (args.datetime != null) query.append("datetime", String(args.datetime)) const url = `${baseUrl.replace(/\/+$/, '')}/collections/${encodeURIComponent(String(args.collectionId))}/items${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(featureCollectionGeoJSON, await res.json()) };