From 54847bda0cb7be06f6b40863c520ff11268ea75b Mon Sep 17 00:00:00 2001 From: Fredrik Johansen Date: Mon, 3 Nov 2025 12:52:43 +0100 Subject: [PATCH] fix(plugin): fixes adding hooks to array when already existing hooks Fixes bug where plugin wouldn't include already registered hooks. This fix makes sure if we have other hooks, it gets included correctly in the array. --- src/index.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7baf2f1..0d6d46e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,19 +76,18 @@ export const dynamodbCache: FastifyPluginAsync = ( routeOptions.config.cache.ttlSeconds || opts.defaultTTLSeconds, // Defaults to "defaultTTLSeconds" which is specified when registering the plugin }); - if (!Array.isArray(routeOptions.onRequest)) { - if (routeOptions.onRequest) { - routeOptions.onRequest = [routeOptions.onRequest]; - } - + if (Array.isArray(routeOptions.onRequest)) { + routeOptions.onRequest = [ + ...(routeOptions.onRequest || []), + onRequestHook, + ]; + } else { routeOptions.onRequest = [onRequestHook]; } - if (!Array.isArray(routeOptions.onSend)) { - if (routeOptions.onSend) { - routeOptions.onSend = [routeOptions.onSend]; - } - + if (Array.isArray(routeOptions.onSend)) { + routeOptions.onSend = [...(routeOptions.onSend || []), onSendHook]; + } else { routeOptions.onSend = [onSendHook]; } }