Skip to content

Commit aed0771

Browse files
committed
feat(ws-manual-ack): add explore ack param
1 parent ad38a4b commit aed0771

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

packages/common/enums/route-paramtypes.enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export enum RouteParamtypes {
1212
HOST = 10,
1313
IP = 11,
1414
RAW_BODY = 12,
15+
ACK = 13,
1516
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { WsParamtype } from '../enums/ws-paramtype.enum';
22

33
export const DEFAULT_CALLBACK_METADATA = {
4+
[`${WsParamtype.ACK}:2`]: { index: 2, data: undefined, pipes: [] },
45
[`${WsParamtype.PAYLOAD}:1`]: { index: 1, data: undefined, pipes: [] },
56
[`${WsParamtype.SOCKET}:0`]: { index: 0, data: undefined, pipes: [] },
67
};

packages/websockets/enums/ws-paramtype.enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { RouteParamtypes } from '@nestjs/common/enums/route-paramtypes.enum';
33
export enum WsParamtype {
44
SOCKET = RouteParamtypes.REQUEST,
55
PAYLOAD = RouteParamtypes.BODY,
6+
ACK = RouteParamtypes.ACK,
67
}

packages/websockets/gateway-metadata-explorer.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import {
55
GATEWAY_SERVER_METADATA,
66
MESSAGE_MAPPING_METADATA,
77
MESSAGE_METADATA,
8+
PARAM_ARGS_METADATA,
89
} from './constants';
910
import { NestGateway } from './interfaces/nest-gateway.interface';
11+
import { ParamsMetadata } from '@nestjs/core/helpers/interfaces';
12+
import { WsParamtype } from './enums/ws-paramtype.enum';
1013

1114
export interface MessageMappingProperties {
1215
message: any;
1316
methodName: string;
1417
callback: (...args: any[]) => Observable<any> | Promise<any>;
18+
isAckHandledManually: boolean;
1519
}
1620

1721
export class GatewayMetadataExplorer {
@@ -38,13 +42,37 @@ export class GatewayMetadataExplorer {
3842
return null;
3943
}
4044
const message = Reflect.getMetadata(MESSAGE_METADATA, callback);
45+
const isAckHandledManually = this.hasAckDecorator(
46+
instancePrototype,
47+
methodName,
48+
);
49+
4150
return {
4251
callback,
4352
message,
4453
methodName,
54+
isAckHandledManually,
4555
};
4656
}
4757

58+
private hasAckDecorator(
59+
instancePrototype: object,
60+
methodName: string,
61+
): boolean {
62+
const paramsMetadata: ParamsMetadata = Reflect.getMetadata(
63+
PARAM_ARGS_METADATA,
64+
instancePrototype.constructor,
65+
methodName,
66+
);
67+
68+
if (!paramsMetadata) {
69+
return false;
70+
}
71+
72+
const params = Object.values(paramsMetadata);
73+
return params.some((param: any) => param.type === WsParamtype.ACK);
74+
}
75+
4876
public *scanForServerHooks(instance: NestGateway): IterableIterator<string> {
4977
for (const propertyKey in instance) {
5078
if (isFunction(propertyKey)) {

0 commit comments

Comments
 (0)