• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A plugin for Strapi Headless CMS that provides the ability to transform the API request or 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

module.exports = ({ env }) => ({
  // ..
 'transformer': {
    enabled: true,
    config: {}
  },
  // ..
});

Sample configuration

module.exports = ({ env }) => ({
  // ..
 'transformer': {
    enabled: true,
    config: {
      responseTransforms: {
        removeAttributesKey: true,
        removeDataKey: true,
      },
      requestTransforms : {
        wrapBodyWithDataKey: true
      },
      hooks: {
        preResponseTransform : (ctx) => console.log('hello from the preResponseTransform hook!'),
        postResponseTransform : (ctx) => console.log('hello from the postResponseTransform hook!')
      },
      contentTypeFilter: {
        mode: 'allow',
        uids: {
          'api::article.article': true,
          'api::category.category': {
            'GET':true,
          }
        }
      },
      plugins: {
        ids: {
          'slugify': true,
        }
      }
    }
  },
  // ..
});

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

The Complete Plugin Configuration Object

Property Description Type Default Required
responseTransforms The transformations to enable for the API response Object N/A No
responseTransforms.removeAttributesKey Removes the attributes key from the response Boolean false No
responseTransforms.removeDataKey Removes the data key from the response Boolean false No
requestTransforms The transformations to enable for an API request Object N/A No
requestTransforms.wrapBodyWithDataKey Auto wraps the body of PUT and POST requests with a data key Boolean false No
hooks The hooks to enable for the plugin Object N/A No
hooks.preResponseTransform A hook that executes before the Response Transforms are applied Function () => {} No
hooks.postResponseTransform A hook that executes after the Response Transforms are applied Function () => {} No
contentTypeFilter The content types to deny or allow the middleware to be registered on. Defaults to allow all content types Object N/A No
contentTypeFilter.mode The filter mode. The current supported modes are none, allow or deny String 'none' No
contentTypeFilter.uids The uids to filter Object {} No
plugins The plugins to deny or allow the middleware to be registered on. Defaults to deny all plugins Object N/A No
plugins.mode The filter mode. The current supported modes are none, allow or deny String 'none' No
plugins.ids The plugin ids to filter. The plugin id is the name you set in the plugins.js file Object {} 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

{
  "data": {
    "id": 1,
    "attributes": {
      "title": "Lorem Ipsum",
      "createdAt": "2022-02-11T01:51:49.902Z",
      "updatedAt": "2022-02-11T01:51:52.797Z",
      "publishedAt": "2022-02-11T01:51:52.794Z",
      "ipsum": {
        "data": {
          "id": 2,
          "attributes": {
            "title": "Dolor sat",
            "createdAt": "2022-02-15T03:45:32.669Z",
            "updatedAt": "2022-02-17T00:30:02.573Z",
            "publishedAt": "2022-02-17T00:07:49.491Z",
          },
        },
      },
    },
  },
  "meta": {},
}

After

{
  "data": {
    "id": 1,
    "title": "Lorem Ipsum",
    "createdAt": "2022-02-11T01:51:49.902Z",
    "updatedAt": "2022-02-11T01:51:52.797Z",
    "publishedAt": "2022-02-11T01:51:52.794Z",
    "ipsum": {
      "data": {
        "id": 2,
        "title": "Dolor sat",
        "createdAt": "2022-02-15T03:45:32.669Z",
        "updatedAt": "2022-02-17T00:30:02.573Z",
        "publishedAt": "2022-02-17T00:07:49.491Z",
      },
    },
  },
  "meta": {},
}

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

{
  "data": {
    "id": 1,
    "attributes": {
      "title": "Lorem Ipsum",
      "createdAt": "2022-02-11T01:51:49.902Z",
      "updatedAt": "2022-02-11T01:51:52.797Z",
      "publishedAt": "2022-02-11T01:51:52.794Z",
      "ipsum": {
        "data": {
          "id":2,
          "attributes": {
            "title": "Dolor sat",
            "createdAt": "2022-02-15T03:45:32.669Z",
            "updatedAt": "2022-02-17T00:30:02.573Z",
            "publishedAt": "2022-02-17T00:07:49.491Z",
          },
        },
      },
    },
  },
  "meta": {},
}

