diff --git a/documentation/docs/10-getting-started/40-web-standards.md b/documentation/docs/10-getting-started/40-web-standards.md index 81fc10d8b25a..fb3b03332f64 100644 --- a/documentation/docs/10-getting-started/40-web-standards.md +++ b/documentation/docs/10-getting-started/40-web-standards.md @@ -24,20 +24,18 @@ An instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Res ### Headers -The [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) interface allows you to read incoming `request.headers` and set outgoing `response.headers`. For example, you can get the `request.headers` as shown below, and use the [`json` convenience function](@sveltejs-kit#json) to send modified `response.headers`: +The [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) interface allows you to read incoming `request.headers` and set outgoing `response.headers`. For example, you can get the `request.headers` as shown below, and use [`Response.json`](https://developer.mozilla.org/en-US/docs/Web/API/Response/json_static) to send modified `response.headers`: ```js // @errors: 2461 /// file: src/routes/what-is-my-user-agent/+server.js -import { json } from '@sveltejs/kit'; - /** @type {import('./$types').RequestHandler} */ export function GET({ request }) { // log all headers console.log(...request.headers); // create a JSON Response using a header we received - return json({ + return Response.json({ // retrieve a specific header userAgent: request.headers.get('user-agent') }, { @@ -54,8 +52,6 @@ When dealing with HTML native form submissions you'll be working with [`FormData ```js // @errors: 2461 /// file: src/routes/hello/+server.js -import { json } from '@sveltejs/kit'; - /** @type {import('./$types').RequestHandler} */ export async function POST(event) { const body = await event.request.formData(); @@ -63,7 +59,7 @@ export async function POST(event) { // log all fields console.log([...body]); - return json({ + return Response.json({ // get a specific field's value name: body.get('name') ?? 'world' }); diff --git a/documentation/docs/20-core-concepts/10-routing.md b/documentation/docs/20-core-concepts/10-routing.md index 2202076c28a0..4ce47d352804 100644 --- a/documentation/docs/20-core-concepts/10-routing.md +++ b/documentation/docs/20-core-concepts/10-routing.md @@ -320,7 +320,7 @@ export function GET({ url }) { The first argument to `Response` can be a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream), making it possible to stream large amounts of data or create server-sent events (unless deploying to platforms that buffer responses, like AWS Lambda). -You can use the [`error`](@sveltejs-kit#error), [`redirect`](@sveltejs-kit#redirect) and [`json`](@sveltejs-kit#json) methods from `@sveltejs/kit` for convenience (but you don't have to). +You can use the [`error`](@sveltejs-kit#error) and [`redirect`](@sveltejs-kit#redirect) methods from `@sveltejs/kit` for convenience (but you don't have to). If an error is thrown (either `error(...)` or an unexpected error), the response will be a JSON representation of the error or a fallback error page — which can be customised via `src/error.html` — depending on the `Accept` header. The [`+error.svelte`](#error) component will _not_ be rendered in this case. You can read more about error handling [here](errors). @@ -361,12 +361,10 @@ By exporting `POST`/`PUT`/`PATCH`/`DELETE`/`OPTIONS`/`HEAD`/`QUERY` handlers, `+ ```js /// file: src/routes/api/add/+server.js -import { json } from '@sveltejs/kit'; - /** @type {import('./$types').RequestHandler} */ export async function POST({ request }) { const { a, b } = await request.json(); - return json(a + b); + return Response.json(a + b); } ``` @@ -380,18 +378,16 @@ Exporting the `fallback` handler will match any unhandled request methods, inclu ```js /// file: src/routes/api/add/+server.js -import { json, text } from '@sveltejs/kit'; - /** @type {import('./$types').RequestHandler} */ export async function POST({ request }) { const { a, b } = await request.json(); - return json(a + b); + return Response.json(a + b); } // This handler will respond to PUT, PATCH, DELETE, etc. /** @type {import('./$types').RequestHandler} */ export async function fallback({ request }) { - return text(`I caught your ${request.method} request!`); + return new Response(`I caught your ${request.method} request!`); } ```