import {z} from 'zod' export type UserRole = 'admin' | 'editor' | 'viewer' | 'external' | 'basic'; export type Permission = {role?: UserRole | undefined, team_scopes?: Array | undefined, view_dev_coaching?: boolean | undefined}; export type TeamMembership = {name: string, joined_at?: string | undefined}; export type CustomInfo = {extra_emails?: Array | undefined, extra_ids?: Array | undefined}; export type UserPatchModel = {name?: string | undefined, email?: string | undefined, permission?: Permission | undefined, team_membership?: Array | undefined, custom_info?: CustomInfo | undefined}; export type PatchApiApiV1UsersUserIdArgs = {user_id: number, body: UserPatchModel}; export type PatchApiApiV1UsersUserIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const connectedUsersModel = z.object({platform_users: z.array(z.object({})).optional(), contributors: z.array(z.object({})).optional()}); export const userModel = z.object({id: z.number().int(), organization_id: z.string(), name: z.string(), email: z.string().optional(), avatar_url: z.string().optional(), extra: z.object({}).optional(), created_at: z.string(), updated_at: z.string().optional(), deleted_at: z.string().optional(), team_membership: z.array(z.object({})).optional(), user_type_id: z.number().int().optional(), connected_users: connectedUsersModel.optional(), permissions: z.object({}).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 }; /** * Patch User * * PATCH /api/v1/users/{user_id} */ export const patchApiApiV1UsersUserId = async (args: PatchApiApiV1UsersUserIdArgs, options?: PatchApiApiV1UsersUserIdOptions) => { const {baseUrl = "https://public-api.linearb.io", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/users/${encodeURIComponent(String(args.user_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(userModel, await res.json()) };