import {z} from 'zod' export type AddressLayoutV2AppliesTo = {country_iso: string, datasets: Array, language?: (string | null) | undefined, script?: (string | null) | undefined}; export type AddressLayoutV2ElementConfigurationWithDefault = {default?: (string | null) | undefined, configuration_by_element?: ({[key: string]: string | null} | null) | undefined}; export type AddressLayoutV2Options = {variation?: number | undefined, flatten_diacritics?: boolean | undefined, use_secondary_postcode?: (string | null) | undefined, enable_enhanced_layout?: boolean | undefined, display_enhanced_info_on_picklist?: boolean | undefined, enable_intelligent_layout?: boolean | undefined, capitalise_unused?: boolean | undefined, separate_elements?: boolean | undefined, element_separator?: AddressLayoutV2ElementConfigurationWithDefault | undefined, retention_element_separator?: AddressLayoutV2ElementConfigurationWithDefault | undefined, prepend_element_separator?: AddressLayoutV2ElementConfigurationWithDefault | undefined, separate_element_separator?: AddressLayoutV2ElementConfigurationWithDefault | undefined, terminate_lines?: boolean | undefined, line_terminator?: AddressLayoutV2ElementConfigurationWithDefault | undefined, conditional_format?: (string | null) | undefined, pad_lines?: boolean | undefined, padding_character?: (string | null) | undefined, multiple_dataplus_delimiter?: (string | null) | undefined, abbreviate_item?: (Array | null) | undefined, capitalise_item?: (Array | null) | undefined, exclude_item?: (Array | null) | undefined, element_extras?: ({[key: string]: string | null} | null) | undefined}; export type AddressLayoutV2LineElement = {element_name?: (string | null) | undefined}; export type AddressLayoutV2Line = {line_name?: (string | null) | undefined, max_width?: (number | null) | undefined, elements?: (Array | null) | undefined}; export type AddressLayoutV2 = {name: string, comment?: (string | null) | undefined, applies_to: Array, options?: AddressLayoutV2Options | undefined, lines: Array}; export type AddressLayoutsV2CreateRequest = {layout: AddressLayoutV2}; export type CreateApiAddressLayoutsV2Args = {'Reference-Id'?: string | undefined, body: AddressLayoutsV2CreateRequest}; export type CreateApiAddressLayoutsV2Options = {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 addressLayoutsV2CreateResult = z.object({id: z.string().nullable().optional()}); export const addressLayoutsV2CreateResponse = z.object({error: responseError.optional(), result: addressLayoutsV2CreateResult.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 }; /** * Creates an address layout. * * POST /address/layouts/v2 * * Also accepts auth: Auth-Token, x-app-key */ export const createApiAddressLayoutsV2 = async (args: CreateApiAddressLayoutsV2Args, options?: CreateApiAddressLayoutsV2Options) => { 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 (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/address/layouts/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(addressLayoutsV2CreateResponse, await res.json()) };