import {z} from 'zod' export type GetApiCustomsGoodsMovementSystemReferenceDataArgs = {Accept: string}; export type GetApiCustomsGoodsMovementSystemReferenceDataOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCustomsGoodsMovementSystemReferenceDataResponse = z.object({routes: z.array(z.object({routeId: z.string(), routeDirection: z.enum(["UK_INBOUND", "UK_OUTBOUND", "GB_TO_NI", "NI_TO_GB"]), routeEffectiveFrom: z.string(), departurePortId: z.number().int(), arrivalPortId: z.number().int(), carrierId: z.number().int(), routeEffectiveTo: z.string().optional()})).optional(), ports: z.array(z.object({portId: z.number().int(), portRegion: z.string().optional(), portEffectiveFrom: z.string(), portDescription: z.string().min(3).max(255), timezoneId: z.string().max(50).optional(), chiefPortCode: z.string().optional(), cdsPortCode: z.string().optional(), officeOfTransitCustomsOfficeCode: z.string().min(8).max(8), portEffectiveTo: z.string().optional(), portCountryCode: z.string(), isOperatingArrivedExportsProcess: z.boolean().optional()})).optional(), carriers: z.array(z.object({carrierId: z.number().int(), carrierName: z.string().min(1).max(255), countryCode: z.string().optional()})).optional(), locations: z.array(z.object({locationId: z.string(), locationDescription: z.string(), address: z.object({lines: z.array(z.string()), town: z.string().optional(), postcode: z.string()}), locationType: z.enum(["IBF", "BCP", "ALL"]), supportedDirections: z.array(z.enum(["UK_INBOUND", "UK_OUTBOUND", "GB_TO_NI", "NI_TO_GB"])), locationEffectiveFrom: z.string(), locationEffectiveTo: z.string().optional(), supportedInspectionTypeIds: z.array(z.string()), requiredInspectionLocations: z.array(z.number().int())})).optional(), inspectionTypes: z.array(z.object({inspectionTypeId: z.string(), description: z.string()})).optional(), ruleFailures: z.array(z.object({ruleId: z.string(), ruleDescription: z.string()})).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 }; /** * This endpoint gets details of all reference data used by GVMS. * * GET /customs/goods-movement-system/reference-data */ export const getApiCustomsGoodsMovementSystemReferenceData = async (args: GetApiCustomsGoodsMovementSystemReferenceDataArgs, options?: GetApiCustomsGoodsMovementSystemReferenceDataOptions) => { const {baseUrl = "https://api.service.hmrc.gov.uk", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) const url = `${baseUrl.replace(/\/+$/, '')}/customs/goods-movement-system/reference-data${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(getApiCustomsGoodsMovementSystemReferenceDataResponse, await res.json()) };