import {z} from 'zod' export type ProjectGroup = {group?: string | undefined, id?: string | undefined}; export type UserSettings = {collapseSidenav?: boolean | undefined, displayAllProjectsForAdmin?: boolean | undefined, itemsPerPage?: number | undefined, lastSeenChangelogVersion?: string | undefined, selectProjectTableView?: boolean | undefined, selectedProjectId?: string | undefined, selectedTheme?: string | undefined}; export type User = {annotations?: {[key: string]: string} | undefined, creationTimestamp?: string | undefined, deletionTimestamp?: string | undefined, email?: string | undefined, id?: string | undefined, isAdmin?: boolean | undefined, name?: string | undefined, projects?: Array | undefined, userSettings?: UserSettings | undefined}; export type CreateApiApiV1ProjectsProjectIdUsersArgs = {project_id: string, body: User}; export type CreateApiApiV1ProjectsProjectIdUsersOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const projectGroup = z.object({group: z.string().optional(), id: z.string().optional()}); export const userSettings = z.object({collapseSidenav: z.boolean().optional(), displayAllProjectsForAdmin: z.boolean().optional(), itemsPerPage: z.number().int().optional(), lastSeenChangelogVersion: z.string().optional(), selectProjectTableView: z.boolean().optional(), selectedProjectId: z.string().optional(), selectedTheme: z.string().optional()}); export const user = z.object({annotations: z.record(z.string(), z.string()).optional(), creationTimestamp: z.string().optional(), deletionTimestamp: z.string().optional(), email: z.string().optional(), id: z.string().optional(), isAdmin: z.boolean().optional(), name: z.string().optional(), projects: z.array(projectGroup).optional(), userSettings: userSettings.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 }; /** * Adds the given user to the given project * * POST /api/v1/projects/{project_id}/users */ export const createApiApiV1ProjectsProjectIdUsers = async (args: CreateApiApiV1ProjectsProjectIdUsersArgs, options: CreateApiApiV1ProjectsProjectIdUsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.project_id))}/users${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(user, await res.json()) };