Is there is a way to access api level middlewares in Strapi. I have added middleware in api level but its effecting other api calls as well.
/* src/[api]/middlewares/best.ts*/
export default (config, { strapi }: { strapi: Strapi }) => {
return async (ctx, next) => {
strapi.log.info(‘In best middleware.’);
await next();
};
};
Yes.
Don’t add the middleware in /config/middlewares.ts (this adds it to all routes).
Add the middleware to the routes you want to apply it on instead.
so when the application starts successfully with that configuration it means that strapi found the middleware. otherwise it would crash on startup with the message that the middleware could not be found.
what’s the request you’re issuing? should be GET /api/funeral-applications because you configured the find route.
API level I am getting console.log messages in terminal. But I need in frontend level (means once i add new data from strapi cms i need to access that middleware).
The Strapi admin panel has its own api which is different from the content-api within the api/ folder. It doesn’t use the same routes, middlewares, controllers, services.
Maybe a lifecycle hook is what you need instead? I.e. a beforeFindMany is available.
In admin panel, While creating the data I need to check required field validations. if i enter wrong details then it should throw an error saying “wrong details you have been enter”. In strapi admin panel required fields are not validating in “draft mode”. It works only while clicking “publish” button.
Tbh this sounds like a Strapi bug to me. I would expect field validation to happen every time I create / update an entity, not just on publish. You might want to create an issue on GitHub for that.
AFAIK a workaround for you could be to use the beforeCreate / beforeUpdate lifecycle hooks. Do your field validation there and throw errors if it fails. IIRC the errors will be displayed in the admin panel.