Skip to content

Commit 3bb21b3

Browse files
jbl428imdudu1
andcommitted
feat: add request body decorator
Co-authored-by: imdudu1 <[email protected]>
1 parent dec06d9 commit 3bb21b3

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

lib/decorators/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export const HTTP_INTERFACE_METADATA = Symbol("HTTP_INTERFACE_METADATA");
22
export const HTTP_EXCHANGE_METADATA = Symbol("HTTP_EXCHANGE_METADATA");
33
export const PATH_VARIABLE_METADATA = Symbol("PATH_VARIABLE_METADATA");
44
export const REQUEST_PARAM_METADATA = Symbol("REQUEST_PARAM_METADATA");
5+
export const REQUEST_BODY_METADATA = Symbol("REQUEST_BODY_METADATA");
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, test, expect } from "vitest";
2+
import { REQUEST_BODY_METADATA } from "./constants";
3+
import {
4+
RequestBody,
5+
type RequestBodyMetadata,
6+
} from "./request-body.decorator";
7+
8+
describe("RequestBody", () => {
9+
test("should set request body metadata with empty key", () => {
10+
// given
11+
class TestService {
12+
request(@RequestBody() query: { foo: string }): string {
13+
return query.foo;
14+
}
15+
}
16+
17+
// when
18+
const result: RequestBodyMetadata = Reflect.getMetadata(
19+
REQUEST_BODY_METADATA,
20+
TestService.prototype,
21+
"request"
22+
);
23+
24+
// then
25+
expect(result).toHaveLength(1);
26+
expect(result.get(0)).toBeUndefined();
27+
});
28+
29+
test("should set request body metadata with key", () => {
30+
// given
31+
class TestService {
32+
request(@RequestBody("foo") bar: string): string {
33+
return bar;
34+
}
35+
}
36+
37+
// when
38+
const result: RequestBodyMetadata = Reflect.getMetadata(
39+
REQUEST_BODY_METADATA,
40+
TestService.prototype,
41+
"request"
42+
);
43+
44+
// then
45+
expect(result).toHaveLength(1);
46+
expect(result.get(0)).toBe("foo");
47+
});
48+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { REQUEST_BODY_METADATA } from "./constants";
2+
3+
export type RequestBodyMetadata = Map<number, string | undefined>;
4+
5+
export function RequestBody(key?: string): ParameterDecorator {
6+
return (target, propertyKey, parameterIndex) => {
7+
if (typeof propertyKey === "undefined") {
8+
return;
9+
}
10+
11+
const metadata =
12+
Reflect.getMetadata(REQUEST_BODY_METADATA, target, propertyKey) ??
13+
new Map();
14+
15+
metadata.set(parameterIndex, key);
16+
17+
Reflect.defineMetadata(
18+
REQUEST_BODY_METADATA,
19+
metadata,
20+
target,
21+
propertyKey
22+
);
23+
};
24+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"homepage": "https://github.com/r2don/nest-http-interface#readme",
2222
"main": "index.js",
2323
"scripts": {
24-
"test": "echo \"Error: no test specified\" && exit 1",
24+
"test": "vitest run",
2525
"prepare": "husky install"
2626
},
2727
"devDependencies": {

0 commit comments

Comments
 (0)