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

/e2e-typesafety-in-nuxt-fullstack-application.html #70

Open
utterances-bot opened this issue Mar 12, 2024 · 4 comments
Open

/e2e-typesafety-in-nuxt-fullstack-application.html #70

utterances-bot opened this issue Mar 12, 2024 · 4 comments

Comments

@utterances-bot
Copy link

E2E Typesafety in Nuxt Fullstack Application | Cody Bontecou

Showcasing a simple approach to sharing types between the server and client within a fullstack Nuxt application.

https://www.codybontecou.com/e2e-typesafety-in-nuxt-fullstack-application.html?source=weeklyVueNews&campaign=136

Copy link

const { data: userData } = await useFetch('/api/getUser')

This falls short in typing. I had hoped it would implicitly provide the data structure that the server is providing, but instead, it's implicitly unknown.

This is not correct. Nuxt provides type safety. The string you pass to useFetch is also a literal.
You can check the generated types in .nuxt/types/nitro-routes.d.ts:

'/api/getUser': {
      'default': Simplify<Serialize<Awaited<ReturnType<typeof import('../../server/api/getUser').default>>>>
}

If it does not work for you check if your are affected by this issue: nuxt/nuxt#23984

The preferred implementation should be to write a data fetching function or an api client and put it into useAsyncData:

const { $api } = useNuxtApp()
const { data } = await useAsyncData(
  'users',
  $api.getUsers,
)

You should use the amazing tools nuxt brings to the table.
Btw instead of api/getUsers.ts a better approach would be api/user/index.get.ts for all users and api/users/[uuid].get.ts for a single user ;)

Copy link

or write a custom useFetch like in this example: https://nuxt.com/docs/examples/advanced/use-custom-fetch-composable

@CodyBontecou
Copy link
Owner

CodyBontecou commented Mar 31, 2024

Hey @woldtwerk, thanks for the in-depth comment.

I'm finally getting around to working on your suggestions and I'm curious about const { $api } = useNuxtApp(). Where is $api coming from here?

It's returning a type of unknown and I can't seem to find anything relating to it in the docs.

Are you referring to the $api variable defined in this blog post https://notes.atinux.com/nuxt-custom-fetch?

Copy link

$api was just an example. You have to write it yourself. It can be named whatever you want.
You can create a client for your api and provide it:

import { api } from './your-api.ts'

export default defineNuxtPlugin(() => ({
  provide: {
    api,
  }
})

In your components you can access it with const { $api } = useNuxtApp()
Indeed similar to the blog post from Sébastien.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants