Skip to content
This repository was archived by the owner on Sep 16, 2025. It is now read-only.

Commit 14cc4c7

Browse files
fix(findSlug REST): incorrect response format (#13)
1 parent cab6045 commit 14cc4c7

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"peerDependencies": {
3838
"@strapi/strapi": "^4.0.7",
39+
"@strapi/utils": "^4.0.7",
3940
"lodash": "^4.17.21",
4041
"yup": "^0.32.9"
4142
},

server/controllers/slug-controller.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
const { getPluginService } = require('../utils/getPluginService');
43
const _ = require('lodash');
4+
const { getPluginService } = require('../utils/getPluginService');
5+
const { transformResponse } = require('../utils/transformEntry');
56

67
module.exports = ({ strapi }) => ({
78
async findSlug(ctx) {
@@ -42,9 +43,10 @@ module.exports = ({ strapi }) => ({
4243
const data = await getPluginService(strapi, 'slugService').findOne(uid, query);
4344

4445
if (data) {
45-
return ctx.send({ data });
46+
ctx.body = transformResponse(data);
47+
} else {
48+
ctx.notFound();
4649
}
47-
ctx.notFound();
4850
} catch (error) {
4951
ctx.badRequest(error.message);
5052
}

server/services/slug-service.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const _ = require('lodash');
34
const { getPluginService } = require('../utils/getPluginService');
45
const { stringToSlug } = require('../utils/stringToSlug');
56

@@ -29,6 +30,17 @@ module.exports = ({ strapi }) => ({
2930
async findOne(uid, query) {
3031
const slugs = await strapi.entityService.findMany(uid, query);
3132

32-
return slugs.length ? slugs[0] : null;
33+
// single
34+
if (slugs && _.isPlainObject(slugs)) {
35+
return slugs;
36+
}
37+
38+
// collection
39+
if (slugs && _.isArray(slugs) && slugs.length) {
40+
return slugs[0];
41+
}
42+
43+
// no result
44+
return null;
3345
},
3446
});

server/utils/transformEntry.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
// copied from: https://raw.githubusercontent.com/strapi/strapi/master/packages/core/strapi/lib/core-api/controller/transform.js
4+
5+
const { isNil, isPlainObject } = require('lodash/fp');
6+
7+
const transformResponse = (resource, meta = {}, { contentType } = {}) => {
8+
if (isNil(resource)) {
9+
return resource;
10+
}
11+
12+
return {
13+
data: transformEntry(resource, contentType),
14+
meta,
15+
};
16+
};
17+
18+
const transformComponent = (data, component) => {
19+
if (Array.isArray(data)) {
20+
return data.map((datum) => transformComponent(datum, component));
21+
}
22+
23+
const res = transformEntry(data, component);
24+
25+
if (isNil(res)) {
26+
return res;
27+
}
28+
29+
const { id, attributes } = res;
30+
return { id, ...attributes };
31+
};
32+
33+
const transformEntry = (entry, type) => {
34+
if (isNil(entry)) {
35+
return entry;
36+
}
37+
38+
if (Array.isArray(entry)) {
39+
return entry.map((singleEntry) => transformEntry(singleEntry, type));
40+
}
41+
42+
if (!isPlainObject(entry)) {
43+
throw new Error('Entry must be an object');
44+
}
45+
46+
const { id, ...properties } = entry;
47+
48+
const attributeValues = {};
49+
50+
for (const key in properties) {
51+
const property = properties[key];
52+
const attribute = type && type.attributes[key];
53+
54+
if (attribute && attribute.type === 'relation') {
55+
const data = transformEntry(property, strapi.contentType(attribute.target));
56+
57+
attributeValues[key] = { data };
58+
} else if (attribute && attribute.type === 'component') {
59+
attributeValues[key] = transformComponent(property, strapi.components[attribute.component]);
60+
} else if (attribute && attribute.type === 'dynamiczone') {
61+
if (isNil(property)) {
62+
attributeValues[key] = property;
63+
}
64+
65+
attributeValues[key] = property.map((subProperty) => {
66+
return transformComponent(subProperty, strapi.components[subProperty.__component]);
67+
});
68+
} else if (attribute && attribute.type === 'media') {
69+
const data = transformEntry(property, strapi.contentType('plugin::upload.file'));
70+
71+
attributeValues[key] = { data };
72+
} else {
73+
attributeValues[key] = property;
74+
}
75+
}
76+
77+
return {
78+
id,
79+
attributes: attributeValues,
80+
};
81+
};
82+
83+
module.exports = {
84+
transformResponse,
85+
};

0 commit comments

Comments
 (0)