import {z} from 'zod' export type CreateIssueDto = {title: string, description: string, environment: string, severity?: ('low' | 'medium' | 'high') | undefined, triggeredAt?: string | undefined, context?: ({alertUrl?: string | undefined, service?: string | undefined, traceId?: string | undefined, spanId?: string | undefined, errorType?: string | undefined, errorMessage?: string | undefined, host?: string | undefined, region?: string | undefined, version?: string | undefined, tags?: Array | undefined, metadata?: {[key: string]: unknown} | undefined}) | undefined}; export type CreateApiIssuesArgs = {'Antimetal-Version'?: string | undefined, body: CreateIssueDto}; export type CreateApiIssuesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createIssueResponseDto = z.object({uuid: z.string(), number: z.number().int().gt(0).lte(9007199254740991), status: z.enum(["investigating", "ready_to_fix", "resolved", "muted"]), title: z.string(), description: z.string(), environment: z.string().optional(), severity: z.enum(["low", "medium", "high"]), createdAt: z.string(), updatedAt: z.string(), triggeredAt: z.string(), latestVersion: z.number().int().gte(1).lte(9007199254740991)}); 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 issue * * POST /issues */ export const createApiIssues = async (args: CreateApiIssuesArgs, options?: CreateApiIssuesOptions) => { const {baseUrl = "https://bff.antimetal.com/api/v2", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Antimetal-Version'] != null) headers.set("Antimetal-Version", String(args['Antimetal-Version'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/issues${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(createIssueResponseDto, await res.json()) };