import {z} from 'zod' export type UpdateLeadRequest = {FirstName?: (string | null) | undefined, LastName?: (string | null) | undefined, Company?: (string | null) | undefined, Title?: (string | null) | undefined, Email?: (string | null) | undefined, Phone?: (string | null) | undefined, MobilePhone?: (string | null) | undefined, Website?: (string | null) | undefined, Street?: (string | null) | undefined, City?: (string | null) | undefined, State?: (string | null) | undefined, PostalCode?: (string | null) | undefined, Country?: (string | null) | undefined, Industry?: (string | null) | undefined, NumberOfEmployees?: (number | null) | undefined, AnnualRevenue?: (number | null) | undefined, Status?: (string | null) | undefined, Rating?: (string | null) | undefined, LeadSource?: (string | null) | undefined, Description?: (string | null) | undefined, OwnerId?: (string | null) | undefined} | {[key: string]: unknown}; export type PatchApiApiV3CrmIntegrationIdLeadsLeadIdArgs = {integration_id: string, lead_id: string, body: UpdateLeadRequest}; export type PatchApiApiV3CrmIntegrationIdLeadsLeadIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const leadResponse = z.object({id: z.string(), properties: z.record(z.string(), z.unknown())}); 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 Lead * * PATCH /api/v3/crm/{integration_id}/leads/{lead_id} */ export const patchApiApiV3CrmIntegrationIdLeadsLeadId = async (args: PatchApiApiV3CrmIntegrationIdLeadsLeadIdArgs, options: PatchApiApiV3CrmIntegrationIdLeadsLeadIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/crm/${encodeURIComponent(String(args.integration_id))}/leads/${encodeURIComponent(String(args.lead_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(leadResponse, await res.json()) };