strapi
strapi copied to clipboard
Problem accessing single types
Version
- Operating System:
Linux - Node Version:
v16.17.0 - Nuxt Version:
3.0.0-rc.12-27736489.2e080c2 - Nitro Version:
0.5.4-27726613.640c2b7 - Package Manager:
[email protected] - Builder:
vite - User Config:
typescript,css,modules,experimental,image - Runtime Modules:
@nuxtjs/[email protected],@pinia/[email protected],@vueuse/[email protected],@nuxt/[email protected],unplugin-icons/nuxt,@nuxtjs/[email protected] - Build Modules:
-
Reproduction Link
n/a
Steps to reproduce
Try to access a "single type" from strapi using find.
What is Expected?
I would expect to be able to get a single type (i.e. /api/homepage) without type errors.
What is actually happening?
So, after looking through the module code, it seems that neither find() or findOne() are appropriate for getting "single types" from strapi. The problem...
-
find()is meant to return an array. Single types return an object so this throws a type error complaining it's not an array. -
findOne()returns an object. That's great and seems more appropriate thanfind()semantically speaking. However, it requires anidargument, which is then used in a template literal to add it onto the end of the content type argument. This is a problem because single types don't use anidin the url.
Solutions?
- I would say the easiest might be to just add another method like
getSingle()(or something with a better name) that doesn't require anidas an argument but still returns the same type asfindOne. - A more ideal solution I feel would be to have
idas an optional parameter infindOne(), however based on my brief venture down that rabbit hole, this would require a lot more work and introducing maybe "rest parameters" or something similar. I'm hoping there's something I'm not aware of that would allow this.
Side Note:
On a somewhat related note (I'm happy to create a separate issue if need be), It seems to me that while not the end of the world, the methods find() and findOne() could maybe be renamed to getEntries() and getEntry(). My reasoning:
- The new names would be more inline with how strapi refer to their api (i.e. "get entries" and "get an entry" ).
-
findOne()isn't horrible butfind()doesn't provide any additional insight into what it might be returning, whilegetEntries()is most likely returning an array if I had to guess.
Again, not the end of the world here, but I feel new naming would provide more consistency and clarity, albeit a slightly more verbose solution.