import {z} from 'zod' export type Position2D = Array; export type Position3D = Array; export type Polygon = {bbox?: (Array | Array | null) | undefined, type: 'Polygon', coordinates: Array>}; export type MultiPolygon = {bbox?: (Array | Array | null) | undefined, type: 'MultiPolygon', coordinates: Array>>}; export type SamplingPointBodyParameters = Polygon | MultiPolygon | null; export type CreateApiDataObservationArgs = {skip?: number | undefined, limit?: number | undefined, determinand?: (string | null) | undefined, samplingPurpose?: (string | null) | undefined, sampleSamplingNotation?: (number | null) | undefined, date?: (string | null) | undefined, dateFrom?: (string | null) | undefined, dateTo?: (string | null) | undefined, complianceOnly?: boolean | undefined, sampleMaterialType?: (string | null) | undefined, easting?: (number | null) | undefined, northing?: (number | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined, radius?: (number | null) | undefined, precannedArea?: (string | null) | undefined, pointNotation?: (string | null) | undefined, prefLabel?: (string | null) | undefined, region?: (string | null) | undefined, area?: (string | null) | undefined, subArea?: (string | null) | undefined, samplingPointStatus?: (string | null) | undefined, samplingPointType?: (string | null) | undefined, 'Accept-Crs'?: ('http://www.opengis.net/def/crs/EPSG/0/27700' | 'http://www.opengis.net/def/crs/EPSG/0/4326') | undefined, 'CSV-Header'?: ('present' | 'absent') | undefined, 'API-Version'?: string | undefined, body: SamplingPointBodyParameters}; export type CreateApiDataObservationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiDataObservationResponse = z.unknown(); 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 }; /** * List and filter observations (including for multiple sampling points) * * POST /data/observation */ export const createApiDataObservation = async (args: CreateApiDataObservationArgs, options?: CreateApiDataObservationOptions) => { const {baseUrl = "https://environment.data.gov.uk/water-quality", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/ld+json") if (args.skip != null) query.append("skip", String(args.skip)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.determinand != null) query.append("determinand", String(args.determinand)) if (args.samplingPurpose != null) query.append("samplingPurpose", String(args.samplingPurpose)) if (args.sampleSamplingNotation != null) query.append("sampleSamplingNotation", String(args.sampleSamplingNotation)) if (args.date != null) query.append("date", String(args.date)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.complianceOnly != null) query.append("complianceOnly", String(args.complianceOnly)) if (args.sampleMaterialType != null) query.append("sampleMaterialType", String(args.sampleMaterialType)) if (args.easting != null) query.append("easting", String(args.easting)) if (args.northing != null) query.append("northing", String(args.northing)) if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.precannedArea != null) query.append("precannedArea", String(args.precannedArea)) if (args.pointNotation != null) query.append("pointNotation", String(args.pointNotation)) if (args.prefLabel != null) query.append("prefLabel", String(args.prefLabel)) if (args.region != null) query.append("region", String(args.region)) if (args.area != null) query.append("area", String(args.area)) if (args.subArea != null) query.append("subArea", String(args.subArea)) if (args.samplingPointStatus != null) query.append("samplingPointStatus", String(args.samplingPointStatus)) if (args.samplingPointType != null) query.append("samplingPointType", String(args.samplingPointType)) if (args['Accept-Crs'] != null) headers.set("Accept-Crs", String(args['Accept-Crs'])) if (args['CSV-Header'] != null) headers.set("CSV-Header", String(args['CSV-Header'])) if (args['API-Version'] != null) headers.set("API-Version", String(args['API-Version'])) const url = `${baseUrl.replace(/\/+$/, '')}/data/observation${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(createApiDataObservationResponse, await res.json()) };