Skip to content

[fix] 1714 rest params in endpoint #2955

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rotten-berries-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow rest parameters in endpoints (issue 1714)
5 changes: 4 additions & 1 deletion packages/kit/src/core/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
throw new Error(`Invalid route ${file} — brackets are unbalanced`);
}

if (/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) {
if (
/.+\[\.\.\.[^\]]+\]/.test(segment) ||
(/\[\.\.\.[^\]]+\].+/.test(segment) && !/\[\.\.\.[^\]]+\]\.json$/.test(segment))
Copy link
Member

Choose a reason for hiding this comment

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

I doubt we want a hard-coded check for [...rest].json.js. .json is a convention and it can be anything else by the user. Also, have you confirm with this change that a SvelteKit app still routes normally? I have a hunch there are other places to fix as well.

With that said, I'm not sure what's the path forward, or maybe we should stick with the issue workaround for now. I'll see if the others has an opinion on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be good to make it more flexible in the future.

But there are strong reasons to implement that fix:

  • .json is the framework convention for endpoints
  • .json is used in the example project
  • because of that we would create unnecessary friction for new users coming to the framework

) {
throw new Error(`Invalid route ${file} — rest parameter must be a standalone segment`);
}

Expand Down
7 changes: 7 additions & 0 deletions packages/kit/src/core/create_manifest_data/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ test('disallows rest parameters inside segments', () => {
);
});

test('allows rest parameters inside endpoints', () => {
const { routes } = create('samples/invalid-rest-endpoint-exclusion');
assert.equal(routes.map((r) => r.type === 'endpoint' && r.file).filter(Boolean), [
'samples/invalid-rest-endpoint-exclusion/[...rest].json.js'
]);
});

test('ignores files and directories with leading underscores', () => {
const { routes } = create('samples/hidden-underscore');

Expand Down