@sveltejs/kit 1.0.0-next.473 → 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
|
@@ -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.
|
|
@@ -147,7 +147,7 @@ export function get_default_build_config({ config, input, ssr, outDir }) {
|
|
|
147
147
|
* @returns {string}
|
|
148
148
|
*/
|
|
149
149
|
export function assets_base(config) {
|
|
150
|
-
return config.paths.assets || config.paths.base || './';
|
|
150
|
+
return config.paths.assets + '/' || config.paths.base + '/' || './';
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
const method_names = new Set(['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']);
|
package/src/runtime/app/forms.js
CHANGED
|
@@ -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(
|
|
20
|
-
/**
|
|
21
|
-
|
|
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
|
-
|
|
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 =
|
|
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(
|
|
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
|
-
|
|
75
|
+
result = await response.json();
|
|
53
76
|
} catch (error) {
|
|
54
|
-
|
|
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);
|
|
@@ -35,6 +35,8 @@ export async function handle_action_json_request(event, options, server) {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
check_named_default_separate(actions);
|
|
39
|
+
|
|
38
40
|
try {
|
|
39
41
|
const data = await call_action(event, actions);
|
|
40
42
|
|
|
@@ -114,6 +116,8 @@ export async function handle_action_request(event, server) {
|
|
|
114
116
|
};
|
|
115
117
|
}
|
|
116
118
|
|
|
119
|
+
check_named_default_separate(actions);
|
|
120
|
+
|
|
117
121
|
try {
|
|
118
122
|
const data = await call_action(event, actions);
|
|
119
123
|
|
|
@@ -141,6 +145,17 @@ export async function handle_action_request(event, server) {
|
|
|
141
145
|
}
|
|
142
146
|
}
|
|
143
147
|
|
|
148
|
+
/**
|
|
149
|
+
* @param {import('types').Actions} actions
|
|
150
|
+
*/
|
|
151
|
+
function check_named_default_separate(actions) {
|
|
152
|
+
if (actions.default && Object.keys(actions).length > 1) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`When using named actions, the default action cannot be used. See the docs for more info: https://kit.svelte.dev/docs/form-actions#named-actions`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
144
159
|
/**
|
|
145
160
|
* @param {import('types').RequestEvent} event
|
|
146
161
|
* @param {NonNullable<import('types').SSRNode['server']['actions']>} actions
|
|
@@ -153,6 +168,9 @@ export async function call_action(event, actions) {
|
|
|
153
168
|
for (const param of url.searchParams) {
|
|
154
169
|
if (param[0].startsWith('/')) {
|
|
155
170
|
name = param[0].slice(1);
|
|
171
|
+
if (name === 'default') {
|
|
172
|
+
throw new Error('Cannot use reserved action name "default"');
|
|
173
|
+
}
|
|
156
174
|
break;
|
|
157
175
|
}
|
|
158
176
|
}
|
package/types/ambient.d.ts
CHANGED
|
@@ -88,16 +88,35 @@ declare module '$app/environment' {
|
|
|
88
88
|
declare module '$app/forms' {
|
|
89
89
|
import type { ActionResult } from '@sveltejs/kit';
|
|
90
90
|
|
|
91
|
+
export type SubmitFunction<
|
|
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>
|
|
95
|
+
> = (input: {
|
|
96
|
+
data: FormData;
|
|
97
|
+
form: HTMLFormElement;
|
|
98
|
+
element: Element;
|
|
99
|
+
cancel: () => void;
|
|
100
|
+
}) =>
|
|
101
|
+
| void
|
|
102
|
+
| ((opts: {
|
|
103
|
+
data: FormData;
|
|
104
|
+
form: HTMLFormElement;
|
|
105
|
+
element: Element;
|
|
106
|
+
result: ActionResult<Success, Invalid>;
|
|
107
|
+
}) => void);
|
|
108
|
+
|
|
91
109
|
/**
|
|
92
110
|
* This action enhances a `<form>` element that otherwise would work without JavaScript.
|
|
93
111
|
* @param form The form element
|
|
94
112
|
* @param options Callbacks for different states of the form lifecycle
|
|
95
113
|
*/
|
|
96
114
|
export function enhance<
|
|
115
|
+
Element extends HTMLFormElement | HTMLInputElement | HTMLButtonElement = HTMLFormElement,
|
|
97
116
|
Success extends Record<string, unknown> | undefined = Record<string, any>,
|
|
98
117
|
Invalid extends Record<string, unknown> | undefined = Record<string, any>
|
|
99
118
|
>(
|
|
100
|
-
|
|
119
|
+
element: Element,
|
|
101
120
|
/**
|
|
102
121
|
* Called upon submission with the given FormData.
|
|
103
122
|
* If `cancel` is called, the form will not be submitted.
|
|
@@ -111,11 +130,7 @@ declare module '$app/forms' {
|
|
|
111
130
|
* - redirects in case of a redirect response
|
|
112
131
|
* - redirects to the nearest error page in case of an unexpected error
|
|
113
132
|
*/
|
|
114
|
-
submit?:
|
|
115
|
-
data: FormData;
|
|
116
|
-
form: HTMLFormElement;
|
|
117
|
-
cancel: () => void;
|
|
118
|
-
}) => void | ((result: ActionResult<Success, Invalid>) => void)
|
|
133
|
+
submit?: SubmitFunction<Element, Success, Invalid>
|
|
119
134
|
): { destroy: () => void };
|
|
120
135
|
|
|
121
136
|
/**
|