Skip to content

Commit 7a49ed8

Browse files
committed
fix: fix tests
1 parent f5d121a commit 7a49ed8

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

packages/root/src/core/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ export {RequestContext, useRequestContext} from './hooks/useRequestContext.js';
2121
export {useTranslations} from './hooks/useTranslations.js';
2222
export * from './plugin.js';
2323
export * from './types.js';
24-
export {replaceParams} from '../render/router.js';
24+
export * from '../utils/url-path-params.js';

packages/root/src/render/render.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ import {
3232
import type {ElementGraph} from '../node/element-graph.js';
3333
import {parseTagNames} from '../utils/elements.js';
3434
import {toHrefLang} from '../utils/i18n.js';
35+
import {replaceParams} from '../utils/url-path-params.js';
3536
import {AssetMap} from './asset-map/asset-map.js';
3637
import {htmlMinify} from './html-minify.js';
3738
import {htmlPretty} from './html-pretty.js';
3839
import {getFallbackLocales} from './i18n-fallbacks.js';
39-
import {normalizeUrlPath, replaceParams, Router} from './router.js';
40+
import {normalizeUrlPath, Router} from './router.js';
4041

4142
const CONTENT_TYPES: Record<string, string> = {
4243
html: 'text/html',

packages/root/src/render/router.ts

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'node:path';
22
import {RootConfig} from '../core/config.js';
33
import {Route, RouteModule} from '../core/types.js';
4+
import {replaceParams, testPathHasParams} from '../utils/url-path-params.js';
45
import {RouteTrie} from './route-trie.js';
56

67
const ROUTES_FILES = import.meta.glob<RouteModule>(
@@ -138,15 +139,9 @@ export class Router {
138139
}
139140
);
140141
}
141-
} else if (
142-
routeModule.getStaticProps &&
143-
!pathHasParams
144-
) {
142+
} else if (routeModule.getStaticProps && !pathHasParams) {
145143
urlPaths.push({urlPath: normalizeUrlPath(urlPathFormat), params: {}});
146-
} else if (
147-
!routeModule.handle &&
148-
!pathHasParams
149-
) {
144+
} else if (!routeModule.handle && !pathHasParams) {
150145
urlPaths.push({urlPath: normalizeUrlPath(urlPathFormat), params: {}});
151146
} else if (
152147
pathHasParams &&
@@ -166,23 +161,6 @@ export class Router {
166161
}
167162
}
168163

169-
export function replaceParams(
170-
urlPathFormat: string,
171-
params: Record<string, string>
172-
) {
173-
const urlPath = urlPathFormat.replaceAll(
174-
/\[\[?(\.\.\.)?([\w\-_]*)\]?\]/g,
175-
(match: string, _wildcard: string, key: string) => {
176-
const val = params[key];
177-
if (!val) {
178-
throw new Error(`unreplaced param ${match} in url: ${urlPathFormat}`);
179-
}
180-
return val;
181-
}
182-
);
183-
return urlPath;
184-
}
185-
186164
export function normalizeUrlPath(
187165
urlPath: string,
188166
options?: {trailingSlash?: boolean}
@@ -192,11 +170,9 @@ export function normalizeUrlPath(
192170
// Remove trailing slash.
193171
if (
194172
testPathHasFileExt(urlPath) ||
195-
(
196-
options?.trailingSlash === false &&
173+
(options?.trailingSlash === false &&
197174
urlPath !== '/' &&
198-
urlPath.endsWith('/')
199-
)
175+
urlPath.endsWith('/'))
200176
) {
201177
urlPath = removeTrailingSlash(urlPath);
202178
}
@@ -219,13 +195,6 @@ export function normalizeUrlPath(
219195
return urlPath;
220196
}
221197

222-
function testPathHasParams(urlPath: string) {
223-
const segments = urlPath.split('/');
224-
return segments.some((segment) => {
225-
return segment.startsWith('[') && segment.includes(']');
226-
});
227-
}
228-
229198
function testPathHasFileExt(urlPath: string) {
230199
const basename = path.basename(removeTrailingSlash(urlPath));
231200
return basename.includes('.');

packages/root/src/render/router.test.ts renamed to packages/root/src/utils/url-path-params.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {assert, test} from 'vitest';
22

3-
import {replaceParams} from './router.js';
3+
import {replaceParams} from './url-path-params.js';
44

55
test('replace params', () => {
66
assert.equal(replaceParams('/foo', {foo: 'bar'}), '/foo');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function replaceParams(
2+
urlPathFormat: string,
3+
params: Record<string, string>
4+
) {
5+
const urlPath = urlPathFormat.replaceAll(
6+
/\[\[?(\.\.\.)?([\w\-_]*)\]?\]/g,
7+
(match: string, _wildcard: string, key: string) => {
8+
const val = params[key];
9+
if (!val) {
10+
throw new Error(`unreplaced param ${match} in url: ${urlPathFormat}`);
11+
}
12+
return val;
13+
}
14+
);
15+
return urlPath;
16+
}
17+
18+
export function testPathHasParams(urlPath: string) {
19+
const segments = urlPath.split('/');
20+
return segments.some((segment) => {
21+
return segment.startsWith('[') && segment.includes(']');
22+
});
23+
}

0 commit comments

Comments
 (0)