This was very interesting read. I was wondering if there is a way to use similar pattern to be able to override existing routes vs creating extra ones.
I looked in the docs, but was not able to find a good example of overriding a route, just a way to configure them.
So I tried this and it worked. Not the best implementation, just experimenting based on the above example.
"use strict";
/**
* post router.
*/
const { createCoreRouter } = require("@strapi/strapi").factories;
const defaultRouter = createCoreRouter("api::post.post");
const customRouter = (innerRouter, routeOveride = [], extraRoutes = []) => {
let routes;
return {
get prefix() {
return innerRouter.prefix;
},
get routes() {
if (!routes) routes = innerRouter.routes;
const newRoutes = routes.map((route) => {
let found = false;
routeOveride.forEach((overide) => {
if (
route.handler === overide.handler &&
route.method === overide.method
) {
found = overide;
}
});
return found || route;
});
return newRoutes.concat(extraRoutes);
},
};
};
const myExtraRoutes = [];
const myOverideRoute = [
{
method: "GET",
path: "/posts/:slug",
handler: "api::post.post.findOne",
},
];
module.exports = customRouter(defaultRouter, myOverideRoute, myExtraRoutes);
any thoughts?