import {MessageType} from '../../utils/Types';
export interface Request<BODY> {
id: number;
command: string;
type: MessageType;
body: BODY;
};
export interface Response<BODY> {
id: number;
command: string;
type: MessageType;
success: boolean;
body?: BODY;
errMessage?: string;
};
export class RequestResponseType<REQ_BODY, RESP_BODY> {
command: string;
constructor(command: string) {
this.command = command;
}
public createRequest(body: REQ_BODY, id: number): Request<REQ_BODY> {
return {
body: body, command: this.command, id: id, type: 'request',
};
}
public createSuccessResponse(body: RESP_BODY, id: number): Response<RESP_BODY> {
return {
body: body, command: this.command, errMessage: undefined, id: id, success: true, type: 'response',
};
}
public createErrorResponse(errMsg: string, id: number): Response<RESP_BODY> {
return {
body: undefined, command: this.command, errMessage: errMsg, id: id, success: false, type: 'response',
};
}
}
export class RequestHandler<REQ_BODY, RESP_BODY> {
type: RequestResponseType<REQ_BODY, RESP_BODY>;
handler: (req: REQ_BODY) => Promise<RESP_BODY>;
constructor(type: RequestResponseType<REQ_BODY, RESP_BODY>, handler: (req: REQ_BODY) => Promise<RESP_BODY>) {
this.type = type;
this.handler = handler;
}
}