kitsu icon indicating copy to clipboard operation
kitsu copied to clipboard

JSDoc comment for `deserialise()` is inconsistent with implementation

Open fspoettel opened this issue 3 years ago • 1 comments

This example states that the linked relationship user will look like this:

user: { type: 'users', id: '2', slug: 'wopian' }

However, according to the linkRelationships documentation and a brief test, the function output looks like this:

user: { data: { type: 'users', id: '2', slug: 'wopian' } }

fspoettel avatar Aug 25 '21 11:08 fspoettel

Thank you, it seems some of the JSDoc examples didn't get updated in the 9.x release.

wopian avatar Aug 25 '21 11:08 wopian

I'm fairly certain this is not an actual problem. If you run the example exactly as-is, you get the expected output. I have removed the variable names from my example below to avoid confusing data: with data =. My REPL output results in the exact same data as the example.

> linkRelationships = require("./linkRelationships").linkRelationships
[Function: linkRelationships]
> linkRelationships(
...   {
.....     attributes: { author: 'Joe' },
.....     relationships: {
.......       author: {
.........         data: { id: '1', type: 'people' }
.........       }
.......     }
.....   },
...   [
...     {
.....       id: '1',
.....       type: 'people',
.....       attributes: { name: 'Joe' }
.....     }
...   ]
... )
{
  attributes: { author: 'Joe' },
  author: { data: { id: '1', type: 'people', name: 'Joe' } }
}

I think its worth noting that linkRelationships is called internally by deserialise, but as can be seen here https://github.com/wopian/kitsu/blob/b3e2624a1e5969e606fc50daa68fdd44ff90abb2/packages/kitsu-core/src/deserialise/index.js#L63 it assigns the result to response.data, which explains the difference between the between the output of deserialise and linkRelationships

pedep avatar Aug 11 '22 10:08 pedep

If I run the linked example exactly as is:

const { deserialise } = require("kitsu-core");

deserialise({
  data: {
     id: '1',
     relationships: {
       user: {
         data: {
           type: 'users',
           id: '2' }
       }
     }
   },
   included: [
     {
       type: 'users',
       id: '2',
       attributes: { slug: 'wopian' }
     }
   ]
})

the output is:

{ data: { id: '1', user: { data: { type: 'users', id: '2', slug: 'wopian' }}}}

while the JSDoc comment says it should be:

{ data: { id: '1', user: { type: 'users', id: '2', slug: 'wopian' } } }

fspoettel avatar Aug 11 '22 11:08 fspoettel

Yes, 9.x added data to relationships to support relationship meta/links objects when the relationship is an array.

This didn't get updated in the documentation and appears I forgot to make this change after this issue was opened :(

wopian avatar Aug 11 '22 11:08 wopian

Thank you for the fix! :)

fspoettel avatar Aug 11 '22 12:08 fspoettel

Released in 10.0.0-alpha.26 and 9.1.26

wopian avatar Aug 11 '22 12:08 wopian