Skip to content

Commit

Permalink
Fix Fastify DeprecationWarning about reply.getResponseTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Jul 6, 2024
1 parent 8fdadc7 commit 6d4a557
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/fastify/plugin.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -82,7 +87,7 @@ const apitallyPlugin: FastifyPluginAsync<ApitallyConfig> = async (
method: request.method,
path: path,
statusCode: reply.statusCode,
responseTime: reply.getResponseTime(),
responseTime: getResponseTime(reply),
requestSize: requestSize,
responseSize: responseSize,
});
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6d4a557

Please sign in to comment.