Suport media format

This commit is contained in:
Alexandre Bodin 2021-08-30 10:23:15 +02:00
parent 68826f7ae0
commit 5a9371b5c3
2 changed files with 108 additions and 0 deletions

View File

@ -119,4 +119,108 @@ describe('Transforms', () => {
meta: {},
});
});
test('Handles relations recursively', () => {
const contentType = {
attributes: {
relation: {
type: 'relation',
target: 'xxx',
},
},
};
global.strapi = {
contentType() {
return {
attributes: {
nestedRelation: {
type: 'relation',
target: 'xxxx',
},
},
};
},
};
expect(
transforms.transformResponse(
{
id: 1,
title: 'Hello',
relation: [{ id: 1, value: 'test', nestedRelation: { id: 2, foo: 'bar' } }],
},
undefined,
{ contentType }
)
).toStrictEqual({
data: {
id: 1,
attributes: {
title: 'Hello',
relation: {
data: [
{
id: 1,
attributes: {
value: 'test',
nestedRelation: {
data: {
id: 2,
attributes: {
foo: 'bar',
},
},
},
},
},
],
},
},
},
meta: {},
});
});
test('Handles media like relations', () => {
const contentType = {
attributes: {
media: {
type: 'media',
},
},
};
global.strapi = {
contentType() {
return undefined;
},
};
expect(
transforms.transformResponse(
{ id: 1, title: 'Hello', media: [{ id: 1, value: 'test' }] },
undefined,
{ contentType }
)
).toStrictEqual({
data: {
id: 1,
attributes: {
title: 'Hello',
media: {
data: [
{
id: 1,
attributes: {
value: 'test',
},
},
],
},
},
},
meta: {},
});
});
});

View File

@ -37,6 +37,10 @@ const transformEntry = (entry, contentType) => {
if (attribute && attribute.type === 'relation') {
const data = transformEntry(property, strapi.contentType(attribute.target));
attributeValues[key] = { data };
} else if (attribute && attribute.type === 'media') {
const data = transformEntry(property, strapi.contentType('plugin::upload.file'));
attributeValues[key] = { data };
} else {
attributeValues[key] = property;