import {z} from 'zod' export type ContactUpdateRequest = {name?: (string | null) | undefined, email?: (string | null) | undefined, source?: (string | null) | undefined, phone?: (string | null) | undefined, comments?: (string | null) | undefined, status?: (string | null) | undefined, tags?: (Array | null) | undefined, employer_name?: (string | null) | undefined, profession?: (string | null) | undefined, title?: (string | null) | undefined, ssn?: (string | null) | undefined, website?: (string | null) | undefined, birth_year?: (number | null) | undefined, birth_month?: (number | null) | undefined, birth_day?: (number | null) | undefined, decision_date?: (number | null) | undefined, business_need_description?: (string | null) | undefined, social_dict_list?: (Array<{[key: string]: unknown}> | null) | undefined}; export type PatchApiCrmContactsContactIdArgs = {contact_id: string, body: ContactUpdateRequest}; export type PatchApiCrmContactsContactIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const contactResponse = z.object({id: z.string(), phone_list: z.array(z.unknown()).nullable(), email_list: z.array(z.unknown()).nullable(), first_name: z.string().nullable(), last_name: z.string().nullable(), name: z.string(), status: z.string(), marketing_source: z.string().nullable().optional(), created_date: z.string().nullable(), updated_date: z.string().nullable(), status_updated_date: z.string().nullable(), tags: z.array(z.string()).nullable().optional(), social_dict_list: z.array(z.record(z.string(), z.unknown())).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 }; /** * Patch Contact * * PATCH /crm/contacts/{contact_id} */ export const patchApiCrmContactsContactId = async (args: PatchApiCrmContactsContactIdArgs, options: PatchApiCrmContactsContactIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/crm/contacts/${encodeURIComponent(String(args.contact_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(contactResponse, await res.json()) };