import {z} from 'zod' export type IContactUpdate = {first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, email?: (string | null) | undefined, channels?: ({[key: string]: unknown} | null) | undefined, user_attributes?: ({[key: string]: unknown} | null) | undefined, last_seen?: (string | null) | undefined, last_sent?: (string | null) | undefined, org_id?: (string | null) | undefined, first_assistant_id?: (string | null) | undefined, last_assistant_id?: (string | null) | undefined, first_widget_id?: (string | null) | undefined, last_widget_id?: (string | null) | undefined, custom_fields?: ({[key: string]: unknown} | null) | undefined}; export type UpdateApiApiV1ContactContactIdArgs = {contact_id: string, body: IContactUpdate}; export type UpdateApiApiV1ContactContactIdOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iContactUpdate = z.object({first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), email: z.string().nullable().optional(), channels: z.record(z.string(), z.unknown()).nullable().optional(), user_attributes: z.record(z.string(), z.unknown()).nullable().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()}); export const iPutResponseBaseIContactUpdate = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iContactUpdate.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 }; /** * Update Contact By Id * * PUT /api/v1/contact/{contact_id} * * Also accepts auth: HTTPBearer */ export const updateApiApiV1ContactContactId = async (args: UpdateApiApiV1ContactContactIdArgs, options: UpdateApiApiV1ContactContactIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/contact/${encodeURIComponent(String(args.contact_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(iPutResponseBaseIContactUpdate, await res.json()) };