From ae9f9cd6a68dc0fd21f55a31693d67179646e40d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 27 Mar 2026 13:55:08 +0100 Subject: [PATCH] fix: type buildRejected reasonCode as ReasonCodeEnumType instead of string MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Eliminates the pointless enum→string→enum round-trip: the callback parameter is now typed as ReasonCodeEnumType directly, removing the as-string casts at call sites and the reverse keyof-typeof reconversion in callers. --- .../ocpp/2.0/OCPP20IncomingRequestService.ts | 4 ++-- src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts | 10 +++++----- .../OCPP20ServiceUtils-enforceMessageLimits.test.ts | 9 ++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index 7871b9f2..a7739c36 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -586,7 +586,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { attributeStatusInfo: { additionalInfo: reason.info, - reasonCode: ReasonCodeEnumType[reason.reasonCode as keyof typeof ReasonCodeEnumType], + reasonCode: reason.reasonCode, }, attributeType: v.attributeType, component: v.component, @@ -657,7 +657,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { attributeStatusInfo: { additionalInfo: reason.info, - reasonCode: ReasonCodeEnumType[reason.reasonCode as keyof typeof ReasonCodeEnumType], + reasonCode: reason.reasonCode, }, attributeType: v.attributeType ?? AttributeEnumType.Actual, component: v.component, diff --git a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts index aa778e43..a29dc1da 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ServiceUtils.ts @@ -190,14 +190,14 @@ export class OCPP20ServiceUtils extends OCPPServiceUtils { data: T[], itemsLimit: number, bytesLimit: number, - buildRejected: (item: T, reason: { info: string; reasonCode: string }) => unknown, + buildRejected: (item: T, reason: { info: string; reasonCode: ReasonCodeEnumType }) => unknown, logger: { debug: (...args: unknown[]) => void } ): { rejected: boolean; results: unknown[] } { if (itemsLimit > 0 && data.length > itemsLimit) { const results = data.map(d => buildRejected(d, { info: `ItemsPerMessage limit ${itemsLimit.toString()} exceeded (${data.length.toString()} requested)`, - reasonCode: ReasonCodeEnumType.TooManyElements as string, + reasonCode: ReasonCodeEnumType.TooManyElements, }) ) logger.debug( @@ -211,7 +211,7 @@ export class OCPP20ServiceUtils extends OCPPServiceUtils { const results = data.map(d => buildRejected(d, { info: `BytesPerMessage limit ${bytesLimit.toString()} exceeded (estimated ${estimatedSize.toString()} bytes)`, - reasonCode: ReasonCodeEnumType.TooLargeElement as string, + reasonCode: ReasonCodeEnumType.TooLargeElement, }) ) logger.debug( @@ -232,7 +232,7 @@ export class OCPP20ServiceUtils extends OCPPServiceUtils { originalData: T[], currentResults: unknown[], bytesLimit: number, - buildRejected: (item: T, reason: { info: string; reasonCode: string }) => unknown, + buildRejected: (item: T, reason: { info: string; reasonCode: ReasonCodeEnumType }) => unknown, logger: { debug: (...args: unknown[]) => void } ): unknown[] { if (bytesLimit > 0) { @@ -242,7 +242,7 @@ export class OCPP20ServiceUtils extends OCPPServiceUtils { const results = originalData.map(d => buildRejected(d, { info: `BytesPerMessage limit ${bytesLimit.toString()} exceeded (actual ${actualSize.toString()} bytes)`, - reasonCode: ReasonCodeEnumType.TooLargeElement as string, + reasonCode: ReasonCodeEnumType.TooLargeElement, }) ) logger.debug( diff --git a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-enforceMessageLimits.test.ts b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-enforceMessageLimits.test.ts index 5b91c733..22650077 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-enforceMessageLimits.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-enforceMessageLimits.test.ts @@ -18,7 +18,7 @@ interface MockLogger { interface RejectedResult { info: string original: TestItem - reasonCode: string + reasonCode: ReasonCodeEnumType } interface TestItem { @@ -58,7 +58,10 @@ function makeMockStation () { /** @returns A builder function that creates rejected result objects */ function makeRejectedBuilder () { - return (item: TestItem, reason: { info: string; reasonCode: string }): RejectedResult => ({ + return ( + item: TestItem, + reason: { info: string; reasonCode: ReasonCodeEnumType } + ): RejectedResult => ({ info: reason.info, original: item, reasonCode: reason.reasonCode, @@ -361,7 +364,7 @@ await describe('OCPP20ServiceUtils.enforceMessageLimits', async () => { const station = makeMockStation() const logger = makeMockLogger() const item = makeItem('WebSocketPingInterval', 'xyz') - const capturedReasons: { info: string; reasonCode: string }[] = [] + const capturedReasons: { info: string; reasonCode: ReasonCodeEnumType }[] = [] OCPP20ServiceUtils.enforceMessageLimits( station, -- 2.53.0