-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Param validators #4334
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
Param validators #4334
Changes from all commits
5799667
1e9e2ca
1db07a4
5fa067b
ca3d714
705721b
7a56ac2
29ea702
2a480cf
4ddcc55
e5245e8
273db5f
7424b04
098f0e6
eb77dfd
e79f4f2
aa415d9
ea24c9f
63b7f4a
fc80f31
07accf9
504ca26
ccc75e3
6d6c595
c6ccdf3
bbb1f80
ce6efd2
714f78c
5ab21cd
72fcf94
a01d452
d6f12c5
31135cc
316cfca
c650933
97c70ae
b45b3d0
3555883
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| Add param validators |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| [breaking] disallow \$ character in dynamic parameters |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,12 @@ const config = { | |
|
|
||
| prerender: { | ||
| default: true | ||
| }, | ||
|
|
||
| vite: { | ||
| build: { | ||
| minify: false | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { s } from '../../utils/misc.js'; | ||
| import { parse_route_key } from '../../utils/routing.js'; | ||
| import { get_mime_lookup } from '../utils.js'; | ||
|
|
||
| /** | ||
|
|
@@ -41,10 +42,13 @@ export function generate_manifest({ build_data, relative_path, routes, format = | |
| }); | ||
|
|
||
| /** @type {(path: string) => string} */ | ||
| const importer = | ||
| const load = | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally liked There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is quite far away from anything touching |
||
| format === 'esm' | ||
| ? (path) => `() => import('${path}')` | ||
| : (path) => `() => Promise.resolve().then(() => require('${path}'))`; | ||
| ? (path) => `import('${path}')` | ||
| : (path) => `Promise.resolve().then(() => require('${path}'))`; | ||
|
|
||
| /** @type {(path: string) => string} */ | ||
| const loader = (path) => `() => ${load(path)}`; | ||
|
|
||
| const assets = build_data.manifest_data.assets.map((asset) => asset.file); | ||
| if (build_data.service_worker) { | ||
|
|
@@ -54,6 +58,8 @@ export function generate_manifest({ build_data, relative_path, routes, format = | |
| /** @param {string} id */ | ||
| const get_index = (id) => id && /** @type {LookupEntry} */ (bundled_nodes.get(id)).index; | ||
|
|
||
| const validators = new Set(); | ||
|
|
||
| // prettier-ignore | ||
| return `{ | ||
| appDir: ${s(build_data.app_dir)}, | ||
|
|
@@ -62,18 +68,25 @@ export function generate_manifest({ build_data, relative_path, routes, format = | |
| _: { | ||
| entry: ${s(build_data.client.entry)}, | ||
| nodes: [ | ||
| ${Array.from(bundled_nodes.values()).map(node => importer(node.path)).join(',\n\t\t\t\t')} | ||
| ${Array.from(bundled_nodes.values()).map(node => loader(node.path)).join(',\n\t\t\t\t')} | ||
| ], | ||
| routes: [ | ||
| ${routes.map(route => { | ||
| const { pattern, names, types } = parse_route_key(route.key); | ||
|
|
||
| types.forEach(type => { | ||
| if (type) validators.add(type); | ||
| }); | ||
|
|
||
| if (route.type === 'page') { | ||
| return `{ | ||
| type: 'page', | ||
| key: ${s(route.key)}, | ||
| pattern: ${route.pattern}, | ||
| params: ${get_params(route.params)}, | ||
| pattern: ${pattern}, | ||
| names: ${s(names)}, | ||
| types: ${s(types)}, | ||
| path: ${route.path ? s(route.path) : null}, | ||
| shadow: ${route.shadow ? importer(`${relative_path}/${build_data.server.vite_manifest[route.shadow].file}`) : null}, | ||
| shadow: ${route.shadow ? loader(`${relative_path}/${build_data.server.vite_manifest[route.shadow].file}`) : null}, | ||
| a: ${s(route.a.map(get_index))}, | ||
| b: ${s(route.b.map(get_index))} | ||
| }`.replace(/^\t\t/gm, ''); | ||
|
|
@@ -86,31 +99,18 @@ export function generate_manifest({ build_data, relative_path, routes, format = | |
|
|
||
| return `{ | ||
| type: 'endpoint', | ||
| pattern: ${route.pattern}, | ||
| params: ${get_params(route.params)}, | ||
| load: ${importer(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)} | ||
| pattern: ${pattern}, | ||
| names: ${s(names)}, | ||
| types: ${s(types)}, | ||
| load: ${loader(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)} | ||
| }`.replace(/^\t\t/gm, ''); | ||
| } | ||
| }).filter(Boolean).join(',\n\t\t\t\t')} | ||
| ] | ||
| ], | ||
| validators: async () => { | ||
| ${Array.from(validators).map(type => `const { validate: ${type} } = await ${load(`${relative_path}/entries/validators/${type}.js`)}`).join('\n\t\t\t\t')} | ||
| return { ${Array.from(validators).join(', ')} }; | ||
| } | ||
| } | ||
| }`.replace(/^\t/gm, ''); | ||
| } | ||
|
|
||
| /** @param {string[]} array */ | ||
| function get_params(array) { | ||
| // given an array of params like `['x', 'y', 'z']` for | ||
| // src/routes/[x]/[y]/[z]/svelte, create a function | ||
| // that turns a RexExpMatchArray into ({ x, y, z }) | ||
| return array.length | ||
| ? '(m) => ({ ' + | ||
| array | ||
| .map((param, i) => { | ||
| return param.startsWith('...') | ||
| ? `${param.slice(3)}: m[${i + 1}] || ''` | ||
| : `${param}: m[${i + 1}]`; | ||
| }) | ||
| .join(', ') + | ||
| '})' | ||
| : 'null'; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.