import {z} from 'zod' export type GetApiV0ApplicationsApplicationRequestIdInformationArgs = {application_request_id: string}; export type GetApiV0ApplicationsApplicationRequestIdInformationOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const problemDetails = z.object({type: z.string(), detail: z.string(), pointer: z.string().optional()}); export const transactionReference = z.string().min(1).max(50); export const transactionFee = z.object({transaction_reference: transactionReference.optional(), fee: z.number().int().optional()}); export const statusResponse = z.object({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(), calculated_fees: z.object({total_fee: z.number().int().optional(), per_transaction: z.array(transactionFee).optional()}).optional(), warnings: z.array(z.string()).optional()}); export const statusDataResponse = z.object({data: statusResponse.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 }; /** * Application information API - Get the information about an application * * GET /v0/applications/{application_request_id}/information */ export const getApiV0ApplicationsApplicationRequestIdInformation = async (args: GetApiV0ApplicationsApplicationRequestIdInformationArgs, options?: GetApiV0ApplicationsApplicationRequestIdInformationOptions) => { 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(/\/+$/, '')}/v0/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(statusDataResponse, await res.json()) };