import {z} from 'zod' export type User = {login?: string | undefined, name?: string | undefined, uuid?: string | undefined}; export type Comment = {uuid?: string | undefined, vulnerability_lookup_origin?: string | undefined, title?: string | undefined, description?: string | undefined, description_format?: string | undefined, vulnerability?: string | undefined, creation_timestamp?: string | undefined, timestamp?: string | undefined, related_vulnerabilities?: Array | undefined, meta?: Record | undefined, author?: User | undefined}; export type CreateApiCommentArgs = {'X-Fields'?: unknown | undefined, body: Comment}; export type CreateApiCommentOptions = {baseUrl: string, apikey?: string | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.object({count: z.number().int().optional(), offset: z.number().int().optional(), limit: z.number().int().optional()}); export const user = z.object({login: z.string().optional(), name: z.string().optional(), uuid: z.string().optional()}); export const comment = z.object({uuid: z.string().optional(), vulnerability_lookup_origin: z.string().optional(), title: z.string().optional(), description: z.string().optional(), description_format: z.string().optional(), vulnerability: z.string().optional(), creation_timestamp: z.string().optional(), timestamp: z.string().optional(), related_vulnerabilities: z.array(z.string()).optional(), meta: z.object({}).optional(), author: user.optional()}); export const commentsList = z.object({metadata: metadata.optional(), data: z.array(comment).optional()}); export const createApiCommentResponse = z.array(commentsList); 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 }; /** * Create a new comment related to a security advisory (vulnerability) * * POST /comment/ */ export const createApiComment = async (args: CreateApiCommentArgs, options: CreateApiCommentOptions) => { const {baseUrl, fetch: fetchFn = fetch, apikey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Fields'] != null) headers.set("X-Fields", String(args['X-Fields'])) if (apikey !== undefined) headers.set("X-API-KEY", apikey) const url = `${baseUrl.replace(/\/+$/, '')}/comment/${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(createApiCommentResponse, await res.json()) };