refactor: add type parameter to OCPP stack for request and response
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 16 Jul 2023 16:09:44 +0000 (18:09 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 16 Jul 2023 16:09:44 +0000 (18:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
src/charging-station/ocpp/OCPPIncomingRequestService.ts
src/charging-station/ocpp/OCPPResponseService.ts

index 6e58309546f82caeec20b64a5b54223855940a0e..eaa84704f77621c92050e5c0f23b3d2fe13672a3 100644 (file)
@@ -305,13 +305,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     ) => boolean;
   }
 
-  public async incomingRequestHandler(
+  public async incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     messageId: string,
     commandName: OCPP16IncomingRequestCommand,
-    commandPayload: JsonType,
+    commandPayload: ReqType,
   ): Promise<void> {
-    let response: JsonType;
+    let response: ResType;
     if (
       chargingStation.getOcppStrictCompliance() === true &&
       chargingStation.inPendingState() === true &&
@@ -341,10 +341,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         try {
           this.validatePayload(chargingStation, commandName, commandPayload);
           // Call the method to build the response
-          response = await this.incomingRequestHandlers.get(commandName)!(
+          response = (await this.incomingRequestHandlers.get(commandName)!(
             chargingStation,
             commandPayload,
-          );
+          )) as ResType;
         } catch (error) {
           // Log
           logger.error(
index 8ebce4f873d8e46e288bda971ff724b3d79a6e26..66f1f6b9fc4756dac7c90455f34fc11769ecdc5e 100644 (file)
@@ -314,11 +314,11 @@ export class OCPP16ResponseService extends OCPPResponseService {
     ) => boolean;
   }
 
-  public async responseHandler(
+  public async responseHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: OCPP16RequestCommand,
-    payload: JsonType,
-    requestPayload: JsonType,
+    payload: ResType,
+    requestPayload: ReqType,
   ): Promise<void> {
     if (
       chargingStation.isRegistered() === true ||
index 23ea8c815267794ce13b6f92d37b24b514cba9a3..bda78d54f3147f5eb621ae5108836c34eea8ee6d 100644 (file)
@@ -48,13 +48,13 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
     ) => boolean;
   }
 
-  public async incomingRequestHandler(
+  public async incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     messageId: string,
     commandName: OCPP20IncomingRequestCommand,
-    commandPayload: JsonType,
+    commandPayload: ReqType,
   ): Promise<void> {
-    let response: JsonType;
+    let response: ResType;
     if (
       chargingStation.getOcppStrictCompliance() === true &&
       chargingStation.inPendingState() === true &&
@@ -84,10 +84,10 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
         try {
           this.validatePayload(chargingStation, commandName, commandPayload);
           // Call the method to build the response
-          response = await this.incomingRequestHandlers.get(commandName)!(
+          response = (await this.incomingRequestHandlers.get(commandName)!(
             chargingStation,
             commandPayload,
-          );
+          )) as ResType;
         } catch (error) {
           // Log
           logger.error(
index 1eec885f9dc2fd1f3a7bb0043cd1f14479c8c6d2..4984b115e48a03d0315a83eeee28ae2c74e488cf 100644 (file)
@@ -87,11 +87,11 @@ export class OCPP20ResponseService extends OCPPResponseService {
     ) => boolean;
   }
 
-  public async responseHandler(
+  public async responseHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: OCPP20RequestCommand,
-    payload: JsonType,
-    requestPayload: JsonType,
+    payload: ResType,
+    requestPayload: ReqType,
   ): Promise<void> {
     if (
       chargingStation.isRegistered() === true ||
index 0ea55a63515f0f479a627299625c1703e5442c15..b7ab8ef66382b1ac0e799294eccca7f676480855 100644 (file)
@@ -33,11 +33,15 @@ export abstract class OCPPIncomingRequestService extends AsyncResource {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as (
+    this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as <
+      ReqType extends JsonType,
+      // eslint-disable-next-line @typescript-eslint/no-unused-vars
+      ResType extends JsonType,
+    >(
       chargingStation: ChargingStation,
       messageId: string,
       commandName: IncomingRequestCommand,
-      commandPayload: JsonType,
+      commandPayload: ReqType,
     ) => Promise<void>;
     this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as <
       T extends JsonType,
@@ -110,10 +114,11 @@ export abstract class OCPPIncomingRequestService extends AsyncResource {
     return OCPPConstants.OCPP_RESPONSE_REJECTED;
   }
 
-  public abstract incomingRequestHandler(
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
+  public abstract incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     messageId: string,
     commandName: IncomingRequestCommand,
-    commandPayload: JsonType,
+    commandPayload: ReqType,
   ): Promise<void>;
 }
index b720c0f136e6c7cf0fac74de9c39b4153ee2a069..7934500ed3ebd7612cfb05ad58b0c68699396dc1 100644 (file)
@@ -31,11 +31,14 @@ export abstract class OCPPResponseService {
       multipleOfPrecision: 2,
     });
     ajvFormats(this.ajv);
-    this.responseHandler = this.responseHandler.bind(this) as (
+    this.responseHandler = this.responseHandler.bind(this) as <
+      ReqType extends JsonType,
+      ResType extends JsonType,
+    >(
       chargingStation: ChargingStation,
       commandName: RequestCommand,
-      payload: JsonType,
-      requestPayload: JsonType,
+      payload: ResType,
+      requestPayload: ReqType,
     ) => Promise<void>;
     this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
       chargingStation: ChargingStation,
@@ -81,10 +84,10 @@ export abstract class OCPPResponseService {
     /* This is intentional */
   }
 
-  public abstract responseHandler(
+  public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand,
-    payload: JsonType,
-    requestPayload: JsonType,
+    payload: ResType,
+    requestPayload: ReqType,
   ): Promise<void>;
 }