Skip to content

Commit 4bec5bf

Browse files
fix: Add explicit type annotations to createUserFn handler
TanStack Start's inputValidator requires explicit type annotation on handler's data parameter. This fixes the 'invalid input: expected object, received undefined' error when creating users.
1 parent fce9596 commit 4bec5bf

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

‎src/server/users.ts‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
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

87
import { z } from 'zod';
98
import { queryOptions } from '@tanstack/react-query';
109
import { SystemRoles } from 'librechat-data-provider';
1110
import { 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';
1412
import { apiFetch, extractApiError } from './utils/api';
1513

1614
// ── Server functions ─────────────────────────────────────────────────
1715

1816
export 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

2927
export 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

4759
export const deleteUserFn = createServerFn({ method: 'POST' })
4860
.inputValidator(z.object({ id: z.string() }))
@@ -57,7 +69,7 @@ export const deleteUserFn = createServerFn({ method: 'POST' })
5769

5870
export 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

Comments
 (0)