import {z} from 'zod' export type CreateApiV3ContactsImportArgs = {body: {items: Array<{email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, phone?: string | undefined, phone2?: string | undefined, title?: string | undefined, company?: string | undefined, companySize?: ('Empty' | 'SelfEmployed' | 'Ten' | 'Fifty' | 'TwoHundred' | 'FiveHundred' | 'OneThousand' | 'FiveThousand' | 'TenThousand' | 'OverTenThousand') | undefined, industry?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, timeZoneId?: string | undefined, linkedInUrl?: string | undefined, linkedInSalesNavigatorUrl?: string | undefined, linkedInRecruiterUrl?: string | undefined, notes?: string | undefined, isOptedOut?: (boolean | null) | undefined, callStatus?: ('none' | 'toCall' | 'called' | null) | undefined, meetingStatus?: ('none' | 'meetingBooked' | null) | undefined, ownerUserId?: (number | null) | undefined, accountId?: (number | null) | undefined, customFields?: (Array<{id?: (number | null) | undefined, name?: (string | null) | undefined, value?: (string | null) | undefined}>) | undefined}>, options?: ({overwriteExisting?: boolean | undefined, skipExisting?: boolean | undefined, skipWithoutEmails?: boolean | undefined, detectTimeZone?: boolean | undefined, restoreDeleted?: boolean | undefined, listIds?: Array | undefined, sequenceId?: (number | null) | undefined, keys?: ({linkedInUrl?: boolean | undefined, phone?: boolean | undefined, firstName?: boolean | undefined, lastName?: boolean | undefined, company?: boolean | undefined, customFieldIds?: Array | undefined}) | undefined}) | undefined}}; export type CreateApiV3ContactsImportOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV3ContactsImportResponse = z.object({items: z.array(z.object({id: z.number().int().nullable().optional(), status: z.enum(["created", "updated", "skipped", "failed", "restored"]).optional(), error: z.string().nullable().optional()})).optional(), added: z.number().int().optional(), updated: z.number().int().optional(), skipped: z.number().int().optional(), failed: z.number().int().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 }; /** * Import contacts * * POST /v3/contacts/import */ export const createApiV3ContactsImport = async (args: CreateApiV3ContactsImportArgs, options?: CreateApiV3ContactsImportOptions) => { const {baseUrl = "https://api.reply.io", 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(/\/+$/, '')}/v3/contacts/import${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(createApiV3ContactsImportResponse, await res.json()) };