3
3
@typescript -eslint/no-explicit-any: 0
4
4
*/
5
5
import {
6
- reactive ,
7
6
computed ,
8
- toRefs ,
9
7
isRef ,
10
- watch ,
11
- Ref
8
+ reactive ,
9
+ Ref ,
10
+ toRefs ,
11
+ watch
12
12
} from '@vue/composition-api'
13
- import { getQueryInfo , getItemsFromQueryInfo , Params } from './utils'
14
13
import debounce from 'lodash/debounce'
14
+ import { getItemsFromQueryInfo , getQueryInfo , Params } from './utils'
15
15
16
16
interface UseFindOptions {
17
17
model : Function
@@ -47,6 +47,9 @@ interface UseFindData {
47
47
find : Function
48
48
}
49
49
50
+ const unwrapParams = ( params : Params | Ref < Params > ) : Params =>
51
+ isRef ( params ) ? params . value : params
52
+
50
53
export default function find ( options : UseFindOptions ) : UseFindData {
51
54
const defaults = {
52
55
model : null ,
@@ -62,24 +65,21 @@ export default function find(options: UseFindOptions): UseFindData {
62
65
options
63
66
)
64
67
65
- const getFetchParams = ( providedParams ?: object ) : Params => {
66
- const provided = isRef ( providedParams )
67
- ? providedParams . value
68
- : providedParams
68
+ const getFetchParams = ( providedParams ?: Params | Ref < Params > ) : Params => {
69
+ const provided = unwrapParams ( providedParams )
69
70
70
71
if ( provided ) {
71
72
return provided
72
- } else {
73
- const fetchParams = isRef ( options . fetchParams )
74
- ? options . fetchParams . value
75
- : options . fetchParams
76
- const params = isRef ( options . params )
77
- ? options . params . value
78
- : options . params
79
-
80
- // Returning null fetchParams allows the query to be skipped.
81
- return fetchParams || fetchParams === null ? fetchParams : params
82
73
}
74
+
75
+ const fetchParams = unwrapParams ( options . fetchParams )
76
+ // Returning null fetchParams allows the query to be skipped.
77
+ if ( fetchParams || fetchParams === null ) {
78
+ return fetchParams
79
+ }
80
+
81
+ const params = unwrapParams ( options . params )
82
+ return params
83
83
}
84
84
85
85
const state = reactive < UseFindState > ( {
@@ -95,9 +95,8 @@ export default function find(options: UseFindOptions): UseFindData {
95
95
const computes = {
96
96
// The find getter
97
97
items : computed < any [ ] > ( ( ) => {
98
- const getterParams : Params = isRef ( params )
99
- ? Object . assign ( { } , params . value )
100
- : { params }
98
+ const getterParams = unwrapParams ( params )
99
+
101
100
if ( getterParams . paginate ) {
102
101
const serviceState = model . store . state [ model . servicePath ]
103
102
const { defaultSkip, defaultLimit } = serviceState . pagination
@@ -125,8 +124,8 @@ export default function find(options: UseFindOptions): UseFindData {
125
124
servicePath : computed < string > ( ( ) => model . servicePath )
126
125
}
127
126
128
- function find < T > ( params : Params ) : T {
129
- params = isRef ( params ) ? params . value : params
127
+ function find < T > ( params : Params | Ref < Params > ) : T {
128
+ params = unwrapParams ( params )
130
129
if ( queryWhen . value && ! state . isLocal ) {
131
130
state . isPending = true
132
131
state . haveBeenRequested = true
@@ -151,7 +150,7 @@ export default function find(options: UseFindOptions): UseFindData {
151
150
return find ( params )
152
151
}
153
152
}
154
- function findProxy < T > ( params ?: Params ) : T {
153
+ function findProxy < T > ( params ?: Params | Ref < Params > ) : T {
155
154
const paramsToUse = getFetchParams ( params )
156
155
157
156
if ( paramsToUse && paramsToUse . debounce ) {
0 commit comments