feat(common): add method options to @sse decorator

This commit is contained in:
Tomer Steinberg
2025-10-31 17:31:02 +02:00
parent ba1647821b
commit fcfea5ed2a
2 changed files with 20 additions and 2 deletions

View File

@@ -6,7 +6,12 @@ import { RequestMethod } from '../../enums/request-method.enum';
*
* @publicApi
*/
export function Sse(path?: string): MethodDecorator {
export function Sse(
path?: string,
options: { [METHOD_METADATA]?: RequestMethod } = {
[METHOD_METADATA]: RequestMethod.GET,
},
): MethodDecorator {
return (
target: object,
key: string | symbol,
@@ -17,7 +22,7 @@ export function Sse(path?: string): MethodDecorator {
Reflect.defineMetadata(PATH_METADATA, path, descriptor.value);
Reflect.defineMetadata(
METHOD_METADATA,
RequestMethod.GET,
options[METHOD_METADATA],
descriptor.value,
);
Reflect.defineMetadata(SSE_METADATA, true, descriptor.value);

View File

@@ -8,6 +8,9 @@ describe('@Sse', () => {
class Test {
@Sse(prefix)
public static test() {}
@Sse(prefix, { method: RequestMethod.POST })
public static testUsingOptions() {}
}
it('should enhance method with expected http status code', () => {
@@ -20,4 +23,14 @@ describe('@Sse', () => {
const metadata = Reflect.getMetadata(SSE_METADATA, Test.test);
expect(metadata).to.be.eql(true);
});
it('should enhance method with expected http status code and method from options', () => {
const path = Reflect.getMetadata(PATH_METADATA, Test.testUsingOptions);
expect(path).to.be.eql('/prefix');
const method = Reflect.getMetadata(METHOD_METADATA, Test.testUsingOptions);
expect(method).to.be.eql(RequestMethod.POST);
const metadata = Reflect.getMetadata(SSE_METADATA, Test.testUsingOptions);
expect(metadata).to.be.eql(true);
});
});