import {z} from 'zod' export type JourneyAddressPayload = {type?: string | undefined, line_1?: string | undefined, line_2?: string | undefined, city?: string | undefined, state?: string | undefined, postal_code?: string | undefined, country_code?: string | undefined}; export type JourneyApplicationBatchPayload = {entities: Array<{external_entity_id?: string | undefined, data: {name_first?: string | undefined, name_middle?: string | undefined, name_last?: string | undefined, birth_date?: string | undefined, document_ssn?: string | undefined, document_national_id?: string | undefined, document_id_card?: string | undefined, document_license?: string | undefined, document_passport?: string | undefined, email_address?: string | undefined, gender?: string | undefined, phone_number?: string | undefined, ip_address_v4?: string | undefined, addresses?: Array | undefined, meta?: Record | undefined}, entity_type?: ('person' | 'business') | undefined, branch_name?: string | undefined, entity_token?: string | undefined, multi_entity_application_batch_key?: string | undefined, auto_decision_node_outcomes?: Record | undefined, external_partner_id?: string | undefined, external_group_id?: string | undefined}>}; export type CreateApiJourneysJourneyTokenBatchesArgs = {journey_token: string, 'alloy-sandbox-use-webhooks'?: boolean | undefined, 'alloy-bypass-formatter'?: boolean | undefined, 'alloy-journey-version'?: number | undefined, body: JourneyApplicationBatchPayload}; export type CreateApiJourneysJourneyTokenBatchesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const journeyApplicationBatchResponseSchema = z.object({journey_batch_status: z.enum(["pending", "complete", "failed", "pre_run"]).optional(), journey_batch_token: z.string().optional(), _links: z.object({self: z.object({href: z.string().optional()}).optional()}).optional(), error: z.string().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 }; /** * Create Journey Application Batch * * POST /journeys/{journey_token}/batches * * Also accepts auth: basic */ export const createApiJourneysJourneyTokenBatches = async (args: CreateApiJourneysJourneyTokenBatchesArgs, options?: CreateApiJourneysJourneyTokenBatchesOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", 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['alloy-sandbox-use-webhooks'] != null) headers.set("alloy-sandbox-use-webhooks", String(args['alloy-sandbox-use-webhooks'])) if (args['alloy-bypass-formatter'] != null) headers.set("alloy-bypass-formatter", String(args['alloy-bypass-formatter'])) if (args['alloy-journey-version'] != null) headers.set("alloy-journey-version", String(args['alloy-journey-version'])) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/journeys/${encodeURIComponent(String(args.journey_token))}/batches${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(journeyApplicationBatchResponseSchema, await res.json()) };