import {z} from 'zod' export type GetApiGithubWorkspacesWorkspaceIdPrLinksArgs = {workspace_id: string}; export type GetApiGithubWorkspacesWorkspaceIdPrLinksOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const workspacePrLink = z.object({pr_number: z.number().int(), repo_full_name: z.string(), pr_html_url: z.string(), linked_at: z.string().nullable(), linked_by_user_id: z.string()}); export const workspacePrLinksResponse = z.object({links: z.record(z.string(), z.array(workspacePrLink))}); 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 Workspace Pr Links * * GET /github/workspaces/{workspace_id}/pr-links */ export const getApiGithubWorkspacesWorkspaceIdPrLinks = async (args: GetApiGithubWorkspacesWorkspaceIdPrLinksArgs, options: GetApiGithubWorkspacesWorkspaceIdPrLinksOptions) => { 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(/\/+$/, '')}/github/workspaces/${encodeURIComponent(String(args.workspace_id))}/pr-links${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(workspacePrLinksResponse, await res.json()) };