Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/common/decorators/http/sse.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions packages/common/test/decorators/sse.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
});
});