Types of changes
- [x] Bug fix (a non-breaking change which fixes an issue)
- [x] New feature (a non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing functionality to change)
Description
Fixes
This PR fixes some examples in the docs (getCollection
and using const
instead of var
). It also fixes some minor issues, like removing the import for defineNuxtConfig
that can be left out of Nuxt 3 modules. Furthermore, it fixes some ESLint warnings, like reordering imports and removing redundant async
keywords.
Using pnpm
As many packages in the Vue ecosystem have switched to pnpm
, it makes sense to apply that change here as well.
Typed composables changes
Instead of passing the expected return type to every separate composable method, with the changes made in this PR, you can pass along all your Collections to useDirectusCollections
and useDirectusItems
and all its methods will be typed automatically. This can be seen in action in the playground, which I updated accordingly. For example, when running:
type News = {
id: number;
title: string;
content: string;
status: string;
};
type Books = {
id: number;
title: string;
content: string;
status: string;
};
type Collections = {
news: News;
books: Books;
};
const { getItems } = useDirectusItems<Collections>();
// 'news' is now automatically typed as 'News[]'
const news = await getItems({
// Your IDE will now autosuggest 'news' and 'books',
// as these are the only available collections
collection: 'news',
params: {
filter: {
content: 'yyeeet',
title: 'Test1',
},
},
})
// 'books' is now automatically typed as { meta: { total_count?: number; filter_count?: number;}; data: Books[]; }
const books = await getItems({
// Your IDE will now autosuggest 'news' and 'books',
// as these are the only available collections
collection: 'books',
params: {
// Adding 'meta' will automatically change the return
// type of the 'getItems' method, because `getItems`
// now has proper function overloads.
meta: '*',
},
})
One thing of importance is that in order to keep it simple with the typings, I removed the possibility of passing a type to methods like getItems
directly, like getItems<News>({ collection: 'news' })
. But in my opinion, this breaking change is justified because the changes in this PR allow you to type less code.
The return type of useDirectusUser
has been updated to align with what the latest version of Directus returns (used to be export type DirectusUser = object | null;
). You can also override this by passing your own type to useDirectusUser<MyUserType>
and useDirectusAuth<MyUserType>
.
Checklist:
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes (if not applicable, please state why)
Actually, none of the examples in the docs include passing a type to any of the composables at all. This would be a big improvement, but is probably unrelated to this PR.