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

Exception with custom controller #26

Closed EsGeh closed 1 year ago

EsGeh commented 1 year ago

When adding a custom to strapi, as defined in the strapi docs, type generation will fail with an ugly exception:

$ yarn t4s

TypeError: Cannot read properties of null (reading 'attributes') at /some/path/node_modules/@oak-digital/types-4-strapi-2/lib/plugins/draft-and-publish/index.js:10:33 at Array.forEach () at Object.addFieldsToContentTypes [as fn] (/some/path/node_modules/@oak-digital/types-4-strapi-2/lib/plugins/draft-and-publish/index.js:7:16) at /some/path/node_modules/@oak-digital/types-4-strapi-2/lib/plugins/PluginManager.js:41:21 at Array.forEach () at PluginManager.invoke (/some/path/node_modules/@oak-digital/types-4-strapi-2/lib/plugins/PluginManager.js:39:26) at InterfaceManager. (/some/path/node_modules/@oak-digital/types-4-strapi-2/lib/program/InterfaceManager.js:166:44) at step (/some/path/node_modules/@oak-digital/types-4-strapi-2/lib/program/InterfaceManager.js:44:23) at Object.next (/some/path/node_modules/@oak-digital/types-4-strapi-2/lib/program/InterfaceManager.js:25:53)
at fulfilled (/some/path/node_modules/@oak-digital/types-4-strapi-2/lib/program/InterfaceManager.js:16:58)

How to reproduce:

As described in the strapi docs, add 2 files like:

/src/api/hello/routes/hello.ts:

export default {
  routes: [
    {
      method: 'GET',
      path: '/hello',
      handler: 'hello.index',
    }
  ]
}

./src/api/hello/controllers/hello.ts

export default {
  async index(ctx, next) { // called by GET /hello
    ctx.body = 'Hello World!'; // we could also send a JSON
  },
};

Maybe that is, because t4s expects ./scr/api/hello/ to contain a content-types/ dir containing some schema.json (?)

Alexnortung commented 1 year ago

Thank you for reporting the issue, yes it is looking for a content type for each api (which is incorrect, but works for basic setups). I think the fix is relatively easy, although I am not really working on this project right now. I will look at it if i get time, else I will be glad to review a PR.

Alexnortung commented 1 year ago

I am working on this now fyi, should be done this week