Skip to content

Commit 7d465db

Browse files
authored
feat: expose encoding helpers with _ prefix (#2910)
* feat: expose encoding helpers with _ prefix Export _encodePath, _encodeParam, _encodeQueryKey, _encodeQueryValue, _encodeHash and _decode from the main entry so integrations like Nuxt can reuse them. Remove the duplicated unplugin encoding module. * test(e2e): define build globals for HMR playground * feat(experimental): expose encoding helpers with _ prefix * test: nah
1 parent 9bd2299 commit 7d465db

8 files changed

Lines changed: 66 additions & 167 deletions

File tree

‎packages/router/__tests__/encoding.spec.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
2+
decode,
23
encodeHash,
34
encodeParam,
5+
encodePath,
46
encodeQueryKey,
57
encodeQueryValue,
6-
// decode,
78
} from '../src/encoding'
89
import { describe, expect, it } from 'vitest'
10+
import { mockWarn } from './vitest-mock-warn'
911

1012
describe('Encoding', () => {
1113
// all ascii chars with a non ascii char at the beginning
@@ -144,4 +146,31 @@ describe('Encoding', () => {
144146
expect(encodeHash(toEncode)).toBe(encodedToEncode)
145147
})
146148
})
149+
150+
describe('path', () => {
151+
it('encodes # ? and % but keeps /', () => {
152+
expect(encodePath('/a#b?c/100%')).toBe('/a%23b%3Fc/100%25')
153+
})
154+
155+
it('handles nullish values and numbers', () => {
156+
expect(encodePath(null)).toBe('')
157+
expect(encodePath(undefined)).toBe('')
158+
expect(encodePath(0)).toBe('0')
159+
})
160+
})
161+
162+
describe('decode', () => {
163+
mockWarn()
164+
165+
it('decodes percent-encoded text', () => {
166+
expect(decode('caf%C3%A9%2F')).toBe('café/')
167+
expect(decode(1)).toBe('1')
168+
expect(decode(null)).toBe(null)
169+
})
170+
171+
it('returns the original text and warns on malformed input', () => {
172+
expect(decode('%E0%A4%A')).toBe('%E0%A4%A')
173+
expect('Error decoding "%E0%A4%A"').toHaveBeenWarned()
174+
})
175+
})
147176
})

‎packages/router/e2e/unplugin/hmr/fixtures/vite-server.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import path from 'node:path'
55
import fs from 'node:fs'
66
import { fileURLToPath } from 'node:url'
77

