import {z} from 'zod' export type GetApiV1ApplicationsApplicationRequestIdInformationArgs = {application_request_id: string}; export type GetApiV1ApplicationsApplicationRequestIdInformationOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const correspondenceDocumentType = z.object({code: z.string().optional(), name: z.string().optional()}); export const correspondenceRecordDocumentItemWithDownloadId = z.object({created_at: z.string().optional(), download_id: z.string().optional(), type: correspondenceDocumentType}); export const correspondenceRecordInApplicationInformation = z.object({documents: z.array(correspondenceRecordDocumentItemWithDownloadId), record_id: z.string()}); export const problemDetails = z.object({type: z.string(), detail: z.string(), pointer: z.string().optional()}); export const applicationInformationResponse = z.object({correspondences: z.array(correspondenceRecordInApplicationInformation).optional(), errors: z.array(problemDetails).optional(), hmlr_reference: z.string().optional(), priority_timestamp: z.string().optional(), application_request_id: z.string().optional(), status: z.enum(["VALIDATING", "SYSTEM_ERROR", "VALIDATION_FAILED", "ACCEPTED_AWAITING_PRIORITY", "ACCEPTED_PRIORITY_PROTECTED"]).optional(), warnings: z.array(z.string()).optional()}); export const applicationInformationDataResponse = z.object({data: applicationInformationResponse.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 }; /** * Get application details * * GET /v1/applications/{application_request_id}/information */ export const getApiV1ApplicationsApplicationRequestIdInformation = async (args: GetApiV1ApplicationsApplicationRequestIdInformationArgs, options?: GetApiV1ApplicationsApplicationRequestIdInformationOptions) => { const {baseUrl = "https://businessgateway.landregistry.gov.uk/bg2/api", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/applications/${encodeURIComponent(String(args.application_request_id))}/information${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(applicationInformationDataResponse, await res.json()) };