import {z} from 'zod' export type ProviderName = 'prospeo' | 'enrow' | 'cleon1' | 'datagma' | 'aviato' | 'wiza' | 'forager_ai' | 'icypeas' | 'contact_out' | 'people_data_labs' | 'lusha' | 'apollo_io' | 'zoominfo' | 'cognism'; export type ProviderCredentials = {api_key?: string | undefined, client_id?: string | undefined, client_secret?: string | undefined, account_id?: string | undefined, account_email?: string | undefined, personal_email_api_key?: string | undefined}; export type ProviderZone = 'front' | 'back' | 'auto'; export type ProviderDataType = 'contact.work_emails' | 'contact.personal_emails' | 'contact.phones'; export type CreateBYOKKeyRequest = {provider: ProviderName, credentials: ProviderCredentials, priority: number, zone?: ProviderZone | undefined, data_types?: Array | undefined, verify?: boolean | undefined, enabled?: boolean | undefined}; export type CreateApiProvidersArgs = {'Sub-Account-ID'?: string | undefined, body: CreateBYOKKeyRequest}; export type CreateApiProvidersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const providerName = z.enum(["prospeo", "enrow", "cleon1", "datagma", "aviato", "wiza", "forager_ai", "icypeas", "contact_out", "people_data_labs", "lusha", "apollo_io", "zoominfo", "cognism"]); export const providerZone = z.enum(["front", "back", "auto"]); export const providerDataType = z.enum(["contact.work_emails", "contact.personal_emails", "contact.phones"]); export const bYOKKey = z.object({id: z.string(), workspace_id: z.string(), sub_account_id: z.string().optional(), provider: providerName, zone: providerZone, priority: z.number().int(), data_types: z.array(providerDataType), verify: z.boolean(), enabled: z.boolean(), created_at: z.string(), updated_at: z.string()}); 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 BYOK key * * POST /providers */ export const createApiProviders = async (args: CreateApiProvidersArgs, options?: CreateApiProvidersOptions) => { const {baseUrl = "https://app.fullenrich.com/api/v2", 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 (args['Sub-Account-ID'] != null) headers.set("Sub-Account-ID", String(args['Sub-Account-ID'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/providers${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(bYOKKey, await res.json()) };