import {z} from 'zod' export type GitHubRepositorySelectionUpdateParams = {installation_id: string, repository_ids: Array, resource_types?: (Array<'files' | 'issues' | 'pull_requests'> | null) | undefined}; export type UpdateApiV1IntegrationsGithubRepositoriesArgs = {body: GitHubRepositorySelectionUpdateParams}; export type UpdateApiV1IntegrationsGithubRepositoriesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const gitHubRepositorySelectionUpdate = z.object({store_id: z.string(), selected_repository_ids: z.array(z.string()), backfill_repository_ids: z.array(z.string()), skipped_repositories: z.record(z.string(), z.string()), resource_types: z.array(z.enum(["files", "issues", "pull_requests"]))}); export const gitHubRepositorySelectionUpdateResponse = z.object({data: gitHubRepositorySelectionUpdate}); 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 }; /** * Update GitHub repositories for store ingestion * * PUT /v1/integrations/github/repositories */ export const updateApiV1IntegrationsGithubRepositories = async (args: UpdateApiV1IntegrationsGithubRepositoriesArgs, options?: UpdateApiV1IntegrationsGithubRepositoriesOptions) => { const {baseUrl = "https://api.mixedbread.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/integrations/github/repositories${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(gitHubRepositorySelectionUpdateResponse, await res.json()) };