chapter-three / next-drupal

Next.js for Drupal has everything you need to build a next-generation front-end for your Drupal site: SSG, SSR, and ISR, Multi-site, Authentication, Webforms, Search API, I18n and Preview mode (works with JSON:API and GraphQL).
https://next-drupal.org
MIT License
630 stars 175 forks source link

drupal_internal__*id is typed as string - should it be number? #686

Open MartinDavi opened 7 months ago

MartinDavi commented 7 months ago

Package containing the bug

next-drupal (NPM package)

Describe the bug

Hey, in types.ts we have:

export interface DrupalTaxonomyTerm extends JsonApiResourceWithPath {
  drupal_internal__tid: string
  changed: string
  default_langcode: boolean
  name: string
  description: string
  weight: number
}

But when I fetch terms from the jsonapi, drupal_internal__tid is a number. Same if it's a node id or what have you.

This just caused a tricky bug where I was trying to compare a string with a number which of course wasn't working.

I'm on 1.6.0.

seoplague commented 5 months ago

And description should it be an object ? { format: "basic_html", processed: "<p>Test description</p>" value: "<p>Test description</p>" }

tjheffner commented 5 months ago

Running into this with the Document media types. Note that DrupalNode and DrupalParagraph are have number as the type for the drupal_internal__*id, but DrupalMedia and DrupalFile have those as string. We are seeing them come through as number in our system.

export interface DrupalNode extends JsonApiResourceWithPath {
    drupal_internal__nid: number;
    drupal_internal__vid: number;
    changed: string;
    created: string;
    title: string;
    default_langcode: boolean;
    sticky: boolean;
}
export interface DrupalParagraph extends JsonApiResource {
    drupal_internal__id: number;
    drupal_internal__revision_id: number;
}
export interface DrupalBlock extends JsonApiResource {
    info: string;
}
export interface DrupalMedia extends JsonApiResource {
    drupal_internal__mid: string;
    drupal_internal__vid: string;
    changed: string;
    created: string;
    name: string;
}
export interface DrupalFile extends JsonApiResource {
    drupal_internal__fid: string;
    changed: string;
    created: string;
    filename: string;
    uri: {
        value: string;
        url: string;
    };
    filesize: number;
    filemime: string;
    resourceIdObjMeta?: DrupalFileMeta;
}
export interface DrupalFileMeta {
    alt?: string;
    title?: string;
    width: number;
    height: number;
}

A second issue is that the types for DrupalFIleMeta (via resourceIdObjMeta) are specific to images, but document files returned via JSON:API have a different resourceIdObjMeta surfaced.

images:

       "image": {
          "data": {
            "type": "file--file",
            "id": "e638fd17-9090-442b-a724-5113133b4d0f",
            "meta": {
              "alt": null,
              "title": null,
              "width": 456,
              "height": 304,
              "drupal_internal__target_id": 1
            }
          },

documents:

        "field_document": {
          "data": {
            "type": "file--file",
            "id": "b17bc9dd-a7ce-469a-9b9f-5550de367cea",
            "meta": {
              "display": true,
              "description": "",
              "drupal_internal__target_id": 10
            }
          },