Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IncomingMessage and ServerResponse not assignable to CompatabilityEvent #13683

Closed
ikadix opened this issue Apr 7, 2022 · 3 comments
Closed

Comments

@ikadix
Copy link
Contributor

ikadix commented Apr 7, 2022

Environment

❯ npx nuxi info
Nuxt CLI v3.0.0-27489107.7379184
RootDir: /home/projects/github-8rmpp2
Nuxt project info:


  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-27489107.7379184
  • Package Manager: npm@7.17.0
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-8rmpp2?file=server/api/test.ts

Describe the bug

When typing req and res in an api handler as described in the docs here

If you attempt to follow the examples for passing req into any of the h3 utilities such as useBody, useQuery or useCookie or similarly pass res into relevant utilites like setCookie you will be met with the following error.

Argument of type 'IncomingMessage' is not assignable to parameter of type 'CompatibilityEvent'.
  Type 'import("http").IncomingMessage' is not assignable to type 'import("stackblitz:/node_modules/h3/dist/index").IncomingMessage'.(2345)

Unclear if this should be raised here or in the h3 repo, please let me know if the latter and I will raise over there.

Additional context

No response

Logs

No response

@ikadix
Copy link
Contributor Author

ikadix commented Apr 7, 2022

I see the conversation here nuxt/framework#4174 mentions moving away from this style of handler.

Should we not be writing these kinds of handlers now / in future ?

@Diizzayy
Copy link
Member

Diizzayy commented Apr 7, 2022

@ikadix

Should we not be writing these kinds of handlers now / in future ?

Indeed, H3 is slowly veering away from legacy (req, res) format, as it currently holds back the framework from evolving to support more features. More info at (unjs/h3#73)

@danielroe
Copy link
Member

Yes, this is a case of migrating your handler to event:

import type { IncomingMessage, ServerResponse } from 'http';
import { useBody, useCookies, useQuery, setCookie, eventHandler } from 'h3';

export default eventHandler(async (event) => {
  const query = useQuery(event);
  const body = await useBody(event); // only for POST | PUT | PATCH | DELETE requests
  const cookies = useCookies(event);

  setCookie(event, 'test', 'test');
});

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale Apr 11, 2022
@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe transferred this issue from nuxt/framework Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants