@sveltejs/kit 1.0.0-next.477 → 1.0.0-next.478

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.477",
3
+ "version": "1.0.0-next.478",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -16,46 +16,43 @@ const ssr = import.meta.env.SSR;
16
16
  export const applyAction = ssr ? guard('applyAction') : client.apply_action;
17
17
 
18
18
  /** @type {import('$app/forms').enhance} */
19
- export function enhance(element, submit = () => {}) {
19
+ export function enhance(form, submit = () => {}) {
20
20
  /**
21
21
  * @param {{
22
- * element: HTMLFormElement | HTMLButtonElement | HTMLInputElement;
23
- * form: HTMLFormElement;
22
+ * action: string;
24
23
  * result: import('types').ActionResult;
25
24
  * }} opts
26
25
  */
27
- const fallback_callback = async ({ element, form, result }) => {
26
+ const fallback_callback = async ({ action, result }) => {
28
27
  if (result.type === 'success') {
29
28
  await invalidateAll();
30
29
  }
31
30
 
32
- const action = element.formAction ?? form.action;
33
-
34
31
  if (location.origin + location.pathname === action.split('?')[0]) {
35
32
  applyAction(result);
36
33
  }
37
34
  };
38
35
 
39
- const form =
40
- element instanceof HTMLFormElement ? element : /** @type {HTMLFormElement} */ (element.form);
41
- if (!form) throw new Error('Element is not associated with a form');
42
-
43
36
  /** @param {SubmitEvent} event */
44
37
  async function handle_submit(event) {
45
38
  event.preventDefault();
46
39
 
47
- const action = element.formAction ?? form.action;
48
-
40
+ // We can't do submitter.formAction directly because that property is always set
41
+ const action = event.submitter?.hasAttribute('formaction')
42
+ ? /** @type {HTMLButtonElement | HTMLInputElement} */ (event.submitter).formAction
43
+ : form.action;
49
44
  const data = new FormData(form);
45
+ const controller = new AbortController();
50
46
 
51
47
  let cancelled = false;
52
48
  const cancel = () => (cancelled = true);
53
49
 
54
50
  const callback =
55
51
  submit({
56
- element,
57
- data,
52
+ action,
58
53
  cancel,
54
+ controller,
55
+ data,
59
56
  form
60
57
  }) ?? fallback_callback;
61
58
  if (cancelled) return;
@@ -69,16 +66,18 @@ export function enhance(element, submit = () => {}) {
69
66
  headers: {
70
67
  accept: 'application/json'
71
68
  },
72
- body: data
69
+ body: data,
70
+ signal: controller.signal
73
71
  });
74
72
 
75
73
  result = await response.json();
76
74
  } catch (error) {
75
+ if (/** @type {any} */ (error)?.name === 'AbortError') return;
77
76
  result = { type: 'error', error };
78
77
  }
79
78
 
80
79
  callback({
81
- element,
80
+ action,
82
81
  data,
83
82
  form,
84
83
  // @ts-expect-error generic constraints stuff we don't care about
@@ -1506,7 +1506,7 @@ async function load_data(url, invalid) {
1506
1506
  * @returns {App.PageError}
1507
1507
  */
1508
1508
  function handle_error(error, event) {
1509
- return hooks.handleError({ error, event }) ?? /** @type {any} */ ({ error: 'Internal Error' });
1509
+ return hooks.handleError({ error, event }) ?? /** @type {any} */ ({ message: 'Internal Error' });
1510
1510
  }
1511
1511
 
1512
1512
  // TODO remove for 1.0
@@ -96,20 +96,19 @@ declare module '$app/forms' {
96
96
  import type { ActionResult } from '@sveltejs/kit';
97
97
 
98
98
  export type SubmitFunction<
99
- Element extends HTMLFormElement | HTMLInputElement | HTMLButtonElement = HTMLFormElement,
100
99
  Success extends Record<string, unknown> | undefined = Record<string, any>,
101
100
  Invalid extends Record<string, unknown> | undefined = Record<string, any>
102
101
  > = (input: {
102
+ action: string;
103
103
  data: FormData;
104
104
  form: HTMLFormElement;
105
- element: Element;
105
+ controller: AbortController;
106
106
  cancel: () => void;
107
107
  }) =>
108
108
  | void
109
109
  | ((opts: {
110
- data: FormData;
111
110
  form: HTMLFormElement;
112
- element: Element;
111
+ action: string;
113
112
  result: ActionResult<Success, Invalid>;
114
113
  }) => void);
115
114
 
@@ -119,14 +118,14 @@ declare module '$app/forms' {
119
118
  * @param options Callbacks for different states of the form lifecycle
120
119
  */
121
120
  export function enhance<
122
- Element extends HTMLFormElement | HTMLInputElement | HTMLButtonElement = HTMLFormElement,
123
121
  Success extends Record<string, unknown> | undefined = Record<string, any>,
124
122
  Invalid extends Record<string, unknown> | undefined = Record<string, any>
125
123
  >(
126
- element: Element,
124
+ form: HTMLFormElement,
127
125
  /**
128
- * Called upon submission with the given FormData.
126
+ * Called upon submission with the given FormData and the `action` that should be triggered.
129
127
  * If `cancel` is called, the form will not be submitted.
128
+ * You can use the abort `controller` to cancel the submission in case another one starts.
130
129
  * If a function is returned, that function is called with the response from the server.
131
130
  * If nothing is returned, the fallback will be used.
132
131
  *
@@ -137,7 +136,7 @@ declare module '$app/forms' {
137
136
  * - redirects in case of a redirect response
138
137
  * - redirects to the nearest error page in case of an unexpected error
139
138
  */
140
- submit?: SubmitFunction<Element, Success, Invalid>
139
+ submit?: SubmitFunction<Success, Invalid>
141
140
  ): { destroy: () => void };
142
141
 
143
142
  /**