Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form actions #6469

Merged
merged 93 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
1c3ba7a
basic actions method
dummdidumm Aug 31, 2022
9ac19bc
add form store
dummdidumm Aug 31, 2022
7b71fa3
remove export let errors in favor of form store
dummdidumm Aug 31, 2022
d880096
make actions an object
dummdidumm Aug 31, 2022
102851b
implement FilesFormData
dummdidumm Sep 1, 2022
e650cb3
restrict values type, fix conversion, switch argument order
dummdidumm Sep 1, 2022
678361d
Merge branch 'master' into form-actions
dummdidumm Sep 1, 2022
5ece9c0
validation->invalid
dummdidumm Sep 1, 2022
9c9dfbe
start writing docs
dummdidumm Sep 1, 2022
b11c578
implement handleFile
dummdidumm Sep 1, 2022
3450eb4
fix tests
dummdidumm Sep 1, 2022
38a253c
test for files
dummdidumm Sep 1, 2022
2da72d9
complete migration message
dummdidumm Sep 1, 2022
62fa93c
support validation error thrown in endpoints
dummdidumm Sep 1, 2022
225be5c
infer file type from handleFile hook
dummdidumm Sep 1, 2022
7ce58ea
add handleFile to build
dummdidumm Sep 1, 2022
9dcb6be
types, cleanup
dummdidumm Sep 1, 2022
f1941a5
$form -> $submitted
dummdidumm Sep 1, 2022
71ed352
woops
dummdidumm Sep 1, 2022
ba0f204
allow arbitrary data on invalid, persist data in success case
dummdidumm Sep 2, 2022
f6be2c5
fix infered FileType
dummdidumm Sep 2, 2022
d8e7137
give JSON response a well-defined shape
dummdidumm Sep 2, 2022
b2e30b9
provide form state through form prop and $page.form
dummdidumm Sep 2, 2022
29294d0
return invalid instead of throwing it
dummdidumm Sep 2, 2022
ba4aa8f
types for actions
dummdidumm Sep 2, 2022
be35a3a
Merge remote-tracking branch 'origin/master' into form-actions
dummdidumm Sep 2, 2022
855c1b8
fix, skip test
dummdidumm Sep 2, 2022
09c10af
Merge remote-tracking branch 'origin/master' into form-actions
dummdidumm Sep 3, 2022
2a14eaf
updateForm (simple version)
dummdidumm Sep 3, 2022
792b659
making a start at enhance
dummdidumm Sep 3, 2022
ca3cae9
bye bye method overrides
dummdidumm Sep 3, 2022
8d9fe0b
update create-svelte default template
dummdidumm Sep 3, 2022
823153f
Merge remote-tracking branch 'origin/master' into form-actions
dummdidumm Sep 3, 2022
6213bf0
remove handleFile
dummdidumm Sep 4, 2022
646013b
full blown enhance and updateForm through $app/forms
dummdidumm Sep 4, 2022
d82933f
fix type reference
dummdidumm Sep 4, 2022
16f1ad4
adjust default template
dummdidumm Sep 4, 2022
9eef9a7
remove $page.form for now (too much of a footgun due to resets)
dummdidumm Sep 4, 2022
f17ef01
tests, ensure form is only reset on page changes
dummdidumm Sep 4, 2022
c208888
?????
dummdidumm Sep 4, 2022
f0d0222
cleanup
dummdidumm Sep 4, 2022
d43a085
docs about multiple forms
dummdidumm Sep 4, 2022
f3926c4
make docs build
dummdidumm Sep 4, 2022
1045828
lint
Rich-Harris Sep 4, 2022
2123945
fix
Rich-Harris Sep 4, 2022
e1aadca
add toggle action
Rich-Harris Sep 4, 2022
b19bb4b
rename generated FormData type to ActionData
Rich-Harris Sep 4, 2022
2647a10
reset form on navigation, not invalidation
Rich-Harris Sep 5, 2022
0eb68e2
silence missing/unused form prop warnings, DRY out code a bit
Rich-Harris Sep 5, 2022
6fe185a
Update packages/kit/src/runtime/server/page/actions.js
Rich-Harris Sep 5, 2022
21b9f46
Update packages/kit/src/runtime/server/page/actions.js
Rich-Harris Sep 5, 2022
7bc739e
Update packages/kit/src/runtime/server/page/actions.js
Rich-Harris Sep 5, 2022
c3c8369
change message to reference actions
Rich-Harris Sep 5, 2022
732093c
Update packages/kit/src/runtime/server/page/actions.js
Rich-Harris Sep 5, 2022
333b87a
Update packages/kit/src/runtime/server/page/actions.js
Rich-Harris Sep 5, 2022
b561522
be more specific about what content-type is accepted
Rich-Harris Sep 5, 2022
e17acfd
Update packages/kit/src/runtime/server/page/render.js
Rich-Harris Sep 5, 2022
a3a21ca
Update packages/kit/src/runtime/server/page/render.js
Rich-Harris Sep 5, 2022
260e0d4
unskip test
Rich-Harris Sep 5, 2022
fa78907
Merge branch 'form-actions' of github.com:sveltejs/kit into form-actions
Rich-Harris Sep 5, 2022
7c8c360
fix types
dummdidumm Sep 5, 2022
b081e8c
tweak enhance function and make todos example work again
dummdidumm Sep 5, 2022
60b8341
changeset
dummdidumm Sep 5, 2022
3be2e9e
deduplicate type usage
dummdidumm Sep 5, 2022
4119273
merge
Rich-Harris Sep 5, 2022
26908ba
lint
dummdidumm Sep 5, 2022
34e515b
wording, make updateForm (now applySubmissionResult) more powerful
dummdidumm Sep 5, 2022
e62a6ea
fix, docs
dummdidumm Sep 5, 2022
91b143c
applySubmissionResult -> applyAction
dummdidumm Sep 5, 2022
b94a89a
change enhance function signature
dummdidumm Sep 5, 2022
5c15c23
fix template
dummdidumm Sep 5, 2022
3275ebb
fix template
dummdidumm Sep 5, 2022
45580ac
merge
Rich-Harris Sep 5, 2022
d11c059
Update .changeset/spicy-pugs-applaud.md
Rich-Harris Sep 5, 2022
4198872
Update packages/kit/src/runtime/app/forms.js
Rich-Harris Sep 6, 2022
a09aad3
Apply suggestions from code review
Rich-Harris Sep 6, 2022
840f714
prettier
Rich-Harris Sep 6, 2022
7eb124b
rename SubmissionResult to ActionResult
Rich-Harris Sep 6, 2022
7ff7e7f
Update packages/kit/types/ambient.d.ts
Rich-Harris Sep 6, 2022
ff023d4
Update packages/kit/types/ambient.d.ts
Rich-Harris Sep 6, 2022
1b194bb
updateForm -> applyAction
Rich-Harris Sep 6, 2022
7b6e988
use RequestEvent instead of ActionEvent
Rich-Harris Sep 6, 2022
9385bd0
fix
Rich-Harris Sep 6, 2022
c393862
invalidate first, delegate redirect/error handling to applyAction
Rich-Harris Sep 6, 2022
bb910be
remove token stuff, simplify a bit
Rich-Harris Sep 6, 2022
e28d384
show +error page without reloading route
Rich-Harris Sep 6, 2022
7ee1425
check action return data can be serialized as JSON
Rich-Harris Sep 6, 2022
3906922
add note to render.js
Rich-Harris Sep 6, 2022
0f1f5c3
merge FetchFormResponse and ActionResult
Rich-Harris Sep 6, 2022
d899b30
update docs
Rich-Harris Sep 6, 2022
5ee4fcd
remove logging
Rich-Harris Sep 6, 2022
a0d7580
tiny docs tweak
dummdidumm Sep 6, 2022
f2cdab2
merge
Rich-Harris Sep 6, 2022
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
6 changes: 6 additions & 0 deletions .changeset/spicy-pugs-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'create-svelte': patch
'@sveltejs/kit': patch
---

