nitrojs / nitrojs/nitro

Types in the Server Utilities example not working

Open
#1,827 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug types v2
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.