Strapi plugin logo for Transformer

Transformer
Plugin verified by Strapi

A plugin for Strapi Headless CMS that provides the ability to transform the API response.

strapi-plugin-transformer

A plugin for Strapi that provides the ability to transform the API request and/or response.

Downloads Install size Package version

Requirements

The installation requirements are the same as Strapi itself and can be found in the documentation on the Quick Start page in the Prerequisites info card.

Support

IMPORTANT: GraphQL is not supported, see #23 and #13 for additional context.

Strapi versions

  • v4.x.x

NOTE: While this plugin may work with the older Strapi versions, they are not supported, it is always recommended to use the latest version of Strapi.

Installation

npm install strapi-plugin-transformer

# OR

yarn add strapi-plugin-transformer

Configuration

The plugin configuration is stored in a config file located at ./config/plugins.js. If this file doesn't exist, you will need to create it.

Minimal Configuration

1module.exports = ({ env }) => ({
2  // ..
3 'transformer': {
4    enabled: true,
5    config: {}
6  },
7  // ..
8});

Sample configuration

1module.exports = ({ env }) => ({
2  // ..
3 'transformer': {
4    enabled: true,
5    config: {
6      responseTransforms: {
7        removeAttributesKey: true,
8        removeDataKey: true,
9      },
10      requestTransforms : {
11        wrapBodyWithDataKey: true
12      },
13      hooks: {
14        preResponseTransform : (ctx) => console.log('hello from the preResponseTransform hook!'),
15        postResponseTransform : (ctx) => console.log('hello from the postResponseTransform hook!')
16      },
17      contentTypeFilter: {
18        mode: 'allow',
19        uids: {
20          'api::article.article': true,
21          'api::category.category': {
22            'GET':true,
23          }
24        }
25      },
26      plugins: {
27        ids: {
28          'slugify': true,
29        }
30      }
31    }
32  },
33  // ..
34});

IMPORTANT NOTE: Make sure any sensitive data is stored in env files.

The Complete Plugin Configuration Object

PropertyDescriptionTypeDefaultRequired
responseTransformsThe transformations to enable for the API responseObjectN/ANo
responseTransforms.removeAttributesKeyRemoves the attributes key from the responseBooleanfalseNo
responseTransforms.removeDataKeyRemoves the data key from the responseBooleanfalseNo
requestTransformsThe transformations to enable for an API requestObjectN/ANo
requestTransforms.wrapBodyWithDataKeyAuto wraps the body of PUT and POST requests with a data keyBooleanfalseNo
hooksThe hooks to enable for the pluginObjectN/ANo
hooks.preResponseTransformA hook that executes before the Response Transforms are appliedFunction() => {}No
hooks.postResponseTransformA hook that executes after the Response Transforms are appliedFunction() => {}No
contentTypeFilterThe content types to deny or allow the middleware to be registered on. Defaults to allow all content typesObjectN/ANo
contentTypeFilter.modeThe filter mode. The current supported modes are none, allow or denyString'none'No
contentTypeFilter.uidsThe uids to filterObject{}No
pluginsThe plugins to deny or allow the middleware to be registered on. Defaults to deny all pluginsObjectN/ANo
plugins.modeThe filter mode. The current supported modes are none, allow or denyString'none'No
plugins.idsThe plugin ids to filter. The plugin id is the name you set in the plugins.js fileObject{}No

Usage

Once the plugin has been installed, configured and enabled any request to the Strapi API will be auto transformed.

Current Supported Transformations

Remove the attributes key

This response transform will remove the attributes key from the response and shift all of its properties up one level.

Before

1{
2  "data": {
3    "id": 1,
4    "attributes": {
5      "title": "Lorem Ipsum",
6      "createdAt": "2022-02-11T01:51:49.902Z",
7      "updatedAt": "2022-02-11T01:51:52.797Z",
8      "publishedAt": "2022-02-11T01:51:52.794Z",
9      "ipsum": {
10        "data": {
11          "id": 2,
12          "attributes": {
13            "title": "Dolor sat",
14            "createdAt": "2022-02-15T03:45:32.669Z",
15            "updatedAt": "2022-02-17T00:30:02.573Z",
16            "publishedAt": "2022-02-17T00:07:49.491Z",
17          },
18        },
19      },
20    },
21  },
22  "meta": {},
23}

After

1{
2  "data": {
3    "id": 1,
4    "title": "Lorem Ipsum",
5    "createdAt": "2022-02-11T01:51:49.902Z",
6    "updatedAt": "2022-02-11T01:51:52.797Z",
7    "publishedAt": "2022-02-11T01:51:52.794Z",
8    "ipsum": {
9      "data": {
10        "id": 2,
11        "title": "Dolor sat",
12        "createdAt": "2022-02-15T03:45:32.669Z",
13        "updatedAt": "2022-02-17T00:30:02.573Z",
14        "publishedAt": "2022-02-17T00:07:49.491Z",
15      },
16    },
17  },
18  "meta": {},
19}

Remove the data key

This response transform will remove the data key from the response and shift the attribute data to be top level.

Before

1{
2  "data": {
3    "id": 1,
4    "attributes": {
5      "title": "Lorem Ipsum",
6      "createdAt": "2022-02-11T01:51:49.902Z",
7      "updatedAt": "2022-02-11T01:51:52.797Z",
8      "publishedAt": "2022-02-11T01:51:52.794Z",
9      "ipsum": {
10        "data": {
11          "id":2,
12          "attributes": {
13            "title": "Dolor sat",
14            "createdAt": "2022-02-15T03:45:32.669Z",
15            "updatedAt": "2022-02-17T00:30:02.573Z",
16            "publishedAt": "2022-02-17T00:07:49.491Z",
17          },
18        },
19      },
20    },
21  },
22  "meta": {},
23}

After

1{
2  "data": {
3    "id": 1,
4    "attributes": {
5      "title": "Lorem Ipsum",
6      "createdAt": "2022-02-11T01:51:49.902Z",
7      "updatedAt": "2022-02-11T01:51:52.797Z",
8      "publishedAt": "2022-02-11T01:51:52.794Z",
9      "ipsum": {
10        "id":2,
11        "attributes": {
12          "title": "Dolor sat",
13          "createdAt": "2022-02-15T03:45:32.669Z",
14          "updatedAt": "2022-02-17T00:30:02.573Z",
15          "publishedAt": "2022-02-17T00:07:49.491Z",
16        },
17      },
18    },
19  },
20  "meta": {},
21}

Auto wrap the body content with a data key

This request transform will auto wrap the body content with a surrounding data key on all enabled routes.

Before

1{
2  "title": "Lorem Ipsum",
3}

After

1{
2  "data": {
3    "title": "Lorem Ipsum",
4  }
5}

Supported Headers

NameDescriptionTypeDefaultRequired
Strapi-Transformer-IgnoreIndicates if transform should be ignored for this requestString'false'No

CORS

By default, CORS will block any custom headers. To enable custom headers to be accepted the cors middlware headers property must include the custom header(s) that should be accepted.

Example CORS configuration

1module.exports = [
2  // ..
3  {
4    name: 'strapi::cors',
5    config: {
6      headers: ['Strapi-Transformer-Ignore'],
7    },
8  },
9  // ..
10]

Bugs

If any bugs are found please report them as a Github Issue

Install now

npm install strapi-plugin-transformer

STATS

140 GitHub stars3846 weekly downloads

Last updated

432 days ago

Strapi Version

4.0.7 and above

Author

github profile image for @ComfortablyCoding
@ComfortablyCoding

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.