From 2b17fa2e1fdd58786c1391ad9dd35d511d004381 Mon Sep 17 00:00:00 2001 From: Stefano Verna Date: Wed, 30 Oct 2024 13:02:23 +0100 Subject: [PATCH] Fix uploadTracks.create() --- .../cma-client-node/__tests__/upload.test.ts | 18 +++++++++++++++++- packages/rest-client-utils/src/serialize.ts | 13 +++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/cma-client-node/__tests__/upload.test.ts b/packages/cma-client-node/__tests__/upload.test.ts index b1651601..4a363003 100644 --- a/packages/cma-client-node/__tests__/upload.test.ts +++ b/packages/cma-client-node/__tests__/upload.test.ts @@ -1,5 +1,5 @@ import { generateNewCmaClient } from '../../../jest-helpers/generateNewCmaClient'; -import { generateId } from '../../cma-client/src'; +import { ApiError, generateId } from '../../cma-client/src'; describe('upload', () => { it.concurrent('upload local file', async () => { @@ -10,6 +10,22 @@ describe('upload', () => { }); expect(upload.path.endsWith('text.txt')).toBeTruthy(); + + try { + await client.uploadTracks.create(upload, { + url_or_upload_request_id: upload.url, + type: 'subtitles', + language_code: 'it-IT', + }); + } catch (e) { + if (!(e instanceof ApiError)) { + throw e; + } + + if (!e.findError('NOT_A_VIDEO')) { + throw e; + } + } }); it.concurrent('upload remote file', async () => { diff --git a/packages/rest-client-utils/src/serialize.ts b/packages/rest-client-utils/src/serialize.ts index 1c0f478f..2ba8f189 100644 --- a/packages/rest-client-utils/src/serialize.ts +++ b/packages/rest-client-utils/src/serialize.ts @@ -49,7 +49,7 @@ export function serializeRequestBody( } as unknown as T; } - const { id, meta, ...otherProperties } = body as any; + const { id, type: typeInBody, meta, ...otherProperties } = body as any; const attributes: Record = {}; const relationships: Record = {}; @@ -111,12 +111,21 @@ export function serializeRequestBody( } } } + + // 'type' key present in data is always ignored.. + // UNLESS the entity has a `type` attribute + if ( + options.attributes.includes('type') && + typeof typeInBody !== 'undefined' + ) { + attributes.type = typeInBody; + } } return { data: { ...(id || options.id ? { id: id || options.id } : {}), - type: options.type, // If a body.type is passed in, it should go in data.attributes. Only options.type should be here at the root. + type: options.type, ...(Object.keys(attributes).length > 0 ? { attributes } : {}), ...(Object.keys(relationships).length > 0 ? { relationships } : {}), ...(meta ? { meta } : {}),