import {z} from 'zod' export type GetApiIssuesArgs = {'Antimetal-Version'?: string | undefined, limit?: number | undefined, startingAfter?: string | undefined, endingBefore?: string | undefined, status?: ('investigating' | 'ready_to_fix' | 'resolved' | 'muted') | undefined, environment?: string | undefined, search?: string | undefined}; export type GetApiIssuesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const issueSummaryDto = z.object({uuid: z.string(), number: z.number().int().gt(0).lte(9007199254740991), title: z.string(), description: z.string(), severity: z.enum(["low", "medium", "high"]), status: z.enum(["investigating", "ready_to_fix", "resolved", "muted"]), environment: z.string(), triggeredAt: z.string(), seedCount: z.number().int().gte(0).lte(9007199254740991)}); export const getApiIssuesResponse = z.object({object: z.literal("list"), data: z.array(issueSummaryDto), has_more: z.boolean(), after: z.string().optional(), before: z.string().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 }; /** * Fetch issues for an organization * * GET /issues */ export const getApiIssues = async (args: GetApiIssuesArgs, options?: GetApiIssuesOptions) => { const {baseUrl = "https://bff.antimetal.com/api/v2", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Antimetal-Version'] != null) headers.set("Antimetal-Version", String(args['Antimetal-Version'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.startingAfter != null) query.append("startingAfter", String(args.startingAfter)) if (args.endingBefore != null) query.append("endingBefore", String(args.endingBefore)) if (args.status != null) query.append("status", String(args.status)) if (args.environment != null) query.append("environment", String(args.environment)) if (args.search != null) query.append("search", String(args.search)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/issues${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(getApiIssuesResponse, await res.json()) };