1
- import { reactive , computed , toRefs , isRef , watch } from '@vue/composition-api'
1
+ /*
2
+ eslint
3
+ @typescript -eslint/no-explicit-any: 0
4
+ */
2
5
import {
3
- getServicePrefix ,
4
- getServiceCapitalization ,
5
- getQueryInfo ,
6
- getItemsFromQueryInfo ,
7
- Params
8
- } from './utils'
6
+ reactive ,
7
+ computed ,
8
+ toRefs ,
9
+ isRef ,
10
+ watch ,
11
+ Ref
12
+ } from '@vue/composition-api'
13
+ import { getQueryInfo , getItemsFromQueryInfo , Params } from './utils'
9
14
import debounce from 'lodash/debounce'
10
15
11
16
const defaults = {
12
17
model : null ,
13
18
params : null ,
14
- queryWhen : ( ) => true ,
15
- qid : 'default'
19
+ queryWhen : ( ) : boolean => true ,
20
+ qid : 'default' ,
21
+ local : false
16
22
}
17
23
18
- export default function find ( options ) {
19
- const { model, params, name, queryWhen, qid } = Object . assign (
24
+ interface UseFindOptions {
25
+ model : Function
26
+ params : Params | Ref < Params >
27
+ fetchParams ?: Params | Ref < Params >
28
+ name ?: string
29
+ queryWhen ?: Function
30
+ qid ?: string
31
+ }
32
+ interface UseFindState {
33
+ debounceTime : null | number
34
+ qid : string
35
+ isFindPending : boolean
36
+ haveBeenRequestedOnce : boolean
37
+ haveLoadedOnce : boolean
38
+ error : null | Error
39
+ latestQuery : null | object
40
+ isLocal : boolean
41
+ }
42
+ interface UseFindData {
43
+ items : Ref < any >
44
+ servicePath : Ref < string >
45
+ isFindPending : Ref < boolean >
46
+ haveBeenRequestedOnce : Ref < boolean >
47
+ haveLoadedOnce : Ref < boolean >
48
+ isLocal : Ref < boolean >
49
+ qid : Ref < string >
50
+ debounceTime : Ref < number >
51
+ find : Function
52
+ findInStore : Function
53
+ latestQuery : Ref < object >
54
+ paginationData : Ref < object >
55
+ queryWhen : Ref < boolean >
56
+ error : Ref < Error >
57
+ }
58
+
59
+ export default function find ( options : UseFindOptions ) : UseFindData {
60
+ const { model, params, queryWhen, qid, local } = Object . assign (
20
61
{ } ,
21
62
defaults ,
22
63
options
23
64
)
24
65
25
- const nameToUse = ( name || model . servicePath ) . replace ( '-' , '_' )
26
- const prefix = getServicePrefix ( nameToUse )
27
- const capitalized = getServiceCapitalization ( nameToUse )
28
- const IS_FIND_PENDING = `isFind${ capitalized } Pending`
29
- const QUERY_WHEN = `${ prefix } QueryWhen`
30
- const FIND_ACTION = `find${ capitalized } `
31
- const FIND_GETTER = `find${ capitalized } InStore`
32
- const DEBOUNCED = `${ FIND_ACTION } Debounced`
33
- const DEBOUNCE_TIME = `${ FIND_ACTION } DebouncedTime`
34
- const HAVE_ITEMS_BEEN_REQUESTED_ONCE = `have${ capitalized } BeenRequestedOnce`
35
- const HAVE_ITEMS_LOADED_ONCE = `have${ capitalized } LoadedOnce`
36
- const PAGINATION = `${ prefix } PaginationData`
37
- const MOST_RECENT_QUERY = `${ prefix } LatestQuery`
38
- const ERROR = `${ prefix } Error`
39
- const QID = `${ prefix } Qid`
66
+ const getFetchParams = ( providedParams ?: object ) : Params => {
67
+ const provided = isRef ( providedParams )
68
+ ? providedParams . value
69
+ : providedParams
40
70
41
- const getFetchParams = ( providedParams ?:object ) => {
42
- if ( providedParams ) {
43
- return providedParams
71
+ if ( provided ) {
72
+ return provided
44
73
} else {
45
- // Returning null fetchParams allows the query to be skipped.
46
- return options . fetchParams || options . fetchParams === null
47
- ? options . fetchParams
74
+ const fetchParams = isRef ( options . fetchParams )
75
+ ? options . fetchParams . value
76
+ : options . fetchParams
77
+ const params = isRef ( options . params )
78
+ ? options . params . value
48
79
: options . params
80
+
81
+ // Returning null fetchParams allows the query to be skipped.
82
+ return fetchParams || fetchParams === null ? fetchParams : params
49
83
}
50
84
}
51
85
52
- const state = reactive ( {
86
+ const state = reactive < UseFindState > ( {
87
+ qid,
88
+ isFindPending : false ,
89
+ haveBeenRequestedOnce : false ,
90
+ haveLoadedOnce : false ,
91
+ error : null ,
92
+ debounceTime : null ,
93
+ latestQuery : null ,
94
+ isLocal : local
95
+ } )
96
+ const computes = {
53
97
// The find getter
54
- [ prefix ] : computed ( ( ) => {
55
- // @ts -ignore
56
- const getterParams :Params = isRef ( params ) ? { ...params . value } : { params }
98
+ items : computed < any [ ] > ( ( ) => {
99
+ const getterParams : Params = isRef ( params )
100
+ ? Object . assign ( { } , params . value )
101
+ : { params }
57
102
if ( getterParams . paginate ) {
58
103
const serviceState = model . store . state [ model . servicePath ]
59
104
const { defaultSkip, defaultLimit } = serviceState . pagination
60
105
const skip = getterParams . query . $skip || defaultSkip
61
106
const limit = getterParams . query . $limit || defaultLimit
62
- const pagination = state [ PAGINATION ] [ getterParams . qid || state [ QID ] ] || { }
107
+ const pagination =
108
+ computes . paginationData [ getterParams . qid || state [ qid ] ] || { }
63
109
const response = skip != null && limit != null ? { limit, skip } : { }
64
110
const queryInfo = getQueryInfo ( getterParams , response )
65
111
const items = getItemsFromQueryInfo (
@@ -74,52 +120,50 @@ export default function find(options) {
74
120
return model . findInStore ( getterParams ) . data
75
121
}
76
122
} ) ,
77
- [ QID ] : qid ,
78
- [ QUERY_WHEN ] : computed ( ( ) => queryWhen ) ,
79
- [ IS_FIND_PENDING ] : false ,
80
- [ HAVE_ITEMS_BEEN_REQUESTED_ONCE ] : false ,
81
- [ HAVE_ITEMS_LOADED_ONCE ] : false ,
82
- [ ERROR ] : null ,
83
- [ PAGINATION ] : computed ( ( ) => {
123
+ queryWhen : computed < boolean > ( ( ) => queryWhen ( ) ) ,
124
+ paginationData : computed ( ( ) => {
84
125
return model . store . state [ model . servicePath ] . pagination
85
126
} ) ,
86
- [ DEBOUNCED ] : null ,
87
- [ DEBOUNCE_TIME ] : null
88
- } )
127
+ servicePath : computed < string > ( ( ) => model . servicePath )
128
+ }
89
129
90
- function find ( params ) {
130
+ function find < T > ( params : Params ) : T {
91
131
params = isRef ( params ) ? params . value : params
92
- if ( state [ QUERY_WHEN ] ) {
93
- console . log ( 'finding' )
94
- state [ IS_FIND_PENDING ] = true
95
- state [ HAVE_ITEMS_BEEN_REQUESTED_ONCE ] = true
132
+ if ( computes . queryWhen . value ) {
133
+ state . isFindPending = true
134
+ state . haveBeenRequestedOnce = true
96
135
97
- model . find ( params ) . then ( response => {
98
- // To prevent thrashing, only clear ERROR on response, not on initial request.
99
- state [ ERROR ] = null
100
- state [ HAVE_ITEMS_LOADED_ONCE ] = true
136
+ return model . find ( params ) . then ( response => {
137
+ // To prevent thrashing, only clear error on response, not on initial request.
138
+ state . error = null
139
+ state . haveLoadedOnce = true
101
140
102
141
const queryInfo = getQueryInfo ( params , response )
103
142
queryInfo . response = response
104
143
queryInfo . isOutdated = false
105
144
106
- state [ MOST_RECENT_QUERY ] = queryInfo
107
- state [ IS_FIND_PENDING ] = false
145
+ state . latestQuery = queryInfo
146
+ state . isFindPending = false
108
147
return response
109
148
} )
110
149
}
111
150
}
112
- function findProxy ( params ?:object ) {
151
+ const methods = {
152
+ findDebounced < Promise > ( params ?: Params ) : Promise {
153
+ return find ( params )
154
+ }
155
+ }
156
+ function findProxy < T > ( params ?: Params ) : T {
113
157
const paramsToUse = getFetchParams ( params )
114
158
115
159
if ( paramsToUse && paramsToUse . debounce ) {
116
- if ( paramsToUse . debounce !== state [ DEBOUNCE_TIME ] ) {
117
- state [ DEBOUNCED ] = debounce ( find , paramsToUse . debounce )
118
- state [ DEBOUNCE_TIME ] = paramsToUse . debounce
160
+ if ( paramsToUse . debounce !== state . debounceTime ) {
161
+ methods . findDebounced = debounce ( find , paramsToUse . debounce )
162
+ state . debounceTime = paramsToUse . debounce
119
163
}
120
- return state [ DEBOUNCED ] ( paramsToUse )
164
+ return methods . findDebounced ( paramsToUse )
121
165
} else if ( paramsToUse ) {
122
- find ( paramsToUse )
166
+ return find ( paramsToUse )
123
167
} else {
124
168
// Set error
125
169
}
@@ -133,8 +177,9 @@ export default function find(options) {
133
177
)
134
178
135
179
return {
180
+ ...computes ,
136
181
...toRefs ( state ) ,
137
- [ FIND_ACTION ] : model . find ,
138
- [ FIND_GETTER ] : model . findInStore
182
+ find : model . find ,
183
+ findInStore : model . findInStore
139
184
}
140
185
}
0 commit comments