Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions etc/brick-types.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export interface BaseRouteConf {
documentId?: string;
exact?: boolean;
hybrid?: boolean;
if?: string | boolean;
menu?: MenuConf;
path: string;
permissionsPreCheck?: string[];
Expand Down
2 changes: 2 additions & 0 deletions packages/brick-kit/src/core/LocationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ export class LocationContext {
const match = matchPath(this.location.pathname, {
path: computedPath,
exact: route.exact,
checkIf: (context) => looseCheckIf(route, context),
getContext: (match) => this.getContext({ match }),
});
if (match !== null) {
if (app.noAuthGuard || route.public || isLoggedIn()) {
Expand Down
4 changes: 4 additions & 0 deletions packages/brick-types/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ export interface RouteConfOfRedirect extends BaseRouteConf {
* 路由的基本配置信息。
*/
export interface BaseRouteConf {
/**
* 条件配置,根据 `if` 的计算结果来决定是否展示路由
*/
if?: string | boolean;
/**
* 路由地址,通常应使用 `${APP.homepage}` 开头。
*/
Expand Down
37 changes: 30 additions & 7 deletions packages/brick-utils/src/matchPath.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { matchPath, toPath } from "./matchPath";
import {
MatchOptions,
CompileOptions,
MatchResult,
} from "@next-core/brick-types";
import { matchPath, MatchPathOptions, toPath } from "./matchPath";
import { MatchResult } from "@next-core/brick-types";

describe("matchPath", () => {
it.each<[string, MatchOptions & CompileOptions, MatchResult]>([
it.each<[string, MatchPathOptions, MatchResult]>([
[
"/",
{
Expand Down Expand Up @@ -72,6 +68,33 @@ describe("matchPath", () => {
},
null,
],
[
"/next",
{
path: ["/:id", "/before"],
exact: true,
checkIf: () => true,
getContext: () => ({} as any),
},
{
path: "/:id",
url: "/next",
isExact: true,
params: {
id: "next",
},
},
],
[
"/next",
{
path: ["/:id", "/before"],
exact: true,
checkIf: () => false,
getContext: () => ({} as any),
},
null,
],
])("matchPath('%s', %j) should return %j", (pathname, options, result) => {
expect(matchPath(pathname, options)).toEqual(result);
});
Expand Down
27 changes: 23 additions & 4 deletions packages/brick-utils/src/matchPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import {
MatchOptions,
MatchResult,
MatchParams,
PluginRuntimeContext,
} from "@next-core/brick-types";

export type MatchPathOptions = MatchOptions &
CompileOptions & {
checkIf?: (context: PluginRuntimeContext) => boolean;
getContext?: (match: any) => PluginRuntimeContext;
};

const cache: Map<string, Map<string, CompileResult>> = new Map();
const cacheLimit = 10000;
let cacheCount = 0;
Expand Down Expand Up @@ -40,9 +47,16 @@ function compilePath(path: string, options: CompileOptions): CompileResult {
*/
export function matchPath(
pathname: string,
options: MatchOptions & CompileOptions
options: MatchPathOptions
): MatchResult {
const { path: p, exact = false, strict = false, sensitive = true } = options;
const {
path: p,
exact = false,
strict = false,
sensitive = true,
checkIf,
getContext,
} = options;

const paths = Array.isArray(p) ? p : [p];

Expand All @@ -69,8 +83,7 @@ export function matchPath(
}

const initialParams: MatchParams = {};

return {
const result = {
path, // the path used to match
url: path === "/" && url === "" ? "/" : url, // the matched portion of the URL
isExact, // whether or not we matched exactly
Expand All @@ -79,6 +92,12 @@ export function matchPath(
return memo;
}, initialParams),
};

if (checkIf && !checkIf(getContext(result))) {
return null;
}

return result;
}, null);
}

Expand Down