Nuxt Server: Extend H3Event with Typesafety
I am trying to create a util like https://nuxt.com/docs/4.x/guide/directory-structure/server#server-utilities so I can reuse validation code like:
or
The util handler would take:
as first argument, and then inject into
event.context.
Then on the endpoint handler itself, event.context.user would be type User if we passed { user: true } to validate it. Same for params, we would get the parsedData from zod.
Is this possible? Some examples out there?Nuxt
server · Nuxt Directory Structure v4
The server/ directory is used to register API and server handlers to your application.
2 Replies
Hi @psycarlo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
You can absolutely create a reusable utility in Nuxt to handle validation and inject typed data into
You can define a utility in
The docs show using helpers like
While you can extend
- You will need to use type assertions or augment the H3 types to get full type safety in your handlers. - The official docs show the runtime pattern, but not a fully type-safe, generic utility as you describe. Summary - The pattern of wrapping handlers for validation and injecting into
event.context, but the official documentation does not provide a full, type-safe example for extending H3Event's context in a generic way. However, the pattern you describe—using server utilities to wrap handlers and inject validated data into event.context—is supported and encouraged in Nuxt.
How to Approach This
1. Custom Server Utilities:You can define a utility in
~/server/utils that wraps your handler, performs validation (e.g., user session, params), and attaches the results to event.context. This is shown in the docs as a general pattern, though not with full type inference for event.context based on the utility's options.
Nuxt 4.x Server Utilities Example
2. Validation Helpers:The docs show using helpers like
requireUserSession (for user validation) and getValidatedRouterParams (for params validation with Zod), which throw or return errors if validation fails. You can compose these in your utility.
Protect API Routes
3. Type Safety:While you can extend
event.context at runtime, TypeScript will not automatically infer the new types unless you augment the H3 types globally or use type assertions in your handlers. The docs do not provide a ready-made solution for dynamic type inference based on utility options.
Example Pattern
You can create a utility like this (simplified):
Note:- You will need to use type assertions or augment the H3 types to get full type safety in your handlers. - The official docs show the runtime pattern, but not a fully type-safe, generic utility as you describe. Summary - The pattern of wrapping handlers for validation and injecting into
event.context is supported and shown in the docs.
- Full compile-time type safety for event.context based on utility options is not provided out-of-the-box in the docs.
- You can use type assertions or global type augmentation for better type safety, but this is not covered in the official examples.
For more, see the Nuxt 4.x server utilities documentation and Protect API Routes.
If you need a fully type-safe, generic utility, you may need to implement additional TypeScript type augmentation yourself, as the knowledge sources do not provide a complete example for this.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@277428305756422144> Kapa.ai is still learning and improving, please let me know how I did by reacting below.