Skip to content

Commit

Permalink
Fix uploadTracks.create()
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Oct 30, 2024
1 parent a441002 commit 2b17fa2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/cma-client-node/__tests__/upload.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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 () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/rest-client-utils/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function serializeRequestBody<T extends { data: unknown } | null>(
} as unknown as T;
}

const { id, meta, ...otherProperties } = body as any;
const { id, type: typeInBody, meta, ...otherProperties } = body as any;

const attributes: Record<string, unknown> = {};
const relationships: Record<string, unknown> = {};
Expand Down Expand Up @@ -111,12 +111,21 @@ export function serializeRequestBody<T extends { data: unknown } | null>(
}
}
}

// '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 } : {}),
Expand Down

0 comments on commit 2b17fa2

Please sign in to comment.