Skip to content

Commit

Permalink
fix: optional params was wrapped inside quotes (#138)
Browse files Browse the repository at this point in the history
close #137
  • Loading branch information
yesmeck authored Feb 27, 2025
1 parent 4abc3fe commit 6153263
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,18 @@ function parse(routes: RouteManifestEntry[]) {
.split('/')
.filter((seg) => seg.startsWith(':') || seg == '*')
.map((param) => param.split('.')[0])
.map((param) => param.replace(':', '')),
.map((param) => {
let keyable = param.replace(':', '');
const isOptional = keyable.match(/\?$/);
if (isOptional) {
keyable = keyable.replace(/\?$/, '');
}
keyable = `'${keyable}'`
if (isOptional) {
keyable = `${keyable}?`
}
return keyable;
})
)
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function exportedQuery(ctx: Context) {
function routes(ctx: Context) {
const routes = ctx.routes.map(({ route, params, fileName }) =>
`"${route}": {
params: ${params.length > 0 ? `{${params.map(param => `'${param}': string | number`).join('; ')}}` : 'never'},
params: ${params.length > 0 ? `{${params.map(param => `${param}: string | number`).join('; ')}}` : 'never'},
query: ExportedQuery<import('${ctx.relativeAppDirPath}/${fileName}').SearchParams>,
}`
);
Expand Down
7 changes: 6 additions & 1 deletion tests/__snapshots__/build.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ exports[`gen route types 1`] = `
query: ExportedQuery<import('../app/root').SearchParams>,
},
"/:lang?/about": {
params: {'lang?': string | number},
params: {'lang'?: string | number},
query: ExportedQuery<import('../app/routes/($lang).about').SearchParams>,
},
"/:provider-key?/about": {
params: {'provider-key'?: string | number},
query: ExportedQuery<import('../app/routes/($provider-key).about').SearchParams>,
},
"/admin": {
params: never,
query: ExportedQuery<import('../app/routes/admin._index').SearchParams>,
Expand Down Expand Up @@ -166,6 +170,7 @@ exports[`gen route types 1`] = `
| 'routes/blog'
| 'routes/blog._index'
| 'routes/auth.$provider-key'
| 'routes/($provider-key).about'
| 'catchall';
export function $path<
Expand Down
6 changes: 6 additions & 0 deletions tests/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ export const testRoutes =
path: "auth/:provider-key",
parentId: "root",
},
"routes/($provider-key).about": {
file: "routes/($provider-key).about.tsx",
id: "routes/($provider-key).about",
path: ":provider-key?/about",
parentId: "root",
},
catchall: {
path: "/somewhere/cool/*",
index: undefined,
Expand Down

0 comments on commit 6153263

Please sign in to comment.