Skip to content

Custom properties for config #2203

@someden

Description

@someden

So, if I understand it right, in 0.19.0 you've added mergeConfig
And this function filter config and remove custom properties from it.
It broke my solution for cancellation:

How it works:

const requestManager = new RequestManager();
const instance = axios.create();
requestManager.patchAxiosInstance(instance);

const search = q =>
  instance.get('/search', {
    cancelable: true, // this is no longer possible in 0.19.0
    params: { q },
  });

Screenshot 2019-06-05 at 16 30 01

The solution:

class RequestManager {
  constructor() {
    this.pendingRequests = new Map();
  }

  patchAxiosInstance(instance) {
    instance.interceptors.request.use(config => {
      if (!config.cancelable) return config; // this is no longer possible in 0.19.0

      const source = axios.CancelToken.source();
      const requestId = `${config.method}_${config.url}`;
      const cancelToken = source.token;
      this.addRequest(requestId, source.cancel);
      return { ...config, cancelToken, requestId };
    });

    instance.interceptors.response.use(response => {
      const { requestId } = response.config; // this is no longer possible in 0.19.0
      if (requestId) {
        this.removeRequest(requestId);
      }
      return response;
    });
  }

  addRequest(requestId, cancelFn) {
    this.cancelRequest(requestId);
    this.pendingRequests.set(requestId, cancelFn);
  }

  removeRequest(requestId) {
    this.pendingRequests.delete(requestId);
  }

  cancelRequest(requestId) {
    if (this.pendingRequests.has(requestId)) {
      const cancelFn = this.pendingRequests.get(requestId);
      cancelFn();
      this.removeRequest(requestId);
    }
  }
}

So, is it possible to relax filter in mergeConfig for custom properties? Because sometimes it is needed and very helpful in interceptors or somewhere else.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions