@varlet/axle 0.0.17 → 0.0.18

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.
Files changed (3) hide show
  1. package/es/use.d.ts +7 -19
  2. package/es/use.js +3 -19
  3. package/package.json +1 -1
package/es/use.d.ts CHANGED
@@ -1,34 +1,22 @@
1
1
  import { type Ref, type UnwrapRef } from 'vue';
2
2
  import { type AxleRequestConfig, createFetchHelper, createModifyHelper } from './instance.js';
3
- export interface CreateUseAxleOptions {
4
- runner?: ReturnType<typeof createFetchHelper> | ReturnType<typeof createModifyHelper>;
5
- url?: string;
6
- data?: any;
7
- params?: any;
8
- config?: AxleRequestConfig;
9
- immediate?: boolean;
10
- extra?: any;
11
- onBefore?(): void;
12
- onSuccess?(response: any, prev: any): any;
13
- onError?(error: Error, prev: Error | undefined): Error;
14
- }
15
3
  export interface RunOptions<P> {
16
4
  url?: string;
17
5
  params?: P;
18
6
  config?: AxleRequestConfig;
19
7
  }
20
- export type Run<D, P> = (options?: RunOptions<P>) => Promise<UnwrapRef<D>>;
21
- export interface UseAxleOptions<D = any, P = Record<string, any>, R = any> {
22
- runner?: ReturnType<typeof createFetchHelper> | ReturnType<typeof createModifyHelper>;
23
- url?: string;
24
- data?: D;
8
+ export type Run<D, R, P> = (options?: RunOptions<P>) => Promise<UnwrapRef<D | R>>;
9
+ export interface UseAxleOptions<D = any, R = any, P = Record<string, any>> {
10
+ url: string;
11
+ data: D | R;
12
+ runner: ReturnType<typeof createFetchHelper> | ReturnType<typeof createModifyHelper>;
25
13
  params?: P;
26
14
  config?: AxleRequestConfig;
27
15
  immediate?: boolean;
28
16
  onBefore?(): void;
29
- onSuccess?(response: R, prev: D): D;
17
+ onSuccess?(response: UnwrapRef<R>, prev: UnwrapRef<D | R>): UnwrapRef<D | R>;
30
18
  onError?(error: Error, prev: Error | undefined): Error;
31
19
  }
32
- export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, P = Record<string, any>, R = any>(options?: UseAxleOptions<D, P, R>) => [data: Ref<UnwrapRef<D>>, run: Run<D, P>, loading: Ref<UnwrapRef<boolean>>, extra: {
20
+ export declare function createUseAxle(): <D = any, R = any, P = Record<string, any>>(options: UseAxleOptions<D, R, P>) => [data: Ref<UnwrapRef<D | R>>, run: Run<D, R, P>, loading: Ref<UnwrapRef<boolean>>, extra: {
33
21
  error: Ref<UnwrapRef<Error | undefined>>;
34
22
  }];
package/es/use.js CHANGED
@@ -1,30 +1,14 @@
1
1
  import { ref } from 'vue';
2
- export function createUseAxle(options = {}) {
3
- var _a, _b, _c, _d;
4
- const defaultRunner = options.runner;
5
- const defaultUrl = options.url;
6
- const defaultInitialData = options.data;
7
- const defaultInitialParams = options.params;
8
- const defaultInitialConfig = options.config;
9
- const defaultImmediate = (_a = options.immediate) !== null && _a !== void 0 ? _a : false;
10
- const defaultOnBefore = (_b = options.onBefore) !== null && _b !== void 0 ? _b : (() => { });
11
- const defaultOnSuccess = (_c = options.onSuccess) !== null && _c !== void 0 ? _c : ((v) => v);
12
- const defaultOnError = (_d = options.onError) !== null && _d !== void 0 ? _d : ((v) => v);
13
- const useAxle = (options = {}) => {
14
- const { runner = defaultRunner, data: initialData = defaultInitialData, url = defaultUrl, params: initialParams = defaultInitialParams, config: initialConfig = defaultInitialConfig, immediate = defaultImmediate, onBefore = defaultOnBefore, onSuccess = defaultOnSuccess, onError = defaultOnError, } = options;
2
+ export function createUseAxle() {
3
+ const useAxle = (options) => {
4
+ const { url, runner, immediate, data: initialData, params: initialParams, config: initialConfig, onBefore = () => { }, onSuccess = (response) => response, onError = (error) => error, } = options;
15
5
  const initialUrl = url;
16
6
  const data = ref(initialData);
17
7
  const loading = ref(false);
18
8
  const error = ref();
19
- if (!runner) {
20
- throw new Error('[Axle]: Cannot found valid runner, so options.runner is required');
21
- }
22
9
  const run = (options = {}) => {
23
10
  var _a;
24
11
  const url = (_a = options.url) !== null && _a !== void 0 ? _a : initialUrl;
25
- if (!url) {
26
- throw new Error('[Axle]: Cannot found valid url, so options.url is required');
27
- }
28
12
  onBefore();
29
13
  loading.value = true;
30
14
  return runner(url, options.params, options.config)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "A useful axios helper",
5
5
  "type": "module",
6
6
  "main": "es/index.js",