66 * @returns Route object with name and params
77 */
88export function getPackageRoute ( pkg : string , version : string | null = null ) {
9- const [ org , name ] = pkg . startsWith ( '@' ) ? pkg . split ( '/' ) : [ null , pkg ]
10- if ( version ) {
11- return {
12- name : 'package-version' ,
13- params : { org, name, version } ,
14- } as const
15- }
16-
179 return {
1810 name : 'package' ,
1911 params : {
20- org,
21- name,
12+ package : [ ...pkg . split ( '/' ) , version ? 'v' : null , version ] . filter (
13+ ( a ) : a is NonNullable < typeof a > => ! ! a ,
14+ ) ,
2215 } ,
2316 } as const
2417}
@@ -36,17 +29,39 @@ export function getPackageRoute(pkg: string, version: string | null = null) {
3629 * @public
3730 */
3831export function usePackageRoute ( ) {
39- const route = useRoute ( 'package-version' )
32+ const route = useRoute ( 'package' )
33+
34+ const data = computed ( ( ) => {
35+ const segments = route . params . package || [ ]
4036
41- const orgName = computed ( ( ) => route . params . org )
42- const requestedVersion = computed ( ( ) => route . params . version || null )
43- const packageName = computed ( ( ) =>
44- orgName . value ? `${ orgName . value } /${ route . params . name } ` : route . params . name ,
45- )
37+ // Find the /v/ separator for version
38+ const vIndex = segments . indexOf ( 'v' )
39+ if ( vIndex !== - 1 && vIndex < segments . length - 1 ) {
40+ return {
41+ packageName : segments . slice ( 0 , vIndex ) . join ( '/' ) ,
42+ requestedVersion : segments . slice ( vIndex + 1 ) . join ( '/' ) ,
43+ }
44+ }
45+
46+ // Parse @ versioned package
47+ const fullPath = segments . join ( '/' )
48+ const versionMatch = fullPath . match ( / ^ ( @ [ ^ / ] + \/ [ ^ / ] + | [ ^ / ] + ) @ ( [ ^ / ] + ) $ / )
49+ if ( versionMatch ) {
50+ const [ , packageName , requestedVersion ] = versionMatch as [ string , string , string ]
51+ return {
52+ packageName,
53+ requestedVersion,
54+ }
55+ }
56+
57+ return {
58+ packageName : fullPath ,
59+ requestedVersion : null as string | null ,
60+ }
61+ } )
4662
4763 return {
48- packageName,
49- requestedVersion,
50- orgName,
64+ packageName : computed ( ( ) => data . value . packageName ) ,
65+ requestedVersion : computed ( ( ) => data . value . requestedVersion ) ,
5166 }
5267}
0 commit comments