strapi-community / strapi-plugin-transformer

A plugin for Strapi Headless CMS that provides the ability to transform the API request or response.
https://market.strapi.io/plugins/strapi-plugin-transformer
MIT License
140 stars 22 forks source link

'prefix' not applying on V2.2.0 and Strapi V4.5.5 #58

Closed MO-Lewis closed 1 year ago

MO-Lewis commented 1 year ago

Hi there,

Great plugin! This was extremely handy for people who didn't want Strapi V4's unnecessary data / attributes nesting system.

However, whilst trying to modify the value of "prefix" in my plugin configuration, it seems that none of the changes I make actually affect the resolved prefix when I start the application.

Each time, I delete the .cache and build folder, and run a clean strapi build, nothing seems to make a difference.

Here's my full plugins.js file, so you can see the other plugins I use:

module.exports = ({ env }) => ({
  transformer: {
    enabled: true,
    config: {
      prefix: '/testtest/',
      responseTransforms: {
        removeAttributesKey: true,
        removeDataKey: true
      }
    }
  },
  ckeditor: {
    enabled: true,
    config: {
      editor: {
        link: {
          addTargetToExternalLinks: true,
          decorators: {
            openInNewTab: {
              mode: 'manual',
              label: 'Open in a new tab',
              defaultValue: true,
              attributes: {
                target: '_blank',
                rel: 'noopener noreferrer'
              }
            }
          }
        }
      }
    }
  },
  upload: {
    config: {
      provider: 'aws-s3',
      providerOptions: {
        accessKeyId: env('AWS_ACCESS_KEY_ID'),
        secretAccessKey: env('AWS_ACCESS_SECRET'),
        region: env('AWS_REGION'),
        params: {
            Bucket: env('AWS_BUCKET_NAME'),
        },
      },
      // These parameters could solve issues with ACL public-read access — see [this issue](https://github.com/strapi/strapi/issues/5868) for details
      actionOptions: {
        upload: {
          ACL: null
        },
        uploadStream: {
          ACL: null
        },
      }
    },
  }
});

And here's all of my dependencies from package.json, so you can see what else I'm working with:

"dependencies": {
    "@_sh/strapi-plugin-ckeditor": "^1.1.2",
    "@strapi/plugin-documentation": "4.5.5",
    "@strapi/plugin-graphql": "4.5.5",
    "@strapi/plugin-i18n": "4.5.5",
    "@strapi/plugin-users-permissions": "4.5.5",
    "@strapi/provider-upload-aws-s3": "4.5.5",
    "@strapi/strapi": "4.5.5",
    "axios": "^0.24.0",
    "better-sqlite3": "^7.6.2",
    "i": "^0.3.7",
    "jsonwebtoken": "^8.5.1",
    "jwk-to-pem": "^2.0.5",
    "knex": "0.21.18",
    "npm": "^9.2.0",
    "pg": "^8.7.1",
    "strapi-plugin-ckeditor5": "^1.14.0",
    "strapi-plugin-transformer": "^2.2.0"
  },

Here's a couple of screenshots showing it not working as intended: image image

It would be fantastic if you could find / fix the issue that's going on here! I'll happily provide any other info you might need.

For context, I'm using AWS Cloudfront and a reverse proxy to host the frontend and backend under the same URL. Anything that's api/* defaults to the Strapi backend. This worked on V3, but upgrading to V4 has caused our endpoints to now become api/api/articles, which we expected.

If I can't outright get rid of the second /api/, then I'll just rename it to /api/v1/*, so it doesn't look as bad.

Thanks in advance for any help / advice you can provide!

ComfortablyCoding commented 1 year ago

Hello and welcome!

Great plugin! This was extremely handy for people who didn't want Strapi V4's unnecessary data / attributes nesting system.

Thanks, I saw quite a few people unhappy with the current response structure and figured it would be a useful plugin. Turns out I was correct :)

However, whilst trying to modify the value of "prefix" in my plugin configuration, it seems that none of the changes I make actually affect the resolved prefix when I start the application.

The plugins prefix setting was not intended to be used for rerouting/modifying the API prefix. It was introduced at a time when i was not as familiar with the Strapi API and was used for determining if a request was from the admin or not.

At this point it is not being utilized by the plugin but is a breaking change to remove.

For context, I'm using AWS Cloudfront and a reverse proxy to host the frontend and backend under the same URL. Anything that's api/* defaults to the Strapi backend. This worked on V3, but upgrading to V4 has caused our endpoints to now become api/api/articles, which we expected.

On v4 you can set the API prefix to be top level like it was in v3 by setting the rest.prefix to / in the api configuration. Any other prefix you may wish to use instead is also accepted of course.

Hope that helps! let me know if any further questions or clarification is needed.

MO-Lewis commented 1 year ago

Thank you so much! I didn't realize Strapi can now do this without the plugin. There are a couple of things to be clarified though.

Using just '/' will mean that my Api would be reachable via localhost:3000//articles?populate=*, with a couple slash. I don't want this, so I changed it to ''.

This then caused the issue with the i18n plugin, which stopped me from accessing the admin panel, which has been reported here https://forum.strapi.io/t/customize-api-prefix-for-apis/13648 and here https://forum.strapi.io/t/customize-api-prefix-for-apis/13648/7.

Finally, I changed it to '/v1', and it's now routing how I want it to!

Just thought I'd add those 3 things in case anyone else stumbles upon this issue in the future :).

Thanks again!

ComfortablyCoding commented 1 year ago

Using just '/' will mean that my Api would be reachable via localhost:3000//articles?populate=*, with a couple slash. I don't want this, so I changed it to ''.

I had never done it myself so / was somewhat of a guess :). Glad you figured it out and thanks for the update!