@sveltejs/kit 1.0.0-next.475 → 1.0.0-next.476

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.475",
3
+ "version": "1.0.0-next.476",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -6,3 +6,5 @@ This module cannot be imported into client-side code.
6
6
  import { env } from '$env/dynamic/private';
7
7
  console.log(env.DEPLOYMENT_SPECIFIC_VARIABLE);
8
8
  ```
9
+
10
+ > In `dev`, `$env/dynamic` always includes environment variables from `.env`. In `prod`, this behavior will depend on your adapter.
@@ -16,32 +16,55 @@ 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(form, submit = () => {}) {
20
- /** @param {import('types').ActionResult} result */
21
- const fallback_callback = async (result) => {
19
+ export function enhance(element, submit = () => {}) {
20
+ /**
21
+ * @param {{
22
+ * element: HTMLFormElement | HTMLButtonElement | HTMLInputElement;
23
+ * form: HTMLFormElement;
24
+ * result: import('types').ActionResult;
25
+ * }} opts
26
+ */
27
+ const fallback_callback = async ({ element, form, result }) => {
22
28
  if (result.type === 'success') {
23
29
  await invalidateAll();
24
30
  }
25
31
 
26
- if (location.origin + location.pathname === form.action.split('?')[0]) {
32
+ const action = element.formAction ?? form.action;
33
+
34
+ if (location.origin + location.pathname === action.split('?')[0]) {
27
35
  applyAction(result);
28
36
  }
29
37
  };
30
38
 
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
+
31
43
  /** @param {SubmitEvent} event */
32
44
  async function handle_submit(event) {
33
45
  event.preventDefault();
34
46
 
47
+ const action = element.formAction ?? form.action;
48
+
35
49
  const data = new FormData(form);
36
50
 
37
51
  let cancelled = false;
38
52
  const cancel = () => (cancelled = true);
39
53
 
40
- const callback = submit({ form, data, cancel }) ?? fallback_callback;
54
+ const callback =
55
+ submit({
56
+ element,
57
+ data,
58
+ cancel,
59
+ form
60
+ }) ?? fallback_callback;
41
61
  if (cancelled) return;
42
62
 
63
+ /** @type {import('types').ActionResult} */
64
+ let result;
65
+
43
66
  try {
44
- const response = await fetch(form.action, {
67
+ const response = await fetch(action, {
45
68
  method: 'POST',
46
69
  headers: {
47
70
  accept: 'application/json'
@@ -49,10 +72,28 @@ export function enhance(form, submit = () => {}) {
49
72
  body: data
50
73
  });
51
74
 
52
- callback(await response.json());
75
+ result = await response.json();
53
76
  } catch (error) {
54
- callback({ type: 'error', error });
77
+ result = { type: 'error', error };
55
78
  }
79
+
80
+ callback({
81
+ element,
82
+ data,
83
+ form,
84
+ // @ts-expect-error generic constraints stuff we don't care about
85
+ result,
86
+ // TODO remove for 1.0
87
+ get type() {
88
+ throw new Error('(result) => {...} has changed to ({ result }) => {...}');
89
+ },
90
+ get location() {
91
+ throw new Error('(result) => {...} has changed to ({ result }) => {...}');
92
+ },
93
+ get error() {
94
+ throw new Error('(result) => {...} has changed to ({ result }) => {...}');
95
+ }
96
+ });
56
97
  }
57
98
 
58
99
  form.addEventListener('submit', handle_submit);
@@ -89,13 +89,22 @@ declare module '$app/forms' {
89
89
  import type { ActionResult } from '@sveltejs/kit';
90
90
 
91
91
  export type SubmitFunction<
92
- Success extends Record<string, unknown> | undefined,
93
- Invalid extends Record<string, unknown> | undefined
92
+ Element extends HTMLFormElement | HTMLInputElement | HTMLButtonElement = HTMLFormElement,
93
+ Success extends Record<string, unknown> | undefined = Record<string, any>,
94
+ Invalid extends Record<string, unknown> | undefined = Record<string, any>
94
95
  > = (input: {
95
96
  data: FormData;
96
97
  form: HTMLFormElement;
98
+ element: Element;
97
99
  cancel: () => void;
98
- }) => void | ((result: ActionResult<Success, Invalid>) => void);
100
+ }) =>
101
+ | void
102
+ | ((opts: {
103
+ data: FormData;
104
+ form: HTMLFormElement;
105
+ element: Element;
106
+ result: ActionResult<Success, Invalid>;
107
+ }) => void);
99
108
 
100
109
  /**
101
110
  * This action enhances a `<form>` element that otherwise would work without JavaScript.
@@ -103,10 +112,11 @@ declare module '$app/forms' {
103
112
  * @param options Callbacks for different states of the form lifecycle
104
113
  */
105
114
  export function enhance<
115
+ Element extends HTMLFormElement | HTMLInputElement | HTMLButtonElement = HTMLFormElement,
106
116
  Success extends Record<string, unknown> | undefined = Record<string, any>,
107
117
  Invalid extends Record<string, unknown> | undefined = Record<string, any>
108
118
  >(
109
- form: HTMLFormElement,
119
+ element: Element,
110
120
  /**
111
121
  * Called upon submission with the given FormData.
112
122
  * If `cancel` is called, the form will not be submitted.
@@ -120,7 +130,7 @@ declare module '$app/forms' {
120
130
  * - redirects in case of a redirect response
121
131
  * - redirects to the nearest error page in case of an unexpected error
122
132
  */
123
- submit?: SubmitFunction<Success, Invalid>
133
+ submit?: SubmitFunction<Element, Success, Invalid>
124
134
  ): { destroy: () => void };
125
135
 
126
136
  /**