mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat(common): add method options to @sse decorator
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user