Skip to content

Commit

Permalink
fix the bug of path-to-regexp upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: YoungHypo <haiboyang@smail.nju.edu.cn>
  • Loading branch information
YoungHypo committed Oct 8, 2024
1 parent 04c8388 commit 0b96f6a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/dashboard/lambda/mock/matchMock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pathToRegexp = require('path-to-regexp');
const { pathToRegexp } = require('path-to-regexp');
const bodyParser = require('body-parser');

const mockFile = require('./index');
Expand Down Expand Up @@ -47,11 +47,11 @@ function normalizeConfig(config) {
const handler = config[key];
const { method, path } = parseKey(key);
const keys = [];
const re = pathToRegexp(path, keys);
const { regexp } = pathToRegexp(path, keys);
memo.push({
method,
path,
re,
regexp,
keys,
handler: createHandler(method, path, handler),
});
Expand Down Expand Up @@ -83,9 +83,9 @@ function matchMock(req) {
}
// eslint-disable-next-line no-restricted-syntax
for (const mock of mockData) {
const { method, re, keys } = mock;
const { method, regexp, keys } = mock;
if (method === exceptMethod) {
const match = re.exec(req.path);
const match = regexp.exec(req.path);
if (match) {
const params = {};

Expand All @@ -104,7 +104,7 @@ function matchMock(req) {
}
}

return mockData.filter(({ method, re }) => method === exceptMethod && re.test(exceptPath))[0];
return mockData.filter(({ method, regexp }) => method === exceptMethod && regexp.test(exceptPath))[0];
}
module.exports = (req, res, next) => {
const match = matchMock(req);
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"numeral": "^2.0.6",
"nzh": "^1.0.4",
"omit.js": "^1.0.0",
"path-to-regexp": "^8.1.0",
"path-to-regexp": "^8.2.0",
"prop-types": "^15.6.2",
"qs": "^6.6.0",
"rc-animate": "^2.6.0",
Expand Down
5 changes: 3 additions & 2 deletions src/dashboard/src/components/PageHeaderWrapper/breadcrumb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import pathToRegexp from 'path-to-regexp';
import { pathToRegexp } from 'path-to-regexp';
import { Link, formatMessage } from 'umi';
import { urlToList } from '../_utils/pathTools';
import { menu } from '../../defaultSettings';
Expand Down Expand Up @@ -33,7 +33,8 @@ export const getBreadcrumb = (breadcrumbNameMap, url) => {
let breadcrumb = breadcrumbNameMap[url];
if (!breadcrumb) {
Object.keys(breadcrumbNameMap).forEach(item => {
if (pathToRegexp(item).test(url)) {
const { regexp } = pathToRegexp(item);
if (regexp.test(url)) {
breadcrumb = breadcrumbNameMap[item];
}
});
Expand Down
5 changes: 3 additions & 2 deletions src/dashboard/src/components/SiderMenu/SiderMenuUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pathToRegexp from 'path-to-regexp';
import { pathToRegexp } from 'path-to-regexp';
import { urlToList } from '../_utils/pathTools';

/**
Expand All @@ -20,7 +20,8 @@ export const getFlatMenuKeys = menuData => {
export const getMenuMatches = (flatMenuKeys, path) =>
flatMenuKeys.filter(item => {
if (item) {
return pathToRegexp(item).test(path);
const { regexp } = pathToRegexp(item);
return regexp.test(path);
}
return false;
});
Expand Down
5 changes: 3 additions & 2 deletions src/dashboard/src/pages/Authorized.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import pathToRegexp from 'path-to-regexp';
import { pathToRegexp } from 'path-to-regexp';
import { connect, Redirect } from 'umi';
import Authorized from '@/utils/Authorized';
import { getAuthority } from '@/utils/authority';
Expand All @@ -12,7 +12,8 @@ function AuthComponent({ children, location, routerData }) {
let authorities;
routeData.forEach(route => {
// match prefix
if (pathToRegexp(`${route.path}(.*)`).test(path)) {
const { regexp } = pathToRegexp(`${route.path}(.*)`);
if (regexp.test(path)) {
authorities = route.authority || authorities;

// get children authority recursively
Expand Down
8 changes: 6 additions & 2 deletions src/dashboard/src/utils/getPageTitle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { formatMessage } from 'umi';
import pathToRegexp from 'path-to-regexp';
import { pathToRegexp } from 'path-to-regexp';
import isEqual from 'lodash/isEqual';
import memoizeOne from 'memoize-one';
import { menu, title } from '../defaultSettings';

export const matchParamsPath = (pathname, breadcrumbNameMap) => {
const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
const pathKey = Object.keys(breadcrumbNameMap).find(key => {
const { regexp } = pathToRegexp(key);
return regexp.test(pathname);
});

return breadcrumbNameMap[pathKey];
};

Expand Down

0 comments on commit 0b96f6a

Please sign in to comment.