import {z} from 'zod' export type GetApiRisksVendorsWithRiskArgs = {vendor_labels?: Array | undefined, vendor_label_ids_match_all?: boolean | undefined, include_unlabeled?: boolean | undefined, min_score?: number | undefined, max_score?: number | undefined, name_prefix?: string | undefined, vendor_ids?: Array | undefined, vendor_label_ids_do_not_match?: boolean | undefined, vendor_tiers?: Array | undefined, portfolios?: Array | undefined, assessed_vendors_only?: boolean | undefined, questionnaire_type_ids?: Array | undefined, vendor_date_added_start?: string | undefined, vendor_date_added_end?: string | undefined, vendor_date_added_before?: boolean | undefined, vendor_date_added_between?: boolean | undefined, risk_id: string}; export type GetApiRisksVendorsWithRiskOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const externalEvidence = z.object({datastore_vendor_id: z.number().int().optional(), id: z.number().int().optional(), name: z.string().optional()}); export const externalLabel = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const externalSurveyMini = z.object({id: z.number().int().optional(), is_public: z.boolean().optional(), name: z.string().optional()}); export const externalVendorWithRisk = z.object({additional_evidence: z.array(externalEvidence).optional(), count_hostnames_failed: z.number().int().optional(), date_detected: z.string().optional(), is_partially_waived: z.boolean().optional(), is_waived: z.boolean().optional(), labels: z.array(externalLabel).optional(), name: z.string().optional(), primary_hostname: z.string().optional(), questionnaires: z.array(externalSurveyMini).optional(), score: z.number().int().optional(), vendor_id: z.number().int().optional(), vendor_tier: z.number().int().optional(), vendor_tier_name: z.string().optional()}); export const externalVendorsWithRiskResponse = z.object({risk_id: z.string().optional(), risk_severity: z.string().optional(), risk_type: z.string().optional(), total_vendors_matching_filter: z.number().int().optional(), vendors_with_risk: z.array(externalVendorWithRisk).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 }; /** * Get vendors affected by a risk * * GET /risks/vendors_with_risk */ export const getApiRisksVendorsWithRisk = async (args: GetApiRisksVendorsWithRiskArgs, options?: GetApiRisksVendorsWithRiskOptions) => { const {baseUrl = "https://cyber-risk.upguard.com/api/public", fetch: fetchFn = fetch, APIKeyInHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.vendor_labels ?? []) query.append("vendor_labels", String(item)) if (args.vendor_label_ids_match_all != null) query.append("vendor_label_ids_match_all", String(args.vendor_label_ids_match_all)) if (args.include_unlabeled != null) query.append("include_unlabeled", String(args.include_unlabeled)) if (args.min_score != null) query.append("min_score", String(args.min_score)) if (args.max_score != null) query.append("max_score", String(args.max_score)) if (args.name_prefix != null) query.append("name_prefix", String(args.name_prefix)) for (const item of args.vendor_ids ?? []) query.append("vendor_ids", String(item)) if (args.vendor_label_ids_do_not_match != null) query.append("vendor_label_ids_do_not_match", String(args.vendor_label_ids_do_not_match)) for (const item of args.vendor_tiers ?? []) query.append("vendor_tiers", String(item)) for (const item of args.portfolios ?? []) query.append("portfolios", String(item)) if (args.assessed_vendors_only != null) query.append("assessed_vendors_only", String(args.assessed_vendors_only)) for (const item of args.questionnaire_type_ids ?? []) query.append("questionnaire_type_ids", String(item)) if (args.vendor_date_added_start != null) query.append("vendor_date_added_start", String(args.vendor_date_added_start)) if (args.vendor_date_added_end != null) query.append("vendor_date_added_end", String(args.vendor_date_added_end)) if (args.vendor_date_added_before != null) query.append("vendor_date_added_before", String(args.vendor_date_added_before)) if (args.vendor_date_added_between != null) query.append("vendor_date_added_between", String(args.vendor_date_added_between)) if (args.risk_id != null) query.append("risk_id", String(args.risk_id)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/risks/vendors_with_risk${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(externalVendorsWithRiskResponse, await res.json()) };