import {z} from 'zod' export type UpdatePersonPayload = {firstName?: (string | null) | undefined, lastName?: (string | null) | undefined, gender?: ('male' | 'female') | undefined, candidateStatus?: ('omitted' | 'applied' | 'sourced' | 'regular') | undefined, overview?: (string | null) | undefined, addedByEmail?: string | undefined, source?: ({system: string, externalId?: (string | null) | undefined}) | undefined, identities?: (Array<{type: 'email' | 'phone' | 'linkedin' | 'linkedin_salesnav' | 'linkedin_recruiter' | 'website', value: string, isPersonal?: boolean | undefined, isPrimary?: boolean | undefined}>) | undefined, headline?: ({role?: (string | null) | undefined, company?: (string | null) | undefined, companyId?: (string | null) | undefined, roleStartedAt?: (string | null) | undefined}) | undefined, location?: ({name?: (string | null) | undefined, locality?: (string | null) | undefined, region?: (string | null) | undefined, metro?: (string | null) | undefined, country?: (string | null) | undefined, streetAddress?: (string | null) | undefined, addressLine2?: (string | null) | undefined, postalCode?: (string | null) | undefined, raw?: (string | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined} | null) | undefined, isContact?: boolean | undefined, customAttributes?: (Array<{customAttributeId: string, optionId?: (string | null) | undefined, value?: (string | number | null) | undefined}>) | undefined}; export type PatchApiApiV1PeopleIdArgs = {id: string, body: UpdatePersonPayload}; export type PatchApiApiV1PeopleIdOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiApiV1PeopleIdResponse = z.object({status: z.literal("ok"), data: z.object({personId: z.string(), identitiesAdded: z.number().int(), customAttributesReplaced: z.number().int(), companyContactCreated: z.number().int()})}); 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 a person * * PATCH /api/v1/people/{id} */ export const patchApiApiV1PeopleId = async (args: PatchApiApiV1PeopleIdArgs, options: PatchApiApiV1PeopleIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/people/${encodeURIComponent(String(args.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(patchApiApiV1PeopleIdResponse, await res.json()) };