diff options
| author | Alex <zuedev@gmail.com> | 2025-03-23 11:58:36 +0000 |
|---|---|---|
| committer | Alex <zuedev@gmail.com> | 2025-03-23 11:58:36 +0000 |
| commit | 93a71aa6eee4e835a1830a9e7fc6f62f9980948c (patch) | |
| tree | 22855145a9d336954eebd8f0fac127d996cbf7a5 /source/library/router/index.js | |
| parent | 5301726732dee6e967b24a8f96176e74c5499144 (diff) | |
| download | zue.dev-93a71aa6eee4e835a1830a9e7fc6f62f9980948c.tar zue.dev-93a71aa6eee4e835a1830a9e7fc6f62f9980948c.tar.gz zue.dev-93a71aa6eee4e835a1830a9e7fc6f62f9980948c.tar.bz2 zue.dev-93a71aa6eee4e835a1830a9e7fc6f62f9980948c.tar.xz zue.dev-93a71aa6eee4e835a1830a9e7fc6f62f9980948c.zip | |
refactor(router): reorganize Router class into separate index file and remove legacy router.js
Diffstat (limited to 'source/library/router/index.js')
| -rw-r--r-- | source/library/router/index.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/source/library/router/index.js b/source/library/router/index.js new file mode 100644 index 0000000..d0914ff --- /dev/null +++ b/source/library/router/index.js @@ -0,0 +1,107 @@ +/* + Router class to handle routing of requests based on the request path. + + @param {Request} request - the incoming request object + @param {Environment} environment - the environment object + @param {Context} context - the context object + + @returns {Response} a new Response object +*/ +export default class Router { + constructor(request, environment, context) { + this.request = request; + this.environment = environment; + this.context = context; + this.routes = new Map(); // Use Map for faster lookups + this.parameters = {}; // Store extracted route parameters + } + + /* + Add a new route to the router. + + @param {string} path - the path to match + @param {function} handler - the handler function to call if the path matches + + @returns {Router} the router object + */ + add(path, handler) { + if (typeof path !== "string") { + throw new TypeError("Path must be a string"); + } + if (typeof handler !== "function") { + throw new TypeError("Handler must be a function"); + } + + const normalizedPath = path.replace(/\/+$/, ""); // Normalize path + const pathRegex = new RegExp( + "^" + + normalizedPath + .replace(/:[^/]+/g, "([^/]+)") // Convert :param to regex group + .replace(/\//g, "\\/") + + "$" + ); + this.routes.set(pathRegex, { handler, originalPath: normalizedPath }); + return this; + } + + /* + Helper function to respond with a JSON object. + + @param {object} body - the JSON object to respond with + + @returns {Response} a new Response object + */ + respond(body, status = 200) { + return new Response(JSON.stringify(body), { + status, + headers: { + "Access-Control-Allow-Origin": "*", + "Content-Type": "application/json", + }, + }); + } + + /* + Route the request to the appropriate handler based on the request path. + + @returns {Response} a new Response object + */ + route() { + try { + const url = new URL(this.request.url); + const normalizedPath = url.pathname.replace(/\/+$/, ""); // Remove trailing slashes + + for (const [pathRegex, { handler, originalPath }] of this.routes) { + const match = normalizedPath.match(pathRegex); + if (match) { + // Extract parameters + const paramNames = [...originalPath.matchAll(/:([^/]+)/g)].map( + (m) => m[1] + ); + this.parameters = paramNames.reduce((params, name, index) => { + params[name] = match[index + 1]; + return params; + }, {}); + + return handler(this.request, this.environment, this.context); + } + } + + // Return 404 if route not found + return this.respond( + { + error: `Route not found: ${normalizedPath}`, + }, + 404 + ); + } catch (error) { + // Handle unexpected errors + return this.respond( + { + error: `Internal Server Error: ${error.message}`, + }, + 500 + ); + } + } +} |
