Skip to content

Commit

Permalink
feat(route manager): added Route Manager, Fixed async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yldrmzffr committed Oct 30, 2023
1 parent ad2eaea commit 44519ae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
43 changes: 21 additions & 22 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,42 @@ import {HttpException} from './exceptions/http.exception';

import {Request, Route} from './interfaces';
import {RequestMethod, Response, RouteHandler} from './types';
import {getRequestBody, parseQueryParams, removeSlash} from './utils';
import {getRequestBody, parseQueryParams} from './utils';
import {MuzuException} from './exceptions/muzu.exception';
import {NotFoundException} from './exceptions/not-found.exception';
import {BadRequestException} from './exceptions/bad-request.exception';
import RouteManager from './route-manager';

export {Request, Response, RouteHandler, HttpException};

export class MuzuServer {
private readonly routes: Route[];
public readonly server: Server;
public readonly routeManager: RouteManager;

constructor() {
this.routes = [];
this.routeManager = new RouteManager();
this.server = createServer(this.handleRequest.bind(this));
}

public addRoute = (route: Route): void => {
this.routes.push(route);
};

public addRoutes = (routes: Route[]): void => {
this.routes.push(...routes);
};
public listen(port: number, callback?: () => void): void {
console.log('🚀 Server is listening on port', port);
console.log('📡 Routes', this.routes);
console.log('📡 Routes', this.routeManager.getRoutes());
this.server.listen(port, callback);
}

public stop(callback?: () => void): void {
this.server.close(callback);
}

private sendResponse(res: Response, statusCode: number, body: Object): void {
private async sendResponse(
res: Response,
statusCode: number,
body: Object
): Promise<void> {
if (body instanceof Promise) {
body = await body;
}

res.writeHead(statusCode, {'Content-Type': 'application/json'});
res.end(JSON.stringify(body));
console.log('📤 Response', {statusCode, body});
Expand All @@ -52,10 +54,7 @@ export class MuzuServer {

req.params = parseQueryParams(url!);

const route = this.routes.find(
req =>
removeSlash(req.url) === removeSlash(path) && req.method === method
);
const route = await this.routeManager.find(path, method);

if (!route) {
throw new NotFoundException(`Route ${method} ${path} not found`, {
Expand Down Expand Up @@ -129,29 +128,29 @@ export class MuzuServer {
} as Route;
});

this.addRoutes(routers.filter(Boolean) as Route[]);
this.routeManager.addRoutes(routers.filter(Boolean) as Route[]);

return target;
};
};

Get = (url: string) => {
Get = (url = '/') => {
return this.HttpMethod(RequestMethod.GET, url);
};

Post = (url: string) => {
Post = (url = '/') => {
return this.HttpMethod(RequestMethod.POST, url);
};

Delete = (url: string) => {
Delete = (url = '/') => {
return this.HttpMethod(RequestMethod.DELETE, url);
};

Put = (url: string) => {
Put = (url = '/') => {
return this.HttpMethod(RequestMethod.PUT, url);
};

Patch = (url: string) => {
Patch = (url = '/') => {
return this.HttpMethod(RequestMethod.PATCH, url);
};
}
30 changes: 30 additions & 0 deletions lib/route-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Route} from './interfaces';
import {removeSlash} from './utils';

export default class RouteManager {
public routes: Route[];
constructor() {
this.routes = [];
}

public addRoute(route: Route): void {
this.routes.push(route);
}

public addRoutes(routes: Route[]): void {
this.routes.push(...routes);
}

public async getRoutes(): Promise<Route[]> {
return this.routes;
}

public async find(
url: string,
method: string | undefined
): Promise<Route | undefined> {
return this.routes.find(
req => removeSlash(req.url) === removeSlash(url) && req.method === method
);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "muzu",
"version": "0.0.4",
"version": "0.0.5",
"description": "",
"main": "build/lib/index.js",
"types": "build/lib/index.d.ts",
Expand Down

0 comments on commit 44519ae

Please sign in to comment.