Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { json } from '@tanstack/react-start'
3import type { User } from '~/utils/users'
4β
5export const Route = createFileRoute('/api/users/$userId')({
6 server: {
7 handlers: {
8 GET: async ({ params, request }) => {
9 console.info(`Fetching users by id=${params.userId}... @`, request.url)
10 try {
11 const res = await fetch(
12 'https://jsonplaceholder.typicode.com/users/' + params.userId,
13 )
14 if (!res.ok) {
15 throw new Error('Failed to fetch user')
16 }
17β
18 const user = (await res.json()) as User
19β
20 return json({
21 id: user.id,
22 name: user.name,
23 email: user.email,
24 })
25 } catch (e) {
26 console.error(e)
27 return json({ error: 'User not found' }, { status: 404 })
28 }
29 },
30 },
31 },
32})