@gati-framework/runtime
Version:
Gati runtime execution engine for running handler-based applications
159 lines • 3.8 kB
JavaScript
/**
* @module runtime/route-manager
* @description Route registration and matching
*/
import { parseRoute, normalizePath, extractParams } from './route-parser.js';
/**
* Route manager for registering and matching routes
*/
export class RouteManager {
routes = [];
constructor(_config = {}) {
// Config will be used in future for strict mode and case sensitivity
}
/**
* Register a new route
*
* @param method - HTTP method
* @param path - Route path pattern
* @param handler - Handler function
*
* @example
* ```typescript
* routeManager.register('GET', '/users/:id', getUserHandler);
* ```
*/
register(method, path, handler) {
const pattern = parseRoute(path);
const route = {
method,
path: pattern.path,
handler,
pattern,
};
this.routes.push(route);
}
/**
* Register a GET route
*/
get(path, handler) {
this.register('GET', path, handler);
}
/**
* Register a POST route
*/
post(path, handler) {
this.register('POST', path, handler);
}
/**
* Register a PUT route
*/
put(path, handler) {
this.register('PUT', path, handler);
}
/**
* Register a PATCH route
*/
patch(path, handler) {
this.register('PATCH', path, handler);
}
/**
* Register a DELETE route
*/
delete(path, handler) {
this.register('DELETE', path, handler);
}
/**
* Register a HEAD route
*/
head(path, handler) {
this.register('HEAD', path, handler);
}
/**
* Register an OPTIONS route
*/
options(path, handler) {
this.register('OPTIONS', path, handler);
}
/**
* Find a matching route for the given method and path
*
* @param method - HTTP method
* @param path - Request path
* @returns RouteMatch if found, null otherwise
*
* @example
* ```typescript
* const match = routeManager.match('GET', '/users/123');
* if (match) {
* console.log(match.params.id); // '123'
* }
* ```
*/
match(method, path) {
const normalizedPath = normalizePath(path);
for (const route of this.routes) {
// Check if method matches
if (route.method !== method) {
continue;
}
// Check if path matches
if (!route.pattern) {
continue;
}
const params = extractParams(normalizedPath, route.pattern);
if (params !== null) {
return {
route,
params,
};
}
}
return null;
}
/**
* Get all registered routes
*/
getRoutes() {
return [...this.routes];
}
/**
* Unregister a route
*/
unregister(method, path) {
const normalizedPath = normalizePath(path);
this.routes = this.routes.filter(route => !(route.method === method && route.path === normalizedPath));
}
/**
* Clear all registered routes
*/
clear() {
this.routes = [];
}
/**
* Get the number of registered routes
*/
size() {
return this.routes.length;
}
}
/**
* Create a new route manager instance
*
* @param config - Router configuration
* @returns New RouteManager instance
*
* @example
* ```typescript
* const router = createRouteManager({
* strict: true,
* caseSensitive: false
* });
*
* router.get('/users/:id', getUserHandler);
* ```
*/
export function createRouteManager(config) {
return new RouteManager(config);
}
//# sourceMappingURL=route-manager.js.map