简体   繁体   English

如何克隆 axios 实例

[英]How to clone an axios instance

I have a global axios instance which I use across my application.我有一个全局 axios 实例,我在我的应用程序中使用它。 I want to update headers locally for a particular request.我想针对特定请求在本地更新标头。 But the headers update is updating the global defaults.但是标题更新正在更新全局默认值。 I wanted to understand the best way of doing this.我想了解执行此操作的最佳方法。 Currently I am hacking my way into resetting the headers.目前我正在设法重置标题。 Also toyed around with the idea of deep cloning the global axios instance.还玩弄了深度克隆全局 axios 实例的想法。 It just feels like an important feature to have, but couldn't find anything the docs, except for a github issues talking about sub-instances.它只是感觉像是一个重要的功能,但找不到任何文档,除了 github 问题谈论子实例。 ( https://github.com/axios/axios/issues/1170 ) https://github.com/axios/axios/issues/1170

EDIT: sorry for not providing code.编辑:抱歉没有提供代码。 This is my setup to give an idea: Following is my global axiosClient(in file apiClient.js ), with some interceptors added(not shown in code).这是我给出一个想法的设置:以下是我的全局 axiosClient(在文件apiClient.js中),添加了一些拦截器(代码中未显示)。

const axiosClient = axios.create({
baseURL,
headers: {
Authorization: <bearer_token>,
'Content-Type': 'application/json',
.
 }
});

In my modules, I import the same client to make api requests as such:在我的模块中,我导入相同的客户端来发出 api 请求,如下所示:

import axiosClient from '../apiClient';

export function someRequest({ file }) {
  let formData = new FormData();
  formData.append('file', file);
  const initHeader = axiosClient.defaults.headers['Content-Type'];
  axiosClient.defaults.headers['Content-Type'] = 'multipart/form-data'; // I want to make this change only for the local instance
  const request = axiosClient.post('parse-rebalance-data', formData);
  axiosClient.defaults.headers['Content-Type'] = initHeader; //I have to reset the changes I made to the axiosClient
  return request;
}

Now my question again is,(1) do I need to do it in this hacky way, or(2) should I look into deep cloning a local copy, or(3) is there a documented way to doing it which I am missing.现在我的问题又是,(1) 我是否需要以这种 hacky 的方式来做,或者 (2) 我应该研究深度克隆本地副本,或者 (3) 是否有记录的方法来做这件事,我错过了.

All that Axios instances do, is set default values for the configuration. Axios 实例所做的所有事情都是为配置设置默认值 You can, however pass any other configuration to each call by using the config parameter, as you would with the global Axios:但是,您可以使用config参数将任何其他配置传递给每个调用,就像使用全局 Axios 一样:

import axiosClient from '../apiClient';

export function someRequest({ file }) {
  let formData = new FormData();
  formData.append('file', file);
  return axiosClient.post('parse-rebalance-data', formData, {headers: {'Content-Type': 'multipart/form-data'}});
}

The headers specified in the config argument are merged with those in the defaults. config参数中指定的标头与默认值中的标头合并。

You can do it like this:你可以这样做:

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'}
});

More you can read here:您可以在这里阅读更多内容:

https://axios-http.com/docs/instance https://axios-http.com/docs/instance

You can actually create an instance from another instance.您实际上可以从另一个实例创建一个实例。

So for example you could have:因此,例如,您可以:

// src/services/http.js

import axios from 'axios';

const http = axios.create({
  headers: {
    Accept: 'application/json'
  },
  withCredentials: true
});

export default http;

And then where you need a more specific instance:然后你需要一个更具体的实例:

// src/services/pouic.js

import http from './services/http';

// Defines common base URL for all services in this module.
const httpPouic = http.create({
  baseURL: 'https://pouic.com/api'
});

export default {
  getPouic: async function () {
    const pouics = await httpPouic({
      method: 'get'
      url: '/pouic-list'
    });

    return pouics.data;
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM