@e7w/easy-routes 0.0.7 → 0.0.8

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/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export type { Loader } from "./types";
2
- export { routerSchema } from "./models/system";
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 { routerSchema } from "./models/system";
1
+ export { RouterSchema } from "./models/system";
2
2
  export { routes } from "./router";
@@ -1,16 +1,16 @@
1
1
  import z from "zod";
2
- import type { Ctx } from "../types";
3
- import type { Params } from "react-router";
4
- export interface RouterSchema {
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 routerSchema: z.ZodType<RouterSchema, RouterSchema>;
8
+ export declare const RouterSchema: z.ZodType<RouterSchema, RouterSchema>;
10
9
  export declare const system: {
11
10
  router: RouterSchema;
12
11
  ready: boolean;
13
- init(): Promise<void>;
12
+ init(req: Ctx["req"], meta: Meta): Promise<void>;
14
13
  hasPermission(permission?: number): boolean;
15
- getCtx(params: Params, request: Request, permission?: number): Ctx;
14
+ getCtx(req: Ctx["req"], permission?: number): Ctx;
16
15
  };
16
+ export {};
@@ -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 routerSchema = z.object({
73
+ export var RouterSchema = z.object({
74
74
  init: z.function({
75
75
  output: z.union([z.promise(z.void()), z.void()]),
76
76
  }),
@@ -107,11 +107,11 @@ export var system = provide((function () {
107
107
  enumerable: false,
108
108
  configurable: true,
109
109
  writable: true,
110
- value: function () {
110
+ value: function (req, meta) {
111
111
  return __awaiter(this, void 0, void 0, function () {
112
112
  return __generator(this, function (_b) {
113
113
  switch (_b.label) {
114
- case 0: return [4 /*yield*/, this.router.init()];
114
+ case 0: return [4 /*yield*/, this.router.init(req, meta)];
115
115
  case 1:
116
116
  _b.sent();
117
117
  this.ready = true;
@@ -134,14 +134,13 @@ export var system = provide((function () {
134
134
  enumerable: false,
135
135
  configurable: true,
136
136
  writable: true,
137
- value: function (params, request, permission) {
137
+ value: function (req, permission) {
138
138
  if (permission === void 0) { permission = 1; }
139
139
  return {
140
140
  title: this.router.defaultTitle || document.title,
141
141
  signed: (permission & 1) === 0 || this.hasPermission(1),
142
142
  noPermission: !this.hasPermission(permission),
143
- params: params,
144
- query: Object.fromEntries(new URL(request.url).searchParams),
143
+ req: req,
145
144
  };
146
145
  }
147
146
  });
@@ -149,7 +148,7 @@ export var system = provide((function () {
149
148
  }()),
150
149
  (function () {
151
150
  var _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
152
- _router_decorators = [inject(routerSchema)];
151
+ _router_decorators = [inject(RouterSchema)];
153
152
  _init_decorators = [loader.load(true)];
154
153
  __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
154
  __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);
package/dist/router.js CHANGED
@@ -85,25 +85,31 @@ 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 (_g) {
92
- switch (_g.label) {
91
+ return __generator(this, function (_h) {
92
+ switch (_h.label) {
93
93
  case 0:
94
+ url = new URL(request.url);
95
+ req = {
96
+ params: params,
97
+ query: Object.fromEntries(url.searchParams),
98
+ pathname: url.pathname.replace(/\/$/, ""),
99
+ };
94
100
  if (!!system.ready) return [3 /*break*/, 2];
95
- return [4 /*yield*/, ((_c = system.init()) === null || _c === void 0 ? void 0 : _c.catch(function () { }))];
101
+ 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
102
  case 1:
97
- _g.sent();
98
- _g.label = 2;
103
+ _h.sent();
104
+ _h.label = 2;
99
105
  case 2:
100
- ctx = system.getCtx(params, request, meta.permission);
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 () { }))];
106
+ ctx = system.getCtx(req, meta.permission);
107
+ 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 () { }))];
102
108
  case 3:
103
- _g.sent();
109
+ _h.sent();
104
110
  if (!ctx.signed) {
105
111
  system.ready = false;
106
- return [2 /*return*/, redirect("/login")];
112
+ return [2 /*return*/, redirect("/login?from=".concat(encodeURIComponent(request.url)))];
107
113
  }
108
114
  if (ctx.noPermission)
109
115
  return [2 /*return*/, redirect("/403")];
@@ -3,12 +3,14 @@ export interface Ctx {
3
3
  title: string;
4
4
  signed: boolean;
5
5
  noPermission: boolean;
6
- params: Params;
7
- query: Params;
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?: 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.7",
3
+ "version": "0.0.8",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"