Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: uppercase endpoint methods #5513

Conversation

elliott-with-the-longest-name-on-github
Copy link
Contributor

Proof-of-concept implementation of #5367.

get => GET
post => POST
put => PUT
patch => PATCH
del => DELETE

The server will throw an error if an endpoint module exports one of the old names. This behavior should be removed for 1.0 (and is marked with a comment like other pre-1.0 throws are).

Closes #5367.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jul 13, 2022

🦋 Changeset detected

Latest commit: 3ba4c2b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@sveltejs/kit Patch
create-svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gtm-nayan
Copy link
Contributor

The tests were more regular than I thought they would be, nice.

I found this while I was taking a quick peek the other day:

/** @type {Record<string, string>} */
const method_names = {
get: 'get',
head: 'head',
post: 'post',
put: 'put',
del: 'delete',
patch: 'patch'
};
/**
* @param {string} cwd
* @param {import('rollup').OutputChunk[]} output
* @param {import('types').ManifestData} manifest_data
*/
function get_methods(cwd, output, manifest_data) {
/** @type {Record<string, string[]>} */
const lookup = {};
output.forEach((chunk) => {
if (!chunk.facadeModuleId) return;
const id = chunk.facadeModuleId.slice(cwd.length + 1);
lookup[id] = chunk.exports;
});
/** @type {Record<string, import('types').HttpMethod[]>} */
const methods = {};
manifest_data.routes.forEach((route) => {
const file = route.type === 'endpoint' ? route.file : route.shadow;
if (file && lookup[file]) {
methods[file] = lookup[file]
.map((x) => /** @type {import('types').HttpMethod} */ (method_names[x]))
.filter(Boolean);
}
});
return methods;
}

methods: route.type === 'page' ? ['get'] : build_data.server.methods[route.file]

It's just passed to adapters so couldn't exactly tell what it's doing using Go to references and doesn't look like any of the adapters in the repo are relying on it but will likely need changing as well.

@elliott-with-the-longest-name-on-github
Copy link
Contributor Author

The tests were more regular than I thought they would be, nice.

Regex find and replace, buddy. 😉

Good catch -- I updated those two files along with the type definition (so we can be reasonably certain there are no lowercase variants remaining).

Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks great to me, i just made a couple of small suggestions

packages/kit/src/runtime/server/endpoint.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/server/utils.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/server/page/load_node.js Outdated Show resolved Hide resolved
packages/kit/src/vite/build/utils.js Outdated Show resolved Hide resolved
packages/kit/src/vite/build/build_server.js Outdated Show resolved Hide resolved
packages/kit/src/vite/build/build_server.js Outdated Show resolved Hide resolved
@@ -409,13 +409,13 @@ async function load_shadow_data(route, event, options, prerender) {
try {
const mod = await route.shadow();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: Remove for 1.0
['get', 'post', 'put', 'patch', 'del'].forEach((m) => {
if (mod[m] !== undefined) {
const replacement = m === 'del' ? 'DELETE' : m.toUpperCase();
throw Error(`Endpoint method "${m}" has changed to "${replacement}"`);
}
});

The error should be here as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yep, i'll rustle up a utility that can be used in both places

@gtm-nayan
Copy link
Contributor

For my own curiosity, @Rich-Harris what's the output of the get_methods function used for? Couldn't find anything in the repo. Also might be nice to add a doc comment.

@Rich-Harris
Copy link
Member

For my own curiosity, @Rich-Harris what's the output of the get_methods function used for?

This is... a good question. The only place it appears to be referenced is here:

/** @type {import('types').RouteDefinition[]} */
const facades = routes.map((route) => ({
id: route.id,
type: route.type,
segments: route.id.split('/').map((segment) => ({
dynamic: segment.includes('['),
rest: segment.includes('[...'),
content: segment
})),
pattern: route.pattern,
methods: route.type === 'page' ? ['get'] : build_data.server.methods[route.file]
}));

I vaguely remember there being some reason that it was useful for adapters to have this information to hand. Perhaps if you're using Architect, where you need to define routes like this:

@http
get /
get /pages
get /pages/:dateID
get /contact
post /contact
get /widgets/*

@Rich-Harris Rich-Harris merged commit 87552ef into sveltejs:master Jul 15, 2022
@Rich-Harris
Copy link
Member

alright, let's go... hopefully the positive emoji reactions on the discussion and the issue are representative of the community as a whole 🤞

@Rich-Harris Rich-Harris mentioned this pull request Jul 15, 2022
camargo added a commit to NASA-AMMOS/aerie-ui that referenced this pull request Jul 18, 2022
- Capitalize  endpoint method names from sveltejs/kit#5513
- Upgrade e2e Banananation from NASA-AMMOS/aerie#238
JosephVolosin pushed a commit to NASA-AMMOS/aerie-ui that referenced this pull request Aug 20, 2024
- Capitalize  endpoint method names from sveltejs/kit#5513
- Upgrade e2e Banananation from NASA-AMMOS/aerie#238
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uppercase handler names (export function GET() {...} etc)
3 participants