import {z} from 'zod' export type CreateApiApiV2CompaniesSearchArgs = {body: {GenericSearchText?: string | undefined, EcosystemIds?: (Array | null) | undefined, IndustryIds?: (Array | null) | undefined, CountryCodes?: (Array | null) | undefined, TagIds?: (Array | null) | undefined, ProductGroupIds?: (Array | null) | undefined, LicenseTypes?: (Array<'Not Attached' | 'Continuous Annual' | 'Continuous 60-day'> | null) | undefined, LicenseStatus?: (Array<'Inactive' | 'Active'> | null) | undefined, GradeLetters?: (Array<'A+' | 'A' | 'A-' | 'B+' | 'B' | 'B-' | 'C+' | 'C' | 'C-' | 'D+' | 'D' | 'D-' | 'F'> | null) | undefined, ControlCodes?: (Array | null) | undefined, CveOrCweCodes?: (Array | null) | undefined, ThreatActorIds?: (Array | null) | undefined, LeakIds?: (Array | null) | undefined, TagConfidences?: (Array<'Low' | 'Medium' | 'High' | 'VeryHigh'> | null) | undefined, HasCriticalVuln?: boolean | undefined, HasLeakIn90Days?: boolean | undefined, HasPoorSsl?: boolean | undefined, HasPoorDdos?: boolean | undefined, HasPoorSmtp?: boolean | undefined, HasPoorDns?: boolean | undefined, CwssCvssBiggerThanValue?: number | undefined, CompanySizeGreaterThan?: (number | null) | undefined, CompanySizeLessThan?: (number | null) | undefined, CountryRiskRatings?: (Array<'Low' | 'Medium' | 'High' | 'Sensitive'> | null) | undefined, NumberOfEmployeeGreaterThan?: (number | null) | undefined, NumberOfEmployeeLessThan?: (number | null) | undefined, IsInOfac?: boolean | undefined, RevenueGreaterThan?: (number | null) | undefined, RevenueLessThan?: (number | null) | undefined, NetIncomeGreaterThan?: (number | null) | undefined, NetIncomeLessThan?: (number | null) | undefined, OperatingIncomeGreaterThan?: (number | null) | undefined, OperatingIncomeLessThan?: (number | null) | undefined, EquityGreaterThan?: (number | null) | undefined, EquityLessThan?: (number | null) | undefined, UseGenAIs?: (Array<'Yes' | 'Unknown'> | null) | undefined}}; export type CreateApiApiV2CompaniesSearchOptions = {baseUrl?: string | undefined, OAuthv2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV2CompaniesSearchResponse = z.object({Key: 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 }; /** * Searches through monitored companies. * * POST /api/v2/companies/search */ export const createApiApiV2CompaniesSearch = async (args: CreateApiApiV2CompaniesSearchArgs, options?: CreateApiApiV2CompaniesSearchOptions) => { const {baseUrl = "https://app.blackkitetech.com", fetch: fetchFn = fetch, OAuthv2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuthv2 !== undefined) headers.set('authorization', `Bearer ${OAuthv2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/companies/search${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(createApiApiV2CompaniesSearchResponse, await res.json()) };