diff options
| author | Alex <zuedev@gmail.com> | 2025-03-23 11:28:19 +0000 |
|---|---|---|
| committer | Alex <zuedev@gmail.com> | 2025-03-23 11:28:19 +0000 |
| commit | 64a611d0cac30ee0c79c1c8724c1996da114d8bf (patch) | |
| tree | 19a2233edd82238811919652a05a4da3926a4c60 | |
| parent | d817a4dfe59cdf778454396a3f938ee4af95981c (diff) | |
| download | zue.dev-64a611d0cac30ee0c79c1c8724c1996da114d8bf.tar zue.dev-64a611d0cac30ee0c79c1c8724c1996da114d8bf.tar.gz zue.dev-64a611d0cac30ee0c79c1c8724c1996da114d8bf.tar.bz2 zue.dev-64a611d0cac30ee0c79c1c8724c1996da114d8bf.tar.xz zue.dev-64a611d0cac30ee0c79c1c8724c1996da114d8bf.zip | |
feat(router): improve Router class with enhanced error handling and path normalization
- Added error handling for unexpected exceptions in the `route` method, returning a 500 response with a descriptive error message.
- Normalized paths in the `route` method to ensure consistent matching by removing trailing slashes.
- Improved the 404 response to include the unmatched path for better debugging.
- Enhanced code comments for clarity and maintainability.
| -rw-r--r-- | source/library/router.js | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/source/library/router.js b/source/library/router.js index 7ca0917..821f84a 100644 --- a/source/library/router.js +++ b/source/library/router.js @@ -12,7 +12,7 @@ export default class Router { this.request = request; this.environment = environment; this.context = context; - this.routes = []; + this.routes = new Map(); // Use Map for faster lookups } /* @@ -24,21 +24,28 @@ export default class Router { @returns {Router} the router object */ add(path, handler) { - return this.routes.push({ - 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 + this.routes.set(normalizedPath, handler); + return this; } /* - Helper function to respond with a JSON object + 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) { + respond(body, status = 200) { return new Response(JSON.stringify(body), { + status, headers: { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json", @@ -50,30 +57,33 @@ export default class Router { 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 - // Use a Map for faster lookups - const route = this.routes.find((r) => r.path === normalizedPath); + const handler = this.routes.get(normalizedPath); - if (route) { - return route.handler(this.request, this.environment, this.context); + if (handler) { + return handler(this.request, this.environment, this.context); } // Return 404 if route not found - return this.respond({ - error: `Route not found: ${normalizedPath}`, - status: 404, - }); + return this.respond( + { + error: `Route not found: ${normalizedPath}`, + }, + 404 + ); } catch (error) { // Handle unexpected errors - return this.respond({ - error: `Internal Server Error: ${error.message}`, - status: 500, - }); + return this.respond( + { + error: `Internal Server Error: ${error.message}`, + }, + 500 + ); } } } |
