import {z} from 'zod' export type CreateApiApiV1ContactUpsertArgs = {first_name: string | null, last_name: string | null, email: string | null, phone_number: string | null}; export type CreateApiApiV1ContactUpsertOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iContactRead = z.object({first_name: z.string().nullable(), last_name: z.string().nullable(), email: z.string().nullable(), channels: z.record(z.string(), z.unknown()).optional(), user_attributes: z.record(z.string(), z.unknown()).optional(), last_seen: z.string().nullable().optional(), last_sent: z.string().nullable().optional(), org_id: z.string().nullable().optional(), first_assistant_id: z.string().nullable().optional(), last_assistant_id: z.string().nullable().optional(), first_widget_id: z.string().nullable().optional(), last_widget_id: z.string().nullable().optional(), custom_fields: z.record(z.string(), z.unknown()).nullable().optional(), id: z.string(), created_at: z.string().nullable()}); export const iPostResponseBaseIContactRead = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iContactRead.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 }; /** * Upsert Contact By Email Or Phone Number * * POST /api/v1/contact/upsert * * Also accepts auth: HTTPBearer */ export const createApiApiV1ContactUpsert = async (args: CreateApiApiV1ContactUpsertArgs, options: CreateApiApiV1ContactUpsertOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.first_name != null) query.append("first_name", String(args.first_name)) if (args.last_name != null) query.append("last_name", String(args.last_name)) if (args.email != null) query.append("email", String(args.email)) if (args.phone_number != null) query.append("phone_number", String(args.phone_number)) if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/contact/upsert${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(iPostResponseBaseIContactRead, await res.json()) };