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

TypeError encountered if model references any `plugin::users-permissions` objects #7

Closed haysclark closed 2 years ago

haysclark commented 2 years ago

I have a Profile object with a 1:1 relationship with a User object. When trying to generate types it throws the following error:

~ t4s "--out" "./new-types"

TypeError: Cannot read properties of undefined (reading 'name')
    at Attributes.attributeToString (/Users/user/org/project/node_modules/@oak-digital/types-4-strapi-2/lib/interface/Attributes.js:73:66)

Truncated schema.json for my Profile object:

{
  "kind": "collectionType",
  "collectionName": "profiles",
  "info": {
    "singularName": "profile",
    "pluralName": "profiles",
    "displayName": "Profile",
    "description": "Profile segments off the non authentication information from Users, allowing for cleaner data separation"
  },
  "options": {
    "draftAndPublish": false
  },
  "pluginOptions": {},
  "attributes": {
    "user": {
      "type": "relation",
      "relation": "oneToOne",
      "target": "plugin::users-permissions.user",
      "inversedBy": "profile"
    },
    "email": {
      "type": "email"
    },
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
  }
}

As a workaround, I made the following patch to line 73 of Attibutes.js. Source

  var dependencyName = (this.RelationNames[apiName] && this.RelationNames[apiName].name) || `any`;

Post patch, this is the exported structure:

export interface IProfile {
  id: number;
  attributes: {
    user?: { data: any | null };
    email: string;
    firstName: string;
    lastName: string;
  };
}

You can see that the user attribute can not access plugin::users-permissions.user.

haysclark commented 2 years ago

Moving forward, it might be best to use @strapi/strapi v4.3.4 which supports exporting types via:

strapi ts:generate-types
Alexnortung commented 2 years ago

@haysclark thanks for posting the issue and making a patch 😃

I am aware that strapi can generate types with ts:generate, but how can we utilize those scripts in this repo?

Alexnortung commented 2 years ago

The problem is that the program does not generate a user interface. This is also related to #4. But maybe this can be solved by transitioning to strapi's own interface generation.

Alexnortung commented 2 years ago

This issue has now been fixed and can be used in the newest version 0.2.9