import {z} from 'zod' export type GetApiContactsArgs = {page?: number | undefined, limit?: number | undefined}; export type GetApiContactsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({id: z.string(), full_name: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), current_job_title: z.string().optional(), linkedin_url: z.string().optional(), location: z.string().optional(), country: z.string().nullable().optional(), country_code: z.string().nullable().optional(), summary: z.string().optional(), image_url: z.string().optional(), headline: z.string().optional(), emails: z.array(z.string()).optional(), phones: z.array(z.string()).optional(), skills: z.array(z.string()).optional(), company: z.object({name: z.string().optional(), linkedin_id: z.string().optional()}).optional(), experience: z.array(z.object({job_id: z.string().optional(), company_id: z.string().optional(), company: z.string().optional(), company_linkedin_id: z.string().optional(), title: z.string().optional(), location: z.string().nullable().optional(), start_date: z.string().optional(), end_date: z.string().nullable().optional(), is_current: z.boolean().optional(), description: z.string().nullable().optional(), created_at: z.string().optional()})).optional(), education: z.array(z.object({degree: z.string().optional(), field_of_study: z.string().nullable().optional(), school: z.string().optional(), start_year: z.number().int().optional(), end_year: z.number().int().optional()})).optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional()}); export const contactsListResponse = z.object({data: z.array(contact).optional(), pagination: z.object({page: z.number().int().optional(), limit: z.number().int().optional(), total: z.number().int().optional()}).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 }; /** * [BETA] List contacts * * GET /contacts */ export const getApiContacts = async (args: GetApiContactsArgs, options?: GetApiContactsOptions) => { const {baseUrl = "https://api.getpg.ai/public-api/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/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(contactsListResponse, await res.json()) };