From 55c595e50ce1c4e247b7c2df5ae787a52de66cf5 Mon Sep 17 00:00:00 2001 From: Terry Casper Date: Sat, 20 Sep 2025 09:52:52 +0000 Subject: [PATCH] Refactor API key response mapping to use TypeScript interfaces for improved type safety --- src/routes/apiKeys.ts | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/routes/apiKeys.ts b/src/routes/apiKeys.ts index 916aeb5..619cf83 100644 --- a/src/routes/apiKeys.ts +++ b/src/routes/apiKeys.ts @@ -187,13 +187,32 @@ export default async function apiKeysRoute(fastify: FastifyInstance) { } // Return masked API keys with metadata - const apiKeys = user.apiKeys.map((key) => ({ - id: key.id, - key: maskApiKey(key.key), // Use our utility function to mask the key - createdAt: key.createdAt.toISOString(), - totalUsed: key.totalUsed, - status: key.status, - })); + interface ApiKeyResponse { + id: string; + key: string; + createdAt: string; + totalUsed?: number; + status: string; + } + + const apiKeys: ApiKeyResponse[] = user.apiKeys.map((key: { + id: string; + key: string; + createdAt: Date; + totalUsed?: number; + status: string; + }): ApiKeyResponse => { + const apiKeyObj: ApiKeyResponse = { + id: key.id, + key: maskApiKey(key.key), + createdAt: key.createdAt.toISOString(), + status: key.status, + }; + if (key.totalUsed !== undefined) { + apiKeyObj.totalUsed = key.totalUsed; + } + return apiKeyObj; + }); return reply.status(HttpStatusCode.OK).send({ success: true,