Skip to content

Commit fcfea5e

Browse files
feat(common): add method options to @sse decorator
1 parent ba16478 commit fcfea5e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/common/decorators/http/sse.decorator.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { RequestMethod } from '../../enums/request-method.enum';
66
*
77
* @publicApi
88
*/
9-
export function Sse(path?: string): MethodDecorator {
9+
export function Sse(
10+
path?: string,
11+
options: { [METHOD_METADATA]?: RequestMethod } = {
12+
[METHOD_METADATA]: RequestMethod.GET,
13+
},
14+
): MethodDecorator {
1015
return (
1116
target: object,
1217
key: string | symbol,
@@ -17,7 +22,7 @@ export function Sse(path?: string): MethodDecorator {
1722
Reflect.defineMetadata(PATH_METADATA, path, descriptor.value);
1823
Reflect.defineMetadata(
1924
METHOD_METADATA,
20-
RequestMethod.GET,
25+
options[METHOD_METADATA],
2126
descriptor.value,
2227
);
2328
Reflect.defineMetadata(SSE_METADATA, true, descriptor.value);

packages/common/test/decorators/sse.decorator.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ describe('@Sse', () => {
88
class Test {
99
@Sse(prefix)
1010
public static test() {}
11+
12+
@Sse(prefix, { method: RequestMethod.POST })
13+
public static testUsingOptions() {}
1114
}
1215

1316
it('should enhance method with expected http status code', () => {
@@ -20,4 +23,14 @@ describe('@Sse', () => {
2023
const metadata = Reflect.getMetadata(SSE_METADATA, Test.test);
2124
expect(metadata).to.be.eql(true);
2225
});
26+
it('should enhance method with expected http status code and method from options', () => {
27+
const path = Reflect.getMetadata(PATH_METADATA, Test.testUsingOptions);
28+
expect(path).to.be.eql('/prefix');
29+
30+
const method = Reflect.getMetadata(METHOD_METADATA, Test.testUsingOptions);
31+
expect(method).to.be.eql(RequestMethod.POST);
32+
33+
const metadata = Reflect.getMetadata(SSE_METADATA, Test.testUsingOptions);
34+
expect(metadata).to.be.eql(true);
35+
});
2336
});

0 commit comments

Comments
 (0)