@@ -12,12 +12,13 @@ import {
12
12
} from '@vue/composition-api'
13
13
import debounce from 'lodash/debounce'
14
14
import { getItemsFromQueryInfo , getQueryInfo , Params } from './utils'
15
+ import { AnyData , ModelStatic , Model } from './service-module/types'
15
16
16
- interface UseFindOptions {
17
- model : Function
17
+ interface UseFindOptions < T > {
18
+ model : ModelStatic < T >
18
19
params : Params | Ref < Params >
19
20
fetchParams ?: Params | Ref < Params >
20
- queryWhen ?: Ref < Function >
21
+ queryWhen ?: Ref < boolean >
21
22
qid ?: string
22
23
local ?: boolean
23
24
lazy ?: boolean
@@ -32,8 +33,8 @@ interface UseFindState {
32
33
latestQuery : null | object
33
34
isLocal : boolean
34
35
}
35
- interface UseFindData {
36
- items : Ref < any >
36
+ interface UseFindData < T > {
37
+ items : Ref < Readonly < Model < T > [ ] > >
37
38
servicePath : Ref < string >
38
39
isPending : Ref < boolean >
39
40
haveBeenRequested : Ref < boolean >
@@ -50,8 +51,8 @@ interface UseFindData {
50
51
const unwrapParams = ( params : Params | Ref < Params > ) : Params =>
51
52
isRef ( params ) ? params . value : params
52
53
53
- export default function find ( options : UseFindOptions ) : UseFindData {
54
- const defaults = {
54
+ export default function find < T extends AnyData = AnyData > ( options : UseFindOptions < T > ) : UseFindData < T > {
55
+ const defaults : UseFindOptions < T > = {
55
56
model : null ,
56
57
params : null ,
57
58
qid : 'default' ,
@@ -94,7 +95,7 @@ export default function find(options: UseFindOptions): UseFindData {
94
95
} )
95
96
const computes = {
96
97
// The find getter
97
- items : computed < any [ ] > ( ( ) => {
98
+ items : computed < Model < T > [ ] > ( ( ) => {
98
99
const getterParams = unwrapParams ( params )
99
100
100
101
if ( getterParams && getterParams . paginate ) {
@@ -113,7 +114,8 @@ export default function find(options: UseFindOptions): UseFindData {
113
114
)
114
115
return items
115
116
} else {
116
- return model . findInStore ( getterParams ) . data
117
+ const found = model . findInStore ( getterParams )
118
+ return Array . isArray ( found ) ? found : found . data
117
119
}
118
120
} ) ,
119
121
paginationData : computed ( ( ) => {
@@ -122,7 +124,7 @@ export default function find(options: UseFindOptions): UseFindData {
122
124
servicePath : computed < string > ( ( ) => model . servicePath )
123
125
}
124
126
125
- function find < T > ( params : Params | Ref < Params > ) : T {
127
+ function find ( params : Params | Ref < Params > ) {
126
128
params = unwrapParams ( params )
127
129
if ( queryWhen . value && ! state . isLocal ) {
128
130
state . isPending = true
@@ -133,22 +135,23 @@ export default function find(options: UseFindOptions): UseFindData {
133
135
state . error = null
134
136
state . haveLoaded = true
135
137
136
- const queryInfo = getQueryInfo ( params , response )
137
- queryInfo . response = response
138
- queryInfo . isOutdated = false
139
-
140
- state . latestQuery = queryInfo
138
+ if ( ! Array . isArray ( response ) ) {
139
+ const queryInfo = getQueryInfo ( params , response )
140
+ queryInfo . response = response
141
+ queryInfo . isOutdated = false
142
+ state . latestQuery = queryInfo
143
+ }
141
144
state . isPending = false
142
145
return response
143
146
} )
144
147
}
145
148
}
146
149
const methods = {
147
- findDebounced < Promise > ( params ?: Params ) : Promise {
150
+ findDebounced ( params ?: Params ) {
148
151
return find ( params )
149
152
}
150
153
}
151
- function findProxy < T > ( params ?: Params | Ref < Params > ) : T {
154
+ function findProxy ( params ?: Params | Ref < Params > ) {
152
155
const paramsToUse = getFetchParams ( params )
153
156
154
157
if ( paramsToUse && paramsToUse . debounce ) {
0 commit comments