import {z} from 'zod' export type GetApiV3ContactsArgs = {top?: number | undefined, skip?: number | undefined, email?: string | undefined, linkedIn?: string | undefined}; export type GetApiV3ContactsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3ContactsResponse = z.object({items: z.array(z.object({id: z.number().int().optional(), email: z.string().optional(), domain: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), phone: z.string().optional(), title: z.string().optional(), company: z.string().optional(), companySize: z.enum(["empty", "selfEmployed", "ten", "fifty", "twoHundred", "fiveHundred", "oneThousand", "fiveThousand", "tenThousand", "overTenThousand"]).optional(), industry: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), timeZoneId: z.string().optional(), linkedInUrl: z.string().optional(), linkedInSalesNavigatorUrl: z.string().optional(), linkedInRecruiterUrl: z.string().optional(), phoneStatus: z.enum(["pending", "invalid", "valid", "validationFailed", "notValidated"]).optional(), notes: z.string().optional(), ownerUserId: z.number().int().optional(), accountId: z.number().int().nullable().optional(), isOptedOut: z.boolean().optional(), callStatus: z.enum(["none", "toCall", "called"]).optional(), meetingStatus: z.enum(["none", "meetingBooked"]).optional(), addingDate: z.string().nullable().optional(), createdAt: z.string().nullable().optional(), lastModifiedAt: z.string().nullable().optional(), customFields: z.array(z.object({key: z.string(), value: z.string().nullable().optional()})).optional()})).optional(), hasMore: z.boolean().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 }; /** * List all contacts * * GET /v3/contacts */ export const getApiV3Contacts = async (args: GetApiV3ContactsArgs, options?: GetApiV3ContactsOptions) => { const {baseUrl = "https://api.reply.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.top != null) query.append("top", String(args.top)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.email != null) query.append("email", String(args.email)) if (args.linkedIn != null) query.append("linkedIn", String(args.linkedIn)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/contacts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV3ContactsResponse, await res.json()) };