refactor: null -> undefined where appropriate
[e-mobility-charging-stations-simulator.git] / ui / web / src / composables / UIClient.ts
index ccd16c215565855d99ec0fa400bb7da0f8dd89bc..bbb4a4499b233a6aead4703eaf1b3de93004e32e 100644 (file)
@@ -14,7 +14,7 @@ type ResponseHandler = {
   reject: (reason?: unknown) => void;
 };
 
-export default class UIClient {
+export class UIClient {
   private static instance: UIClient | null = null;
 
   private ws!: WebSocket;
@@ -71,7 +71,7 @@ export default class UIClient {
   public async startTransaction(
     hashId: string,
     connectorId: number,
-    idTag: string | undefined
+    idTag: string | undefined,
   ): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.START_TRANSACTION, {
       hashIds: [hashId],
@@ -82,7 +82,7 @@ export default class UIClient {
 
   public async stopTransaction(
     hashId: string,
-    transactionId: number | undefined
+    transactionId: number | undefined,
   ): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.STOP_TRANSACTION, {
       hashIds: [hashId],
@@ -92,7 +92,7 @@ export default class UIClient {
 
   public async startAutomaticTransactionGenerator(
     hashId: string,
-    connectorId: number
+    connectorId: number,
   ): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR, {
       hashIds: [hashId],
@@ -102,7 +102,7 @@ export default class UIClient {
 
   public async stopAutomaticTransactionGenerator(
     hashId: string,
-    connectorId: number
+    connectorId: number,
   ): Promise<ResponsePayload> {
     return this.sendRequest(ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR, {
       hashIds: [hashId],
@@ -113,7 +113,7 @@ export default class UIClient {
   private openWS(): void {
     this.ws = new WebSocket(
       `ws://${config.uiServer.host}:${config.uiServer.port}`,
-      config.uiServer.protocol
+      config.uiServer.protocol,
     );
     this.ws.onmessage = this.responseHandler.bind(this);
     this.ws.onerror = (errorEvent) => {
@@ -128,7 +128,7 @@ export default class UIClient {
     id: string,
     procedureName: ProcedureName,
     resolve: (value: ResponsePayload | PromiseLike<ResponsePayload>) => void,
-    reject: (reason?: unknown) => void
+    reject: (reason?: unknown) => void,
   ): void {
     this.responseHandlers.set(id, { procedureName, resolve, reject });
   }
@@ -143,11 +143,11 @@ export default class UIClient {
 
   private async sendRequest(
     command: ProcedureName,
-    data: RequestPayload
+    data: RequestPayload,
   ): Promise<ResponsePayload> {
     let uuid: string;
     return promiseWithTimeout(
-      new Promise((resolve, reject) => {
+      new Promise<ResponsePayload>((resolve, reject) => {
         uuid = crypto.randomUUID();
         const msg = JSON.stringify([uuid, command, data]);
 
@@ -166,7 +166,7 @@ export default class UIClient {
       Error(`Send request '${command}' message timeout`),
       () => {
         this.responseHandlers.delete(uuid);
-      }
+      },
     );
   }
 
@@ -174,7 +174,7 @@ export default class UIClient {
     const response = JSON.parse(messageEvent.data) as ProtocolResponse;
 
     if (Array.isArray(response) === false) {
-      throw new Error(`Response not an array: ${JSON.stringify(response, null, 2)}`);
+      throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`);
     }
 
     const [uuid, responsePayload] = response;
@@ -192,7 +192,7 @@ export default class UIClient {
       }
       this.deleteResponseHandler(uuid);
     } else {
-      throw new Error(`Not a response to a request: ${JSON.stringify(response, null, 2)}`);
+      throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`);
     }
   }
 }