import {z} from 'zod' export type CreatePersonPayload = {firstName?: (string | null) | undefined, lastName?: (string | null) | undefined, isContact?: boolean | undefined, gender?: ('male' | 'female') | undefined, addedByEmail: string, 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}>, 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, compensation?: (Array<{type: 'actual' | 'expected', basicSalary?: number | undefined, totalSalary?: number | undefined, currency: 'USD' | 'EUR' | 'JPY' | 'GBP' | 'AUD' | 'CAD' | 'CHF' | 'CNY' | 'HKD' | 'NZD' | 'SEK' | 'NOK' | 'MXN' | 'SGD' | 'RUB' | 'ZAR' | 'TRY' | 'BRL' | 'INR' | 'KRW' | 'DKK' | 'PLN' | 'ILS' | 'HUF' | 'CZK' | 'RON' | 'THB' | 'MYR' | 'IDR' | 'VND' | 'PHP' | 'SAR' | 'AED' | 'QAR' | 'KWD' | 'JOD' | 'CLP' | 'COP' | 'PEN' | 'ARS' | 'UYU' | 'CRC' | 'PKR' | 'BDT' | 'LKR' | 'EGP' | 'NGN' | 'TWD' | 'KES' | 'GHS' | 'UGX' | 'TZS' | 'MAD' | 'BWP' | 'BGN' | 'UAH' | 'KZT' | 'GEL' | 'ISK' | 'BHD' | 'OMR', basis?: ('gross' | 'net') | undefined, date: string}>) | undefined, customAttributes?: (Array<{customAttributeId: string, optionId?: (string | null) | undefined, value?: (string | number | null) | undefined}>) | undefined, experience?: (Array<{companyName: string, companyId?: (string | null) | undefined, companyLinkedinId?: (string | null) | undefined, companyDomain?: (string | null) | undefined, role: string, description?: (string | null) | undefined, startDate: string, endDate?: (string | null) | undefined}>) | undefined, education?: (Array<{institutionName: string, degree?: (string | null) | undefined, fieldOfStudy?: (string | null) | undefined, grade?: (string | null) | undefined, description?: (string | null) | undefined, startDate?: (string | null) | undefined, endDate?: (string | null) | undefined}>) | undefined}; export type CreateApiApiV1PeopleArgs = {body: CreatePersonPayload}; export type CreateApiApiV1PeopleOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1PeopleResponse = z.object({status: z.literal("ok"), data: z.object({personId: z.string(), created: z.literal(true), identitiesCreated: z.number().int(), customAttributesCreated: z.number().int(), queued: z.object({experiences: z.number().int(), educations: z.number().int(), compensation: z.number().int(), companyContact: 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 }; /** * Create a person * * POST /api/v1/people */ export const createApiApiV1People = async (args: CreateApiApiV1PeopleArgs, options: CreateApiApiV1PeopleOptions) => { 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiApiV1PeopleResponse, await res.json()) };