import {z} from 'zod' export type GetApiAddressLayoutsV2Args = {'Reference-Id'?: string | undefined, 'Timeout-Seconds'?: number | undefined, country_iso?: string | undefined, datasets?: Array | undefined, name_contains?: string | undefined}; export type GetApiAddressLayoutsV2Options = {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 addressLayoutV2Status = z.enum(["CreationInProgress", "Completed"]); export const addressLayoutsV2ListResponseModel = z.object({id: z.string().nullable().optional(), name: z.string().min(1).max(100), applies_to: z.array(addressLayoutV2AppliesTo).nullable().optional(), status: addressLayoutV2Status.optional()}); export const addressLayoutsV2ListResponse = z.object({error: responseError.optional(), result: z.array(addressLayoutsV2ListResponseModel).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 }; /** * Gets a list of the address layouts. * * GET /address/layouts/v2 * * Also accepts auth: Auth-Token, x-app-key */ export const getApiAddressLayoutsV2 = async (args: GetApiAddressLayoutsV2Args, options?: GetApiAddressLayoutsV2Options) => { 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 (args.country_iso != null) query.append("country_iso", String(args.country_iso)) for (const item of args.datasets ?? []) query.append("datasets", String(item)) if (args.name_contains != null) query.append("name_contains", String(args.name_contains)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(addressLayoutsV2ListResponse, await res.json()) };