11/**
22 * Server functions for user management.
33 *
4- * Calls the LibreChat Admin API (/api/admin/users) for list, search, and delete.
5- * Create user is not yet wired.
4+ * Calls the LibreChat Admin API (/api/admin/users) for list, search, create, and delete.
65 */
76
87import { z } from 'zod' ;
98import { queryOptions } from '@tanstack/react-query' ;
109import { SystemRoles } from 'librechat-data-provider' ;
1110import { createServerFn } from '@tanstack/react-start' ;
12- import type { AdminUserSearchResult } from '@librechat/data-schemas' ;
13- import type { TUser } from 'librechat-data-provider' ;
11+ import type { AdminUserListItem , AdminUserSearchResult } from '@librechat/data-schemas' ;
1412import { apiFetch , extractApiError } from './utils/api' ;
1513
1614// ── Server functions ─────────────────────────────────────────────────
1715
1816export const getUsersFn = createServerFn ( { method : 'GET' } ) . handler (
19- async ( ) : Promise < { users : TUser [ ] } > => {
17+ async ( ) : Promise < { users : AdminUserListItem [ ] } > => {
2018 const response = await apiFetch ( '/api/admin/users' ) ;
2119 if ( ! response . ok ) {
2220 throw new Error ( `Failed to fetch users: ${ response . status } ` ) ;
2321 }
24- const json = ( await response . json ( ) ) as { users : TUser [ ] } ;
22+ const json = ( await response . json ( ) ) as { users : AdminUserListItem [ ] } ;
2523 return { users : json . users ?? [ ] } ;
2624 } ,
2725) ;
2826
2927export const usersQueryOptions = queryOptions ( {
3028 queryKey : [ 'users' ] ,
31- queryFn : ( ) => getUsersFn ( ) . then ( ( r ) => r . users ) ,
29+ queryFn : ( ) => getUsersFn ( ) . then ( ( r ) => r . users as AdminUserListItem [ ] ) ,
3230 staleTime : 30_000 ,
3331} ) ;
3432
@@ -40,9 +38,23 @@ export const createUserFn = createServerFn({ method: 'POST' })
4038 role : z . nativeEnum ( SystemRoles ) ,
4139 } ) ,
4240 )
43- . handler ( async ( ) : Promise < { user : TUser } > => {
44- throw new Error ( 'Not implemented: createUserFn' ) ;
45- } ) ;
41+ . handler (
42+ async ( {
43+ data,
44+ } : {
45+ data : { name : string ; email : string ; role : SystemRoles } ;
46+ } ) : Promise < { user : AdminUserListItem } > => {
47+ const response = await apiFetch ( '/api/admin/users' , {
48+ method : 'POST' ,
49+ body : JSON . stringify ( data ) ,
50+ } ) ;
51+ if ( ! response . ok ) {
52+ await extractApiError ( response , 'Failed to create user' ) ;
53+ }
54+ const json = ( await response . json ( ) ) as { user : AdminUserListItem } ;
55+ return json ;
56+ } ,
57+ ) ;
4658
4759export const deleteUserFn = createServerFn ( { method : 'POST' } )
4860 . inputValidator ( z . object ( { id : z . string ( ) } ) )
@@ -57,7 +69,7 @@ export const deleteUserFn = createServerFn({ method: 'POST' })
5769
5870export const searchUsersFn = createServerFn ( { method : 'GET' } )
5971 . inputValidator ( z . object ( { query : z . string ( ) } ) )
60- . handler ( async ( { data } ) : Promise < { users : AdminUserSearchResult [ ] } > => {
72+ . handler ( async ( { data } ) => {
6173 const response = await apiFetch ( `/api/admin/users/search?q=${ encodeURIComponent ( data . query ) } ` ) ;
6274 if ( ! response . ok ) {
6375 await extractApiError ( response , 'Failed to search users' ) ;
0 commit comments