import {z} from 'zod' export type GetApiFourthPartyListArgs = {page_size?: number | undefined, page_token?: string | undefined, labels?: Array | undefined, tier?: Array | undefined, min_score?: number | undefined, max_score?: number | undefined, portfolios?: Array | undefined}; export type GetApiFourthPartyListOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const fourthPartyWatchedVendor = z.object({id: z.number().int().optional(), labels: z.array(z.string()).optional(), name: z.string().optional(), score: z.number().int().optional(), tier: z.number().int().optional()}); export const fourthPartyProduct = z.object({description: z.string().optional(), link: z.string().optional(), name: z.string().optional(), tag: z.string().optional(), vendors: z.array(fourthPartyWatchedVendor).optional()}); export const fourthParty = z.object({id: z.number().int().optional(), name: z.string().optional(), primary_hostname: z.string().optional(), products: z.array(fourthPartyProduct).optional(), score: z.number().int().optional(), vendors: z.array(fourthPartyWatchedVendor).optional()}); export const getFourthPartiesV1RespBody = z.object({fourth_parties: z.array(fourthParty).optional(), next_page_token: z.string().optional(), total_results: z.number().int().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 fourth party vendors * * GET /fourth_party/list */ export const getApiFourthPartyList = async (args: GetApiFourthPartyListArgs, options?: GetApiFourthPartyListOptions) => { 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") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) for (const item of args.labels ?? []) query.append("labels", String(item)) for (const item of args.tier ?? []) query.append("tier", String(item)) 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)) for (const item of args.portfolios ?? []) query.append("portfolios", String(item)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/fourth_party/list${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(getFourthPartiesV1RespBody, await res.json()) };