@sveltejs/kit 1.2.4 → 1.2.5

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.2.4",
3
+ "version": "1.2.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -24,9 +24,6 @@ export async function handle_action_json_request(event, options, server) {
24
24
  const actions = server?.actions;
25
25
 
26
26
  if (!actions) {
27
- if (server) {
28
- maybe_throw_migration_error(server);
29
- }
30
27
  // TODO should this be a different error altogether?
31
28
  const no_actions_error = error(405, 'POST method not allowed. No actions exist for this page');
32
29
  return action_json(
@@ -50,6 +47,10 @@ export async function handle_action_json_request(event, options, server) {
50
47
  try {
51
48
  const data = await call_action(event, actions);
52
49
 
50
+ if (__SVELTEKIT_DEV__) {
51
+ validate_action_return(data);
52
+ }
53
+
53
54
  if (data instanceof ActionFailure) {
54
55
  return action_json({
55
56
  type: 'failure',
@@ -124,7 +125,6 @@ export async function handle_action_request(event, server) {
124
125
  const actions = server.actions;
125
126
 
126
127
  if (!actions) {
127
- maybe_throw_migration_error(server);
128
128
  // TODO should this be a different error altogether?
129
129
  event.setHeaders({
130
130
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
@@ -142,13 +142,22 @@ export async function handle_action_request(event, server) {
142
142
  try {
143
143
  const data = await call_action(event, actions);
144
144
 
145
+ if (__SVELTEKIT_DEV__) {
146
+ validate_action_return(data);
147
+ }
148
+
145
149
  if (data instanceof ActionFailure) {
146
- return { type: 'failure', status: data.status, data: data.data };
150
+ return {
151
+ type: 'failure',
152
+ status: data.status,
153
+ data: data.data
154
+ };
147
155
  } else {
148
156
  return {
149
157
  type: 'success',
150
158
  status: 200,
151
- data: /** @type {Record<string, any> | undefined} */ (data)
159
+ // @ts-expect-error this will be removed upon serialization, so `undefined` is the same as omission
160
+ data
152
161
  };
153
162
  }
154
163
  } catch (e) {
@@ -185,7 +194,7 @@ function check_named_default_separate(actions) {
185
194
  * @param {NonNullable<import('types').SSRNode['server']['actions']>} actions
186
195
  * @throws {Redirect | ActionFailure | HttpError | Error}
187
196
  */
188
- export async function call_action(event, actions) {
197
+ async function call_action(event, actions) {
189
198
  const url = new URL(event.request.url);
190
199
 
191
200
  let name = 'default';
@@ -213,16 +222,16 @@ export async function call_action(event, actions) {
213
222
  return action(event);
214
223
  }
215
224
 
216
- /**
217
- * @param {import('types').SSRNode['server']} server
218
- */
219
- function maybe_throw_migration_error(server) {
220
- for (const method of ['POST', 'PUT', 'PATCH', 'DELETE']) {
221
- if (/** @type {any} */ (server)[method]) {
222
- throw new Error(
223
- `${method} method no longer allowed in +page.server, use actions instead. See the PR for more info: https://github.com/sveltejs/kit/pull/6469`
224
- );
225
- }
225
+ /** @param {any} data */
226
+ function validate_action_return(data) {
227
+ if (data instanceof Redirect) {
228
+ throw new Error(`Cannot \`return redirect(...)\` — use \`throw redirect(...)\` instead`);
229
+ }
230
+
231
+ if (data instanceof HttpError) {
232
+ throw new Error(
233
+ `Cannot \`return error(...)\` — use \`throw error(...)\` or \`return fail(...)\` instead`
234
+ );
226
235
  }
227
236
  }
228
237