import {z} from 'zod' export type DocumentUploadUrlData = {document_type: 'ABSTRACT' | 'ACKAPU' | 'ADJAR' | 'ADJREVU' | 'ADMIN_LTRS' | 'ADV1' | 'ADV2' | 'AGREEMENT' | 'AMS1' | 'AN1' | 'AP1' | 'APPL_FRM' | 'APPOINTMENT_OF_TRUSTEE' | 'AS1' | 'AS2' | 'AS3' | 'ASSENT' | 'ASSIGNMENT' | 'BIRTH_CERT' | 'BOUND_PLAN' | 'CC' | 'CCD' | 'CERT_CON_CH' | 'CERT_DEATH' | 'CERT_RED_EX' | 'CERT_REG_CH' | 'CERT_S89' | 'CH1' | 'CH2' | 'CH3' | 'CHARG_ORD' | 'CHARGE' | 'CIVIL_CERT' | 'CM2' | 'CM5' | 'CM6' | 'CN1' | 'CNFRM_DTMN_LTA_LGHA' | 'CNFRM_NO_DEED_SUR' | 'CNFRM_NO_LTA_LGHA' | 'CNSNT_ADMIN_COURT' | 'COMP_DOC' | 'CONSENT' | 'CONST_COMP' | 'CONTR_RES' | 'CONV_CERT' | 'CONV_CERT_DEATH' | 'CONVEYANCE' | 'COR_CERT' | 'CORRES' | 'COSR' | 'COURT_ORD' | 'CRALIQ' | 'CRED_DEC' | 'CRED_RES' | 'CRT_FORM_7' | 'CRWALIQ' | 'DB' | 'DEATH_CERT' | 'DEBENTURE' | 'DEC_AB_NULL_MARR' | 'DEED' | 'DEED_POLL' | 'DEED_REG_DIS' | 'DEED_SUR' | 'DISCHARGE' | 'DJP' | 'DL' | 'DOP' | 'DRIVING_LICENCE' | 'DS1' | 'DS2' | 'DS3' | 'DS3_SHORT' | 'EOB' | 'EOL' | 'EVD_NO_RENEW' | 'EVIDENCE' | 'EV_OV_NAME_CHANGE' | 'EX1' | 'EX1A' | 'EX3' | 'FORM_600' | 'FORM_600CH' | 'FORM_DI' | 'FORM_JO' | 'FR1' | 'GENDER_CERT' | 'GVD' | 'HR1' | 'HR2' | 'HR4' | 'ID1' | 'ID2' | 'ID3' | 'ID4' | 'ID5' | 'ID_EV' | 'ID_FRM' | 'INDENTURE' | 'JP1' | 'K17_OR_K18' | 'K20' | 'K22' | 'K3' | 'K4' | 'LEASE' | 'LICENCE' | 'LIQ_CERT_1' | 'LIQ_CERT_2' | 'LIQ_CERT_3' | 'LIQ01' | 'LNDLD_CNSNT' | 'LONP' | 'LR_CORRES' | 'LTT' | 'MARR_CERT' | 'MEMO' | 'MORD' | 'MRTG_DEED' | 'NOD' | 'ORD_DISS_NULL_CP' | 'PASSPORT' | 'PLAN' | 'POA' | 'PROBATE' | 'PSS1' | 'REVERS_INT' | 'RN' | 'RX1' | 'RX2' | 'RX3' | 'RX4' | 'RXC' | 'RXCD' | 'SC' | 'SDLT' | 'SEV' | 'SHRF_RTN' | 'SIDE_LTR' | 'SOT' | 'SSALIQ' | 'STAT_DEC' | 'SUB_CHARGE' | 'TP1' | 'TP2' | 'TR1' | 'TR2' | 'TR4' | 'TR5' | 'TRANSFER' | 'TRUSTEE_RESOLUTION' | 'UN1' | 'UN2' | 'UN3' | 'UN4' | 'UT1' | 'VAR_CHARGE' | 'VAR_COV' | 'VAR_EAS' | 'VAR_LEASE' | 'VESTING_ORDER' | 'WCT' | 'WINDING_UP' | 'WIT_STATE', additional_provider_filter?: string | undefined, file_length: number, file_sha256: string}; export type DocumentUploadUrlRequest = {data: DocumentUploadUrlData}; export type CreateApiV1DocumentsUrlArgs = {body: DocumentUploadUrlRequest}; export type CreateApiV1DocumentsUrlOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const documentUploadUrlResponse = z.object({upload_url: z.string().optional(), document_id: z.string().optional()}); export const documentUploadUrlDataResponse = z.object({data: documentUploadUrlResponse.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 }; /** * Request upload URL for your application attachments * * POST /v1/documents/url */ export const createApiV1DocumentsUrl = async (args: CreateApiV1DocumentsUrlArgs, options?: CreateApiV1DocumentsUrlOptions) => { const {baseUrl = "https://businessgateway.landregistry.gov.uk/bg2/api", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/documents/url${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(documentUploadUrlDataResponse, await res.json()) };