From b029800b92bf60e26936b3e8bc67da4e83f55318 Mon Sep 17 00:00:00 2001 From: David Mears Date: Mon, 2 Sep 2024 16:59:21 +0100 Subject: [PATCH] Refactor defineEventHandler wrapping This was to avoid having a handler fed into a another defineEventHandler which would wrap the first EventHandler in a second EventHandler. Although the docs pointed us in the direction of doing such a double-wrapping, we found this way to avoid doing so. A downside of this is that endpoints are forced to use a specific function (e.g. 'defineEventHandler' or in this case 'defineCachedEventHandler') since they all route through the wrapper's choice of function - this means if we wanted to use e.g. defineLazyEventHandler from h3 in some endpoint, it (probably?) couldn't use our custom wrapper, unless we introduce some switch statement that lets endpoints specify which EventHandler wrapping function to use. I am aware of the following wrapping function choices: - defineEventHandler - defineCachedEventHandler - defineLazyEventHandler --- server/api/metadata.get.ts | 14 +++++------ server/api/versions.get.ts | 9 ++++--- server/utils/defineEventHandlerWithErrors.ts | 20 ---------------- server/utils/defineRApiEventHandler.ts | 25 ++++++++++++++++++++ 4 files changed, 36 insertions(+), 32 deletions(-) delete mode 100644 server/utils/defineEventHandlerWithErrors.ts create mode 100644 server/utils/defineRApiEventHandler.ts diff --git a/server/api/metadata.get.ts b/server/api/metadata.get.ts index 09a17b26..61b8f5ec 100644 --- a/server/api/metadata.get.ts +++ b/server/api/metadata.get.ts @@ -1,13 +1,13 @@ import { getMetaData } from "@/server/handlers/metadata"; -import { defineEventHandlerWithErrors } from "@/server/utils/defineEventHandlerWithErrors"; +import { defineRApiEventHandler } from "~/server/utils/defineRApiEventHandler"; import type { MetaDataResponse } from "@/types/daedalusApiResponseTypes"; -export default defineEventHandlerWithErrors( - // TODO: Consider cacheing this server-side https://nitro.unjs.io/guide/cache - defineEventHandler(async (event): Promise => { +export default defineRApiEventHandler( + async (event): Promise => { // Delegate to getMetaData so that the logic can be unit-tested. - const versionDataResponse = await getMetaData(event); + const metaDataResponse = await getMetaData(event); - return versionDataResponse; - }), + return metaDataResponse; + }, + { maxAge: 60 }, ); diff --git a/server/api/versions.get.ts b/server/api/versions.get.ts index 389d9285..bcb2bcc5 100644 --- a/server/api/versions.get.ts +++ b/server/api/versions.get.ts @@ -1,13 +1,12 @@ import { getVersionData } from "@/server/handlers/versions"; -import { defineEventHandlerWithErrors } from "@/server/utils/defineEventHandlerWithErrors"; +import { defineRApiEventHandler } from "@/server/utils/defineRApiEventHandler"; import type { VersionDataResponse } from "@/types/daedalusApiResponseTypes"; -export default defineEventHandlerWithErrors( - // TODO: Consider cacheing this server-side https://nitro.unjs.io/guide/cache - defineEventHandler(async (event): Promise => { +export default defineRApiEventHandler( + async (event): Promise => { // Delegate to getVersionData so that the logic can be unit-tested. const versionDataResponse = await getVersionData(event); return versionDataResponse; - }), + }, ); diff --git a/server/utils/defineEventHandlerWithErrors.ts b/server/utils/defineEventHandlerWithErrors.ts deleted file mode 100644 index f26f9748..00000000 --- a/server/utils/defineEventHandlerWithErrors.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { EventHandler, EventHandlerRequest } from "h3"; -import type { ApiResponse } from "@/types/daedalusApiResponseTypes"; - -export const defineEventHandlerWithErrors = ( - handler: EventHandler, -): EventHandler => - defineEventHandler(async (event) => { - const response = await handler(event) as ApiResponse; - - if (response.errors || !response.data) { - throw createError({ - statusCode: response.statusCode, - statusMessage: response.statusText, - message: errorMessage(response.errors), - data: response.errors, - }); - } else { - return response.data; - } - }); diff --git a/server/utils/defineRApiEventHandler.ts b/server/utils/defineRApiEventHandler.ts new file mode 100644 index 00000000..cf0c4aed --- /dev/null +++ b/server/utils/defineRApiEventHandler.ts @@ -0,0 +1,25 @@ +import type { EventHandler, H3Event } from "h3"; +import type { CachedEventHandlerOptions } from "nitropack"; +import type { ApiResponse } from "@/types/daedalusApiResponseTypes"; + +const defaultCacheDurationInSeconds = 0; + +// A wrapper for Nuxt's defineEventHandler that handles errors from the R API. +export const defineRApiEventHandler = ( + callback: (event: H3Event) => Promise, + cacheOptions: CachedEventHandlerOptions, +): EventHandler => + defineCachedEventHandler(async (event) => { + const response = await callback(event) as ApiResponse; + + if (response.errors || !response.data) { + throw createError({ + statusCode: response.statusCode, + statusMessage: response.statusText, + message: errorMessage(response.errors), + data: response.errors, + }); + } else { + return response.data; + } + }, { maxAge: defaultCacheDurationInSeconds, ...cacheOptions });