import {z} from 'zod' export type GetApiApiReposNamespaceRepositoryIssuesIndexArgs = {namespace: string, repository: string, index: number}; export type GetApiApiReposNamespaceRepositoryIssuesIndexOptions = {baseUrl: string, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reactionGroup = z.object({count: z.number().int(), emoji: z.string(), reacted: z.boolean()}); export const commentResponse = z.object({author_id: z.string(), author_username: z.string(), body: z.string(), edited_at: z.string().nullable().optional(), id: z.string(), reactions: z.array(reactionGroup)}); export const milestoneBrief = z.object({closed: z.boolean(), due_date: z.string().nullable().optional(), id: z.string(), title: z.string()}); export const issueStatus = z.enum(["open", "in_progress", "completed", "not_planned"]); export const issueResponse = z.object({assignee_ids: z.array(z.string()), assignees: z.array(z.string()), author_id: z.string(), author_username: z.string(), body: z.string(), comment_count: z.number().int(), git_bug_id: z.string(), id: z.string(), index: z.number().int(), labels: z.array(z.string()), milestone: milestoneBrief.nullable().optional(), priority: z.string(), reactions: z.array(reactionGroup), status: issueStatus, title: z.string(), updated_at: z.string()}); export const issueDetailResponse = z.object({comments: z.array(commentResponse), issue: issueResponse}); 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 Issue * * GET /api/repos/{namespace}/{repository}/issues/{index} */ export const getApiApiReposNamespaceRepositoryIssuesIndex = async (args: GetApiApiReposNamespaceRepositoryIssuesIndexArgs, options: GetApiApiReposNamespaceRepositoryIssuesIndexOptions) => { 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))}/issues/${encodeURIComponent(String(args.index))}${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(issueDetailResponse, await res.json()) };