import {z} from 'zod' export type GetApiWasteCarriersBrokersRegistrationIdArgs = {id: string}; export type GetApiWasteCarriersBrokersRegistrationIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.object({publisher: z.string(), licence: z.string(), documentation: z.string(), version: z.string().optional(), comment: z.string().optional(), hasFormat: z.array(z.string()), limit: z.number().int().optional(), offset: z.number().int().optional()}); export const iD = z.string(); export const rDFtype = z.string(); export const holderType = z.string(); export const holderDetail = z.object({"@id": iD, name: z.string(), tradingName: z.string().optional(), type: z.union([z.object({"@id": holderType.optional()}), z.array(z.object({"@id": holderType.optional()}))]).optional()}); export const registrationType = z.object({"@id": iD, notation: z.string().optional(), label: z.string().optional(), prefLabel: z.string().optional(), seeAlso: z.union([z.string(), z.object({"@id": z.string().optional()})]).optional()}); export const tier = z.object({"@id": iD.optional(), label: z.string().optional()}); export const postcodeURI = z.string(); export const addressDetail = z.object({address: z.string(), postcode: z.union([z.number().int(), z.string()]).optional(), organization_name: z.string().optional(), street_address: z.string().optional(), locality: z.string().optional(), postcodeURI: z.object({"@id": postcodeURI}).optional()}); export const wasteCarrierBrokerRegistrationDetail = z.object({"@id": iD, register: z.object({"@id": z.enum(["http://environment.data.gov.uk/public-register/waste-operations/register", "http://environment.data.gov.uk/public-register/end-of-life-vehicles/register", "http://environment.data.gov.uk/public-register/industrial-installations/register", "http://environment.data.gov.uk/public-register/water-discharges/register", "http://environment.data.gov.uk/public-register/radioactive-substance/register", "http://environment.data.gov.uk/public-register/waste-carriers-brokers/register", "http://environment.data.gov.uk/public-register/waste-exemptions/register", "http://environment.data.gov.uk/public-register/water-discharge-exemptions/register", "http://environment.data.gov.uk/public-register/scrap-metal-dealers/register", "http://environment.data.gov.uk/public-register/enforcement-action/register", "http://environment.data.gov.uk/public-register/flood-risk-exemptions/register"]), label: z.string(), type: z.object({"@id": iD.optional()}).optional()}), registrationNumber: z.union([z.string(), z.number()]), type: z.array(z.object({"@id": rDFtype.optional()})), holder: z.union([holderDetail, z.array(holderDetail)]).optional(), applicantType: z.object({"@id": z.string().optional(), prefLabel: z.string().optional()}).optional(), registrationDate: z.string(), expiryDate: z.string().optional(), registrationType: registrationType.optional(), tier: tier, regime: z.object({"@id": z.string().optional(), prefLabel: z.string().optional()}), site: z.object({"@id": iD, siteAddress: addressDetail, location: z.object({}).optional()}).optional()}); export const getApiWasteCarriersBrokersRegistrationIdResponse = z.object({meta: metadata.optional(), items: z.array(wasteCarrierBrokerRegistrationDetail).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 all details about a single waste carrier, broker, or dealer * * GET /waste-carriers-brokers/registration/{id} */ export const getApiWasteCarriersBrokersRegistrationId = async (args: GetApiWasteCarriersBrokersRegistrationIdArgs, options?: GetApiWasteCarriersBrokersRegistrationIdOptions) => { const {baseUrl = "https://environment.data.gov.uk/public-register", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/waste-carriers-brokers/registration/${encodeURIComponent(String(args.id))}${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(getApiWasteCarriersBrokersRegistrationIdResponse, await res.json()) };