import {z} from 'zod' export type GetApiCustomsGoodsMovementSystemMovementsArgs = {Accept: string, Authorization: string}; export type GetApiCustomsGoodsMovementSystemMovementsOptions = {baseUrl?: string | undefined, userRestricted?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCustomsGoodsMovementSystemMovementsResponse = z.array(z.object({gmrId: z.string(), direction: z.union([z.literal("UK_INBOUND"), z.literal("UK_OUTBOUND"), z.literal("GB_TO_NI"), z.literal("NI_TO_GB")]), isUnaccompanied: z.boolean().optional(), inspectionRequired: z.boolean().optional(), reportToLocations: z.array(z.object({inspectionTypeId: z.string(), locationIds: z.array(z.string())})).optional(), vehicleRegNum: z.string().optional(), trailerRegistrationNums: z.array(z.string()).optional(), containerReferenceNums: z.array(z.string()).optional(), state: z.union([z.literal("NOT_FINALISABLE"), z.literal("OPEN"), z.literal("CHECKED_IN"), z.literal("EMBARKED"), z.literal("COMPLETED")]), createdDateTime: z.string(), updatedDateTime: z.string(), plannedCrossing: z.object({routeId: z.string(), localDateTimeOfDeparture: z.string().optional()}).optional(), checkedInCrossing: z.object({routeId: z.string(), localDateTimeOfArrival: z.string()}).optional(), actualCrossing: z.object({routeId: z.string(), localDateTimeOfArrival: z.string()}).optional(), links: z.array(z.object({href: z.string(), method: z.literal("GET"), rel: z.literal("get-goods-movement-record")})).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 }; /** * List Goods Movement Records * * GET /customs/goods-movement-system/movements */ export const getApiCustomsGoodsMovementSystemMovements = async (args: GetApiCustomsGoodsMovementSystemMovementsArgs, options?: GetApiCustomsGoodsMovementSystemMovementsOptions) => { const {baseUrl = "https://api.service.hmrc.gov.uk", fetch: fetchFn = fetch, userRestricted} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (userRestricted !== undefined) headers.set('authorization', `Bearer ${userRestricted}`) const url = `${baseUrl.replace(/\/+$/, '')}/customs/goods-movement-system/movements${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(getApiCustomsGoodsMovementSystemMovementsResponse, await res.json()) };