import {z} from 'zod' export type SavedCompaniesListIdsDto = {includes: Array}; export type ViewEnrichedCompaniesDto = {start?: number | undefined, take?: number | undefined, search?: string | undefined, tags?: Array | undefined, createdDate?: Array | undefined, revealType?: (Array<'enriched' | 'notEnriched' | 'imported'>) | undefined, listIds?: SavedCompaniesListIdsDto | undefined}; export type CreateApiV1EnrichCompanySavedArgs = {body: ViewEnrichedCompaniesDto}; export type CreateApiV1EnrichCompanySavedOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1EnrichCompanySavedResponse = z.object({message: z.string().optional(), payload: z.object({companies: z.array(z.object({company_id: z.number().int().optional(), company_name: z.string().optional(), company_logo_url: z.string().optional(), internalId: z.number().int().optional(), revealType: z.enum(["enriched", "notEnriched", "imported"]).optional(), company_size_range: z.string().optional(), company_locations: z.array(z.string()).optional(), employees_count_by_department: z.record(z.string(), z.number().int().nullable()).optional(), employees_count_by_seniority: z.record(z.string(), z.number().int().nullable()).optional(), employee_reviews: z.object({}).optional(), tags: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional(), lists: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional(), revealFeedback: z.object({sentiment: z.string().nullable().optional(), message: z.string().nullable().optional()}).nullable().optional()})).optional(), pagination: z.object({start: z.number().int().optional(), next: z.number().int().optional()}).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 }; /** * List companies already saved or enriched in your account (Saved tab), with filters * * POST /v1/enrich/company/saved */ export const createApiV1EnrichCompanySaved = async (args: CreateApiV1EnrichCompanySavedArgs, options: CreateApiV1EnrichCompanySavedOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/enrich/company/saved${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(createApiV1EnrichCompanySavedResponse, await res.json()) };