@varlet/axle 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -218,7 +218,11 @@ const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abor
218
218
  immediate: true,
219
219
  // The number of retries after a failed request, defaults 0
220
220
  retry: 3,
221
+ // Whether the value needs to be reset before requesting, defaults false
222
+ resetValue: true,
221
223
  // Request params, defaults {}
224
+ // When params is an object, it will be carried when sending the first request (immediate)
225
+ // When params is a function, it will be carried every time a request is sent.
222
226
  params: { current: 1, pageSize: 10 },
223
227
  // Axios config, see https://axios-http.com
224
228
  config: { headers: {} },
package/README.zh-CN.md CHANGED
@@ -218,7 +218,11 @@ const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abor
218
218
  immediate: true,
219
219
  // 错误重试次数, 默认值: 0
220
220
  retry: 3,
221
+ // 请求前是否需要重置 value, 默认值: false
222
+ resetValue: true,
221
223
  // 请求参数, 默认值: {}
224
+ // 当参数是一个对象时,发送第一个请求(immediate)时将携带它
225
+ // 当参数是一个函数时,每次发送请求时都会携带它。
222
226
  params: { current: 1, pageSize: 10 },
223
227
  // Axios 配置, see https://axios-http.com
224
228
  config: { headers: {} },
package/es/use.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface RunOptions<P> {
4
4
  url?: string;
5
5
  params?: P;
6
6
  config?: AxleRequestConfig;
7
+ resetValue?: boolean;
7
8
  _retryCount?: number;
8
9
  }
9
10
  export type Run<R, P> = (options?: RunOptions<P>) => Promise<R>;
@@ -18,8 +19,9 @@ export interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
18
19
  url: string;
19
20
  method: RunnerMethod;
20
21
  value?: V;
21
- params?: P;
22
+ params?: P | (() => P);
22
23
  retry?: number;
24
+ resetValue?: boolean;
23
25
  config?: AxleRequestConfig;
24
26
  immediate?: boolean;
25
27
  onBefore?(refs: UseAxleRefs<V>): void;
package/es/use.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { ref } from 'vue';
2
+ import { isFunction } from '@varlet/shared';
2
3
  export function createUseAxle(options) {
3
4
  const { axle, onTransform: defaultOnTransform } = options;
4
5
  const useAxle = (options) => {
5
6
  var _a;
6
- const { url, method, immediate, value: initialValue, params: initialParams, config: initialConfig, retry = 0, onBefore = () => { }, onAfter = () => { }, onTransform = (_a = defaultOnTransform) !== null && _a !== void 0 ? _a : ((response) => response), onSuccess = () => { }, onError = () => { }, } = options;
7
+ const { url, method, immediate, value: initialValue, resetValue: initialResetValue, params: initialParamsOrGetter, config: initialConfig, retry = 0, onBefore = () => { }, onAfter = () => { }, onTransform = (_a = defaultOnTransform) !== null && _a !== void 0 ? _a : ((response) => response), onSuccess = () => { }, onError = () => { }, } = options;
7
8
  const initialUrl = url;
8
9
  const value = ref(initialValue);
9
10
  const loading = ref(false);
@@ -20,17 +21,22 @@ export function createUseAxle(options) {
20
21
  };
21
22
  let controller = new AbortController();
22
23
  const run = async (options = {}) => {
23
- var _a;
24
+ var _a, _b, _c;
24
25
  if (controller.signal.aborted) {
25
26
  controller = new AbortController();
26
27
  }
28
+ const resetValue = (_b = (_a = options.resetValue) !== null && _a !== void 0 ? _a : initialResetValue) !== null && _b !== void 0 ? _b : false;
29
+ if (resetValue === true) {
30
+ value.value = initialValue;
31
+ }
27
32
  uploadProgress.value = 0;
28
33
  downloadProgress.value = 0;
29
- const url = (_a = options.url) !== null && _a !== void 0 ? _a : initialUrl;
34
+ const url = (_c = options.url) !== null && _c !== void 0 ? _c : initialUrl;
30
35
  onBefore(refs);
31
36
  loading.value = true;
32
37
  try {
33
- const response = await axle[method](url, options.params, Object.assign({ signal: controller.signal, onUploadProgress(event) {
38
+ const getterParams = isFunction(initialParamsOrGetter) ? initialParamsOrGetter() : {};
39
+ const response = await axle[method](url, Object.assign(Object.assign({}, getterParams), options.params), Object.assign({ signal: controller.signal, onUploadProgress(event) {
34
40
  var _a;
35
41
  uploadProgress.value = (_a = event.progress) !== null && _a !== void 0 ? _a : 0;
36
42
  },
@@ -63,7 +69,7 @@ export function createUseAxle(options) {
63
69
  if (immediate) {
64
70
  run({
65
71
  url: initialUrl,
66
- params: initialParams,
72
+ params: isFunction(initialParamsOrGetter) ? {} : initialParamsOrGetter,
67
73
  config: initialConfig,
68
74
  });
69
75
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "es/index.js",
@@ -29,13 +29,13 @@
29
29
  "author": "haoziqaq <357229046@qq.com>",
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
- "@varlet/shared": "^2.16.5",
32
+ "@varlet/shared": "^2.19.2",
33
33
  "axios": "^1.4.0",
34
34
  "qs": "^6.11.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/qs": "^6.9.7",
38
- "@varlet/cli": "^2.16.5",
38
+ "@varlet/release": "0.0.3",
39
39
  "rimraf": "^5.0.1",
40
40
  "typescript": "^5.1.5",
41
41
  "vue": "3.3.4"
@@ -55,6 +55,6 @@
55
55
  "bootstrap": "pnpm i && pnpm build && pnpm --filter ./playground i",
56
56
  "dev": "tsc -w",
57
57
  "build": "rimraf ./es && tsc",
58
- "release": "pnpm build && varlet-cli release"
58
+ "release": "pnpm build && vr release"
59
59
  }
60
60
  }