From 6d4a557430f415135112e4c237d4a214d1e54f57 Mon Sep 17 00:00:00 2001 From: Simon Gurcke Date: Sat, 6 Jul 2024 17:44:55 +1000 Subject: [PATCH] Fix Fastify DeprecationWarning about reply.getResponseTime() --- src/fastify/plugin.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/fastify/plugin.ts b/src/fastify/plugin.ts index c996b73..7a05f96 100644 --- a/src/fastify/plugin.ts +++ b/src/fastify/plugin.ts @@ -1,4 +1,9 @@ -import type { FastifyError, FastifyPluginAsync, FastifyRequest } from "fastify"; +import type { + FastifyError, + FastifyPluginAsync, + FastifyReply, + FastifyRequest, +} from "fastify"; import fp from "fastify-plugin"; import { ApitallyClient } from "../common/client.js"; @@ -82,7 +87,7 @@ const apitallyPlugin: FastifyPluginAsync = async ( method: request.method, path: path, statusCode: reply.statusCode, - responseTime: reply.getResponseTime(), + responseTime: getResponseTime(reply), requestSize: requestSize, responseSize: responseSize, }); @@ -149,6 +154,15 @@ const getConsumer = (request: FastifyRequest) => { return null; }; +const getResponseTime = (reply: FastifyReply) => { + if (reply.elapsedTime !== undefined) { + return reply.elapsedTime; + } else if (reply.getResponseTime !== undefined) { + return reply.getResponseTime(); + } + return 0; +}; + const extractAjvErrors = (message: string): ValidationError[] => { const regex = /(?<=^|, )((?:headers|params|query|querystring|body)[/.][^ ]+)(?= )/g;