1- import { watch } from 'vue'
21import { isFunction } from 'rattail'
32import { type AxleInstance , type AxleRequestConfig , type RunnerMethod } from './instance'
43import {
5- normalizeValueGetter ,
64 UseAxleInstance ,
75 UseAxleOptionsWithRunnable ,
86 type UseAxle ,
@@ -11,6 +9,7 @@ import {
119} from './use'
1210
1311export type ApiPathParams = Record < string , any > | ( ( ) => Record < string , any > )
12+
1413export type ApiWatchOptions = WatchOptions & {
1514 pathParams ?: boolean
1615}
@@ -35,25 +34,21 @@ export function createApi(axle: AxleInstance, useAxle: UseAxle) {
3534 function use < V = R > ( options ?: ApiUseOptionsWithRunnable < V , R , P , D > ) : UseAxleInstance < V , R | undefined , P , D >
3635 function use < V = R > ( options ?: ApiUseOptions < V , R , P , D > ) : UseAxleInstance < V , R , P , D >
3736 function use < V = R > ( options : any = { } ) : any {
38- const { pathParams = { } , watch : apiWatch , ...rest } = options
39-
40- const enableWatchPathParams = isFunction ( pathParams ) && ( apiWatch ?. pathParams || apiWatch === true )
37+ const { pathParams = { } , watch : watchOptions , ...rest } = options
38+ const enableWatchPathParams = isFunction ( pathParams ) && ( watchOptions ?. pathParams || watchOptions === true )
39+ const normalizedWatchOptions =
40+ watchOptions === true
41+ ? { params : true , config : true , _custom : enableWatchPathParams ? pathParams : undefined }
42+ : watchOptions === false
43+ ? { params : false , config : false }
44+ : { params : false , config : false , ...watchOptions , _custom : enableWatchPathParams ? pathParams : undefined }
4145
42- const [ data , run , extra ] = useAxle < V , R , P , D > ( {
46+ return useAxle < V , R , P , D > ( {
4347 url : ( ) => patchUrl ( url , pathParams ) ,
4448 method,
45- watch : apiWatch ,
49+ watch : normalizedWatchOptions ,
4650 ...rest ,
4751 } )
48-
49- watch (
50- ( ) => [ enableWatchPathParams ? normalizeValueGetter ( pathParams ) : undefined ] ,
51- ( ) => {
52- run ( )
53- } ,
54- )
55-
56- return [ data , run , extra ]
5752 }
5853
5954 function patchUrl ( url : string , pathParams : ApiPathParams ) {
0 commit comments