nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
55.13k stars 5.04k forks source link

__vite_ssr_import_1__.useRequestFetch(...).create is not a function #30091

Open Barbapapazes opened 3 days ago

Barbapapazes commented 3 days ago

Environment


Reproduction

  1. Create a new plugin named api.ts with the following content:
export default defineNuxtPlugin({
  async setup() {
    const runtimeConfig = useRuntimeConfig()
    const requestFetch = useRequestFetch()

    const api = useRequestFetch().create()

    return {
      provide: {
        api,
      },
    }
  },
})
  1. Start the server
npm run dev
  1. Open your browser and you'll see the following issue: __vite_ssr_import_1__.useRequestFetch(...).create is not a function.

Describe the bug

I'm trying to create an API version of the $fetch like in this video: https://www.youtube.com/watch?v=jXH8Tr-exhI&t=575s

While this works very well on the client, it does not works on server side, when a request need to be performed from the server. Why I want this? Because I'm trying to link a Nuxt application with a Laravel API and I need to forward the authentication cookie to get the user data from <laravel>/api/user and this is only possible with useRequestFetch.

But the typing system is not complainig about this:

Image

Additional context

No response

Logs

danielroe commented 2 days ago

This seems either like:

Barbapapazes commented 2 days ago

Since the useRequestEvent will return the correct fetch on client-side the useRequestFetch must be the same as $fetch (and this explains why the type is the same). So I think it's more the second point and an issue with the behaviour of $fetch.

https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/ssr.ts#L46

pi0 commented 2 days ago

event.$fetch in Nitro is not extendable. It is a wrapper for h3.fetchWithEvent (the types are inaccurate and I guess we need to fix it most importantly -- PR welcome for upstream 👍🏼 ).

Adding all ofetch nested utils costs, and the exact purpose of event.$fetch is to proxy incoming request not extending it. If you believe there are use cases for event.$fetch.create please feel free to open a discussion.