@shuvi/router 1.0.45-pre.0 → 1.0.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,4 +3,4 @@ export interface IRouteBaseObject<Element = any> extends Omit<IRouteRecord<Eleme
3
3
  children?: IRouteBaseObject<Element>[];
4
4
  }
5
5
  export declare function rankRouteBranches<T extends [string, ...any[]]>(branches: T[]): T[];
6
- export declare function matchRoutes<T extends IRouteBaseObject>(routes: T[], location: string | PartialLocation): IRouteMatch<T>[] | null;
6
+ export declare function matchRoutes<T extends IRouteBaseObject>(routes: T[], location: string | PartialLocation, basename?: string): IRouteMatch<T>[] | null;
@@ -1,5 +1,5 @@
1
1
  import { matchPathname } from './matchPathname';
2
- import { joinPaths, resolvePath } from './utils';
2
+ import { joinPaths, normalizeBase, resolvePath, stripBase } from './utils';
3
3
  import { tokensToParser, comparePathParserScore } from './pathParserRanker';
4
4
  import { tokenizePath } from './pathTokenizer';
5
5
  function matchRouteBranch(branch, pathname) {
@@ -61,15 +61,20 @@ function flattenRoutes(routes, branches = [], parentPath = '', parentRoutes = []
61
61
  });
62
62
  return branches;
63
63
  }
64
- export function matchRoutes(routes, location) {
64
+ export function matchRoutes(routes, location, basename = '') {
65
65
  if (typeof location === 'string') {
66
66
  location = resolvePath(location);
67
67
  }
68
- let pathname = location.pathname;
69
- // If pathname is empty, it means invalid pathname and cannot match any route.
70
- // This only happens when basename is set but url is not start with basename
71
- if (!pathname) {
72
- return null;
68
+ let pathname = location.pathname || '/';
69
+ if (basename) {
70
+ const normalizedBasename = normalizeBase(basename);
71
+ const pathnameWithoutBase = stripBase(pathname, normalizedBasename);
72
+ if (pathnameWithoutBase) {
73
+ pathname = pathnameWithoutBase;
74
+ }
75
+ else {
76
+ return null;
77
+ }
73
78
  }
74
79
  let branches = flattenRoutes(routes);
75
80
  branches = rankRouteBranches(branches);
@@ -1,18 +1,19 @@
1
1
  import * as qs from 'query-string';
2
- import { PathRecord, Path } from '../types';
2
+ import { PathRecord, PartialPath, Path } from '../types';
3
3
  export declare const trimTrailingSlashes: (path: string) => string;
4
4
  export declare const normalizeSlashes: (path: string) => string;
5
5
  export declare const joinPaths: (paths: string[]) => string;
6
6
  export declare const splitPath: (path: string) => string[];
7
7
  export declare function normalizeBase(base: string): string;
8
8
  export declare function parseQuery(queryStr: string): qs.ParsedQuery<string>;
9
- export declare function pathToString({ pathname, search, hash, query }: Path, basename?: string): string;
9
+ export declare function pathToString({ pathname, search, hash, query }: PartialPath, basename?: string): string;
10
10
  /**
11
11
  * Parses a string URL path into its separate pathname, search, and hash components.
12
12
  */
13
13
  export declare function resolvePath(to: PathRecord, fromPathname?: string): Path;
14
14
  /**
15
15
  * Strips off the base from the beginning of a location.pathname in a non-case-sensitive way.
16
+ * If the base does not match at the beginning, null will be returned.
16
17
  *
17
18
  * @param pathname - location.pathname
18
19
  * @param base - base to strip off
package/esm/utils/path.js CHANGED
@@ -24,9 +24,6 @@ export function pathToString({ pathname = '/', search = '', hash = '', query = {
24
24
  }
25
25
  const pathString = pathname + search + hash;
26
26
  if (basename) {
27
- if (pathString === '/') {
28
- return basename;
29
- }
30
27
  return joinPaths([basename, pathString]);
31
28
  }
32
29
  return pathString;
@@ -98,6 +95,7 @@ export function resolvePath(to, fromPathname = '/') {
98
95
  }
99
96
  /**
100
97
  * Strips off the base from the beginning of a location.pathname in a non-case-sensitive way.
98
+ * If the base does not match at the beginning, null will be returned.
101
99
  *
102
100
  * @param pathname - location.pathname
103
101
  * @param base - base to strip off
@@ -3,4 +3,4 @@ export interface IRouteBaseObject<Element = any> extends Omit<IRouteRecord<Eleme
3
3
  children?: IRouteBaseObject<Element>[];
4
4
  }
5
5
  export declare function rankRouteBranches<T extends [string, ...any[]]>(branches: T[]): T[];
6
- export declare function matchRoutes<T extends IRouteBaseObject>(routes: T[], location: string | PartialLocation): IRouteMatch<T>[] | null;
6
+ export declare function matchRoutes<T extends IRouteBaseObject>(routes: T[], location: string | PartialLocation, basename?: string): IRouteMatch<T>[] | null;
@@ -65,15 +65,20 @@ function flattenRoutes(routes, branches = [], parentPath = '', parentRoutes = []
65
65
  });
66
66
  return branches;
67
67
  }
68
- function matchRoutes(routes, location) {
68
+ function matchRoutes(routes, location, basename = '') {
69
69
  if (typeof location === 'string') {
70
70
  location = (0, utils_1.resolvePath)(location);
71
71
  }
72
- let pathname = location.pathname;
73
- // If pathname is empty, it means invalid pathname and cannot match any route.
74
- // This only happens when basename is set but url is not start with basename
75
- if (!pathname) {
76
- return null;
72
+ let pathname = location.pathname || '/';
73
+ if (basename) {
74
+ const normalizedBasename = (0, utils_1.normalizeBase)(basename);
75
+ const pathnameWithoutBase = (0, utils_1.stripBase)(pathname, normalizedBasename);
76
+ if (pathnameWithoutBase) {
77
+ pathname = pathnameWithoutBase;
78
+ }
79
+ else {
80
+ return null;
81
+ }
77
82
  }
78
83
  let branches = flattenRoutes(routes);
79
84
  branches = rankRouteBranches(branches);
@@ -1,18 +1,19 @@
1
1
  import * as qs from 'query-string';
2
- import { PathRecord, Path } from '../types';
2
+ import { PathRecord, PartialPath, Path } from '../types';
3
3
  export declare const trimTrailingSlashes: (path: string) => string;
4
4
  export declare const normalizeSlashes: (path: string) => string;
5
5
  export declare const joinPaths: (paths: string[]) => string;
6
6
  export declare const splitPath: (path: string) => string[];
7
7
  export declare function normalizeBase(base: string): string;
8
8
  export declare function parseQuery(queryStr: string): qs.ParsedQuery<string>;
9
- export declare function pathToString({ pathname, search, hash, query }: Path, basename?: string): string;
9
+ export declare function pathToString({ pathname, search, hash, query }: PartialPath, basename?: string): string;
10
10
  /**
11
11
  * Parses a string URL path into its separate pathname, search, and hash components.
12
12
  */
13
13
  export declare function resolvePath(to: PathRecord, fromPathname?: string): Path;
14
14
  /**
15
15
  * Strips off the base from the beginning of a location.pathname in a non-case-sensitive way.
16
+ * If the base does not match at the beginning, null will be returned.
16
17
  *
17
18
  * @param pathname - location.pathname
18
19
  * @param base - base to strip off
package/lib/utils/path.js CHANGED
@@ -33,9 +33,6 @@ function pathToString({ pathname = '/', search = '', hash = '', query = {} }, ba
33
33
  }
34
34
  const pathString = pathname + search + hash;
35
35
  if (basename) {
36
- if (pathString === '/') {
37
- return basename;
38
- }
39
36
  return (0, exports.joinPaths)([basename, pathString]);
40
37
  }
41
38
  return pathString;
@@ -109,6 +106,7 @@ function resolvePath(to, fromPathname = '/') {
109
106
  exports.resolvePath = resolvePath;
110
107
  /**
111
108
  * Strips off the base from the beginning of a location.pathname in a non-case-sensitive way.
109
+ * If the base does not match at the beginning, null will be returned.
112
110
  *
113
111
  * @param pathname - location.pathname
114
112
  * @param base - base to strip off
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/router",
3
- "version": "1.0.45-pre.0",
3
+ "version": "1.0.46",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -28,7 +28,7 @@
28
28
  "node": ">= 16.0.0"
29
29
  },
30
30
  "dependencies": {
31
- "@shuvi/utils": "1.0.45-pre.0",
31
+ "@shuvi/utils": "1.0.46",
32
32
  "query-string": "6.13.8"
33
33
  }
34
34
  }