import {z} from 'zod' export type CreateApiApiV2SupplychainSearchArgs = {body: {Relationships?: (Array<'Unmonitored 3rd Party' | '3rd Party' | '4th Party'> | null) | undefined, CountryCodes?: (Array | null) | undefined, IndustryIds?: (Array | null) | undefined, TagIds?: (Array | null) | undefined, EcosystemIds?: (Array | null) | undefined, GradeLetters?: (Array<'A+' | 'A' | 'A-' | 'B+' | 'B' | 'B-' | 'C+' | 'C' | 'C-' | 'D+' | 'D' | 'D-' | 'F'> | null) | undefined, DetectionTypes?: (Array<'No Detection Type Found' | 'Domain detected in TXT Records' | 'SSL Certificate Issuer' | 'Tech/Product detected on an IT asset' | 'Domain detected in DNS records' | 'IP Ownership (ISP)' | 'Domain detected in MX Records' | 'Company detected in Export/Import data' | 'DoD Award detected' | 'Cloud Asset Detected' | 'Mentioned in job posting' | 'Internet Traffic Detected' | 'Monitored Entity'> | null) | undefined, AssetCountryCodes?: (Array | null) | undefined, RsiMin?: (number | null) | undefined, DbiMin?: (number | null) | undefined, FairMin?: (number | null) | undefined, ConcentrationRiskMin?: (number | null) | undefined, ComplianceRatingMax?: (number | null) | undefined}}; export type CreateApiApiV2SupplychainSearchOptions = {baseUrl?: string | undefined, OAuthv2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV2SupplychainSearchResponse = 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 supply chain relationships. * * POST /api/v2/supplychain/search */ export const createApiApiV2SupplychainSearch = async (args: CreateApiApiV2SupplychainSearchArgs, options?: CreateApiApiV2SupplychainSearchOptions) => { 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/supplychain/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(createApiApiV2SupplychainSearchResponse, await res.json()) };