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

Add support of TS type annotations on ofetch.create #406

Open
1 task done
ysemennikov opened this issue Jun 19, 2024 · 3 comments
Open
1 task done

Add support of TS type annotations on ofetch.create #406

ysemennikov opened this issue Jun 19, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@ysemennikov
Copy link

ysemennikov commented Jun 19, 2024

Describe the feature

Hi everyone!

I really love using ofetch because it's very powerful and covers almost all use cases.

The one feature that is missing for me is type annotatations. I believe this could be automated using tools like openapi-typescript, which can generate TS-types based on OpenAPI specification.

So what I mean is:

// import types auto-generated from the OpenAPI spec
// they're generated by openapi-typescript
import type { paths } from './autogenerated-types/my-api';

const myApiFetch = ofetch.create<paths>(options)  // provide paths as generic to ofetch.create

// intellisense for request URL, method, body, etc.
myApiFetch('/endpoint', {
  method: 'POST',
  body: {}
})

The openapi-typescript has its own package called openapi-fetch with support of this feature, but since ofetch is great integrated into Nuxt 3, it would be very nice to also have this possibility here. And ofetch has more features than the openapi-fetch.

Additional information

  • Would you be willing to help implement this feature?
@ysemennikov ysemennikov added the enhancement New feature or request label Jun 19, 2024
@TimGuendel
Copy link

+1 This would be really nice

@martin-juul
Copy link

The typing issue doesn't seem to appear if you use the library like this:

export function createLibrary(data: CreateLibraryRequest) {
  return apiFetch<Library>(route('api.library.create'), {
    method: 'POST',
    body: data,
  })
}

I ran into the same issue like you, but if i adjust my request all seems to be good.

@ysemennikov
Copy link
Author

The typing issue doesn't seem to appear if you use the library like this:

export function createLibrary(data: CreateLibraryRequest) {
  return apiFetch<Library>(route('api.library.create'), {
    method: 'POST',
    body: data,
  })
}

I ran into the same issue like you, but if i adjust my request all seems to be good.

You are right, but in this case you need to manually define a return type for each request you send.

I suggest another feature - creating an ofetch instance and passing a generic type like this:
const apiFetch = ofetch.create<apiTypes>().

And then all requests will be already properly typed.

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

No branches or pull requests

3 participants