N
Nuxt5mo ago
Viridian

Use incoming event type in defineEventHandler

I want to run some custom logic using a function that takes the event from an event handler in a .post.ts file:
export default defineEventHandler(async (event) => {
const handledEvent = handlePlayerSearchEvent(event);
export default defineEventHandler(async (event) => {
const handledEvent = handlePlayerSearchEvent(event);
However, I want to use the type of the event in my function parameter so it doesn't error. What type do I use? When I hover over it, I get (parameter) event: H3Event<EventHandlerRequest> but I can't seem to import that into my own code, and I'm also not sure if that's the type I want to be using. Is there an easier type that would work for this provided by Nuxt? I basically just want to be able to get the event typed properly, passed into that function and then parsed with readValidatedBody, etc.
3 Replies
Cue
Cue5mo ago
import type { H3Event } from 'h3'
import type { H3Event } from 'h3'
Viridian
ViridianOP5mo ago
Ah, that works? Solid I ask because I'm doing some stuff with Effect and trying to make things work:
const validateBody = <T>(schema: S.Schema<T>) => async (event: H3Event<EventHandlerRequest>) =>
await readValidatedBody(event, S.decodeUnknownSync(schema))
const validateBody = <T>(schema: S.Schema<T>) => async (event: H3Event<EventHandlerRequest>) =>
await readValidatedBody(event, S.decodeUnknownSync(schema))
I assume readValidatedBody doesn't have the result of the parsing as its return type but rather just a Promise? Right now, it's returning Promise<T> as decodeUnknownSync and similar from Effect normally returns an Effect (in this case, it would be an Effect with either T or ParseError), I was curious I can tell it works with Zod though
Simon
Simon4mo ago
Sorry for hijacking this thread but did you manage to get Effect working with Nitro? I find so little information on it so I have to take desperate measures, digging deep into discord messages. 😅
Want results from more Discord servers?
Add your server