@e7w/easy-routes 0.0.8 → 0.1.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # 自用React-router配置
2
2
 
3
- /src/pages/\*\*/page.tsx为页面
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
- /src/pages/\*\*/layout.tsx为layout
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
- /src/pages/\*\*/meta.ts配置如下
21
+ src/\*\*/pages/\*\*/meta.ts配置如下
22
22
 
23
23
  ```ts
24
24
  export const permission: number = 0b0; // 默认是0b0代表需要登录,只有最右边的一位代表登录,其他位可以自己定enum代表权限
@@ -7,8 +7,7 @@ interface RouterSchema {
7
7
  }
8
8
  export declare const RouterSchema: z.ZodType<RouterSchema, RouterSchema>;
9
9
  export declare const system: {
10
- router: RouterSchema;
11
- ready: boolean;
10
+ router?: RouterSchema;
12
11
  init(req: Ctx["req"], meta: Meta): Promise<void>;
13
12
  hasPermission(permission?: number): boolean;
14
13
  getCtx(req: Ctx["req"], permission?: number): Ctx;
@@ -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 = [];
@@ -90,18 +91,9 @@ export var system = provide((function () {
90
91
  enumerable: true,
91
92
  configurable: true,
92
93
  writable: true,
93
- value: (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _router_initializers, {
94
- init: function () { },
95
- permission: 0,
96
- defaultTitle: document.title,
97
- }))
98
- });
99
- Object.defineProperty(this, "ready", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: (__runInitializers(this, _router_extraInitializers), false)
94
+ value: (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _router_initializers, void 0))
104
95
  });
96
+ __runInitializers(this, _router_extraInitializers);
105
97
  }
106
98
  Object.defineProperty(System.prototype, "init", {
107
99
  enumerable: false,
@@ -109,12 +101,12 @@ export var system = provide((function () {
109
101
  writable: true,
110
102
  value: function (req, meta) {
111
103
  return __awaiter(this, void 0, void 0, function () {
112
- return __generator(this, function (_b) {
113
- switch (_b.label) {
114
- case 0: return [4 /*yield*/, this.router.init(req, meta)];
104
+ var _b;
105
+ return __generator(this, function (_c) {
106
+ switch (_c.label) {
107
+ case 0: return [4 /*yield*/, ((_b = this.router) === null || _b === void 0 ? void 0 : _b.init(req, meta))];
115
108
  case 1:
116
- _b.sent();
117
- this.ready = true;
109
+ _c.sent();
118
110
  return [2 /*return*/];
119
111
  }
120
112
  });
@@ -126,8 +118,9 @@ export var system = provide((function () {
126
118
  configurable: true,
127
119
  writable: true,
128
120
  value: function (permission) {
121
+ var _b;
129
122
  if (permission === void 0) { permission = 0; }
130
- return (this.router.permission & permission) === permission;
123
+ return ((((_b = this.router) === null || _b === void 0 ? void 0 : _b.permission) || 0) & permission) === permission;
131
124
  }
132
125
  });
133
126
  Object.defineProperty(System.prototype, "getCtx", {
@@ -135,9 +128,10 @@ export var system = provide((function () {
135
128
  configurable: true,
136
129
  writable: true,
137
130
  value: function (req, permission) {
131
+ var _b, _c;
138
132
  if (permission === void 0) { permission = 1; }
139
133
  return {
140
- title: this.router.defaultTitle || document.title,
134
+ title: (_c = (_b = this.router) === null || _b === void 0 ? void 0 : _b.defaultTitle) !== null && _c !== void 0 ? _c : document.title,
141
135
  signed: (permission & 1) === 0 || this.hasPermission(1),
142
136
  noPermission: !this.hasPermission(permission),
143
137
  req: req,
@@ -149,7 +143,7 @@ export var system = provide((function () {
149
143
  (function () {
150
144
  var _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
151
145
  _router_decorators = [inject(RouterSchema)];
152
- _init_decorators = [loader.load(true)];
146
+ _init_decorators = [(_b = loader).once.bind(_b), loader.load(true)];
153
147
  __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);
154
148
  __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);
155
149
  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/pages/**/index.tsx", {
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/pages/**/layout.tsx", {
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/pages/**/meta.ts", {
62
+ var metas = import.meta.glob("/src/**/pages/**/meta.ts", {
63
63
  eager: true,
64
64
  });
65
65
  var routes = [];
@@ -95,20 +95,16 @@ filePaths.forEach(function (filePath) {
95
95
  req = {
96
96
  params: params,
97
97
  query: Object.fromEntries(url.searchParams),
98
- pathname: url.pathname.replace(/\/$/, ""),
98
+ pathname: url.pathname.replace(/\/?$/, "/"),
99
99
  };
100
- if (!!system.ready) return [3 /*break*/, 2];
101
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 () { }))];
102
101
  case 1:
103
102
  _h.sent();
104
- _h.label = 2;
105
- case 2:
106
103
  ctx = system.getCtx(req, meta.permission);
107
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 () { }))];
108
- case 3:
105
+ case 2:
109
106
  _h.sent();
110
107
  if (!ctx.signed) {
111
- system.ready = false;
112
108
  return [2 /*return*/, redirect("/login?from=".concat(encodeURIComponent(request.url)))];
113
109
  }
114
110
  if (ctx.noPermission)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e7w/easy-routes",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"