import {z} from 'zod' export type GetApiApiProjectsProjectIdBranchesBranchIdMergePreviewArgs = {projectId: string, branchId: string | 'main'}; export type GetApiApiProjectsProjectIdBranchesBranchIdMergePreviewOptions = {baseUrl: string, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiProjectsProjectIdBranchesBranchIdMergePreviewResponse = z.object({canMerge: z.boolean(), isUpToDate: z.boolean(), needsRebase: z.boolean(), changes: z.array(z.object({type: z.enum(["added", "deleted", "modified", "renamed"]), file: z.unknown(), from: z.unknown(), to: z.unknown()})), conflicts: z.array(z.object({reason: z.enum(["modify/modify", "delete/modify", "add/add", "directory/file"]), path: z.string(), base: z.unknown(), ours: z.unknown(), theirs: z.unknown(), deleted: z.enum(["ours", "theirs"]).optional(), modified: z.unknown()})), stats: z.object({filesAdded: z.number(), filesDeleted: z.number(), filesModified: z.number(), filesRenamed: z.number().optional(), totalChanges: z.number()}), message: z.string()}); 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 merge preview * * GET /api/projects/{projectId}/branches/{branchId}/merge-preview * * Also accepts auth: internalAuth */ export const getApiApiProjectsProjectIdBranchesBranchIdMergePreview = async (args: GetApiApiProjectsProjectIdBranchesBranchIdMergePreviewArgs, options: GetApiApiProjectsProjectIdBranchesBranchIdMergePreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/projects/${encodeURIComponent(String(args.projectId))}/branches/${encodeURIComponent(String(args.branchId))}/merge-preview${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(getApiApiProjectsProjectIdBranchesBranchIdMergePreviewResponse, await res.json()) };