import {z} from 'zod' export type AddressLookupV2RequestAttributes = {locality_lookup?: (Array | null) | undefined, postal_code_lookup?: (Array | null) | undefined}; export type Key = {type?: (string | null) | undefined, value?: (string | null) | undefined}; export type AddressInputComponentName = {forename?: (string | null) | undefined, middlename?: (string | null) | undefined, surname?: (string | null) | undefined}; export type AddressLookupV2Request = {country_iso: string, datasets: Array | null, max_addresses?: (number | null) | undefined, max_suggestions?: (number | null) | undefined, attributes?: AddressLookupV2RequestAttributes | undefined, key?: Key | undefined, preferred_language?: (Array | null) | undefined, preferred_script?: (Array | null) | undefined, names?: (Array | null) | undefined, layouts?: (Array | null) | undefined}; export type CreateApiAddressLookupV2Args = {'Reference-Id'?: string | undefined, 'Timeout-Seconds'?: number | undefined, 'Add-Addresses'?: string | undefined, 'Add-FinalAddress'?: boolean | undefined, body: AddressLookupV2Request}; export type CreateApiAddressLookupV2Options = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const localityItem = z.object({name: z.string().nullable().optional(), code: z.string().nullable().optional(), description: z.string().nullable().optional()}); export const localityComponents = z.object({region: localityItem.optional(), sub_region: localityItem.optional(), town: localityItem.optional(), district: localityItem.optional(), sub_district: localityItem.optional(), type: z.string().nullable().optional()}); export const postalCode = z.object({full_name: z.string().nullable().optional(), primary: z.string().nullable().optional(), secondary: z.string().nullable().optional()}); export const what3WordsComponent = z.object({name: z.string().nullable().optional(), description: z.string().nullable().optional()}); export const suggestion = z.object({locality: localityComponents.optional(), postal_code: postalCode.optional(), postal_code_key: z.string().nullable().optional(), locality_key: z.string().nullable().optional(), what3words: what3WordsComponent.optional()}); export const addressSuggestionV2Hub = z.object({text: z.string().nullable().optional(), matched: z.array(z.array(z.number().int())).nullable().optional(), global_address_key: z.string().nullable().optional(), format: z.string().nullable().optional(), dataset: z.string().nullable().optional(), names: z.array(z.string()).nullable().optional(), uprn: z.string().nullable().optional()}); export const addressLookupV2ResultAddressFormatted = z.object({layout_name: z.string().nullable().optional(), address: z.record(z.string(), z.unknown()).nullable().optional()}); export const addressLookupV2Result = z.object({more_results_available: z.boolean().optional(), confidence: z.string().nullable().optional(), suggestions_key: z.string().nullable().optional(), suggestions_prompt: z.string().nullable().optional(), suggestions: z.array(suggestion).nullable().optional(), addresses: z.array(addressSuggestionV2Hub).nullable().optional(), addresses_formatted: z.array(addressLookupV2ResultAddressFormatted).nullable().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 }; /** * Lookup an address based on a key. * * POST /address/lookup/v2 * * Also accepts auth: Auth-Token, x-app-key */ export const createApiAddressLookupV2 = async (args: CreateApiAddressLookupV2Args, options?: CreateApiAddressLookupV2Options) => { const {baseUrl = "https://api.experianaperture.io", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Reference-Id'] != null) headers.set("Reference-Id", String(args['Reference-Id'])) if (args['Timeout-Seconds'] != null) headers.set("Timeout-Seconds", String(args['Timeout-Seconds'])) if (args['Add-Addresses'] != null) headers.set("Add-Addresses", String(args['Add-Addresses'])) if (args['Add-FinalAddress'] != null) headers.set("Add-FinalAddress", String(args['Add-FinalAddress'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/address/lookup/v2${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(addressLookupV2Result, await res.json()) };