@sveltejs/kit 1.0.0-next.376 → 1.0.0-next.377
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/assets/server/index.js +30 -20
- package/dist/chunks/index2.js +1 -1
- package/dist/cli.js +1 -1
- package/dist/vite.js +12 -13
- package/package.json +1 -1
- package/types/index.d.ts +1 -1
- package/types/private.d.ts +1 -1
package/assets/server/index.js
CHANGED
|
@@ -145,12 +145,6 @@ function is_pojo(body) {
|
|
|
145
145
|
return true;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
/** @param {import('types').RequestEvent} event */
|
|
149
|
-
function normalize_request_method(event) {
|
|
150
|
-
const method = event.request.method.toLowerCase();
|
|
151
|
-
return method === 'delete' ? 'del' : method; // 'delete' is a reserved word
|
|
152
|
-
}
|
|
153
|
-
|
|
154
148
|
/**
|
|
155
149
|
* Serialize an error into a JSON string, by copying its `name`, `message`
|
|
156
150
|
* and (in dev) `stack`, plus any custom properties, plus recursively
|
|
@@ -192,6 +186,19 @@ function clone_error(error, get_stack) {
|
|
|
192
186
|
return object;
|
|
193
187
|
}
|
|
194
188
|
|
|
189
|
+
// TODO: Remove for 1.0
|
|
190
|
+
/** @param {Record<string, any>} mod */
|
|
191
|
+
function check_method_names(mod) {
|
|
192
|
+
['get', 'post', 'put', 'patch', 'del'].forEach((m) => {
|
|
193
|
+
if (m in mod) {
|
|
194
|
+
const replacement = m === 'del' ? 'DELETE' : m.toUpperCase();
|
|
195
|
+
throw Error(
|
|
196
|
+
`Endpoint method "${m}" has changed to "${replacement}". See https://github.com/sveltejs/kit/discussions/5359 for more information.`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
195
202
|
/** @type {import('types').SSRErrorPage} */
|
|
196
203
|
const GENERIC_ERROR = {
|
|
197
204
|
id: '__error'
|
|
@@ -238,24 +245,25 @@ function is_text(content_type) {
|
|
|
238
245
|
* @returns {Promise<Response>}
|
|
239
246
|
*/
|
|
240
247
|
async function render_endpoint(event, mod, options) {
|
|
241
|
-
const method =
|
|
248
|
+
const { method } = event.request;
|
|
249
|
+
|
|
250
|
+
check_method_names(mod);
|
|
242
251
|
|
|
243
252
|
/** @type {import('types').RequestHandler} */
|
|
244
253
|
let handler = mod[method];
|
|
245
254
|
|
|
246
|
-
if (!handler && method === '
|
|
247
|
-
handler = mod.
|
|
255
|
+
if (!handler && method === 'HEAD') {
|
|
256
|
+
handler = mod.GET;
|
|
248
257
|
}
|
|
249
258
|
|
|
250
259
|
if (!handler) {
|
|
251
260
|
const allowed = [];
|
|
252
261
|
|
|
253
|
-
for (const method in ['
|
|
254
|
-
if (mod[method]) allowed.push(method
|
|
262
|
+
for (const method in ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) {
|
|
263
|
+
if (mod[method]) allowed.push(method);
|
|
255
264
|
}
|
|
256
265
|
|
|
257
|
-
if (mod.
|
|
258
|
-
if (mod.get || mod.head) allowed.push('HEAD');
|
|
266
|
+
if (mod.GET || mod.HEAD) allowed.push('HEAD');
|
|
259
267
|
|
|
260
268
|
return event.request.headers.get('x-sveltekit-load')
|
|
261
269
|
? // TODO would be nice to avoid these requests altogether,
|
|
@@ -263,7 +271,7 @@ async function render_endpoint(event, mod, options) {
|
|
|
263
271
|
new Response(undefined, {
|
|
264
272
|
status: 204
|
|
265
273
|
})
|
|
266
|
-
: new Response(`${
|
|
274
|
+
: new Response(`${method} method not allowed`, {
|
|
267
275
|
status: 405,
|
|
268
276
|
headers: {
|
|
269
277
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
|
|
@@ -327,7 +335,7 @@ async function render_endpoint(event, mod, options) {
|
|
|
327
335
|
}
|
|
328
336
|
|
|
329
337
|
return new Response(
|
|
330
|
-
method !== '
|
|
338
|
+
method !== 'HEAD' && !bodyless_status_codes.has(status) ? normalized_body : undefined,
|
|
331
339
|
{
|
|
332
340
|
status,
|
|
333
341
|
headers
|
|
@@ -2620,13 +2628,15 @@ async function load_shadow_data(route, event, options, prerender) {
|
|
|
2620
2628
|
try {
|
|
2621
2629
|
const mod = await route.shadow();
|
|
2622
2630
|
|
|
2623
|
-
|
|
2631
|
+
check_method_names(mod);
|
|
2632
|
+
|
|
2633
|
+
if (prerender && (mod.POST || mod.PUT || mod.DELETE || mod.PATCH)) {
|
|
2624
2634
|
throw new Error('Cannot prerender pages that have endpoints with mutative methods');
|
|
2625
2635
|
}
|
|
2626
2636
|
|
|
2627
|
-
const method =
|
|
2628
|
-
const is_get = method === '
|
|
2629
|
-
const handler = method === '
|
|
2637
|
+
const { method } = event.request;
|
|
2638
|
+
const is_get = method === 'HEAD' || method === 'GET';
|
|
2639
|
+
const handler = method === 'HEAD' ? mod.HEAD || mod.GET : mod[method];
|
|
2630
2640
|
|
|
2631
2641
|
if (!handler && !is_get) {
|
|
2632
2642
|
return {
|
|
@@ -2673,7 +2683,7 @@ async function load_shadow_data(route, event, options, prerender) {
|
|
|
2673
2683
|
data.body = body;
|
|
2674
2684
|
}
|
|
2675
2685
|
|
|
2676
|
-
const get = (method === '
|
|
2686
|
+
const get = (method === 'HEAD' && mod.HEAD) || mod.GET;
|
|
2677
2687
|
if (get) {
|
|
2678
2688
|
const { status, headers, body } = validate_shadow_output(await get(event));
|
|
2679
2689
|
add_cookies(/** @type {string[]} */ (data.cookies), headers);
|
package/dist/chunks/index2.js
CHANGED
|
@@ -83,7 +83,7 @@ function create_builder({ config, build_data, prerendered, log }) {
|
|
|
83
83
|
content: segment
|
|
84
84
|
})),
|
|
85
85
|
pattern: route.pattern,
|
|
86
|
-
methods: route.type === 'page' ? ['
|
|
86
|
+
methods: route.type === 'page' ? ['GET'] : build_data.server.methods[route.file]
|
|
87
87
|
}));
|
|
88
88
|
|
|
89
89
|
const seen = new Set();
|
package/dist/cli.js
CHANGED
package/dist/vite.js
CHANGED
|
@@ -315,6 +315,17 @@ function remove_svelte_kit(config) {
|
|
|
315
315
|
.filter((plugin) => plugin.name !== 'vite-plugin-svelte-kit');
|
|
316
316
|
}
|
|
317
317
|
|
|
318
|
+
const method_names = new Set(['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']);
|
|
319
|
+
|
|
320
|
+
// If we'd written this in TypeScript, it could be easy...
|
|
321
|
+
/**
|
|
322
|
+
* @param {string} str
|
|
323
|
+
* @returns {str is import('types').HttpMethod}
|
|
324
|
+
*/
|
|
325
|
+
function is_http_method(str) {
|
|
326
|
+
return method_names.has(str);
|
|
327
|
+
}
|
|
328
|
+
|
|
318
329
|
/**
|
|
319
330
|
* @param {{
|
|
320
331
|
* hooks: string;
|
|
@@ -563,16 +574,6 @@ async function build_server(options, client) {
|
|
|
563
574
|
};
|
|
564
575
|
}
|
|
565
576
|
|
|
566
|
-
/** @type {Record<string, string>} */
|
|
567
|
-
const method_names = {
|
|
568
|
-
get: 'get',
|
|
569
|
-
head: 'head',
|
|
570
|
-
post: 'post',
|
|
571
|
-
put: 'put',
|
|
572
|
-
del: 'delete',
|
|
573
|
-
patch: 'patch'
|
|
574
|
-
};
|
|
575
|
-
|
|
576
577
|
/**
|
|
577
578
|
* @param {string} cwd
|
|
578
579
|
* @param {import('rollup').OutputChunk[]} output
|
|
@@ -593,9 +594,7 @@ function get_methods(cwd, output, manifest_data) {
|
|
|
593
594
|
const file = route.type === 'endpoint' ? route.file : route.shadow;
|
|
594
595
|
|
|
595
596
|
if (file && lookup[file]) {
|
|
596
|
-
methods[file] = lookup[file]
|
|
597
|
-
.map((x) => /** @type {import('types').HttpMethod} */ (method_names[x]))
|
|
598
|
-
.filter(Boolean);
|
|
597
|
+
methods[file] = lookup[file].filter(is_http_method);
|
|
599
598
|
}
|
|
600
599
|
});
|
|
601
600
|
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -244,7 +244,7 @@ export interface RequestEvent<Params extends Record<string, string> = Record<str
|
|
|
244
244
|
}
|
|
245
245
|
|
|
246
246
|
/**
|
|
247
|
-
* A `(event: RequestEvent) => RequestHandlerOutput` function exported from an endpoint that corresponds to an HTTP verb (`
|
|
247
|
+
* A `(event: RequestEvent) => RequestHandlerOutput` function exported from an endpoint that corresponds to an HTTP verb (`GET`, `PUT`, `PATCH`, etc) and handles requests with that method.
|
|
248
248
|
*
|
|
249
249
|
* It receives `Params` as the first generic argument, which you can skip by using [generated types](/docs/types#generated-types) instead.
|
|
250
250
|
*
|
package/types/private.d.ts
CHANGED
|
@@ -142,7 +142,7 @@ export interface CspDirectives {
|
|
|
142
142
|
>;
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
export type HttpMethod = '
|
|
145
|
+
export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
146
146
|
|
|
147
147
|
export interface JSONObject {
|
|
148
148
|
[key: string]: JSONValue;
|