Skip to content

Commit f5b8ba3

Browse files
authored
feat: add @Controller decorator (#23)
1 parent f19e7ed commit f5b8ba3

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

.changeset/chilly-carrots-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@auralis/core": minor
3+
---
4+
5+
feat: add `@Controller` decorator
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { Constructor } from "../utilities/constructor.util.ts";
2+
import { ensureControllerRef } from "../utilities/registry.util.ts";
3+
4+
/**
5+
* Controller decorator for defining a controller class.
6+
*
7+
* This decorator does not apply response headers.
8+
*
9+
* @param path The base path for the controller that usually starts with a slash.
10+
*
11+
* @see `RestController` if you want to create a RESTful controller.
12+
*/
13+
export function Controller(path: string): ClassDecorator {
14+
return function (target) {
15+
const controller = target as unknown as Constructor;
16+
17+
const controllerRef = ensureControllerRef(controller);
18+
19+
controllerRef.path = path;
20+
controllerRef.responseHeaders ??= {};
21+
22+
if (process.env.AURALIS_DEBUG) {
23+
console.debug("[Controller]:", {
24+
args: [target],
25+
controller,
26+
path,
27+
});
28+
}
29+
};
30+
}

packages/core/src/decorators/rest-controller.decorator.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import type { Constructor } from "../utilities/constructor.util.ts";
22
import { ensureControllerRef } from "../utilities/registry.util.ts";
33

4+
/**
5+
* REST controller decorator for defining a RESTful controller class.
6+
*
7+
* This decorator applies the `Content-Type: application/json; charset=utf-8` response header.
8+
*
9+
* @param path The base path for the controller that usually starts with a slash.
10+
*
11+
* @see `Controller` if you want to create a basic controller that does not apply response headers.
12+
*/
413
export function RestController(path: string): ClassDecorator {
514
return function (target) {
615
const controller = target as unknown as Constructor;

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type { Auralis } from "./auralis.ts";
2+
export { Controller } from "./decorators/controller.decorator.ts";
23
export { Delete } from "./decorators/delete.decorator.ts";
34
export { Get } from "./decorators/get.decorator.ts";
45
export { HttpMethod } from "./decorators/http-method.decorator.ts";

0 commit comments

Comments
 (0)