Skip to content

Commit 6153263

Browse files
authored
fix: optional params was wrapped inside quotes (#138)
close #137
1 parent 4abc3fe commit 6153263

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/build.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,18 @@ function parse(routes: RouteManifestEntry[]) {
116116
.split('/')
117117
.filter((seg) => seg.startsWith(':') || seg == '*')
118118
.map((param) => param.split('.')[0])
119-
.map((param) => param.replace(':', '')),
119+
.map((param) => {
120+
let keyable = param.replace(':', '');
121+
const isOptional = keyable.match(/\?$/);
122+
if (isOptional) {
123+
keyable = keyable.replace(/\?$/, '');
124+
}
125+
keyable = `'${keyable}'`
126+
if (isOptional) {
127+
keyable = `${keyable}?`
128+
}
129+
return keyable;
130+
})
120131
)
121132
);
122133
});

src/template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function exportedQuery(ctx: Context) {
2323
function routes(ctx: Context) {
2424
const routes = ctx.routes.map(({ route, params, fileName }) =>
2525
`"${route}": {
26-
params: ${params.length > 0 ? `{${params.map(param => `'${param}': string | number`).join('; ')}}` : 'never'},
26+
params: ${params.length > 0 ? `{${params.map(param => `${param}: string | number`).join('; ')}}` : 'never'},
2727
query: ExportedQuery<import('${ctx.relativeAppDirPath}/${fileName}').SearchParams>,
2828
}`
2929
);

tests/__snapshots__/build.test.ts.snap

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ exports[`gen route types 1`] = `
1414
query: ExportedQuery<import('../app/root').SearchParams>,
1515
},
1616
"/:lang?/about": {
17-
params: {'lang?': string | number},
17+
params: {'lang'?: string | number},
1818
query: ExportedQuery<import('../app/routes/($lang).about').SearchParams>,
1919
},
20+
"/:provider-key?/about": {
21+
params: {'provider-key'?: string | number},
22+
query: ExportedQuery<import('../app/routes/($provider-key).about').SearchParams>,
23+
},
2024
"/admin": {
2125
params: never,
2226
query: ExportedQuery<import('../app/routes/admin._index').SearchParams>,
@@ -166,6 +170,7 @@ exports[`gen route types 1`] = `
166170
| 'routes/blog'
167171
| 'routes/blog._index'
168172
| 'routes/auth.$provider-key'
173+
| 'routes/($provider-key).about'
169174
| 'catchall';
170175
171176
export function $path<

tests/fixture.ts

+6
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ export const testRoutes =
190190
path: "auth/:provider-key",
191191
parentId: "root",
192192
},
193+
"routes/($provider-key).about": {
194+
file: "routes/($provider-key).about.tsx",
195+
id: "routes/($provider-key).about",
196+
path: ":provider-key?/about",
197+
parentId: "root",
198+
},
193199
catchall: {
194200
path: "/somewhere/cool/*",
195201
index: undefined,

0 commit comments

Comments
 (0)