import {z} from 'zod' export type GetApiReposRepoIdCompareArgs = {base_id?: string | undefined, other_id?: string | undefined}; export type GetApiReposRepoIdCompareOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const objectStatus = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const fileMode = z.union([z.literal(16877), z.literal(33188), z.literal(33261), z.literal(40960)]); export const storageUri = z.string(); export const storageBackend = z.union([z.literal(1), z.literal(2)]); export const fileEntry = z.object({path: z.string(), prev_path: z.string().optional(), status: objectStatus, mode: fileMode, mtime: z.string().optional(), blob: z.object({storage_uri: storageUri, storage_backend: storageBackend, size: z.number().int(), sha: z.string()}).optional()}); export const comparisonItem = z.object({base_item: fileEntry.optional(), other_item: fileEntry.optional(), status: objectStatus}); export const getApiReposRepoIdCompareResponse = z.object({object: z.literal("ComparisonItem"), items: z.array(comparisonItem), cascaded_changes_count: z.number().int()}); 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 }; /** * Compare two refs (commit/branch/workspace/tag/tree) in this repo * * GET /repos/{repo_id}/compare */ export const getApiReposRepoIdCompare = async (args: GetApiReposRepoIdCompareArgs, options?: GetApiReposRepoIdCompareOptions) => { const {baseUrl = "https://api.diversion.dev/v0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.base_id != null) query.append("base_id", String(args.base_id)) if (args.other_id != null) query.append("other_id", String(args.other_id)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/repos/${encodeURIComponent(String(args.repo_id))}/compare${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(getApiReposRepoIdCompareResponse, await res.json()) };