import {z} from 'zod' export type GetApiPullRequestsCliSuggestionsArgs = {prUrl: string, repositoryId: string, prNumber: string, format: string, severity: string, category: string, 'x-team-key': string, authorization: string}; export type GetApiPullRequestsCliSuggestionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pullRequestSuggestionCommentDto = z.object({id: z.object({}), pullRequestReviewId: z.object({})}); export const pullRequestSuggestionDto = z.object({id: z.string().optional(), suggestionContent: z.string().optional(), oneSentenceSummary: z.string().optional(), label: z.string().optional(), severity: z.string().optional(), deliveryStatus: z.string().optional(), relevantLinesStart: z.number().optional(), relevantLinesEnd: z.number().optional(), existingCode: z.string().optional(), improvedCode: z.string().optional(), filePath: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), comment: pullRequestSuggestionCommentDto.optional()}); export const pullRequestSuggestionsGroupDto = z.object({files: z.array(pullRequestSuggestionDto), prLevel: z.array(pullRequestSuggestionDto)}); export const pullRequestSuggestionsPayloadDto = z.object({prNumber: z.number(), repositoryId: z.string(), repositoryFullName: z.string().optional(), suggestions: pullRequestSuggestionsGroupDto}); export const pullRequestSuggestionsResponseDto = z.object({statusCode: z.number(), type: z.string(), data: pullRequestSuggestionsPayloadDto}); 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 PR suggestions (CLI) via GET * * GET /pull-requests/cli/suggestions */ export const getApiPullRequestsCliSuggestions = async (args: GetApiPullRequestsCliSuggestionsArgs, options?: GetApiPullRequestsCliSuggestionsOptions) => { const {baseUrl = "http://localhost:3001", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.prUrl != null) query.append("prUrl", String(args.prUrl)) if (args.repositoryId != null) query.append("repositoryId", String(args.repositoryId)) if (args.prNumber != null) query.append("prNumber", String(args.prNumber)) if (args.format != null) query.append("format", String(args.format)) if (args.severity != null) query.append("severity", String(args.severity)) if (args.category != null) query.append("category", String(args.category)) if (args['x-team-key'] != null) headers.set("x-team-key", String(args['x-team-key'])) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/pull-requests/cli/suggestions${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(pullRequestSuggestionsResponseDto, await res.json()) };