版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

115 строки
4.0 KiB

  1. import { shallowRef, ref } from 'vue-demi';
  2. import { isString, until } from '@vueuse/shared';
  3. import axios, { AxiosError } from 'axios';
  4. var __defProp = Object.defineProperty;
  5. var __defProps = Object.defineProperties;
  6. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  7. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  8. var __hasOwnProp = Object.prototype.hasOwnProperty;
  9. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  10. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  11. var __spreadValues = (a, b) => {
  12. for (var prop in b || (b = {}))
  13. if (__hasOwnProp.call(b, prop))
  14. __defNormalProp(a, prop, b[prop]);
  15. if (__getOwnPropSymbols)
  16. for (var prop of __getOwnPropSymbols(b)) {
  17. if (__propIsEnum.call(b, prop))
  18. __defNormalProp(a, prop, b[prop]);
  19. }
  20. return a;
  21. };
  22. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  23. function useAxios(...args) {
  24. const url = typeof args[0] === "string" ? args[0] : void 0;
  25. const argsPlaceholder = isString(url) ? 1 : 0;
  26. let defaultConfig = {};
  27. let instance = axios;
  28. let options = { immediate: !!argsPlaceholder, shallow: true };
  29. const isAxiosInstance = (val) => !!(val == null ? void 0 : val.request);
  30. if (args.length > 0 + argsPlaceholder) {
  31. if (isAxiosInstance(args[0 + argsPlaceholder]))
  32. instance = args[0 + argsPlaceholder];
  33. else
  34. defaultConfig = args[0 + argsPlaceholder];
  35. }
  36. if (args.length > 1 + argsPlaceholder) {
  37. if (isAxiosInstance(args[1 + argsPlaceholder]))
  38. instance = args[1 + argsPlaceholder];
  39. }
  40. if (args.length === 2 + argsPlaceholder && !isAxiosInstance(args[1 + argsPlaceholder]) || args.length === 3 + argsPlaceholder)
  41. options = args[args.length - 1];
  42. const response = shallowRef();
  43. const data = options.shallow ? shallowRef() : ref();
  44. const isFinished = ref(false);
  45. const isLoading = ref(false);
  46. const isAborted = ref(false);
  47. const error = shallowRef();
  48. const cancelTokenSource = axios.CancelToken.source;
  49. let cancelToken = cancelTokenSource();
  50. const abort = (message) => {
  51. if (isFinished.value || !isLoading.value)
  52. return;
  53. cancelToken.cancel(message);
  54. cancelToken = cancelTokenSource();
  55. isAborted.value = true;
  56. isLoading.value = false;
  57. isFinished.value = false;
  58. };
  59. const loading = (loading2) => {
  60. isLoading.value = loading2;
  61. isFinished.value = !loading2;
  62. };
  63. const waitUntilFinished = () => new Promise((resolve, reject) => {
  64. until(isFinished).toBe(true).then(() => resolve(result)).catch(reject);
  65. });
  66. const then = (onFulfilled, onRejected) => waitUntilFinished().then(onFulfilled, onRejected);
  67. const execute = (executeUrl = url, config = {}) => {
  68. error.value = void 0;
  69. const _url = typeof executeUrl === "string" ? executeUrl : url != null ? url : config.url;
  70. if (_url === void 0) {
  71. error.value = new AxiosError(AxiosError.ERR_INVALID_URL);
  72. isFinished.value = true;
  73. return { then };
  74. }
  75. abort();
  76. loading(true);
  77. instance(_url, __spreadProps(__spreadValues(__spreadValues({}, defaultConfig), typeof executeUrl === "object" ? executeUrl : config), { cancelToken: cancelToken.token })).then((r) => {
  78. var _a;
  79. response.value = r;
  80. const result2 = r.data;
  81. data.value = result2;
  82. (_a = options.onSuccess) == null ? void 0 : _a.call(options, result2);
  83. }).catch((e) => {
  84. var _a;
  85. error.value = e;
  86. (_a = options.onError) == null ? void 0 : _a.call(options, e);
  87. }).finally(() => loading(false));
  88. return { then };
  89. };
  90. if (options.immediate && url)
  91. execute();
  92. const result = {
  93. response,
  94. data,
  95. error,
  96. finished: isFinished,
  97. loading: isLoading,
  98. isFinished,
  99. isLoading,
  100. cancel: abort,
  101. isAborted,
  102. canceled: isAborted,
  103. aborted: isAborted,
  104. isCanceled: isAborted,
  105. abort,
  106. execute
  107. };
  108. return __spreadProps(__spreadValues({}, result), {
  109. then
  110. });
  111. }
  112. export { useAxios };