Skip to content

Commit

Permalink
simplify ENDPOINT_METHODS and PAGE_METHODS stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 13, 2023
1 parent fc8a03d commit 2145088
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
12 changes: 2 additions & 10 deletions packages/kit/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ export const SVELTE_KIT_ASSETS = '/_svelte_kit_assets';

export const GENERATED_COMMENT = '// this file is generated — do not edit it\n';

export const ENDPOINT_METHODS = new Set([
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS',
'HEAD'
]);
export const ENDPOINT_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'];

export const PAGE_METHODS = new Set(['GET', 'POST', 'HEAD']);
export const PAGE_METHODS = ['GET', 'POST', 'HEAD'];
15 changes: 7 additions & 8 deletions packages/kit/src/core/postbuild/analyse.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ async function analyse({ manifest_path, env }) {
prerender = mod.prerender;
}

Object.keys(mod).forEach((key) => {
const method = /** @type {import('types').HttpMethod} */ (key);
if (mod[method] && ENDPOINT_METHODS.has(method)) {
api_methods.push(method);
} else if (mod.fallback && key === 'fallback') {
api_methods.push('*');
}
});
for (const method of /** @type {import('types').HttpMethod[]} */ (ENDPOINT_METHODS)) {
if (mod[method]) api_methods.push(method);
}

if (mod.fallback) {
api_methods.push('*');
}

config = mod.config;
entries = mod.entries;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function is_endpoint_request(event) {
const { method, headers } = event.request;

// These methods exist exclusively for endpoints
if (ENDPOINT_METHODS.has(method) && !PAGE_METHODS.has(method)) {
if (ENDPOINT_METHODS.includes(method) && !PAGE_METHODS.includes(method)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function method_not_allowed(mod, method) {

/** @param {Partial<Record<import('types').HttpMethod, any>>} mod */
export function allowed_methods(mod) {
const allowed = Array.from(ENDPOINT_METHODS).filter((method) => method in mod);
const allowed = ENDPOINT_METHODS.filter((method) => method in mod);

if ('GET' in mod || 'HEAD' in mod) allowed.push('HEAD');

Expand Down

0 comments on commit 2145088

Please sign in to comment.