Skip to content

Middleware Example Using Axios

Francisco Ruiz edited this page Sep 15, 2024 · 2 revisions

Middleware example using axios

The following example shows how to add a middleware to intercept the requests for the axios library.

First, we declare the axios instance to the container:

import { Container } from 'packetjs-di';
import axios from 'axios';
import properties from './properties/index.json';
import { Properties } from './@types';

// Create the container
const container = new Container();

// Add configuration properties
container.addProps(properties);

// Add the axios service
container.add('axios', ({ props }: { props: Properties }) => {
  return axios.create({
    baseURL: props.axios.baseURL, // jsonplaceholder.typicode.com
  });
});

Next, we register the middleware:

import { AxiosResponse } from 'axios';
import { Comments, HelperDefinition } from "./@types";

container.middleware.add('axios', async (next, context, args) => {
  // We can access the service name, method name and container
  const { container, methodName } = context;

  // We can override the arguments if needed
  console.log('arguments', args);

  // Call the next middleware and get the response
  const response: AxiosResponse = await next(args);

  // Here we can modify the response
  if (methodName === 'get' && response.status === 200 && response.config.url === '/comments') {
    const { data: comments }: { data: Comments[] } = response;

    // Get the Helper from the container
    const Helper = container.get<HelperDefinition>('Helper');

    // Update comments by reference
    comments.forEach((comment) => {
      comment.random = Helper.getRandom();
      comment.uniqId = Helper.getUniqId();
    });
  }

  return response;
}, {
  priority: 1,
  name: 'axiosService'
});

The context object contains information about the service name, method name and container. You can get any service declared in the container through the Container instance using the get(), getAll() or getFactory() methods.

Here's how to make the request that will be processed by the middleware:

const axios = container.get<Axios>('axios');

axios.get('/comments').then(({ data: comments }) => {
  console.log(comments);
});

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