Types in the Server Utilities example not working
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 11.2k
- Forks
- 899
- Avg merge
- 2d 24m
- Merged PRs (30d)
- 40
Description
Environment
- Operating System: Darwin
- Node Version: v18.18.1
- Nuxt Version: 3.7.4
- CLI Version: 3.9.0
- Nitro Version: 2.6.3
- Package Manager: pnpm@8.9.0
- Builder: -
- User Config: devtools, typescript, runtimeConfig, experimental, modules
- Runtime Modules: @nuxtjs/tailwindcss@6.8.0
- Build Modules: -
Reproduction
~/server/utils/handler.ts
import type { EventHandler, EventHandlerRequest } from 'h3';
export const defineWrappedResponseHandler = <T extends EventHandlerRequest, D>(
handler: EventHandler<T, D>,
): EventHandler<T, D> =>
defineEventHandler<T>(async (event) => {
try {
// do something before the route handler
const response = await handler(event);
// do something after the route handler
return { response };
} catch (err) {
// Error handling
return { err };
}
});
~/server/routes/api/auth/signup.post.ts
export default defineWrappedResponseHandler(async (event) => {
return { message: 'awesome!' };
});
~/pages/index.vue
<script setup lang="ts">
const signup = async () => {
const data = await $fetch('/api/auth/signup', { method: 'POST' }); // type data = { message: string }
};
</script>
Describe the bug
Shouldn't the type be type data = { response: { message: string }}? or am I understanding it wrong?
Any help would be greatly appreciated!
Additional context
Not quite sure if the problem is directly related to nuxt...
Logs
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with server/utils/handler.ts and the server/routes/api/auth/signup.post.ts example, then inspect how the result is inferred in pages/index.vue. Compare the wrapper's declared EventHandler types with the observed $fetch type and determine whether the example or Nitro's typing is responsible. Done means the expected response type is established and the example or implementation is corrected accordingly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nuxt, typescript
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100