import {z} from 'zod' export type GetApiReposOwnerRepoPagesBuildsBuildIdArgs = {owner: string, repo: string, build_id: number}; export type GetApiReposOwnerRepoPagesBuildsBuildIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nullableSimpleUser = z.object({name: z.string().nullable().optional(), email: z.string().nullable().optional(), login: z.string(), id: z.number().int(), node_id: z.string(), avatar_url: z.string(), gravatar_id: z.string().nullable(), url: z.string(), html_url: z.string(), followers_url: z.string(), following_url: z.string(), gists_url: z.string(), starred_url: z.string(), subscriptions_url: z.string(), organizations_url: z.string(), repos_url: z.string(), events_url: z.string(), received_events_url: z.string(), type: z.string(), site_admin: z.boolean(), starred_at: z.string().optional()}).nullable(); export const pageBuild = z.object({url: z.string(), status: z.string(), error: z.object({message: z.string().nullable()}), pusher: nullableSimpleUser, commit: z.string(), duration: z.number().int(), created_at: z.string(), updated_at: 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 GitHub Enterprise Server Pages build * * GET /repos/{owner}/{repo}/pages/builds/{build_id} */ export const getApiReposOwnerRepoPagesBuildsBuildId = async (args: GetApiReposOwnerRepoPagesBuildsBuildIdArgs, options?: GetApiReposOwnerRepoPagesBuildsBuildIdOptions) => { const {baseUrl = "http://HOSTNAME/api/v3", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/repos/${encodeURIComponent(String(args.owner))}/${encodeURIComponent(String(args.repo))}/pages/builds/${encodeURIComponent(String(args.build_id))}${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(pageBuild, await res.json()) };