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 CreateApiDataSamplingPointArgs = {skip?: number | undefined, limit?: number | 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, 'API-Version'?: string | undefined, body: SamplingPointBodyParameters}; export type CreateApiDataSamplingPointOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const hydraViewModel = z.object({first: z.string().nullable().optional(), last: z.string().nullable().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional()}); export const geometryModel = z.object({id: z.string(), "@type": z.string().optional(), asWKT: z.string()}); export const samplingPointStatusModel = z.record(z.string(), z.unknown()); export const samplingPointTypeModel = z.record(z.string(), z.unknown()); export const samplingPointRegionModel = z.record(z.string(), z.unknown()); export const samplingPointAreaModel = z.record(z.string(), z.unknown()); export const samplingPointSubAreaModel = z.record(z.string(), z.unknown()); export const samplingPointModel = z.object({id: z.string(), "@type": z.array(z.string()).optional(), altLabel: z.string(), prefLabel: z.string().nullable().optional(), notation: z.string().nullable().optional(), hasObservations: z.string().nullable().optional(), geometry: geometryModel.nullable().optional(), samplingPointStatus: samplingPointStatusModel.nullable().optional(), samplingPointType: samplingPointTypeModel.nullable().optional(), region: samplingPointRegionModel.nullable().optional(), area: samplingPointAreaModel.nullable().optional(), subArea: samplingPointSubAreaModel.nullable().optional()}); export const hydraSamplingPointModel = z.object({"@context": z.string().optional(), "@type": z.string().optional(), totalItems: z.number().int().optional(), view: hydraViewModel.nullable().optional(), member: z.array(samplingPointModel)}); export const geoJSONGeometry = z.object({coordinates: z.array(z.number()), type: z.string().optional()}); export const geoJSONProperties = z.object({name: z.string().nullable(), status: z.string().nullable(), type: z.string().nullable(), region: z.string().nullable(), area: z.string().nullable(), subArea: z.string().nullable(), notation: z.string().nullable(), isDistributionOf: z.string()}); export const geoJSONFeatureModel = z.object({type: z.string().optional(), geometry: geoJSONGeometry, properties: geoJSONProperties}); export const hydraGeoJSONFeatureModel = z.object({"@type": z.string().optional(), member: z.array(geoJSONFeatureModel), totalItems: z.number().int().nullable().optional(), view: z.record(z.string(), z.unknown()).nullable().optional()}); export const createApiDataSamplingPointResponse = z.union([hydraSamplingPointModel, hydraGeoJSONFeatureModel]); 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 sampling points (including GeoJSON Polygon or MultiPolygon) * * POST /data/sampling-point */ export const createApiDataSamplingPoint = async (args: CreateApiDataSamplingPointArgs, options?: CreateApiDataSamplingPointOptions) => { 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.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['API-Version'] != null) headers.set("API-Version", String(args['API-Version'])) const url = `${baseUrl.replace(/\/+$/, '')}/data/sampling-point${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(createApiDataSamplingPointResponse, await res.json()) };