import {z} from 'zod' export type GetApiApiOrgIdUsersEmailIdArgs = {org_id: string, email_id: string}; export type GetApiApiOrgIdUsersEmailIdOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const userRole = z.enum(["root", "admin", "editor", "viewer", "user", "service_account", "sre_agent"]); export const orgRoleMapping = z.object({org_id: z.string(), org_name: z.string(), role: userRole}); export const lockoutState = z.object({email: z.string(), failed_attempts: z.number().int(), last_failed_at: z.number().int().nullable().optional(), locked: z.boolean(), locked_until: z.number().int().nullable().optional(), lockout_level: z.number().int(), retry_after_secs: z.number().int().nullable().optional()}); export const userDetailsResponse = z.object({created_at: z.number().int(), description: z.string().nullable().optional(), email: z.string(), first_name: z.string().optional(), is_external: z.boolean().optional(), is_system: z.boolean().optional(), last_name: z.string().optional(), orgs: z.array(orgRoleMapping).nullable().optional(), role: z.string(), token: z.string().nullable().optional(), lockout: lockoutState}); 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 }; /** * Get a single organization user * * GET /api/{org_id}/users/{email_id} */ export const getApiApiOrgIdUsersEmailId = async (args: GetApiApiOrgIdUsersEmailIdArgs, options: GetApiApiOrgIdUsersEmailIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/users/${encodeURIComponent(String(args.email_id))}${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(userDetailsResponse, await res.json()) };