-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
dda7c27
commit b029800
Showing
4 changed files
with
36 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}), | ||
}, | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |