config.ts 797 B

12345678910111213141516171819202122232425262728293031
  1. import { AxiosRequestConfig } from "axios";
  2. import { excludeProps } from "./utils";
  3. /**
  4. * 默认配置
  5. */
  6. export const defaultConfig: AxiosRequestConfig = {
  7. baseURL: "",
  8. timeout: 10000, //10秒超时
  9. headers: {
  10. Accept: "application/json, text/plain, */*",
  11. "Content-Type": "application/json",
  12. "X-Requested-With": "XMLHttpRequest"
  13. }
  14. };
  15. export function genConfig(config?: AxiosRequestConfig): AxiosRequestConfig {
  16. if (!config) {
  17. return defaultConfig;
  18. }
  19. const { headers } = config;
  20. if (headers && typeof headers === "object") {
  21. defaultConfig.headers = {
  22. ...defaultConfig.headers,
  23. ...headers
  24. };
  25. }
  26. return { ...excludeProps(config!, "headers"), ...defaultConfig };
  27. }
  28. export const METHODS = ["post", "get", "put", "delete", "option", "patch"];