Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions docs/start/framework/react/guide/isr.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ Server functions can also set cache headers for dynamic data endpoints:
```tsx
// routes/api/products/$productId.ts
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'

export const Route = createFileRoute('/api/products/$productId')({
server: {
handlers: {
GET: async ({ params, request }) => {
const product = await db.products.findById(params.productId)

return json(
return Response.json(
{ product },
{
headers: {
Expand All @@ -113,7 +112,6 @@ For API routes, you can use middleware to set cache headers:
// routes/api/products/$productId.ts
import { createFileRoute } from '@tanstack/react-router'
import { createMiddleware } from '@tanstack/react-start'
import { json } from '@tanstack/react-start'

const cacheMiddleware = createMiddleware().server(async ({ next }) => {
const result = await next()
Expand All @@ -133,7 +131,7 @@ export const Route = createFileRoute('/api/products/$productId')({
handlers: {
GET: async ({ params }) => {
const product = await db.products.findById(params.productId)
return json({ product })
return Response.json({ product })
},
},
},
Expand Down Expand Up @@ -164,7 +162,6 @@ While time-based revalidation works well for most cases, you may need to invalid
```tsx
// routes/api/revalidate.ts
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'

export const Route = createFileRoute('/api/revalidate')({
server: {
Expand All @@ -174,7 +171,7 @@ export const Route = createFileRoute('/api/revalidate')({

// Verify secret token
if (secret !== process.env.REVALIDATE_SECRET) {
return json({ error: 'Invalid token' }, { status: 401 })
return Response.json({ error: 'Invalid token' }, { status: 401 })
}

// Trigger CDN purge via your CDN's API
Expand All @@ -192,7 +189,7 @@ export const Route = createFileRoute('/api/revalidate')({
},
)

return json({ revalidated: true })
return Response.json({ revalidated: true })
},
},
},
Expand Down
9 changes: 4 additions & 5 deletions docs/start/framework/react/guide/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const Route = createFileRoute('/api/users')({
middleware: [requestLogger],
handlers: {
GET: async () => {
return json({ users: await getUsers() })
return Response.json({ users: await getUsers() })
},
},
},
Expand Down Expand Up @@ -185,7 +185,6 @@ Create server routes for health monitoring:
```tsx
// routes/health.ts
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'

export const Route = createFileRoute('/health')({
server: {
Expand All @@ -200,7 +199,7 @@ export const Route = createFileRoute('/health')({
version: process.env.npm_package_version,
}

return json(checks)
return Response.json(checks)
},
},
},
Expand Down Expand Up @@ -314,7 +313,7 @@ export const Route = createFileRoute('/metrics')({
server: {
handlers: {
GET: async () => {
return json({
return Response.json({
system: {
uptime: process.uptime(),
memory: process.memoryUsage(),
Expand Down Expand Up @@ -459,7 +458,7 @@ export const Route = createFileRoute('/admin/errors')({
...data,
}))

return json({ errors })
return Response.json({ errors })
},
},
},
Expand Down
10 changes: 4 additions & 6 deletions docs/start/framework/react/guide/server-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,19 @@ export const Route = createFileRoute('/hello')({
// {"message":"Hello, World!"}
```

## Using the `json` helper function
## Using the `Response.json` helper function

Or you can use the `json` helper function to automatically set the `Content-Type` header to `application/json` and serialize the JSON object for you.
Or you can use the [`Response.json`](https://developer.mozilla.org/en-US/docs/Web/API/Response/json_static) helper function to automatically set the `Content-Type` header to `application/json` and serialize the JSON object for you.

```ts
// routes/hello.ts
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'

export const Route = createFileRoute('/hello')({
server: {
handlers: {
GET: async ({ request }) => {
return json({ message: 'Hello, World!' })
return Response.json({ message: 'Hello, World!' })
},
},
},
Expand All @@ -397,7 +396,6 @@ You can set the status code of the response by passing it as a property of the s
```ts
// routes/hello.ts
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'

export const Route = createFileRoute('/hello')({
server: {
Expand All @@ -409,7 +407,7 @@ export const Route = createFileRoute('/hello')({
status: 404,
})
}
return json(user)
return Response.json(user)
},
},
},
Expand Down
9 changes: 4 additions & 5 deletions docs/start/framework/solid/guide/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const Route = createFileRoute('/api/users')({
middleware: [requestLogger],
handlers: {
GET: async () => {
return json({ users: await getUsers() })
return Response.json({ users: await getUsers() })
},
},
},
Expand Down Expand Up @@ -185,7 +185,6 @@ Create server routes for health monitoring:
```tsx
// routes/health.ts
import { createFileRoute } from '@tanstack/solid-router'
import { json } from '@tanstack/solid-start'

export const Route = createFileRoute('/health')({
server: {
Expand All @@ -200,7 +199,7 @@ export const Route = createFileRoute('/health')({
version: process.env.npm_package_version,
}

return json(checks)
return Response.json(checks)
},
},
},
Expand Down Expand Up @@ -314,7 +313,7 @@ export const Route = createFileRoute('/metrics')({
server: {
handlers: {
GET: async () => {
return json({
return Response.json({
system: {
uptime: process.uptime(),
memory: process.memoryUsage(),
Expand Down Expand Up @@ -459,7 +458,7 @@ export const Route = createFileRoute('/admin/errors')({
...data,
}))

return json({ errors })
return Response.json({ errors })
},
},
},
Expand Down
10 changes: 4 additions & 6 deletions docs/start/framework/solid/guide/server-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,19 @@ export const Route = createFileRoute('/hello')({
// {"message":"Hello, World!"}
```

## Using the `json` helper function
## Using the `Response.json` helper function

Or you can use the `json` helper function to automatically set the `Content-Type` header to `application/json` and serialize the JSON object for you.
Or you can use the [`Response.json`](https://developer.mozilla.org/en-US/docs/Web/API/Response/json_static) helper function to automatically set the `Content-Type` header to `application/json` and serialize the JSON object for you.

```ts
// routes/hello.ts
import { createFileRoute } from '@tanstack/solid-router'
import { json } from '@tanstack/solid-start'

export const Route = createFileRoute('/hello')({
server: {
handlers: {
GET: async ({ request }) => {
return json({ message: 'Hello, World!' })
return Response.json({ message: 'Hello, World!' })
},
},
},
Expand All @@ -397,7 +396,6 @@ You can set the status code of the response by passing it as a property of the s
```ts
// routes/hello.ts
import { createFileRoute } from '@tanstack/solid-router'
import { json } from '@tanstack/solid-start'

export const Route = createFileRoute('/hello')({
server: {
Expand All @@ -409,7 +407,7 @@ export const Route = createFileRoute('/hello')({
status: 404,
})
}
return json(user)
return Response.json(user)
},
},
},
Expand Down
3 changes: 1 addition & 2 deletions e2e/react-start/basic-react-query/src/routes/api.users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'
import axios from 'redaxios'
import type { User } from '../utils/users'

Expand All @@ -16,7 +15,7 @@ export const Route = createFileRoute('/api/users')({
console.info('Fetching users... @', request.url)
const res = await axios.get<Array<User>>(`${queryURL}/users`)
const list = res.data.slice(0, 10)
return json(
return Response.json(
list.map((u) => ({ id: u.id, name: u.name, email: u.email })),
)
},
Expand Down
5 changes: 2 additions & 3 deletions e2e/react-start/basic-react-query/src/routes/api/users.$id.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'
import axios from 'redaxios'
import type { User } from '../../utils/users'

Expand All @@ -16,14 +15,14 @@ export const Route = createFileRoute('/api/users/$id')({
console.info(`Fetching users by id=${params.id}... @`, request.url)
try {
const res = await axios.get<User>(`${queryURL}/users/` + params.id)
return json({
return Response.json({
id: res.data.id,
name: res.data.name,
email: res.data.email,
})
} catch (e) {
console.error(e)
return json({ error: 'User not found' }, { status: 404 })
return Response.json({ error: 'User not found' }, { status: 404 })
}
},
},
Expand Down
3 changes: 1 addition & 2 deletions e2e/react-start/basic/src/routes/api.users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'
import axios from 'redaxios'

import type { User } from '~/utils/users'
Expand All @@ -19,7 +18,7 @@ export const Route = createFileRoute('/api/users')({

const list = res.data.slice(0, 10)

return json(
return Response.json(
list.map((u) => ({ id: u.id, name: u.name, email: u.email })),
)
},
Expand Down
5 changes: 2 additions & 3 deletions e2e/react-start/basic/src/routes/api/users.$id.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'
import axios from 'redaxios'
import type { User } from '~/utils/users'

Expand All @@ -17,14 +16,14 @@ export const Route = createFileRoute('/api/users/$id')({
try {
const res = await axios.get<User>(`${queryURL}/users/` + params.id)

return json({
return Response.json({
id: res.data.id,
name: res.data.name,
email: res.data.email,
})
} catch (e) {
console.error(e)
return json({ error: 'User not found' }, { status: 404 })
return Response.json({ error: 'User not found' }, { status: 404 })
}
},
},
Expand Down
3 changes: 1 addition & 2 deletions e2e/react-start/custom-basepath/src/routes/api.users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'
import axios from 'redaxios'

import type { User } from '~/utils/users'
Expand All @@ -19,7 +18,7 @@ export const Route = createFileRoute('/api/users')({

const list = res.data.slice(0, 10)

return json(
return Response.json(
list.map((u) => ({ id: u.id, name: u.name, email: u.email })),
)
},
Expand Down
5 changes: 2 additions & 3 deletions e2e/react-start/custom-basepath/src/routes/api/users.$id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { json } from '@tanstack/react-start'
import axios from 'redaxios'
import type { User } from '~/utils/users'
import { createFileRoute } from '@tanstack/react-router'
Expand All @@ -17,14 +16,14 @@ export const Route = createFileRoute('/api/users/$id')({
try {
const res = await axios.get<User>(`${queryURL}/users/` + params.id)

return json({
return Response.json({
id: res.data.id,
name: res.data.name,
email: res.data.email,
})
} catch (e) {
console.error(e)
return json({ error: 'User not found' }, { status: 404 })
return Response.json({ error: 'User not found' }, { status: 404 })
}
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createFileRoute } from '@tanstack/react-router'
import { createMiddleware, json } from '@tanstack/react-start'
import { createMiddleware } from '@tanstack/react-start'

const testParentMiddleware = createMiddleware().server(async ({ next }) => {
const result = await next({ context: { testParent: true } })
Expand All @@ -18,7 +18,7 @@ export const Route = createFileRoute('/api/middleware-context')({
middleware: [testMiddleware],
handlers: {
GET: ({ request, context }) => {
return json({
return Response.json({
url: request.url,
context: context,
expectedContext: { testParent: true, test: true },
Expand Down
3 changes: 1 addition & 2 deletions e2e/react-start/server-routes/src/routes/api/only-any.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'

export const Route = createFileRoute('/api/only-any')({
server: {
handlers: {
ANY: ({ request }) => {
return json(
return Response.json(
{
handler: 'ANY',
method: request.method,
Expand Down
3 changes: 1 addition & 2 deletions e2e/solid-start/basic-solid-query/src/routes/api.users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createFileRoute } from '@tanstack/solid-router'
import { json } from '@tanstack/solid-start'
import axios from 'redaxios'
import type { User } from '../utils/users'

Expand All @@ -16,7 +15,7 @@ export const Route = createFileRoute('/api/users')({
console.info('Fetching users... @', request.url)
const res = await axios.get<Array<User>>(`${queryURL}/users`)
const list = res.data.slice(0, 10)
return json(
return Response.json(
list.map((u) => ({ id: u.id, name: u.name, email: u.email })),
)
},
Expand Down
Loading
Loading