8+
// The playground config loads the unplugin from source, which imports runtime
9+
// modules that expect the build-time globals (see tsdown.config.ts).
10+
Object.assign(globalThis, { __DEV__: true, __BROWSER__: false })
11+
812
type ViteFixtures = {
913
devServer: ViteDevServer
1014
baseURL: string

‎packages/router/src/encoding.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export function commonEncode(text: string | number | null | undefined): string {
7878
*
7979
* @param text - string to encode
8080
* @returns encoded string
81+
*
82+
* @internal Exposed for integrations (e.g. Nuxt). Not covered by semver.
8183
*/
8284
export function encodeHash(text: string): string {
8385
return commonEncode(text)
@@ -92,6 +94,8 @@ export function encodeHash(text: string): string {
9294
*
9395
* @param text - string to encode
9496
* @returns encoded string
97+
*
98+
* @internal Exposed for integrations (e.g. Nuxt). Not covered by semver.
9599
*/
96100
export function encodeQueryValue(text: string | number): string {
97101
return (
@@ -112,6 +116,8 @@ export function encodeQueryValue(text: string | number): string {
112116
* Like `encodeQueryValue` but also encodes the `=` character.
113117
*
114118
* @param text - string to encode
119+
*
120+
* @internal Exposed for integrations (e.g. Nuxt). Not covered by semver.
115121
*/
116122
export function encodeQueryKey(text: string | number): string {
117123
return encodeQueryValue(text).replace(EQUAL_RE, '%3D')
@@ -122,6 +128,8 @@ export function encodeQueryKey(text: string | number): string {
122128
*
123129
* @param text - string to encode
124130
* @returns encoded string
131+
*
132+
* @internal Exposed for integrations (e.g. Nuxt). Not covered by semver.
125133
*/
126134
export function encodePath(text: string | number | null | undefined): string {
127135
return commonEncode(text).replace(HASH_RE, '%23').replace(IM_RE, '%3F')
@@ -135,6 +143,8 @@ export function encodePath(text: string | number | null | undefined): string {
135143
*
136144
* @param text - string to encode
137145
* @returns encoded string
146+
*
147+
* @internal Exposed for integrations (e.g. Nuxt). Not covered by semver.
138148
*/
139149
export function encodeParam(text: string | number | null | undefined): string {
140150
return encodePath(text).replace(SLASH_RE, '%2F')
@@ -146,6 +156,8 @@ export function encodeParam(text: string | number | null | undefined): string {
146156
*
147157
* @param text - string to decode
148158
* @returns decoded string
159+
*
160+
* @internal Exposed for integrations (e.g. Nuxt). Not covered by semver.
149161
*/
150162
export function decode(text: string | number): string
151163
export function decode(text: null | undefined): null

‎packages/router/src/experimental/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ export {
166166
type ExtractParamParserType as _ExtractParamParserType,
167167
} from './route-resolver/matchers/param-parsers'
168168
export { MatchMiss as _MatchMiss } from './route-resolver/matchers/errors'
169+
// no semver guarantee, used by integrations like Nuxt
170+
export {
171+
encodePath as _encodePath,
172+
encodeParam as _encodeParam,
173+
encodeQueryKey as _encodeQueryKey,
174+
encodeQueryValue as _encodeQueryValue,
175+
encodeHash as _encodeHash,
176+
decode as _decode,
177+
} from './encoding'
169178
export {
170179
mergeRouteRecord as _mergeRouteRecord,
171180
mergeRouteRecordViews as _mergeRouteRecordViews,

‎packages/router/src/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ export { createRouterMatcher } from './matcher'
1111
export type { RouterMatcher } from './matcher'
1212

1313
export { parseQuery, stringifyQuery } from './query'
14+
// internal: no semver guarantee, used by integrations like Nuxt
15+
export {
16+
encodePath as _encodePath,
17+
encodeParam as _encodeParam,
18+
encodeQueryKey as _encodeQueryKey,
19+
encodeQueryValue as _encodeQueryValue,
20+
encodeHash as _encodeHash,
21+
decode as _decode,
22+
} from './encoding'
1423
export type {
1524
LocationQuery,
1625
LocationQueryRaw,

‎packages/router/src/unplugin/core/treeNodeValue.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
} from './customBlock'
55
import { joinPath, mergeRouteRecordOverride } from './utils'
66
import { diagnostics } from '../diagnostics'
7-
import { encodePath } from '../utils/encoding'
7+
import { encodePath } from '../../encoding'
88
import { isAbsolutePath } from '../utils'
99

1010
export const enum TreeNodeType {

‎packages/router/src/unplugin/diagnostics.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,7 @@ export const diagnostics = /*#__PURE__*/ defineDiagnostics({
150150
fix: 'Use one of the supported languages: json5, json, yaml, yml.',
151151
},
152152

153-
// --- utils/encoding.ts ---
154-
VUE_ROUTER_B0016: {
155-
why: (p: { text: string }) =>
156-
`Error decoding "${p.text}". Using original value`,
157-
fix: 'Ensure the value is correctly percent-encoded.',
158-
},
153+
// VUE_ROUTER_B0016: retired (removed duplicated encoding.ts), do not reuse
159154

160155
// --- codegen/generateRouteParams.ts ---
161156
VUE_ROUTER_B0017: {

‎packages/router/src/unplugin/utils/encoding.ts‎

Lines changed: 0 additions & 159 deletions
This file was deleted.

0 commit comments

Comments
 (0)