import {z} from 'zod' export type SeverityLevel = 'critical' | 'high' | 'medium' | 'low'; export type LabelType = 'performance_and_optimization' | 'security' | 'error_handling' | 'refactoring' | 'maintainability' | 'potential_issues' | 'code_style' | 'documentation_and_comments' | 'kody_rules' | 'breaking_changes' | 'bug' | 'performance' | 'cross_file'; export type IssueStatus = 'open' | 'resolved' | 'dismissed'; export type GetApiIssuesArgs = {title?: string | undefined, severity?: SeverityLevel | undefined, category?: LabelType | undefined, status?: IssueStatus | undefined, organizationId?: string | undefined, repositoryName?: string | undefined, prNumber?: number | undefined, filePath?: string | undefined, prAuthor?: string | undefined, beforeAt?: string | undefined, afterAt?: string | undefined}; export type GetApiIssuesOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const apiArrayResponseDto = z.object({statusCode: z.number(), type: z.string(), data: z.array(z.object({}))}); 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 issues * * GET /issues */ export const getApiIssues = async (args: GetApiIssuesArgs, options?: GetApiIssuesOptions) => { const {baseUrl = "http://localhost:3001", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.title != null) query.append("title", String(args.title)) if (args.severity != null) query.append("severity", String(args.severity)) if (args.category != null) query.append("category", String(args.category)) if (args.status != null) query.append("status", String(args.status)) if (args.organizationId != null) query.append("organizationId", String(args.organizationId)) if (args.repositoryName != null) query.append("repositoryName", String(args.repositoryName)) if (args.prNumber != null) query.append("prNumber", String(args.prNumber)) if (args.filePath != null) query.append("filePath", String(args.filePath)) if (args.prAuthor != null) query.append("prAuthor", String(args.prAuthor)) if (args.beforeAt != null) query.append("beforeAt", String(args.beforeAt)) if (args.afterAt != null) query.append("afterAt", String(args.afterAt)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) 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(apiArrayResponseDto, await res.json()) };