Is there is a way to access api level middleware in strapi v4.5

System Information
  • Strapi Version: v4.5.6
  • Operating System: windows 11
  • Database: postgres
  • Node Version: v16.14.0
  • NPM Version: v8.6.0
  • Yarn Version: 1.22.19

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();
};
};

/config/middlewares.ts/
“api::funeral-application.best”,

1 Like

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.

See this doc:

Yes. I have added middleware to the route. But i didn’t get any console.log() message on terminal. pls find the below screen shot

1 Like

your middleware definition looks correct to me.

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).

Once i click funeral-application menu GET request will occurred. At that time as well middleware need to be access.

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.

So, we cannot access the middleware at the admin panel level ??

Actually i need to write the middle to handle validates the at the admin panel level. Could u pls help on this

The content-api middlewares are not accessable from the admin panel to the best of my knowledge.

What is it you want to achieve/validate?

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.

So to check required fields validations i am writing middleware. Is there any option to check validation while creating the data?

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.

1 Like

Thank you for a such a detailed answer. Thanks a lot.

1 Like