Function middlewareProviderCreator

Generates a configurable middleware provider for the application to use. This is a mix of a middleware creator and a middleware provider: it gives you the flexibility of the creator, with the resources of the provider.

  class MyServiceWithMiddleware {
constructor(private readonly responsesBuilder: ResponsesBuilder) {}
getMiddleware() {
return (_, res) => {
this.responsesBuilder.json({ res, data: { message } });
};
}
}
const myMiddlewareProvider = middlewareProviderCreator(
({ serviceName = 'myMiddleware' }) =>
(app) => {
app.set(
serviceName,
() => new MyServiceWithMiddleware(app.get('responsesBuilder')),
);
return middleware(() =>
app.get<MyServiceWithMiddleware>(serviceName).getMiddleware(),
);
},
);

// ...
container.use(myMiddlewareProvider);
  container.use(myMiddlewareProvider({ serviceName: 'something' }));