import {z} from 'zod' export type CreateApiAddressBulkV1BatchesBatchIdStartArgs = {'Reference-Id'?: string | undefined, batch_id: string}; export type CreateApiAddressBulkV1BatchesBatchIdStartOptions = {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 bulkAddressV1StartBatchResult = z.object({batch_id: z.string().nullable().optional(), batch_reference_id: z.string().nullable().optional(), status: z.string().nullable().optional()}); export const bulkAddressV1StartBatchResponse = z.object({error: responseError.optional(), result: bulkAddressV1StartBatchResult.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 }; /** * Start processing a batch of address to be validated. * * POST /address/bulk/v1/batches/{batch_id}/start * * Also accepts auth: Auth-Token */ export const createApiAddressBulkV1BatchesBatchIdStart = async (args: CreateApiAddressBulkV1BatchesBatchIdStartArgs, options?: CreateApiAddressBulkV1BatchesBatchIdStartOptions) => { 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 (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/address/bulk/v1/batches/${encodeURIComponent(String(args.batch_id))}/start${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(bulkAddressV1StartBatchResponse, await res.json()) };