import {z} from 'zod' export type GetApiApiReposNamespaceRepositoryBranchTreeFilesArgs = {namespace: string, repository: string, tree: string}; export type GetApiApiReposNamespaceRepositoryBranchTreeFilesOptions = {baseUrl: string, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fileCommitInfo = z.object({authorEmail: z.string(), authorName: z.string(), authorUid: z.string().nullable().optional(), message: z.string(), time: z.number().int(), sha1: z.string()}); export const fileType = z.enum(["tree", "blob", "blobExecutable", "link", "commit"]); export const fileEntry = z.object({commit: fileCommitInfo, fileName: z.string(), fileType: fileType, submoduleTargetOid: z.string().nullable().optional()}); export const branchFilesResponse = z.object({files: z.array(fileEntry), truncated: z.boolean()}); 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 Files * * GET /api/repos/{namespace}/{repository}/branch/{tree}/files */ export const getApiApiReposNamespaceRepositoryBranchTreeFiles = async (args: GetApiApiReposNamespaceRepositoryBranchTreeFilesArgs, options: GetApiApiReposNamespaceRepositoryBranchTreeFilesOptions) => { const {baseUrl, fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (cookieAuth !== undefined) cookies.push("gitarena-session=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/repos/${encodeURIComponent(String(args.namespace))}/${encodeURIComponent(String(args.repository))}/branch/${encodeURIComponent(String(args.tree))}/files${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(branchFilesResponse, await res.json()) };