import {z} from 'zod' export type GetApiProvidersArgs = {'Sub-Account-ID'?: string | undefined}; export type GetApiProvidersOptions = {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 providerMaskedCredentials = z.object({api_key: z.string().optional(), account_id: z.string().optional(), account_email: z.string().optional(), personal_email_api_key: z.string().optional(), client_id: z.string().optional(), client_secret: z.string().optional()}); export const bYOKKeyWithMaskedCredentials = 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(), credentials: providerMaskedCredentials}); export const getApiProvidersResponse = z.array(bYOKKeyWithMaskedCredentials); 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 BYOK keys * * GET /providers */ export const getApiProviders = async (args: GetApiProvidersArgs, options?: GetApiProvidersOptions) => { const {baseUrl = "https://app.fullenrich.com/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiProvidersResponse, await res.json()) };