I've started to play with middlewares, it's great !
Here's an example of how I can inject a middleware when calling endpoints /api/playlists or /api/playlists/:id (I edited the file src/api/playlist/routes/playlist.js).
module.exports = createCoreRouter('api::playlist.playlist', {
 config: {
   find: {
     middlewares: ['api::playlist.playlist.find']
   },
   findOne: {
     middlewares: ['api::playlist.playlist.find-one']
   },
 },
})

Of course, I also created my middlewares in src/api/playlist/middlewares/find.js and src/api/playlist/middlewares/find-one.js)
But know, I want to add another middleware to update the responses returned by the API when calling /api/users or /api/users/:id.
Since there is no directory src/api/user in the filetree, how should I register a middleware for this ?
Thanks

In Strapi V4, How should I register a middleware to alter the responses returned by /user or /user/:id?
I finally found out :

create the file src/extensions/user-permissions/strapi-server.js

This is mine. It registers a middleware for each of those endpoints:

GET /users/me : plugin::spiff-api.user-me

GET /users : plugin::spiff-api.user-find

GET /users/:id : plugin::spiff-api.user-find-one
("use strict");

module.exports = (plugin) => {
  //if you see this, the configuration do loads:
  console.log("Custom strapi-server.js for user-permissions");

  //get api routes for 'user-permissions'
  const apiRoutes = plugin.routes['content-api'].routes;

  //add middleware for GET /users/me
  apiRoutes
    .filter(route => route.handler === 'user.me')
    .map(route => {
      route.config.middlewares = [
        ...(route.config.middlewares || []),
        'plugin::spiff-api.user-me'//middleware name
      ];
      return route;
    });

    //add middleware for GET /users/:id
    apiRoutes
      .filter(route => route.handler === 'user.findOne')
      .map(route => {
        route.config.middlewares = [
          ...(route.config.middlewares || []),
          'plugin::spiff-api.user-find-one'//middleware name
        ];
        return route;
      });

  //add middleware for GET /users
  apiRoutes
    .filter(route => route.handler === 'user.find')
    .map(route => {
      console.log(route)
      route.config.middlewares = [
        ...(route.config.middlewares || []),
        'plugin::spiff-api.user-find'//middleware name
      ];
      return route;
    });

return plugin;
};



Then, create your middlewares middleware, eg.
    export default (config, { strapi })=> {
      return async (ctx, next) => {
        console.info("running middleware 'user-find-one.js'");
        console.log();

        //update your query here if needed
        //eg. populate 'favoritePosts'
        ctx.query.populate = {
          ...ctx.query.populate ?? {},
          favoritePosts: {}
        }

        const controller = strapi.plugin('users-permissions').controller('user');
        await controller.findOne(ctx);//this populates ctx.body
        const response = ctx.body;

        //update your response here if needed

        ctx.body = response;
        
        await next();//not sure why this stands for

      }
    }

