版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 lines
4.0 KiB

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