datocms / js-rest-api-clients

REST API clients to interact with DatoCMS
MIT License
11 stars 7 forks source link

Fix serializeRequestBody() body.type vs options.type parsing #31

Closed arcataroger closed 1 month ago

arcataroger commented 1 month ago

serializeRequestBody() was mis-applying the body.type parameter into its data.type output, instead of data.attributes.type where it would belong. This was breaking uploadTracks.create() because it passes in both a body.type and an options.type, e.g.:


const {serializeRequestBody} = require("@datocms/rest-client-utils");

const body = {
    type: 'subtitles',
    'url_or_upload_request_id': 'test',
}

console.log(serializeRequestBody(body, {
    type: 'upload_track',
    attributes: [
        'url_or_upload_request_id',
        'type',
        'name',
        'language_code',
        'closed_captions',
    ],
    relationships: [],
}));

That was incorrectly returning:

{
  data: {
    type: 'subtitles', // This is wrong, should be 'upload_track'
    attributes: { url_or_upload_request_id: 'test' } // This is missing 'subtitles'
  }
}

Fixed version instead returns:

{
  data: {
    type: 'upload_track',
    attributes: { type: 'subtitles', url_or_upload_request_id: 'test' }
  }
}