Skip to content

Global Middleware Considerations

Francisco Ruiz edited this page Aug 25, 2024 · 1 revision

Global Middleware considerations

Similar to service middleware, you can use the container to access the registered services. This enables you to have full control for implementing the necessary logic in your global middleware, but it has the same limitation. If you call any service within the global middleware, you will get an infinite loop in your global middleware.

To avoid the infinite loop, you can use the proxyMiddleware option to disable the middleware proxy, but you need to do this for each service that you want to use in the global middleware.

container.middleware.addGlobal((next, context, args) => {
  const { container } = context;

  // Get the services
  const services = container.getAll({ proxyMiddleware: false });

  // Now we can use the services without the infinite loop
  const random = services.Service1().getRandom();
  const uniqId = services.Service2().getUniqId();

  return next(args);
})

1. Getting Started

2. Core Concepts

3. Middleware

4. Caching and Memoization

5. Typescript

6. Considerations

7. API Reference

8. Examples and Use Cases

9. Contributing

We welcome contributions from the community! Whether it's reporting a bug, suggesting a feature, or contributing code, your involvement is key to making Packet.js better. Please check out this section for more details.

10. Support

If you encounter any issues or bugs while using packetjs, or if you have any feature requests, please feel free to open an issue in our GitHub repository. You can report issues, track progress, and engage in discussions with the community via the following link:

11. FAQ

If you have any questions or need further assistance, feel free to reach out via the discussion board.

12. License

Clone this wiki locally