mattmilburn / strapi-plugin-menus

A plugin for Strapi CMS to customize the structure of menus and menu items.
MIT License
110 stars 27 forks source link

Modify/extend endpoints response #155

Open javier-creativedog-agency opened 6 months ago

javier-creativedog-agency commented 6 months ago

Hi, first of all, great plugin! It has been very useful.

We are trying to modify/extend the response of one endpoint: /api/menus/:ID, basically we need to add some data to the response object. and we are using this guide as reference: https://docs.strapi.io/dev-docs/plugins-extension#within-the-extensions-folder.

//File: src/extensions/menus/strapi-server.js

module.exports = (plugin) => {
  const defaultAttrs = plugin.contentTypes["menu-item"].schema.attributes;

  const customAttrs = {
    category: {
      type: "relation",
      relation: "oneToOne",
      target: "api::category.category",
    },
  };

  plugin.contentTypes["menu-item"].schema.attributes = {
    ...defaultAttrs,
    ...customAttrs,
  };

  console.log(plugin.controllers.menu);

  return plugin;
};

We used this strategy in previous developments modifying plugins but this time when trying to get the required controller (plugin.controllers.menu) instead of getting an object with the default methods like in any other plugin (find, findOne, create, etc.), we are getting an anonymous function, presumably because plugin.controllers.menu is set via createCoreController function, so we can't modify the findOne method like in others plugins.

Our question is: There is a preferred way to extend or modify the response specific to this plugin? Is it even possible?

Thanks in advance for your help!

mattmilburn commented 5 months ago

Hi @javier-creativedog-agency Can you tell me which Strapi version you are using and which version of the Menus plugin as well? If you're able to share your package.json that would be very helpful 👍🏻

If you were able to do this previously but it suddenly stopped working, it makes me wonder if Strapi itself changed something after an upgrade.