After

{
  "data": {
    "id": 1,
    "attributes": {
      "title": "Lorem Ipsum",
      "createdAt": "2022-02-11T01:51:49.902Z",
      "updatedAt": "2022-02-11T01:51:52.797Z",
      "publishedAt": "2022-02-11T01:51:52.794Z",
      "ipsum": {
        "id":2,
        "attributes": {
          "title": "Dolor sat",
          "createdAt": "2022-02-15T03:45:32.669Z",
          "updatedAt": "2022-02-17T00:30:02.573Z",
          "publishedAt": "2022-02-17T00:07:49.491Z",
        },
      },
    },
  },
  "meta": {},
}

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

{
  "title": "Lorem Ipsum",
}

After

{
  "data": {
    "title": "Lorem Ipsum",
  }
}

Supported Headers

Name Description Type Default Required
Strapi-Transformer-Ignore Indicates if transform should be ignored for this request String '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

module.exports = [
  // ..
  {
    name: 'strapi::cors',
    config: {
      headers: ['Strapi-Transformer-Ignore'],
    },
  },
  // ..
]

Bugs

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

More Repositories

1

awesome-strapi

A curated list of awesome things related to Strapi
JavaScript
1,500
star
2

strapi-tool-dockerize

Easy add support for docker to your strapi project
JavaScript
531
star
3

strapi-provider-upload-google-cloud-storage

Google Cloud Storage Upload Provider for Strapi
JavaScript
209
star
4

strapi-plugin-local-image-sharp

Dynamically resize, format and optimize images based on url modifiers.
JavaScript
64
star
5

jekyll-strapi

Jekyll plugin to retrieve content from any Strapi API.
Ruby
57
star
6

strapi-plugin-io

A plugin for Socket IO integration with Strapi CMS.
JavaScript
55
star
7

strapi-plugin-publisher

A plugin for Strapi Headless CMS that provides the ability provides the ability to easily schedule publishing and unpublishing of any content type.
JavaScript
47
star
8

strapi-tool-deployify

Easy deploy strapi to cloud platforms
JavaScript
45
star
9

strapi-plugin-slugify

A plugin for Strapi Headless CMS that provides the ability to auto slugify a field for any content type.
JavaScript
45
star
10

strapi-plugin-redis

Plugin used to centralize management of Redis connections in Strapi
JavaScript
41
star
11

strapi-plugin-website-builder

A plugin for Strapi Headless CMS that provides the ability to trigger website builds manually, periodically or through model events.
JavaScript
35
star
12

strapi-plugin-url-alias

🌐 Unique, autogenerated URL paths.
TypeScript
21
star
13

strapi-plugin-protected-populate

JavaScript
20
star
14

strapi-typed-fronend

make strapi types compatible with frontend
TypeScript
14
star
15

website

The repo for the website development competition of the @strapi-community.
TypeScript
11
star
16

strapi-plugin-notes

A plugin for Strapi Headless CMS that provides the ability to add notes to entity records.
JavaScript
9
star
17

strapi-plugin-measurement-protocol

Send data to Google Analytics with Measurement Protocol.
JavaScript
8
star
18

jekyll-blog

Simple Jekyll blog powered by Strapi
Ruby
7
star
19

strapi-plugin-multitenancy

JavaScript
6
star
20

eslint-config

Shared eslint configuration for Strapi v4 plugins & applications.
JavaScript
5
star
21

strapi-plugin-audit

Audit Log plugin for Strapi v4
JavaScript
3
star
22

strapi-plugin-search

A Strapi CMS plugin that provides search engine agnostic sync support
TypeScript
3
star
23

strapi-ai-bot

Discord bot used by the Strapi Community Discord Server
JavaScript
3
star
24

website-backend

JavaScript
1
star
25

docus-theme

Vue
1
star