hi, I found 3 different ways to create api types for the frontend project.
all of them have some drawbacks:
- use the openAPI documentation plugin and generate types with the help of some library (orval / openapi-ts or any other).
pros: you get all the types including errors.
And it enables you to autogenerate the api code as well.
cons:
- the docs plugin doesn’t flag most properties as required. so
data,attributes,idand many others are optional and your code looks likeres?.data?.attributes?.titlewhich is annoying and wasteful. - you need to run the script with each build
-
use the strapi-plugin-schemas-to-ts plugin by @mancku mentioned above (grate plugin, thank you
)
pros: it generated the types by itself in the strapi project, and you just import them if you have a monorepo or copy otherwise
cons: many of the properties are required which is annoying if you are mocking the data and a bit dangerous cause they could be empty in reality. -
use the method described in strapi blog. the idea there is that you add types to your frontend project that use the auto generated
contentTypes.d.tsto create the api types.
pros: no new generated types, and it is based on types that strapi generates and hopefully uses itself.
also the types are quite accurate optional / required wise
cons: currently it is a piece of code you add to your project, so you have to maintain it. and I already found some minor mistakes in the code.
I hope it helps ![]()
** I can put only 2 links in the post… so some links are missing