Oak-Digital / types-4-strapi-2

Automate the creation of typescript interfaces for your strapi 4 projects
MIT License
24 stars 4 forks source link

Any relation to user object (of users-permissions) from another collection type results in an exception #22

Closed icanhazpython closed 1 year ago

icanhazpython commented 1 year ago

On a bog-standard fresh-out-of-the-box strapi v4.7.1 install, create a collection type that contains a relation to the users collection. Types-4-strapi-2 will throw this exception because the users collection does not exist on the filesystem (presumably it is part of strapi internals and stored elsewhere):

TypeError: Cannot read properties of undefined (reading 'file')
    at Attributes.attributeToString (types-4-strapi-2/lib/attributes/Attributes.js:114:71)
    at Attributes.toFieldsString (types-4-strapi-2/lib/attributes/Attributes.js:219:37)
    at Attributes.toString (types-4-strapi-2/lib/attributes/Attributes.js:229:27)
    at Interface.attributesToString (types-4-strapi-2/lib/interface/Interface.js:44:22)
    at Interface.getInterfaceFieldsString (types-4-strapi-2/lib/interface/Interface.js:67:29)
    at Interface.getInerfaceString (types-4-strapi-2/lib/interface/Interface.js:54:26)
    at Interface.toString (types-4-strapi-2/lib/interface/Interface.js:80:52)
    at InterfaceManager.<anonymous> (types-4-strapi-2/lib/program/InterfaceManager.js:229:39)
    at Generator.next (<anonymous>)
    at types-4-strapi-2/lib/program/InterfaceManager.js:8:71

This is one reason why I'm curious that this project doesn't simply query the strapi content-types API at api/content-type-builder/content-types. It would seem that the return data provides all that analyzing the json schemas from the filesystem would, with the addition of the schema for the users objects (displayed in the API response as plugin::users-permissions.user). In my case, I have added additional fields to the user object, so this tool has a blind spot there, as that data appears to be only available via the API and not the json schema files on the filesystem.

Alexnortung commented 1 year ago

Thanks for reporting the issue and giving your suggestion. I did think about querying strapi at some point when developing this, but was very unsure how. I will definitely try to look into querying it in this way.

LouisMarzorati commented 1 year ago

any updates on this?

Alexnortung commented 1 year ago

Sorry no, I have been busy with other porjects. Feel free to open a PR :)

Alexnortung commented 1 year ago

Update: I am working on this now, should probably be ready this week

Alexnortung commented 1 year ago

Alright, v1 makes it possible to use different readers. The v0 reader would only read the generated files by strapi. v1 keeps it this way by default, but introduces another reader named load-strapi you should use this reader if you have relations to users or other content types not defined by the files.

example with cli

t4s --reader load-strapi

I will publish v1 today after I have made some tests