Skip to content

Commit 1621e38

Browse files
committed
feat: enhance logging functionality and improve type definitions in core components
1 parent bb189e4 commit 1621e38

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

src/core/Application.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { RouteMetadata, ServerOptions } from "@/types";
33
import { defaultOptions } from "@/utils/constants";
44
import { controllerMetadata, routeMetadata } from "@/utils/meta-data";
55
import { Container } from "@/di/Container";
6+
import { Logger } from "@/utils/logger";
67

78
export class Application {
89
private readonly app: Hono;
@@ -26,6 +27,7 @@ export class Application {
2627
const routes = routeMetadata.get(Controller) ?? [];
2728
const instance = Container.resolve(Controller);
2829

30+
Logger.success(`${Controller.name}: ${controllerPath}`);
2931
const controllerGroup = new Hono();
3032

3133
for (const route of routes) {
@@ -36,7 +38,7 @@ export class Application {
3638

3739
this.app.route(this.options.basePath ?? '', baseGroup);
3840
}
39-
private getControllerPath(Controller: any): string {
41+
private getControllerPath(Controller: Function): string {
4042
const path = controllerMetadata.get(Controller) ?? "";
4143
return path.replace(/\/$/, ""); // Normalize trailing slash
4244
}
@@ -49,6 +51,8 @@ export class Application {
4951
const { method, path, handler } = route;
5052
const normalizedPath = path === "/" ? "" : path;
5153

54+
Logger.info(`${method.toUpperCase()}: "${normalizedPath}"`);
55+
5256
group.on(
5357
method.toUpperCase(),
5458
normalizedPath,

src/decorators/Controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Container } from "@/di/Container";
22
import { controllerMetadata } from "@/utils/meta-data";
33

44
export function Controller(path: string) {
5-
return (target: object) => {
5+
return (target: Function) => {
66
controllerMetadata.set(target, path);
77
// Auto-register the controller as a singleton
88
Container.register(target, () => new (target as any)(), "singleton");

src/decorators/Inject.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export function Inject(token: unknown) {
1111
};
1212
}
1313

14+
export function Injectable() {
15+
return (constructor: new (...args: any[]) => any) => {
16+
Container.register(constructor, () => createInstance(constructor));
17+
};
18+
}
19+
20+
1421
export function createInstance<T>(constructor: new (...args: any[]) => T): T {
1522
const metadata = injectionMetadata.get(constructor) ?? [];
1623
const args = metadata.map((token) => {

src/di/Container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class Container {
1616
this.registry.set(token, { factory, lifetime });
1717
}
1818

19-
static resolve<T>(token: unknown): T {
19+
static resolve<T>(token: T): T {
2020
// Detect circular dependencies
2121
if (this.resolutionStack.includes(token)) {
2222
const cycleStart = this.resolutionStack.indexOf(token);

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type ServerOptions = {
22
basePath?: string;
3-
controllers?: any[];
3+
controllers?: Function[];
44
middlewares?: any[];
55
};
66

src/utils/logger.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// console.log("\x1b[32m Output with green text \x1b[0m")
2+
// console.log("\x1b[35m Output with magenta text \x1b[0m")
3+
// console.log("\x1b[34m Output with blue text \x1b[0m")
4+
// console.log("\x1b[36m Output with cyan text \x1b[0m")
5+
6+
// console.log("\x1b[41m Output with red background \x1b[0m")
7+
// console.log("\x1b[42m Output with green background \x1b[0m")
8+
// console.log("\x1b[43m Output with yellow background \x1b[0m")
9+
10+
11+
export class Logger {
12+
public static info(message: string): void {
13+
console.log(`\x1b[36m${message}\x1b[0m`);
14+
}
15+
16+
public static warn(message: string): void {
17+
console.log(`\x1b[33mWARN: ${message}\x1b[0m`);
18+
}
19+
20+
public static error(message: string): void {
21+
console.log(`\x1b[31mERROR: ${message}\x1b[0m`);
22+
}
23+
24+
public static success(message: string): void {
25+
console.log(`\x1b[34m${message}\x1b[0m`);
26+
}
27+
28+
public static debug(message: string): void {
29+
console.log(`\x1b[34mDEBUG: ${message}\x1b[0m`);
30+
}
31+
}

src/utils/meta-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RouteMetadata } from "@/types";
22

3-
export const controllerMetadata = new WeakMap<object, string>();
3+
export const controllerMetadata = new WeakMap<Function, string>();
44
export const routeMetadata = new WeakMap<object, RouteMetadata[]>();
55
export const injectionMetadata = new WeakMap<object, unknown[]>();

0 commit comments

Comments
 (0)