import {z} from 'zod' export type GetApiManageAdminUserGroupOptions = {baseUrl: string, APIKeyCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const userRole = z.enum(["basic", "admin"]); export const userInfo = z.object({id: z.string(), email: z.string(), is_active: z.boolean(), is_superuser: z.boolean(), is_verified: z.boolean(), role: userRole}); export const documentSource = z.enum(["ingestion_api", "slack", "web", "google_drive", "gmail", "requesttracker", "github", "gitlab", "guru", "bookstack", "confluence", "discourse", "slab", "salesforce", "jira", "productboard", "file", "notion", "zulip", "linear", "hubspot", "document360", "gong", "google_sites", "zendesk", "loopio", "sharepoint", "teams", "freshdesk"]); export const inputType = z.enum(["load_state", "poll", "event"]); export const connectorSnapshot = z.object({name: z.string(), source: documentSource, input_type: inputType, connector_specific_config: z.object({}), refresh_freq: z.number().int().optional(), disabled: z.boolean(), id: z.number().int(), credential_ids: z.array(z.number().int()), time_created: z.string(), time_updated: z.string()}); export const credentialSnapshot = z.object({credential_json: z.object({}), admin_public: z.boolean(), id: z.number().int(), user_id: z.string().optional(), time_created: z.string(), time_updated: z.string()}); export const connectorCredentialPairDescriptor = z.object({id: z.number().int(), name: z.string().optional(), connector: connectorSnapshot, credential: credentialSnapshot}); export const userGroup = z.object({id: z.number().int(), name: z.string(), users: z.array(userInfo), cc_pairs: z.array(connectorCredentialPairDescriptor), is_up_to_date: z.boolean(), is_up_for_deletion: z.boolean()}); export const getApiManageAdminUserGroupResponse = z.array(userGroup); 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 User Groups * * GET /manage/admin/user-group */ export const getApiManageAdminUserGroup = async (options: GetApiManageAdminUserGroupOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyCookie !== undefined) cookies.push("fastapiusersauth=" + encodeURIComponent(APIKeyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/manage/admin/user-group${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(getApiManageAdminUserGroupResponse, await res.json()) };