Skip to content

Commit 090570f

Browse files
optimize and test rewrite
1 parent 254cb88 commit 090570f

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

‎packages/router-core/src/rewrite.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { joinPaths, trimPath } from './path'
22
import type { LocationRewrite } from './router'
33

4-
/** Compose multiple rewrite pairs into a single in/out rewrite. */
54
/** Compose multiple rewrite pairs into a single in/out rewrite. */
65
export function composeRewrites(rewrites: Array<LocationRewrite>) {
76
return {
@@ -20,21 +19,17 @@ export function composeRewrites(rewrites: Array<LocationRewrite>) {
2019
} satisfies LocationRewrite
2120
}
2221

23-
/** Create a rewrite pair that strips/adds a basepath on input/output. */
2422
/** Create a rewrite pair that strips/adds a basepath on input/output. */
2523
export function rewriteBasepath(opts: {
2624
basepath: string
2725
caseSensitive?: boolean
2826
}) {
2927
const trimmedBasepath = trimPath(opts.basepath)
3028
const normalizedBasepath = `/${trimmedBasepath}`
31-
const normalizedBasepathWithSlash = `${normalizedBasepath}/`
3229
const checkBasepath = opts.caseSensitive
3330
? normalizedBasepath
3431
: normalizedBasepath.toLowerCase()
35-
const checkBasepathWithSlash = opts.caseSensitive
36-
? normalizedBasepathWithSlash
37-
: normalizedBasepathWithSlash.toLowerCase()
32+
const checkBasepathWithSlash = `${checkBasepath}/`
3833

3934
return {
4035
input: ({ url }) => {
@@ -58,7 +53,6 @@ export function rewriteBasepath(opts: {
5853
} satisfies LocationRewrite
5954
}
6055

61-
/** Execute a location input rewrite if provided. */
6256
/** Execute a location input rewrite if provided. */
6357
export function executeRewriteInput(
6458
rewrite: LocationRewrite | undefined,
@@ -75,7 +69,6 @@ export function executeRewriteInput(
7569
return url
7670
}
7771

78-
/** Execute a location output rewrite if provided. */
7972
/** Execute a location output rewrite if provided. */
8073
export function executeRewriteOutput(
8174
rewrite: LocationRewrite | undefined,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, expect, test } from 'vitest'
2+
import {
3+
composeRewrites,
4+
executeRewriteInput,
5+
executeRewriteOutput,
6+
rewriteBasepath,
7+
} from '../src/rewrite'
8+
import type { LocationRewrite } from '../src'
9+
10+
describe('rewrite helpers', () => {
11+
test('basepath rewrite strips input and restores output', () => {
12+
const rewrite = rewriteBasepath({ basepath: '/app/' })
13+
const inputUrl = new URL('https://example.com/app/posts')
14+
15+
expect(executeRewriteInput(rewrite, inputUrl).pathname).toBe('/posts')
16+
17+
const outputUrl = new URL('https://example.com/posts')
18+
expect(executeRewriteOutput(rewrite, outputUrl).pathname).toBe('/app/posts')
19+
})
20+
21+
test('basepath rewrite handles root output', () => {
22+
const rewrite = rewriteBasepath({ basepath: '/app' })
23+
const url = new URL('https://example.com/')
24+
25+
expect(executeRewriteOutput(rewrite, url).pathname).toBe('/app/')
26+
})
27+
28+
test('case-insensitive basepath input preserves original suffix casing', () => {
29+
const rewrite = rewriteBasepath({ basepath: '/App' })
30+
const url = new URL('https://example.com/app/Users')
31+
32+
expect(executeRewriteInput(rewrite, url).pathname).toBe('/Users')
33+
})
34+
35+
test('composeRewrites applies input forward and output backward', () => {
36+
const calls: Array<string> = []
37+
const first: LocationRewrite = {
38+
input: ({ url }) => {
39+
calls.push('first-in')
40+
url.pathname += 'a'
41+
return url
42+
},
43+
output: ({ url }) => {
44+
calls.push('first-out')
45+
url.pathname += 'd'
46+
return url
47+
},
48+
}
49+
const second: LocationRewrite = {
50+
input: ({ url }) => {
51+
calls.push('second-in')
52+
url.pathname += 'b'
53+
return url
54+
},
55+
output: ({ url }) => {
56+
calls.push('second-out')
57+
url.pathname += 'c'
58+
return url
59+
},
60+
}
61+
62+
const rewrite = composeRewrites([first, second])
63+
64+
expect(
65+
executeRewriteInput(rewrite, new URL('https://example.com/')).pathname,
66+
).toBe('/ab')
67+
expect(
68+
executeRewriteOutput(rewrite, new URL('https://example.com/')).pathname,
69+
).toBe('/cd')
70+
expect(calls).toEqual(['first-in', 'second-in', 'second-out', 'first-out'])
71+
})
72+
})

0 commit comments

Comments
 (0)