Open History to see the last commit
1import { createFileRoute } from '@tanstack/solid-router'
2import { json } from '@tanstack/solid-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 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
19 const user = (await res.json()) as User
20β
21 return json({
22 id: user.id,
23 name: user.name,
24 email: user.email,
25 })
26 } catch (e) {
27 console.error(e)
28 return json({ error: 'User not found' }, { status: 404 })
29 }
30 },
31 },
32 },
33})