import {z} from 'zod' export type GetApiBreachArgs = {id?: string | undefined}; export type GetApiBreachOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const breach = z.object({assignee_user_email: z.string().optional(), breach_status: z.string().optional(), breach_type: z.string().optional(), date_notified: z.string().optional(), date_occurred: z.string().optional(), date_published: z.string().optional(), description: z.string().optional(), domain: z.string().optional(), employee_count: z.number().int().optional(), exposed_data_classes: z.array(z.string()).optional(), id: z.number().int().optional(), name: z.string().optional(), severity: z.string().optional(), title: z.string().optional(), total_exposures: z.number().int().optional()}); export const breachComment = z.object({comment: z.string().optional(), created_at: z.string().optional(), created_by_email: z.string().optional()}); export const identityBreachResponsePayloadBody = z.object({breach: breach.optional(), comments: z.array(breachComment).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 the details for an identity breach * * GET /breach */ export const getApiBreach = async (args: GetApiBreachArgs, options?: GetApiBreachOptions) => { 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.id != null) query.append("id", String(args.id)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/breach${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(identityBreachResponsePayloadBody, await res.json()) };