[breaking] Replace `POST`/`PUT`/`PATCH`/`DELETE` in `+page.server.js` with `export const actions`
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"files": [
"**/CHANGELOG.md",
"**/.svelte-kit/**",
"documentation/**/*.md",
"packages/package/test/fixtures/**/expected/**/*",
"packages/package/test/watch/expected/**/*",
"packages/package/test/watch/package/**/*",
Expand Down
69 changes: 1 addition & 68 deletions documentation/docs/03-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,74 +112,7 @@ During client-side navigation, SvelteKit will load this data from the server, wh

Like `+page.js`, `+page.server.js` can export [page options](/docs/page-options) — `prerender`, `ssr` and `csr`.

#### Actions

`+page.server.js` can also declare _actions_, which correspond to the `POST`, `PATCH`, `PUT` and `DELETE` HTTP methods. A request made to the page with one of these methods will invoke the corresponding action before rendering the page.

An action can return a `{ status?, errors }` object if there are validation errors (`status` defaults to `400`), or an optional `{ location }` object to redirect the user to another page:

```js
/// file: src/routes/login/+page.server.js

// @filename: ambient.d.ts
declare global {
const createSessionCookie: (userid: string) => string;
const hash: (content: string) => string;
const db: {
findUser: (name: string) => Promise<{
id: string;
username: string;
password: string;
}>
}
}

export {};

// @filename: index.js
// ---cut---
import { error } from '@sveltejs/kit';

/** @type {import('./$types').Action} */
export async function POST({ cookies, request, url }) {
const values = await request.formData();

const username = /** @type {string} */ (values.get('username'));
const password = /** @type {string} */ (values.get('password'));

const user = await db.findUser(username);

if (!user) {
return {
status: 403,
errors: {
username: 'No user with this username'
}
};
}

if (user.password !== hash(password)) {
return {
status: 403,
errors: {
password: 'Incorrect password'
}
};
}

cookies.set('sessionid', createSessionCookie(user.id), {
httpOnly: true
});

return {
location: url.searchParams.get('redirectTo') ?? '/'
};
}
```

If validation `errors` are returned, they will be available inside `+page.svelte` as `export let errors`.

> The actions API will likely change in the near future: https://github.com/sveltejs/kit/discussions/5875
A `+page.server.js` file can also export _actions_. If `load` lets you read data from the server, `actions` let you write data _to_ the server using the `<form>` element. To learn how to use them, see the [form actions](/docs/form-actions) section.

### +error

Expand Down
Loading