import {z} from 'zod' export type GetApiAddressLayoutsV2NameArgs = {'Reference-Id'?: string | undefined, 'Timeout-Seconds'?: number | undefined, name: string}; export type GetApiAddressLayoutsV2NameOptions = {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 addressLayoutV2AppliesTo = z.object({country_iso: z.string().min(3).max(3), datasets: z.array(z.string()), language: z.string().nullable().optional(), script: z.string().nullable().optional()}); export const addressLayoutV2ElementConfigurationWithDefault = z.object({default: z.string().nullable().optional(), configuration_by_element: z.record(z.string(), z.string().nullable()).nullable().optional()}); export const addressLayoutV2Options = z.object({variation: z.number().int().optional(), flatten_diacritics: z.boolean().optional(), use_secondary_postcode: z.string().nullable().optional(), enable_enhanced_layout: z.boolean().optional(), display_enhanced_info_on_picklist: z.boolean().optional(), enable_intelligent_layout: z.boolean().optional(), capitalise_unused: z.boolean().optional(), separate_elements: z.boolean().optional(), element_separator: addressLayoutV2ElementConfigurationWithDefault.optional(), retention_element_separator: addressLayoutV2ElementConfigurationWithDefault.optional(), prepend_element_separator: addressLayoutV2ElementConfigurationWithDefault.optional(), separate_element_separator: addressLayoutV2ElementConfigurationWithDefault.optional(), terminate_lines: z.boolean().optional(), line_terminator: addressLayoutV2ElementConfigurationWithDefault.optional(), conditional_format: z.string().nullable().optional(), pad_lines: z.boolean().optional(), padding_character: z.string().nullable().optional(), multiple_dataplus_delimiter: z.string().nullable().optional(), abbreviate_item: z.array(z.string()).nullable().optional(), capitalise_item: z.array(z.string()).nullable().optional(), exclude_item: z.array(z.string()).nullable().optional(), element_extras: z.record(z.string(), z.string().nullable()).nullable().optional()}); export const addressLayoutV2LineElement = z.object({element_name: z.string().nullable().optional()}); export const addressLayoutV2Line = z.object({line_name: z.string().nullable().optional(), max_width: z.number().int().nullable().optional(), elements: z.array(addressLayoutV2LineElement).nullable().optional()}); export const addressLayoutV2Status = z.enum(["CreationInProgress", "Completed"]); export const addressLayoutsV2GetResponseModel = z.object({id: z.string().nullable().optional(), name: z.string().min(1).max(100), comment: z.string().nullable().optional(), applies_to: z.array(addressLayoutV2AppliesTo), options: addressLayoutV2Options.optional(), lines: z.array(addressLayoutV2Line), status: addressLayoutV2Status.optional(), license_id: z.string().nullable().optional()}); export const addressLayoutsV2GetResult = z.object({layout: addressLayoutsV2GetResponseModel.optional()}); export const addressLayoutsV2GetResponse = z.object({error: responseError.optional(), result: addressLayoutsV2GetResult.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 }; /** * Gets an address layout. * * GET /address/layouts/v2/{name} * * Also accepts auth: Auth-Token, x-app-key */ export const getApiAddressLayoutsV2Name = async (args: GetApiAddressLayoutsV2NameArgs, options?: GetApiAddressLayoutsV2NameOptions) => { 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/layouts/v2/${encodeURIComponent(String(args.name))}${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(addressLayoutsV2GetResponse, await res.json()) };