import {z} from 'zod' export type AddressInputComponentName = {forename?: (string | null) | undefined, middlename?: (string | null) | undefined, surname?: (string | null) | undefined}; export type AddressRestrictionRules = {exists?: (boolean | null) | undefined, must_be?: (Array | null) | undefined, must_not_be?: (Array | null) | undefined}; export type AddressLocalityConstrains = {region?: AddressRestrictionRules | undefined}; export type AddressInputComponents = {unspecified?: (Array | null) | undefined, names?: (Array | null) | undefined, locality?: AddressLocalityConstrains | undefined}; export type AddressNameValuePair = {name?: (string | null) | undefined, value?: (string | null) | undefined}; export type AddressSearchV1Request = {country_iso: string, datasets: Array | null, max_suggestions?: (number | null) | undefined, components: AddressInputComponents, location?: (string | null) | undefined, options?: (Array | null) | undefined, preferred_language?: (Array | null) | undefined, preferred_script?: (Array | null) | undefined, street?: (string | null) | undefined, town?: (string | null) | undefined, region?: (string | null) | undefined, postal_code?: (string | null) | undefined}; export type CreateApiAddressSearchV1Args = {'Reference-Id'?: string | undefined, 'Timeout-Seconds'?: number | undefined, Transliterate?: boolean | undefined, 'Add-Retention'?: boolean | undefined, body: AddressSearchV1Request}; export type CreateApiAddressSearchV1Options = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const responseError = z.object({type: z.string().nullable().optional(), title: z.string().nullable().optional(), detail: z.string().nullable().optional(), instance: z.string().nullable().optional()}); export const addressNameValuePair = z.object({name: z.string().nullable().optional(), value: z.string().nullable().optional()}); export const addressSuggestion = z.object({global_address_key: z.string().nullable().optional(), text: z.string().nullable().optional(), matched: z.array(z.array(z.number().int())).nullable().optional(), format: z.string().nullable().optional(), stepin: z.string().nullable().optional(), number_of_addresses: z.number().int().optional(), dataset: z.string().nullable().optional(), additional_attributes: z.array(addressNameValuePair).nullable().optional(), names: z.array(z.string()).nullable().optional(), uprn: z.string().nullable().optional()}); export const addressSearchV1Result = 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(addressSuggestion).nullable().optional()}); export const addressSearchV1Response = z.object({error: responseError.optional(), result: addressSearchV1Result.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 }; /** * Search for an address. * * POST /address/search/v1 * * Also accepts auth: Auth-Token, x-app-key */ export const createApiAddressSearchV1 = async (args: CreateApiAddressSearchV1Args, options?: CreateApiAddressSearchV1Options) => { 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.Transliterate != null) headers.set("Transliterate", String(args.Transliterate)) if (args['Add-Retention'] != null) headers.set("Add-Retention", String(args['Add-Retention'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/address/search/v1${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(addressSearchV1Response, await res.json()) };