import {z} from 'zod' export type GetApiV2ProjectsProjIdReposRepoIdArgs = {proj_id: string, repo_id: string}; export type GetApiV2ProjectsProjIdReposRepoIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const policyRepoStatus = z.enum(["invalid", "pending", "valid"]); export const sSHAuthDataRead = z.object({auth_type: z.literal("ssh").optional(), username: z.string(), public_key: z.string().optional(), private_key: z.string()}); export const policyRepoRead = z.object({id: z.string(), status: policyRepoStatus, key: z.string(), url: z.string(), main_branch_name: z.string().optional(), credentials: sSHAuthDataRead, activate_when_validated: z.boolean().optional()}); 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 Policy Repo * * GET /v2/projects/{proj_id}/repos/{repo_id} */ export const getApiV2ProjectsProjIdReposRepoId = async (args: GetApiV2ProjectsProjIdReposRepoIdArgs, options: GetApiV2ProjectsProjIdReposRepoIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/projects/${encodeURIComponent(String(args.proj_id))}/repos/${encodeURIComponent(String(args.repo_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(policyRepoRead, await res.json()) };