Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

users.tsx

37 lines876 Bytes

1import { queryOptions } from '@tanstack/solid-query'
2import axios from 'redaxios'
3​
4export type User = {
5 id: number
6 name: string
7 email: string
8}
9​
10const PORT =
11 import.meta.env.VITE_SERVER_PORT || process.env.VITE_SERVER_PORT || 3000
12​
13export const DEPLOY_URL = `http://localhost:${PORT}`
14​
15export const usersQueryOptions = () =>
16 queryOptions({
17 queryKey: ['users'],
18 queryFn: () =>
19 axios
20 .get<Array<User>>(DEPLOY_URL + '/api/users')
21 .then((r) => r.data)
22 .catch(() => {
23 throw new Error('Failed to fetch users')
24 }),
25 })
26​
27export const userQueryOptions = (id: string) =>
28 queryOptions({
29 queryKey: ['users', id],
30 queryFn: () =>
31 axios
32 .get<User>(DEPLOY_URL + '/api/users/' + id)
33 .then((r) => r.data)
34 .catch(() => {
35 throw new Error('Failed to fetch user')
36 }),
37 })