import {z} from 'zod' export type ProjectVisibility = 'org' | 'restricted'; export type SetProjectVisibilityRequest = {visibility: ProjectVisibility}; export type UpdateApiApiV1ProjectsProjectIdVisibilityArgs = {project_id: string, body: SetProjectVisibilityRequest}; export type UpdateApiApiV1ProjectsProjectIdVisibilityOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const projectVisibility = z.enum(["org", "restricted"]); export const projectGrant = z.object({user_id: z.string(), email: z.string().nullable(), display_name: z.string().nullable(), granted_by: z.string().nullable(), created_at: z.string()}); export const projectSharing = z.object({visibility: projectVisibility, grants: z.array(projectGrant)}); 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 }; /** * Set Project Visibility * * PUT /api/v1/projects/{project_id}/visibility */ export const updateApiApiV1ProjectsProjectIdVisibility = async (args: UpdateApiApiV1ProjectsProjectIdVisibilityArgs, options: UpdateApiApiV1ProjectsProjectIdVisibilityOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.project_id))}/visibility${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(projectSharing, await res.json()) };