import {z} from 'zod' export type GetApiApiV3ContactsArgs = {institution_id?: string | undefined, ids?: Array | undefined}; export type GetApiApiV3ContactsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const searchMetadata = z.object({reason: z.string(), citations: z.array(z.string()), direct_match: z.boolean(), verified: z.boolean().nullable().optional()}); export const appTypesContactsContactResponseContactResponse = z.object({id: z.number().int().nullable().optional(), full_name: z.string(), title: z.string(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), institution_id: z.string(), updated_at: z.string().nullable().optional(), scrape_timestamp: z.string().nullable().optional(), scraper_filename: z.string().nullable().optional(), email_status: z.string().nullable().optional(), entity_name: z.string().nullable().optional(), mailing_address: z.string().nullable().optional(), search_metadata: searchMetadata.nullable().optional()}); export const getApiApiV3ContactsResponse = z.array(appTypesContactsContactResponseContactResponse); 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 }; /** * Get Contacts * * GET /api/v3/contacts */ export const getApiApiV3Contacts = async (args: GetApiApiV3ContactsArgs, options: GetApiApiV3ContactsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.institution_id != null) query.append("institution_id", String(args.institution_id)) for (const item of args.ids ?? []) query.append("ids", String(item)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/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(getApiApiV3ContactsResponse, await res.json()) };