import {z} from 'zod' export type GetApiAddressSuggestionsStepinV1GlobalAddressKeyArgs = {'Reference-Id'?: string | undefined, 'Timeout-Seconds'?: number | undefined, global_address_key: string}; export type GetApiAddressSuggestionsStepinV1GlobalAddressKeyOptions = {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 addressSuggestionsV1Response = 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 }; /** * Step into a suggestion. * * GET /address/suggestions/stepin/v1/{global_address_key} * * Also accepts auth: Auth-Token, x-app-key */ export const getApiAddressSuggestionsStepinV1GlobalAddressKey = async (args: GetApiAddressSuggestionsStepinV1GlobalAddressKeyArgs, options?: GetApiAddressSuggestionsStepinV1GlobalAddressKeyOptions) => { const {baseUrl = "https://api.experianaperture.io", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/address/suggestions/stepin/v1/${encodeURIComponent(String(args.global_address_key))}${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(addressSuggestionsV1Response, await res.json()) };