dohomi / storyblok-generate-ts

Generates TypeScript interface types based on Storyblok component file
MIT License
101 stars 33 forks source link

AssetStoryblok `id` should not be required #72

Closed caroillemann closed 2 weeks ago

caroillemann commented 3 weeks ago

I'm experiencing an issue with the id field in the generated AssetStoryblok type.

When I add a field of type 'Asset' in Storyblok and do not select an image, this is the JSON output I see when previewing the 'Published JSON':

"favicon": {
  "id": null,
  "alt": null,
  "name": "",
  "focus": null,
  "title": null,
  "source": null,
  "filename": "",
  "copyright": null,
  "fieldtype": "asset",
  "meta_data": {}
}

As you can see, the id is null when no asset is selected. However, in the type generator code, the id is included in the array of required fields: https://github.com/dohomi/storyblok-generate-ts/blob/f4d79806dc4d57568a9e317b0d30675f92929999/src/genericTypes.ts#L36

Am I missing something, or do you agree this should be changed?

For additional context, here is a full Story object:

{
  "story": {
    "name": "Test",
    "created_at": "2024-08-28T09:54:50.506Z",
    "published_at": "2024-08-28T09:55:50.337Z",
    "id": 540942391,
    "uuid": "0b2d4fab-05b3-4bfa-9e28-67f5ad2f410f",
    "content": {
      "_uid": "8c2183c1-6bd9-4fd3-b73d-9af3fb903f67",
      "footer": [],
      "header": [],
      "favicon": {
        "id": null,
        "alt": null,
        "name": "",
        "focus": null,
        "title": null,
        "source": null,
        "filename": "",
        "copyright": null,
        "fieldtype": "asset",
        "meta_data": {}
      },
      "component": "TemplateGlobalContent",
      "siteTitle": "Testing without image"
    },
    "slug": "test",
    "full_slug": "domain/global/test",
    "sort_by_date": null,
    "position": 0,
    "tag_list": [],
    "is_startpage": false,
    "parent_id": 532487839,
    "meta_data": null,
    "group_id": "ef1d2d0a-2684-411b-9a78-111054bf68d5",
    "first_published_at": "2024-08-28T09:55:50.337Z",
    "release_id": null,
    "lang": "default",
    "path": null,
    "alternates": [],
    "default_full_slug": null,
    "translated_slugs": null
  },
  "cv": 1724838950,
  "rels": [],
  "links": []
}
dohomi commented 3 weeks ago

I never came across your issue.. I am sure that the assets have an ID, if nothing is selected I assume the entire field should be null

caroillemann commented 3 weeks ago

Yes, when you add an asset in the field, it has an ID and the object looks like this:

"file": {
  "id": 17136175,
  "alt": "",
  "name": "",
  "focus": "",
  "title": "",
  "source": "",
  "filename": "https://a.storyblok.com/f/123456/482x482/9cb1e9ae94/transparent-png.png",
  "copyright": "",
  "fieldtype": "asset",
  "meta_data": {},
  "is_external_url": false
},

But if nothing is selected, you get this object where id is null:

"file": {
  "id": null,
  "alt": null,
  "name": "",
  "focus": null,
  "title": null,
  "source": null,
  "filename": "",
  "copyright": null,
  "fieldtype": "asset",
  "meta_data": {}
},