Skip to content

Commit

Permalink
Update save.js
Browse files Browse the repository at this point in the history
A PR in SvelteKit introduces a number of breaking changes that

lay the foundation for streaming request/response bodies
enable multipart form handling (including file uploads)
better align SvelteKit with modern platforms that deal with `Request` and `Response` objects natively

Hooks (`handle`, `handleError` and `getSession`) and endpoints previously received a proprietary `Request` object:
```ts
interface Request<Locals = Record<string, any>, Body = unknown> {
  url: URL;
  method: string;
  headers: RequestHeaders;
  rawBody: RawBody;
  params: Record<string, string>;
  body: ParameterizedBody<Body>;
  locals: Locals;
}
```
Instead, they now receive a `RequestEvent`:

```ts
export interface RequestEvent<Locals = Record<string, any>> {
  request: Request; // https://developer.mozilla.org/en-US/docs/Web/API/Request
  url: URL; // https://developer.mozilla.org/en-US/docs/Web/API/URL
  params: Record<string, string>;
  locals: Locals;
}
```
`method` and `headers` are no longer necessary as they exist on the `request` object. (`url` is still provided, since the `URL` object contains conveniences like `url.searchParams.get('foo')`, whereas `request.url` is a string.)

To access the request body use the text/json/arrayBuffer/formData methods, e.g. `body = await request.json()`.
See sveltejs/kit#3384 for details
  • Loading branch information
BliiTzZ authored Mar 2, 2022
1 parent dc7a5bc commit 81511b8
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/routes/auth/save.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as api from '$lib/api.js';
import { respond } from './_respond';

export async function post({ body: user, locals }) {
export async function post({ request, locals }) {
let user = await request.json();

if (!locals.user) {
return {
status: 401
Expand Down

0 comments on commit 81511b8

Please sign in to comment.