nuxt-open-fetch icon indicating copy to clipboard operation
nuxt-open-fetch copied to clipboard

Type interface for client query should allow for reactive values

Open adamdehaven opened this issue 1 year ago • 2 comments

Environment

  • Operating System: Darwin
  • Node Version: v20.14.0
  • Nuxt Version: 3.12.2
  • CLI Version: 3.12.0
  • Nitro Version: 2.9.6
  • Package Manager: [email protected]
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://github.com/adamdehaven/nuxt-open-fetch/tree/chore/query-interface-error

Ensure you checkout the chore/query-interface-error branch.

If you run the playground with pnpm dev you'll notice I also added a <select> element at the top to demonstrate the reactivity works correctly; it's just an issue with the query[property] type interface(s).

Describe the bug

Nuxt's useFetch (the underlying implementation under use[client] allows for passing in reactive ref values for the query property:

// Example source: https://nuxt.com/docs/api/composables/use-fetch#usage

const param1 = ref('value1')
const { data } = await useFetch('/api/modules', {
  query: { 
    param1, // Reactive 
    param2: 'value2', 
  }
})

Clients generated by nuxt-open-fetch actually do work with this implementation; however, the type interface shows an error as the properties are typed only to the raw value.

Example

Utilizing the /playground/app.vue file from this repo, if I change the existing /pet/{petId} call to this:

const status = useState<'available' | undefined>(() => undefined)
const { data, execute } = await usePets('/pet/findByStatus', {
  immediate: false,
  query: {
    status, // This query points to a reactive variable
  },
})

I see the following TypeScript error:

typescript error

However, you'll notice if you actually run the playground, not only is the example fully-functional, but it also correctly auto-watches the query.status property and runs the query again when the status ref changes.

If I change usePets to the native useFetch you'll notice there are no type errors, and the query.status property is correctly typed: no error

Additional context

Functionality is correct, but the type interfaces for query properties need to allow for both static and reactive values, just like the native useFetch.

Logs

No response

adamdehaven avatar Jun 28 '24 16:06 adamdehaven

@enkot I'm trying to figure out where the interface needs to be modified; glad to help if you want to take a look

adamdehaven avatar Jun 28 '24 16:06 adamdehaven

@enkot just bumping this and https://github.com/enkot/nuxt-open-fetch/pull/53 again if possible?

Let me know if I can provide more info

adamdehaven avatar Jul 30 '24 15:07 adamdehaven

Hi @adamdehaven

Thank you for the issue! Should be fixed in https://github.com/enkot/nuxt-open-fetch/releases/tag/v0.9.4

enkot avatar Oct 31 '24 22:10 enkot