arcanajs 2.1.1 → 2.1.2
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/framework/cli/index.d.ts +1 -0
- package/framework/cli/index.js +107 -0
- package/framework/cli/webpack.config.d.ts +3 -0
- package/framework/cli/webpack.config.js +200 -0
- package/framework/lib/client/index.d.ts +2 -0
- package/framework/lib/client/index.js +40 -0
- package/framework/lib/index.d.ts +17 -0
- package/framework/lib/index.js +33 -0
- package/framework/lib/server/ArcanaJSMiddleware.d.ts +16 -0
- package/framework/lib/server/ArcanaJSMiddleware.js +99 -0
- package/framework/lib/server/ArcanaJSServer.d.ts +21 -0
- package/framework/lib/server/ArcanaJSServer.js +140 -0
- package/framework/lib/server/ControllerBinder.d.ts +4 -0
- package/framework/lib/server/ControllerBinder.js +32 -0
- package/framework/lib/server/CsrfMiddleware.d.ts +2 -0
- package/framework/lib/server/CsrfMiddleware.js +34 -0
- package/framework/lib/server/DynamicRouter.d.ts +2 -0
- package/framework/lib/server/DynamicRouter.js +50 -0
- package/framework/lib/server/ResponseHandlerMiddleware.d.ts +27 -0
- package/framework/lib/server/ResponseHandlerMiddleware.js +30 -0
- package/framework/lib/server/Router.d.ts +94 -0
- package/framework/lib/server/Router.js +203 -0
- package/framework/lib/server/default-index.html +12 -0
- package/framework/lib/server.d.ts +6 -0
- package/framework/lib/server.js +28 -0
- package/framework/lib/shared/ArcanaJSApp.d.ts +11 -0
- package/framework/lib/shared/ArcanaJSApp.js +79 -0
- package/framework/lib/shared/components/Body.d.ts +6 -0
- package/framework/lib/shared/components/Body.js +11 -0
- package/framework/lib/shared/components/Head.d.ts +4 -0
- package/framework/lib/shared/components/Head.js +125 -0
- package/framework/lib/shared/components/Link.d.ts +6 -0
- package/framework/lib/shared/components/Link.js +17 -0
- package/framework/lib/shared/components/NavLink.d.ts +8 -0
- package/framework/lib/shared/components/NavLink.js +16 -0
- package/framework/lib/shared/components/Page.d.ts +6 -0
- package/framework/lib/shared/components/Page.js +16 -0
- package/framework/lib/shared/context/HeadContext.d.ts +6 -0
- package/framework/lib/shared/context/HeadContext.js +5 -0
- package/framework/lib/shared/context/PageContext.d.ts +1 -0
- package/framework/lib/shared/context/PageContext.js +5 -0
- package/framework/lib/shared/context/RouterContext.d.ts +13 -0
- package/framework/lib/shared/context/RouterContext.js +13 -0
- package/framework/lib/shared/core/ArcanaJSApp.d.ts +13 -0
- package/framework/lib/shared/core/ArcanaJSApp.js +98 -0
- package/framework/lib/shared/hooks/useDynamicComponents.d.ts +1 -0
- package/framework/lib/shared/hooks/useDynamicComponents.js +20 -0
- package/framework/lib/shared/hooks/useHead.d.ts +1 -0
- package/framework/lib/shared/hooks/useHead.js +7 -0
- package/framework/lib/shared/hooks/useLocation.d.ts +5 -0
- package/framework/lib/shared/hooks/useLocation.js +13 -0
- package/framework/lib/shared/hooks/usePage.d.ts +1 -0
- package/framework/lib/shared/hooks/usePage.js +7 -0
- package/framework/lib/shared/hooks/useParams.d.ts +1 -0
- package/framework/lib/shared/hooks/useParams.js +13 -0
- package/framework/lib/shared/hooks/useQuery.d.ts +1 -0
- package/framework/lib/shared/hooks/useQuery.js +9 -0
- package/framework/lib/shared/hooks/useRouter.d.ts +1 -0
- package/framework/lib/shared/hooks/useRouter.js +13 -0
- package/framework/lib/shared/utils/createSingletonContext.d.ts +11 -0
- package/framework/lib/shared/utils/createSingletonContext.js +21 -0
- package/package.json +5 -5
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ArcanaJSServer = void 0;
|
|
7
|
+
const compression_1 = __importDefault(require("compression"));
|
|
8
|
+
const cookie_parser_1 = __importDefault(require("cookie-parser"));
|
|
9
|
+
const express_1 = __importDefault(require("express"));
|
|
10
|
+
const fs_1 = __importDefault(require("fs"));
|
|
11
|
+
const helmet_1 = __importDefault(require("helmet"));
|
|
12
|
+
const path_1 = __importDefault(require("path"));
|
|
13
|
+
const ArcanaJSMiddleware_1 = require("./ArcanaJSMiddleware");
|
|
14
|
+
const CsrfMiddleware_1 = require("./CsrfMiddleware");
|
|
15
|
+
const DynamicRouter_1 = require("./DynamicRouter");
|
|
16
|
+
const ResponseHandlerMiddleware_1 = require("./ResponseHandlerMiddleware");
|
|
17
|
+
class ArcanaJSServer {
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.config = config;
|
|
20
|
+
this.app = (0, express_1.default)();
|
|
21
|
+
this.initialize();
|
|
22
|
+
}
|
|
23
|
+
initialize() {
|
|
24
|
+
let { staticDir = "public", distDir = "dist/public", indexFile = "dist/public/index.html", views, viewsContext, routes, layout, } = this.config;
|
|
25
|
+
if (!views && viewsContext) {
|
|
26
|
+
views = {};
|
|
27
|
+
viewsContext.keys().forEach((key) => {
|
|
28
|
+
const viewName = key.replace(/^\.\/(.*)\.tsx$/, "$1");
|
|
29
|
+
views[viewName] = viewsContext(key).default;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (!views) {
|
|
33
|
+
// Auto-discovery using fs (Server-side only)
|
|
34
|
+
views = this.discoverViews();
|
|
35
|
+
}
|
|
36
|
+
if (!views || Object.keys(views).length === 0) {
|
|
37
|
+
console.warn("No views found. Please check your views directory.");
|
|
38
|
+
views = {};
|
|
39
|
+
}
|
|
40
|
+
// Security and Performance
|
|
41
|
+
this.app.use((0, helmet_1.default)({
|
|
42
|
+
contentSecurityPolicy: false,
|
|
43
|
+
}));
|
|
44
|
+
this.app.use((0, compression_1.default)());
|
|
45
|
+
this.app.use((0, cookie_parser_1.default)());
|
|
46
|
+
this.app.use((0, CsrfMiddleware_1.createCsrfMiddleware)());
|
|
47
|
+
this.app.use(ResponseHandlerMiddleware_1.responseHandler);
|
|
48
|
+
// Static files
|
|
49
|
+
this.app.use(express_1.default.static(path_1.default.resolve(process.cwd(), distDir), {
|
|
50
|
+
index: false,
|
|
51
|
+
maxAge: "1y",
|
|
52
|
+
}));
|
|
53
|
+
this.app.use(express_1.default.static(path_1.default.resolve(process.cwd(), staticDir), {
|
|
54
|
+
index: false,
|
|
55
|
+
maxAge: "1d",
|
|
56
|
+
}));
|
|
57
|
+
// ArcanaJS Middleware
|
|
58
|
+
this.app.use((0, ArcanaJSMiddleware_1.createArcanaJSMiddleware)({
|
|
59
|
+
views,
|
|
60
|
+
indexFile: path_1.default.resolve(process.cwd(), indexFile),
|
|
61
|
+
layout,
|
|
62
|
+
}));
|
|
63
|
+
// Custom Routes
|
|
64
|
+
if (routes) {
|
|
65
|
+
if (Array.isArray(routes)) {
|
|
66
|
+
routes.forEach((route) => this.app.use(route));
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.app.use(routes);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Dynamic Router
|
|
73
|
+
this.app.use((0, DynamicRouter_1.createDynamicRouter)(views));
|
|
74
|
+
// 404 Fallback
|
|
75
|
+
this.app.use((req, res) => {
|
|
76
|
+
res.status(404).renderPage("NotFoundPage");
|
|
77
|
+
});
|
|
78
|
+
// Error Handler
|
|
79
|
+
this.app.use((err, req, res, next) => {
|
|
80
|
+
console.error(err);
|
|
81
|
+
const message = process.env.NODE_ENV === "production"
|
|
82
|
+
? "Internal Server Error"
|
|
83
|
+
: err.message;
|
|
84
|
+
res.status(500).renderPage("ErrorPage", { message });
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
discoverViews() {
|
|
88
|
+
const views = {};
|
|
89
|
+
const viewsDir = this.config.viewsDir
|
|
90
|
+
? path_1.default.resolve(process.cwd(), this.config.viewsDir)
|
|
91
|
+
: path_1.default.resolve(process.cwd(), "src/views");
|
|
92
|
+
const traverse = (dir) => {
|
|
93
|
+
if (!fs_1.default.existsSync(dir))
|
|
94
|
+
return;
|
|
95
|
+
const files = fs_1.default.readdirSync(dir);
|
|
96
|
+
files.forEach((file) => {
|
|
97
|
+
const fullPath = path_1.default.join(dir, file);
|
|
98
|
+
const stat = fs_1.default.statSync(fullPath);
|
|
99
|
+
if (stat.isDirectory()) {
|
|
100
|
+
traverse(fullPath);
|
|
101
|
+
}
|
|
102
|
+
else if (file.endsWith(".tsx") || file.endsWith(".jsx")) {
|
|
103
|
+
const relativePath = path_1.default.relative(viewsDir, fullPath);
|
|
104
|
+
const viewName = relativePath.replace(/\.(tsx|jsx)$/, "");
|
|
105
|
+
try {
|
|
106
|
+
// Use __non_webpack_require__ if available to avoid Webpack bundling issues
|
|
107
|
+
// or standard require if running in Node directly
|
|
108
|
+
const requireFunc = typeof __non_webpack_require__ !== "undefined"
|
|
109
|
+
? __non_webpack_require__
|
|
110
|
+
: module.require;
|
|
111
|
+
// We need to register ts-node if we are requiring .tsx files directly
|
|
112
|
+
// This is a simplified approach. In a real framework, we'd handle compilation.
|
|
113
|
+
if (file.endsWith(".tsx") || file.endsWith(".ts")) {
|
|
114
|
+
try {
|
|
115
|
+
requireFunc("ts-node/register");
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
// Ignore if already registered or not found (might be pre-compiled)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const pageModule = requireFunc(fullPath);
|
|
122
|
+
views[viewName] = pageModule.default || pageModule;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
console.error(`Failed to load view ${viewName}:`, error);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
traverse(viewsDir);
|
|
131
|
+
return views;
|
|
132
|
+
}
|
|
133
|
+
start() {
|
|
134
|
+
const PORT = this.config.port || process.env.PORT || 3000;
|
|
135
|
+
this.app.listen(PORT, () => {
|
|
136
|
+
console.log(`Server is running on http://localhost:${PORT}`);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.ArcanaJSServer = ArcanaJSServer;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ControllerBinder {
|
|
4
|
+
static handle(controller, method) {
|
|
5
|
+
// Instantiate the controller once (Singleton pattern) for performance
|
|
6
|
+
// This assumes controllers are stateless, which is best practice.
|
|
7
|
+
let instance;
|
|
8
|
+
try {
|
|
9
|
+
instance = new controller();
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
// Fallback if controller is not a class or fails to instantiate
|
|
13
|
+
console.warn(`Failed to instantiate controller ${controller.name}`, e);
|
|
14
|
+
instance = controller;
|
|
15
|
+
}
|
|
16
|
+
return async (req, res, next) => {
|
|
17
|
+
try {
|
|
18
|
+
// Call the method
|
|
19
|
+
if (typeof instance[method] === "function") {
|
|
20
|
+
await instance[method](req, res, next);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
throw new Error(`Method ${method} not found on controller ${controller.name}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
next(error);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.default = ControllerBinder;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createCsrfMiddleware = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const CSRF_COOKIE_NAME = "_csrf";
|
|
9
|
+
const createCsrfMiddleware = () => {
|
|
10
|
+
return (req, res, next) => {
|
|
11
|
+
// 1. Generate or retrieve token
|
|
12
|
+
let token = req.cookies[CSRF_COOKIE_NAME];
|
|
13
|
+
if (!token) {
|
|
14
|
+
token = crypto_1.default.randomBytes(32).toString("hex");
|
|
15
|
+
res.cookie(CSRF_COOKIE_NAME, token, {
|
|
16
|
+
httpOnly: true,
|
|
17
|
+
secure: process.env.NODE_ENV === "production",
|
|
18
|
+
sameSite: "strict",
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
// 2. Expose token to the response locals (for injection into the view)
|
|
22
|
+
res.locals.csrfToken = token;
|
|
23
|
+
// 3. Verify token on state-changing methods
|
|
24
|
+
const method = req.method.toUpperCase();
|
|
25
|
+
if (["POST", "PUT", "DELETE", "PATCH"].includes(method)) {
|
|
26
|
+
const headerToken = req.headers["x-csrf-token"];
|
|
27
|
+
if (!headerToken || headerToken !== token) {
|
|
28
|
+
return res.status(403).json({ error: "Invalid CSRF Token" });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
next();
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
exports.createCsrfMiddleware = createCsrfMiddleware;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDynamicRouter = void 0;
|
|
4
|
+
const createDynamicRouter = (views) => {
|
|
5
|
+
return (req, res, next) => {
|
|
6
|
+
// Remove leading slash
|
|
7
|
+
let path = req.path.substring(1);
|
|
8
|
+
// Handle root path mapping to "index" if not handled elsewhere
|
|
9
|
+
if (path === "") {
|
|
10
|
+
path = "index";
|
|
11
|
+
}
|
|
12
|
+
// 1. Exact match
|
|
13
|
+
if (views[path]) {
|
|
14
|
+
return res.renderPage(path, {});
|
|
15
|
+
}
|
|
16
|
+
// 2. Dynamic match
|
|
17
|
+
for (const viewName of Object.keys(views)) {
|
|
18
|
+
if (!viewName.includes("["))
|
|
19
|
+
continue;
|
|
20
|
+
// Convert view path to regex
|
|
21
|
+
// e.g., "users/[id]" -> "^users/([^/]+)$"
|
|
22
|
+
// We need to escape special regex characters first, but keep [ and ] for our logic
|
|
23
|
+
// Actually, simpler: split by '/' and match segments
|
|
24
|
+
const pageParts = viewName.split("/");
|
|
25
|
+
const pathParts = path.split("/");
|
|
26
|
+
if (pageParts.length !== pathParts.length)
|
|
27
|
+
continue;
|
|
28
|
+
let match = true;
|
|
29
|
+
const params = {};
|
|
30
|
+
for (let i = 0; i < pageParts.length; i++) {
|
|
31
|
+
const pagePart = pageParts[i];
|
|
32
|
+
const pathPart = pathParts[i];
|
|
33
|
+
if (pagePart.startsWith("[") && pagePart.endsWith("]")) {
|
|
34
|
+
const paramName = pagePart.slice(1, -1);
|
|
35
|
+
params[paramName] = pathPart;
|
|
36
|
+
}
|
|
37
|
+
else if (pagePart !== pathPart) {
|
|
38
|
+
match = false;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (match) {
|
|
43
|
+
return res.renderPage(viewName, {}, params);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// If not found, pass to the next middleware (usually 404 handler)
|
|
47
|
+
next();
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
exports.createDynamicRouter = createDynamicRouter;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { NextFunction, Request, Response } from "express";
|
|
2
|
+
declare global {
|
|
3
|
+
namespace Express {
|
|
4
|
+
interface Response {
|
|
5
|
+
/**
|
|
6
|
+
* Sends a success response with a standard format.
|
|
7
|
+
*
|
|
8
|
+
* @param data - The data payload to include in the response (default: {}).
|
|
9
|
+
* @param message - A descriptive message for the success (default: "Success").
|
|
10
|
+
* @param status - The HTTP status code to return (default: 200).
|
|
11
|
+
* @returns The Express Response object.
|
|
12
|
+
*/
|
|
13
|
+
success: (data?: string | object | null, message?: string, status?: number) => Response;
|
|
14
|
+
/**
|
|
15
|
+
* Sends an error response with a standard format.
|
|
16
|
+
*
|
|
17
|
+
* @param message - A descriptive message for the error (default: "Error").
|
|
18
|
+
* @param status - The HTTP status code to return (default: 500).
|
|
19
|
+
* @param error - Additional error details or object (default: null).
|
|
20
|
+
* @param data - Optional data payload to include in the error response (default: null).
|
|
21
|
+
* @returns The Express Response object.
|
|
22
|
+
*/
|
|
23
|
+
error: (message?: string, status?: number, error?: string | object | null, data?: string | object | null) => Response;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export declare const responseHandler: (req: Request, res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.responseHandler = void 0;
|
|
4
|
+
const responseHandler = (req, res, next) => {
|
|
5
|
+
res.success = (data = {}, message = "Success", status = 200) => {
|
|
6
|
+
return res.status(status).json({
|
|
7
|
+
success: true,
|
|
8
|
+
message,
|
|
9
|
+
data,
|
|
10
|
+
error: null,
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
res.error = (message = "Error", status = 500, error = null, data = null) => {
|
|
14
|
+
const errorResponse = error
|
|
15
|
+
? typeof error === "object" && error !== null
|
|
16
|
+
? error instanceof Error
|
|
17
|
+
? error.toString()
|
|
18
|
+
: error
|
|
19
|
+
: error.toString()
|
|
20
|
+
: null;
|
|
21
|
+
return res.status(status).json({
|
|
22
|
+
success: false,
|
|
23
|
+
message,
|
|
24
|
+
data,
|
|
25
|
+
error: errorResponse,
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
next();
|
|
29
|
+
};
|
|
30
|
+
exports.responseHandler = responseHandler;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import express, { Router as ExpressRouter, RequestHandler } from "express";
|
|
2
|
+
/**
|
|
3
|
+
* Provides Routing syntax for defining routes with prefixes, middlewares, and groups
|
|
4
|
+
*/
|
|
5
|
+
export declare class Router {
|
|
6
|
+
private router;
|
|
7
|
+
private middlewareStack;
|
|
8
|
+
private prefixStack;
|
|
9
|
+
constructor();
|
|
10
|
+
/**
|
|
11
|
+
* Create a new router instance
|
|
12
|
+
*/
|
|
13
|
+
static create(): Router;
|
|
14
|
+
/**
|
|
15
|
+
* Add middleware to the current stack
|
|
16
|
+
*/
|
|
17
|
+
middleware(...middleware: RequestHandler[]): Router;
|
|
18
|
+
/**
|
|
19
|
+
* Add prefix to the current stack
|
|
20
|
+
*/
|
|
21
|
+
prefix(prefix: string): Router;
|
|
22
|
+
/**
|
|
23
|
+
* Create a route group
|
|
24
|
+
*/
|
|
25
|
+
group(callback: (router: Router) => void): Router;
|
|
26
|
+
/**
|
|
27
|
+
* Define a GET route
|
|
28
|
+
*/
|
|
29
|
+
get(path: string, ...args: any[]): Router;
|
|
30
|
+
/**
|
|
31
|
+
* Define a POST route
|
|
32
|
+
*/
|
|
33
|
+
post(path: string, ...args: any[]): Router;
|
|
34
|
+
/**
|
|
35
|
+
* Define a PUT route
|
|
36
|
+
*/
|
|
37
|
+
put(path: string, ...args: any[]): Router;
|
|
38
|
+
/**
|
|
39
|
+
* Define a DELETE route
|
|
40
|
+
*/
|
|
41
|
+
delete(path: string, ...args: any[]): Router;
|
|
42
|
+
/**
|
|
43
|
+
* Define a PATCH route
|
|
44
|
+
*/
|
|
45
|
+
patch(path: string, ...args: any[]): Router;
|
|
46
|
+
/**
|
|
47
|
+
* Define an OPTIONS route
|
|
48
|
+
*/
|
|
49
|
+
options(path: string, ...args: any[]): Router;
|
|
50
|
+
/**
|
|
51
|
+
* Get the underlying Express router
|
|
52
|
+
*/
|
|
53
|
+
getRouter(): ExpressRouter;
|
|
54
|
+
/**
|
|
55
|
+
* Mount this router to an Express app or router
|
|
56
|
+
*/
|
|
57
|
+
mount(app: express.Application | ExpressRouter, basePath?: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Clone the current router instance
|
|
60
|
+
*/
|
|
61
|
+
private _clone;
|
|
62
|
+
/**
|
|
63
|
+
* Add a route to the router
|
|
64
|
+
*/
|
|
65
|
+
private _addRoute;
|
|
66
|
+
/**
|
|
67
|
+
* Build the full path with prefixes
|
|
68
|
+
*/
|
|
69
|
+
private _buildPath;
|
|
70
|
+
/**
|
|
71
|
+
* Build the route handler
|
|
72
|
+
*/
|
|
73
|
+
private _buildHandler;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Static Route class for Laravel-like syntax
|
|
77
|
+
*/
|
|
78
|
+
export declare class Route {
|
|
79
|
+
private static _router;
|
|
80
|
+
static create(): Router;
|
|
81
|
+
static middleware(...middleware: RequestHandler[]): Router;
|
|
82
|
+
static prefix(prefix: string): Router;
|
|
83
|
+
static group(callback: (router: Router) => void): Router;
|
|
84
|
+
static get(path: string, ...args: any[]): Router;
|
|
85
|
+
static post(path: string, ...args: any[]): Router;
|
|
86
|
+
static put(path: string, ...args: any[]): Router;
|
|
87
|
+
static delete(path: string, ...args: any[]): Router;
|
|
88
|
+
static patch(path: string, ...args: any[]): Router;
|
|
89
|
+
static options(path: string, ...args: any[]): Router;
|
|
90
|
+
static getRouter(): ExpressRouter;
|
|
91
|
+
static mount(app: express.Application, basePath?: string): void;
|
|
92
|
+
static reset(): void;
|
|
93
|
+
}
|
|
94
|
+
export default Route;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Route = exports.Router = void 0;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const ControllerBinder_1 = __importDefault(require("./ControllerBinder"));
|
|
9
|
+
/**
|
|
10
|
+
* Provides Routing syntax for defining routes with prefixes, middlewares, and groups
|
|
11
|
+
*/
|
|
12
|
+
class Router {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.router = express_1.default.Router();
|
|
15
|
+
this.middlewareStack = [];
|
|
16
|
+
this.prefixStack = [];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Create a new router instance
|
|
20
|
+
*/
|
|
21
|
+
static create() {
|
|
22
|
+
return new Router();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Add middleware to the current stack
|
|
26
|
+
*/
|
|
27
|
+
middleware(...middleware) {
|
|
28
|
+
const newRouter = this._clone();
|
|
29
|
+
newRouter.middlewareStack = [...this.middlewareStack, ...middleware];
|
|
30
|
+
return newRouter;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Add prefix to the current stack
|
|
34
|
+
*/
|
|
35
|
+
prefix(prefix) {
|
|
36
|
+
const newRouter = this._clone();
|
|
37
|
+
newRouter.prefixStack = [
|
|
38
|
+
...this.prefixStack,
|
|
39
|
+
prefix.replace(/^\/|\/$/g, ""),
|
|
40
|
+
];
|
|
41
|
+
return newRouter;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a route group
|
|
45
|
+
*/
|
|
46
|
+
group(callback) {
|
|
47
|
+
callback(this);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Define a GET route
|
|
52
|
+
*/
|
|
53
|
+
get(path, ...args) {
|
|
54
|
+
const action = args.pop();
|
|
55
|
+
const middlewares = args;
|
|
56
|
+
return this._addRoute("get", path, action, middlewares);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Define a POST route
|
|
60
|
+
*/
|
|
61
|
+
post(path, ...args) {
|
|
62
|
+
const action = args.pop();
|
|
63
|
+
const middlewares = args;
|
|
64
|
+
return this._addRoute("post", path, action, middlewares);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Define a PUT route
|
|
68
|
+
*/
|
|
69
|
+
put(path, ...args) {
|
|
70
|
+
const action = args.pop();
|
|
71
|
+
const middlewares = args;
|
|
72
|
+
return this._addRoute("put", path, action, middlewares);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Define a DELETE route
|
|
76
|
+
*/
|
|
77
|
+
delete(path, ...args) {
|
|
78
|
+
const action = args.pop();
|
|
79
|
+
const middlewares = args;
|
|
80
|
+
return this._addRoute("delete", path, action, middlewares);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Define a PATCH route
|
|
84
|
+
*/
|
|
85
|
+
patch(path, ...args) {
|
|
86
|
+
const action = args.pop();
|
|
87
|
+
const middlewares = args;
|
|
88
|
+
return this._addRoute("patch", path, action, middlewares);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Define an OPTIONS route
|
|
92
|
+
*/
|
|
93
|
+
options(path, ...args) {
|
|
94
|
+
const action = args.pop();
|
|
95
|
+
const middlewares = args;
|
|
96
|
+
return this._addRoute("options", path, action, middlewares);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Get the underlying Express router
|
|
100
|
+
*/
|
|
101
|
+
getRouter() {
|
|
102
|
+
return this.router;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Mount this router to an Express app or router
|
|
106
|
+
*/
|
|
107
|
+
mount(app, basePath = "/") {
|
|
108
|
+
app.use(basePath, this.router);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Clone the current router instance
|
|
112
|
+
*/
|
|
113
|
+
_clone() {
|
|
114
|
+
const newRouter = new Router();
|
|
115
|
+
newRouter.router = this.router;
|
|
116
|
+
newRouter.middlewareStack = [...this.middlewareStack];
|
|
117
|
+
newRouter.prefixStack = [...this.prefixStack];
|
|
118
|
+
return newRouter;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Add a route to the router
|
|
122
|
+
*/
|
|
123
|
+
_addRoute(method, path, action, routeMiddlewares = []) {
|
|
124
|
+
const fullPath = this._buildPath(path);
|
|
125
|
+
const handler = this._buildHandler(action);
|
|
126
|
+
const flatMiddlewares = routeMiddlewares.flat(Infinity);
|
|
127
|
+
const middlewares = [...this.middlewareStack, ...flatMiddlewares, handler];
|
|
128
|
+
this.router[method](fullPath, ...middlewares);
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Build the full path with prefixes
|
|
133
|
+
*/
|
|
134
|
+
_buildPath(path) {
|
|
135
|
+
const cleanPath = path.replace(/^\//, "");
|
|
136
|
+
const prefixes = this.prefixStack.filter((p) => p !== "");
|
|
137
|
+
if (prefixes.length === 0) {
|
|
138
|
+
return `/${cleanPath}`;
|
|
139
|
+
}
|
|
140
|
+
return `/${prefixes.join("/")}/${cleanPath}`.replace(/\/+/g, "/");
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Build the route handler
|
|
144
|
+
*/
|
|
145
|
+
_buildHandler(action) {
|
|
146
|
+
if (typeof action === "function") {
|
|
147
|
+
return action;
|
|
148
|
+
}
|
|
149
|
+
if (Array.isArray(action) && action.length === 2) {
|
|
150
|
+
const [controller, method] = action;
|
|
151
|
+
return ControllerBinder_1.default.handle(controller, method);
|
|
152
|
+
}
|
|
153
|
+
throw new Error('Action must be a function or array [Controller, "method"]');
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.Router = Router;
|
|
157
|
+
/**
|
|
158
|
+
* Static Route class for Laravel-like syntax
|
|
159
|
+
*/
|
|
160
|
+
class Route {
|
|
161
|
+
static create() {
|
|
162
|
+
return Router.create();
|
|
163
|
+
}
|
|
164
|
+
static middleware(...middleware) {
|
|
165
|
+
return this._router.middleware(...middleware);
|
|
166
|
+
}
|
|
167
|
+
static prefix(prefix) {
|
|
168
|
+
return this._router.prefix(prefix);
|
|
169
|
+
}
|
|
170
|
+
static group(callback) {
|
|
171
|
+
return this._router.group(callback);
|
|
172
|
+
}
|
|
173
|
+
static get(path, ...args) {
|
|
174
|
+
return this._router.get(path, ...args);
|
|
175
|
+
}
|
|
176
|
+
static post(path, ...args) {
|
|
177
|
+
return this._router.post(path, ...args);
|
|
178
|
+
}
|
|
179
|
+
static put(path, ...args) {
|
|
180
|
+
return this._router.put(path, ...args);
|
|
181
|
+
}
|
|
182
|
+
static delete(path, ...args) {
|
|
183
|
+
return this._router.delete(path, ...args);
|
|
184
|
+
}
|
|
185
|
+
static patch(path, ...args) {
|
|
186
|
+
return this._router.patch(path, ...args);
|
|
187
|
+
}
|
|
188
|
+
static options(path, ...args) {
|
|
189
|
+
return this._router.options(path, ...args);
|
|
190
|
+
}
|
|
191
|
+
static getRouter() {
|
|
192
|
+
return this._router.getRouter();
|
|
193
|
+
}
|
|
194
|
+
static mount(app, basePath = "/") {
|
|
195
|
+
return this._router.mount(app, basePath);
|
|
196
|
+
}
|
|
197
|
+
static reset() {
|
|
198
|
+
this._router = new Router();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
exports.Route = Route;
|
|
202
|
+
Route._router = new Router();
|
|
203
|
+
exports.default = Route;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<!--HEAD_CONTENT-->
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"><!--APP_CONTENT--></div>
|
|
10
|
+
<!--ARCANAJS_DATA_SCRIPT-->
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from "./server/ArcanaJSMiddleware";
|
|
2
|
+
export * from "./server/ArcanaJSServer";
|
|
3
|
+
export { default as ControllerBinder } from "./server/ControllerBinder";
|
|
4
|
+
export * from "./server/DynamicRouter";
|
|
5
|
+
export * from "./server/Router";
|
|
6
|
+
export { default as Route } from "./server/Router";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.Route = exports.ControllerBinder = void 0;
|
|
21
|
+
__exportStar(require("./server/ArcanaJSMiddleware"), exports);
|
|
22
|
+
__exportStar(require("./server/ArcanaJSServer"), exports);
|
|
23
|
+
var ControllerBinder_1 = require("./server/ControllerBinder");
|
|
24
|
+
Object.defineProperty(exports, "ControllerBinder", { enumerable: true, get: function () { return __importDefault(ControllerBinder_1).default; } });
|
|
25
|
+
__exportStar(require("./server/DynamicRouter"), exports);
|
|
26
|
+
__exportStar(require("./server/Router"), exports);
|
|
27
|
+
var Router_1 = require("./server/Router");
|
|
28
|
+
Object.defineProperty(exports, "Route", { enumerable: true, get: function () { return __importDefault(Router_1).default; } });
|