Transform and simplify the API response in Strapi 4
You may want to remove the data from the API response
For example:
{
data: {
attributes: {
name: "Some User Name",
age: 22
}
}
}
Forcing you to write or destructure more levels like: data.attributes.name
Becomes:
{
data: {
// attributes: { // Gone for good
name: "Some User Name",
age: 22
//}
}
}
Now makes you to chain less levels in your frontend or app: data.name
Install the Plugin:
npm install strapi-plugin-transformer
OR
yarn add strapi-plugin-transformer
Add this to your config file:
./strapi-project/config/plugins.ts
export default ({ env }) => ({
transformer: {
enabled: true,
config: {
responseTransforms: {
removeAttributesKey: true,
removeDataKey: true,
},
},
},
});
Last updated on