Skip to content

Commit b13fb1b

Browse files
feat(core): introduce ResponseEntity, ResponseBody and produces support
Co-authored-by: Junie <junie@jetbrains.com>
1 parent db2f912 commit b13fb1b

37 files changed

Lines changed: 1014 additions & 200 deletions

src/controller/BaseBootApplication.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Provider,
1111
RouteMetadata
1212
} from "../domain/types";
13-
import { RequestMethod } from "../domain/enums";
13+
import { ContentMimeType, RequestMethod } from "../domain/enums";
1414
import {
1515
EventDispatcher,
1616
RequestFactory,
@@ -155,8 +155,11 @@ export abstract class BaseBootApplication {
155155
req: HttpRequest,
156156
status: number | undefined,
157157
headers: HttpHeaders | undefined,
158-
data: unknown
159-
): HttpResponse => this._responseBuilder.create(req, status, headers, data)
158+
data: unknown,
159+
produce?: ContentMimeType,
160+
isResponseBody?: boolean
161+
): HttpResponse =>
162+
this._responseBuilder.create(req, status, headers, data, produce, isResponseBody)
160163
);
161164

162165
return { request, response };
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { RESPONSE_BODY_METADATA } from "../../domain/constants";
2+
3+
/**
4+
* Decorator that tells the controller that the return object should be serialized
5+
* directly into the HTTP response body.
6+
*
7+
* In this framework, this is the default behavior for all controller methods,
8+
* but this decorator is provided for compatibility with Spring Boot patterns.
9+
*
10+
* @returns {MethodDecorator & ClassDecorator} A decorator.
11+
*
12+
* @example
13+
* ```TypeScript
14+
* import { ResponseBody, GetMapping, Controller } from "bootgs";
15+
*
16+
* @Controller()
17+
* class MyController {
18+
* @GetMapping("/data")
19+
* @ResponseBody
20+
* getData() {
21+
* return { message: "Hello" };
22+
* }
23+
* }
24+
* ```
25+
*/
26+
export function ResponseBody(): MethodDecorator & ClassDecorator {
27+
return (
28+
target: object,
29+
propertyKey?: string | symbol,
30+
descriptor?: TypedPropertyDescriptor<any>
31+
): void => {
32+
if (propertyKey) {
33+
if (descriptor && descriptor.value) {
34+
Reflect.defineMetadata(RESPONSE_BODY_METADATA, true, descriptor.value);
35+
} else {
36+
Reflect.defineMetadata(RESPONSE_BODY_METADATA, true, target, propertyKey);
37+
}
38+
} else {
39+
Reflect.defineMetadata(RESPONSE_BODY_METADATA, true, target);
40+
}
41+
};
42+
}

src/controller/decorators/RestController.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { HttpController } from "../../controller/decorators";
1+
import { HttpController } from "./HttpController";
2+
import { ResponseBody } from "./ResponseBody";
23

34
/**
45
* Decorator that marks a class as a REST controller.
56
*
7+
* @param {string} [path] - The base path for all routes in the controller.
8+
* @returns {ClassDecorator} A decorator.
9+
*
610
* @example
711
* ```TypeScript
812
* import { RestController, Get, Param } from "bootgs";
@@ -16,4 +20,9 @@ import { HttpController } from "../../controller/decorators";
1620
* }
1721
* ```
1822
*/
19-
export const RestController = HttpController;
23+
export function RestController(path?: string): ClassDecorator {
24+
return (target: object): void => {
25+
HttpController(path)(target as any);
26+
ResponseBody()(target as any);
27+
};
28+
}

src/controller/decorators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export * from "./ResponseStatus";
1616
export * from "./ControllerAdvice";
1717
export * from "./ExceptionHandler";
1818
export * from "./Value";
19+
export * from "./ResponseBody";

src/controller/decorators/routing/DeleteMapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RequestMethod } from "../../../domain/enums";
2-
import { createHttpDecorator } from "../../../repository";
2+
import { createHttpDecorator, HttpDecoratorOptions } from "../../../repository";
33

44
/**
55
* Route handler decorator for HTTP DELETE requests.
66
*
7-
* @param {string} [path] - Route path (optional).
7+
* @param {string | HttpDecoratorOptions} [options] - Route path or options (optional).
88
* @returns {MethodDecorator} A method decorator.
99
*
1010
* @example

src/controller/decorators/routing/GetMapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RequestMethod } from "../../../domain/enums";
2-
import { createHttpDecorator } from "../../../repository";
2+
import { createHttpDecorator, HttpDecoratorOptions } from "../../../repository";
33

44
/**
55
* Route handler decorator for HTTP GET requests.
66
*
7-
* @param {string} [path] - Route path (optional).
7+
* @param {string | HttpDecoratorOptions} [options] - Route path or options (optional).
88
* @returns {MethodDecorator} A method decorator.
99
*
1010
* @example

src/controller/decorators/routing/HeadMapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RequestMethod } from "../../../domain/enums";
2-
import { createHttpDecorator } from "../../../repository";
2+
import { createHttpDecorator, HttpDecoratorOptions } from "../../../repository";
33

44
/**
55
* Route handler decorator for HTTP HEAD requests.
66
*
7-
* @param {string} [path] - Route path (optional).
7+
* @param {string | HttpDecoratorOptions} [options] - Route path or options (optional).
88
* @returns {MethodDecorator} A method decorator.
99
*
1010
* @example

src/controller/decorators/routing/OptionsMapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RequestMethod } from "../../../domain/enums";
2-
import { createHttpDecorator } from "../../../repository";
2+
import { createHttpDecorator, HttpDecoratorOptions } from "../../../repository";
33

44
/**
55
* Route handler decorator for HTTP OPTIONS requests.
66
*
7-
* @param {string} [path] - Route path (optional).
7+
* @param {string | HttpDecoratorOptions} [options] - Route path or options (optional).
88
* @returns {MethodDecorator} A method decorator.
99
*
1010
* @example

src/controller/decorators/routing/PatchMapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RequestMethod } from "../../../domain/enums";
2-
import { createHttpDecorator } from "../../../repository";
2+
import { createHttpDecorator, HttpDecoratorOptions } from "../../../repository";
33

44
/**
55
* Route handler decorator for HTTP PATCH requests.
66
*
7-
* @param {string} [path] - Route path (optional).
7+
* @param {string | HttpDecoratorOptions} [options] - Route path or options (optional).
88
* @returns {MethodDecorator} A method decorator.
99
*
1010
* @example

src/controller/decorators/routing/PostMapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { RequestMethod } from "../../../domain/enums";
2-
import { createHttpDecorator } from "../../../repository";
2+
import { createHttpDecorator, HttpDecoratorOptions } from "../../../repository";
33

44
/**
55
* Route handler decorator for HTTP POST requests.
66
*
7-
* @param {string} [path] - Route path (optional).
7+
* @param {string | HttpDecoratorOptions} [options] - Route path or options (optional).
88
* @returns {MethodDecorator} A method decorator.
99
*
1010
* @example

0 commit comments

Comments
 (0)