No types
Closed this issue · 2 comments
Looks like this issue: #259 is still relevant, as types are still not exposed if you import $jsonApi using useNuxt()
as the docs suggest:
import { useNuxtApp } from '#app'
const { $jsonApi } = useNuxtApp()
In this code, $jsonApi
will be defined as any
, and if you investigate the generated typings for this module, there in fact aren't any types exported for that object.
A workaround I've put together in the meantime to allow you to use this module with types, is to create a new composable called use-api.ts
:
import type Kitsu from 'kitsu';
export default function (): Kitsu {
const { $jsonApi } = useNuxtApp();
return <Kitsu>$jsonApi;
}
Then you can just:
const $api = useApi();
As the types for $jsonApi are just those of the wrapped Kitsu library, this ensures that the returned $api
is already of type Kitsu
, and all your calls work as expected.
It would be nice to not have to wrap this component, but that said, I may end up wanting to do so anyway, to add custom XSRF functionality or redirecting on 401s, etc. So for anyone else who is experiencing this issue, I hope this helps!
@DanHulton I just released version 2.0.3
. See if that fixes the types for you.
Works great, I'll close this issue down!
(That said, I'll still probably keep a form of my useApi()
composable around, since it gives me the opportunity to extend it with custom methods, behaviours, etc. But this at least means I don't have to manually add the Kitsu type!)