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

users.$userId.ts

30 lines906 Bytes

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 const user = (await res.json()) as User
18 return json({
19 id: user.id,
20 name: user.name,
21 email: user.email,
22 })
23 } catch (e) {
24 console.error(e)
25 return json({ error: 'User not found' }, { status: 404 })
26 }
27 },
28 },
29 },
30})