build(deps): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIHttpServer.ts
index da347672df86af76f8c1ce314410161b33e01b89..b52df5721bafad4a4b9810ad68791d7b9ca8b68c 100644 (file)
@@ -1,11 +1,8 @@
-import type { IncomingMessage, RequestListener, ServerResponse } from 'http';
+import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http';
 
 import { StatusCodes } from 'http-status-codes';
 
-import { AbstractUIServer } from './AbstractUIServer';
-import { UIServerUtils } from './UIServerUtils';
-import BaseError from '../../exception/BaseError';
-import type { UIServerConfiguration } from '../../types/ConfigurationData';
+import { BaseError } from '../../exception';
 import {
   type ProcedureName,
   type Protocol,
@@ -14,13 +11,14 @@ import {
   type ProtocolVersion,
   type RequestPayload,
   ResponseStatus,
-} from '../../types/UIProtocol';
-import logger from '../../utils/Logger';
-import Utils from '../../utils/Utils';
+  type UIServerConfiguration,
+} from '../../types';
+import { Utils, logger } from '../../utils';
+import { AbstractUIServer, UIServerUtils } from '../internal';
 
 const moduleName = 'UIHttpServer';
 
-export default class UIHttpServer extends AbstractUIServer {
+export class UIHttpServer extends AbstractUIServer {
   public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
     super(uiServerConfiguration);
   }
@@ -60,15 +58,17 @@ export default class UIHttpServer extends AbstractUIServer {
     }
   }
 
-  public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string {
+  public logPrefix = (modName?: string, methodName?: string, prefixSuffix?: string): string => {
     const logMsgPrefix = prefixSuffix ? `UI HTTP Server ${prefixSuffix}` : 'UI HTTP Server';
     const logMsg =
-      modName && methodName ? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
+      Utils.isNotEmptyString(modName) && Utils.isNotEmptyString(methodName)
+        ? ` ${logMsgPrefix} | ${modName}.${methodName}:`
+        : ` ${logMsgPrefix} |`;
     return Utils.logPrefix(logMsg);
-  }
+  };
 
   private requestListener(req: IncomingMessage, res: ServerResponse): void {
-    this.authenticate(req, err => {
+    this.authenticate(req, (err) => {
       if (err) {
         res
           .writeHead(StatusCodes.UNAUTHORIZED, {
@@ -94,7 +94,7 @@ export default class UIHttpServer extends AbstractUIServer {
         throw new BaseError(`Unsupported UI protocol version: '${fullProtocol}'`);
       }
       this.registerProtocolVersionUIService(version);
-      req.on('error', error => {
+      req.on('error', (error) => {
         logger.error(
           `${this.logPrefix(moduleName, 'requestListener.req.onerror')} Error on HTTP request:`,
           error
@@ -103,14 +103,14 @@ export default class UIHttpServer extends AbstractUIServer {
       if (req.method === 'POST') {
         const bodyBuffer = [];
         req
-          .on('data', chunk => {
+          .on('data', (chunk) => {
             bodyBuffer.push(chunk);
           })
           .on('end', () => {
             const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
             this.uiServices
               .get(version)
-              .requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {}))
+              ?.requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {}))
               .catch(() => {
                 /* Error caught by AbstractUIService */
               });