Skip to content

Commit

Permalink
Refactor defineEventHandler wrapping
Browse files Browse the repository at this point in the history
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
  • Loading branch information
david-mears-2 committed Sep 2, 2024
1 parent dda7c27 commit b029800
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
14 changes: 7 additions & 7 deletions server/api/metadata.get.ts
Original file line number Diff line number Diff line change
@@ -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<MetaDataResponse> => {
export default defineRApiEventHandler(
async (event): Promise<MetaDataResponse> => {
// 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 },
);
9 changes: 4 additions & 5 deletions server/api/versions.get.ts
Original file line number Diff line number Diff line change
@@ -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<VersionDataResponse> => {
export default defineRApiEventHandler(
async (event): Promise<VersionDataResponse> => {
// Delegate to getVersionData so that the logic can be unit-tested.
const versionDataResponse = await getVersionData(event);

return versionDataResponse;
}),
},
);
20 changes: 0 additions & 20 deletions server/utils/defineEventHandlerWithErrors.ts

This file was deleted.

25 changes: 25 additions & 0 deletions server/utils/defineRApiEventHandler.ts
Original file line number Diff line number Diff line change
@@ -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<ApiResponse>,
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 });

0 comments on commit b029800

Please sign in to comment.