@e7w/easy-routes 0.0.7 → 0.0.9
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.
- package/README.md +3 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/models/system.d.ts +7 -8
- package/dist/models/system.js +9 -15
- package/dist/router.js +19 -17
- package/dist/types/index.d.ts +6 -4
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 自用React-router配置
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
src/\*\*/pages/\*\*/page.tsx为页面
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
export function Page() {}
|
|
@@ -8,7 +8,7 @@ export function Page() {}
|
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
src/\*\*/pages/\*\*/layout.tsx为layout
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
export function Layout() {
|
|
@@ -18,7 +18,7 @@ export function Layout() {
|
|
|
18
18
|
|
|
19
19
|
---
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
src/\*\*/pages/\*\*/meta.ts配置如下
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
export const permission: number = 0b0; // 默认是0b0代表需要登录,只有最右边的一位代表登录,其他位可以自己定enum代表权限
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
export {
|
|
1
|
+
export type { Ctx } from "./types";
|
|
2
|
+
export { RouterSchema } from "./models/system";
|
|
3
3
|
export { routes } from "./router";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { RouterSchema } from "./models/system";
|
|
2
2
|
export { routes } from "./router";
|
package/dist/models/system.d.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import z from "zod";
|
|
2
|
-
import type { Ctx } from "../types";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
init(): void | Promise<void>;
|
|
2
|
+
import type { Ctx, Meta } from "../types";
|
|
3
|
+
interface RouterSchema {
|
|
4
|
+
init(req: Ctx["req"], meta: Meta): void | Promise<void>;
|
|
6
5
|
permission: number;
|
|
7
6
|
defaultTitle?: string;
|
|
8
7
|
}
|
|
9
|
-
export declare const
|
|
8
|
+
export declare const RouterSchema: z.ZodType<RouterSchema, RouterSchema>;
|
|
10
9
|
export declare const system: {
|
|
11
10
|
router: RouterSchema;
|
|
12
|
-
|
|
13
|
-
init(): Promise<void>;
|
|
11
|
+
init(req: Ctx["req"], meta: Meta): Promise<void>;
|
|
14
12
|
hasPermission(permission?: number): boolean;
|
|
15
|
-
getCtx(
|
|
13
|
+
getCtx(req: Ctx["req"], permission?: number): Ctx;
|
|
16
14
|
};
|
|
15
|
+
export {};
|
package/dist/models/system.js
CHANGED
|
@@ -70,7 +70,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
70
70
|
};
|
|
71
71
|
import { inject, loader, provide } from "@e7w/easy-model";
|
|
72
72
|
import z from "zod";
|
|
73
|
-
export var
|
|
73
|
+
export var RouterSchema = z.object({
|
|
74
74
|
init: z.function({
|
|
75
75
|
output: z.union([z.promise(z.void()), z.void()]),
|
|
76
76
|
}),
|
|
@@ -79,6 +79,7 @@ export var routerSchema = z.object({
|
|
|
79
79
|
});
|
|
80
80
|
export var system = provide((function () {
|
|
81
81
|
var _a;
|
|
82
|
+
var _b;
|
|
82
83
|
var _instanceExtraInitializers = [];
|
|
83
84
|
var _router_decorators;
|
|
84
85
|
var _router_initializers = [];
|
|
@@ -96,25 +97,19 @@ export var system = provide((function () {
|
|
|
96
97
|
defaultTitle: document.title,
|
|
97
98
|
}))
|
|
98
99
|
});
|
|
99
|
-
|
|
100
|
-
enumerable: true,
|
|
101
|
-
configurable: true,
|
|
102
|
-
writable: true,
|
|
103
|
-
value: (__runInitializers(this, _router_extraInitializers), false)
|
|
104
|
-
});
|
|
100
|
+
__runInitializers(this, _router_extraInitializers);
|
|
105
101
|
}
|
|
106
102
|
Object.defineProperty(System.prototype, "init", {
|
|
107
103
|
enumerable: false,
|
|
108
104
|
configurable: true,
|
|
109
105
|
writable: true,
|
|
110
|
-
value: function () {
|
|
106
|
+
value: function (req, meta) {
|
|
111
107
|
return __awaiter(this, void 0, void 0, function () {
|
|
112
108
|
return __generator(this, function (_b) {
|
|
113
109
|
switch (_b.label) {
|
|
114
|
-
case 0: return [4 /*yield*/, this.router.init()];
|
|
110
|
+
case 0: return [4 /*yield*/, this.router.init(req, meta)];
|
|
115
111
|
case 1:
|
|
116
112
|
_b.sent();
|
|
117
|
-
this.ready = true;
|
|
118
113
|
return [2 /*return*/];
|
|
119
114
|
}
|
|
120
115
|
});
|
|
@@ -134,14 +129,13 @@ export var system = provide((function () {
|
|
|
134
129
|
enumerable: false,
|
|
135
130
|
configurable: true,
|
|
136
131
|
writable: true,
|
|
137
|
-
value: function (
|
|
132
|
+
value: function (req, permission) {
|
|
138
133
|
if (permission === void 0) { permission = 1; }
|
|
139
134
|
return {
|
|
140
135
|
title: this.router.defaultTitle || document.title,
|
|
141
136
|
signed: (permission & 1) === 0 || this.hasPermission(1),
|
|
142
137
|
noPermission: !this.hasPermission(permission),
|
|
143
|
-
|
|
144
|
-
query: Object.fromEntries(new URL(request.url).searchParams),
|
|
138
|
+
req: req,
|
|
145
139
|
};
|
|
146
140
|
}
|
|
147
141
|
});
|
|
@@ -149,8 +143,8 @@ export var system = provide((function () {
|
|
|
149
143
|
}()),
|
|
150
144
|
(function () {
|
|
151
145
|
var _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
152
|
-
_router_decorators = [inject(
|
|
153
|
-
_init_decorators = [loader.load(true)];
|
|
146
|
+
_router_decorators = [inject(RouterSchema)];
|
|
147
|
+
_init_decorators = [(_b = loader).once.bind(_b), loader.load(true)];
|
|
154
148
|
__esDecorate(_a, null, _init_decorators, { kind: "method", name: "init", static: false, private: false, access: { has: function (obj) { return "init" in obj; }, get: function (obj) { return obj.init; } }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
155
149
|
__esDecorate(null, null, _router_decorators, { kind: "field", name: "router", static: false, private: false, access: { has: function (obj) { return "router" in obj; }, get: function (obj) { return obj.router; }, set: function (obj, value) { obj.router = value; } }, metadata: _metadata }, _router_initializers, _router_extraInitializers);
|
|
156
150
|
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
package/dist/router.js
CHANGED
|
@@ -51,15 +51,15 @@ import { DefaultOutlet } from "./components/DefaultOutlet";
|
|
|
51
51
|
import { ErrorBoundary } from "./components/ErrorBoundary";
|
|
52
52
|
import { ProgressLayout } from "./components/ProgressLayout";
|
|
53
53
|
import { system } from "./models/system";
|
|
54
|
-
var pages = import.meta.glob("/src
|
|
54
|
+
var pages = import.meta.glob("/src/**/pages/**/index.tsx", {
|
|
55
55
|
eager: true,
|
|
56
56
|
import: "Page",
|
|
57
57
|
});
|
|
58
|
-
var layouts = import.meta.glob("/src
|
|
58
|
+
var layouts = import.meta.glob("/src/**/pages/**/layout.tsx", {
|
|
59
59
|
eager: true,
|
|
60
60
|
import: "Layout",
|
|
61
61
|
});
|
|
62
|
-
var metas = import.meta.glob("/src
|
|
62
|
+
var metas = import.meta.glob("/src/**/pages/**/meta.ts", {
|
|
63
63
|
eager: true,
|
|
64
64
|
});
|
|
65
65
|
var routes = [];
|
|
@@ -85,25 +85,27 @@ filePaths.forEach(function (filePath) {
|
|
|
85
85
|
index: true,
|
|
86
86
|
loader: hasPage
|
|
87
87
|
? function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
88
|
-
var ctx;
|
|
89
|
-
var _c, _d, _e, _f;
|
|
88
|
+
var url, req, ctx;
|
|
89
|
+
var _c, _d, _e, _f, _g;
|
|
90
90
|
var params = _b.params, request = _b.request;
|
|
91
|
-
return __generator(this, function (
|
|
92
|
-
switch (
|
|
91
|
+
return __generator(this, function (_h) {
|
|
92
|
+
switch (_h.label) {
|
|
93
93
|
case 0:
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
url = new URL(request.url);
|
|
95
|
+
req = {
|
|
96
|
+
params: params,
|
|
97
|
+
query: Object.fromEntries(url.searchParams),
|
|
98
|
+
pathname: url.pathname.replace(/\/?$/, "/"),
|
|
99
|
+
};
|
|
100
|
+
return [4 /*yield*/, ((_d = (_c = system.init(req, meta)) === null || _c === void 0 ? void 0 : _c.catch) === null || _d === void 0 ? void 0 : _d.call(_c, function () { }))];
|
|
96
101
|
case 1:
|
|
97
|
-
|
|
98
|
-
|
|
102
|
+
_h.sent();
|
|
103
|
+
ctx = system.getCtx(req, meta.permission);
|
|
104
|
+
return [4 /*yield*/, ((_g = (_f = (_e = meta.loader) === null || _e === void 0 ? void 0 : _e.call(meta, ctx)) === null || _f === void 0 ? void 0 : _f.catch) === null || _g === void 0 ? void 0 : _g.call(_f, function () { }))];
|
|
99
105
|
case 2:
|
|
100
|
-
|
|
101
|
-
return [4 /*yield*/, ((_f = (_e = (_d = meta.loader) === null || _d === void 0 ? void 0 : _d.call(meta, ctx, meta)) === null || _e === void 0 ? void 0 : _e.catch) === null || _f === void 0 ? void 0 : _f.call(_e, function () { }))];
|
|
102
|
-
case 3:
|
|
103
|
-
_g.sent();
|
|
106
|
+
_h.sent();
|
|
104
107
|
if (!ctx.signed) {
|
|
105
|
-
|
|
106
|
-
return [2 /*return*/, redirect("/login")];
|
|
108
|
+
return [2 /*return*/, redirect("/login?from=".concat(encodeURIComponent(request.url)))];
|
|
107
109
|
}
|
|
108
110
|
if (ctx.noPermission)
|
|
109
111
|
return [2 /*return*/, redirect("/403")];
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,12 +3,14 @@ export interface Ctx {
|
|
|
3
3
|
title: string;
|
|
4
4
|
signed: boolean;
|
|
5
5
|
noPermission: boolean;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
req: {
|
|
7
|
+
params: Params;
|
|
8
|
+
query: Params;
|
|
9
|
+
pathname: string;
|
|
10
|
+
};
|
|
8
11
|
data?: any;
|
|
9
12
|
}
|
|
10
13
|
export type Meta = {
|
|
11
14
|
permission?: number;
|
|
12
|
-
loader
|
|
15
|
+
loader?(ctx: Ctx): void | Promise<void>;
|
|
13
16
|
} & Record<string, unknown>;
|
|
14
|
-
export type Loader = (ctx: Ctx, meta: Meta) => void | Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e7w/easy-routes",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc --build && copyfiles -u 1 src/**/*.css dist/",
|
|
15
|
+
"lint": "eslint ."
|
|
16
|
+
},
|
|
13
17
|
"dependencies": {
|
|
14
18
|
"@e7w/easy-model": "^0.2.1",
|
|
15
19
|
"nprogress": "^0.2.0",
|
|
@@ -40,9 +44,5 @@
|
|
|
40
44
|
"README.md",
|
|
41
45
|
"LICENSE.md"
|
|
42
46
|
],
|
|
43
|
-
"license": "MIT"
|
|
44
|
-
"scripts": {
|
|
45
|
-
"build": "tsc --build && copyfiles -u 1 src/**/*.css dist/",
|
|
46
|
-
"lint": "eslint ."
|
|
47
|
-
}
|
|
47
|
+
"license": "MIT"
|
|
48
48
|
}
|