Skip to content

Commit 71f1147

Browse files
Merge pull request #415 from feathersjs-ecosystem/useFindParams
Use find params
2 parents 5991797 + 5ae7627 commit 71f1147

File tree

3 files changed

+87
-26
lines changed

3 files changed

+87
-26
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
"directories": {
103103
"lib": "lib"
104104
},
105+
"peerDependencies": {
106+
"@vue/composition-api": "^0.3.4"
107+
},
105108
"dependencies": {
106109
"@feathersjs/adapter-commons": "^4.4.3",
107110
"@feathersjs/commons": "^4.4.3",
@@ -110,7 +113,6 @@
110113
"@types/inflection": "^1.5.28",
111114
"@types/lodash": "^4.14.149",
112115
"@types/npm": "^2.0.31",
113-
"@vue/composition-api": "^0.3.4",
114116
"bson-objectid": "^1.3.0",
115117
"debug": "^4.1.1",
116118
"events": "^3.1.0",
@@ -141,6 +143,7 @@
141143
"@types/mocha": "^5.2.7",
142144
"@typescript-eslint/eslint-plugin": "^2.16.0",
143145
"@typescript-eslint/parser": "^2.16.0",
146+
"@vue/composition-api": "^0.3.4",
144147
"@vue/eslint-config-prettier": "^6.0.0",
145148
"@vue/eslint-config-typescript": "^5.0.1",
146149
"@vue/test-utils": "^1.0.0-beta.30",

src/useFind.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ eslint
33
@typescript-eslint/no-explicit-any: 0
44
*/
55
import {
6-
reactive,
76
computed,
8-
toRefs,
97
isRef,
10-
watch,
11-
Ref
8+
reactive,
9+
Ref,
10+
toRefs,
11+
watch
1212
} from '@vue/composition-api'
13-
import { getQueryInfo, getItemsFromQueryInfo, Params } from './utils'
1413
import debounce from 'lodash/debounce'
14+
import { getItemsFromQueryInfo, getQueryInfo, Params } from './utils'
1515

1616
interface UseFindOptions {
1717
model: Function
@@ -47,6 +47,9 @@ interface UseFindData {
4747
find: Function
4848
}
4949

50+
const unwrapParams = (params: Params | Ref<Params>): Params =>
51+
isRef(params) ? params.value : params
52+
5053
export default function find(options: UseFindOptions): UseFindData {
5154
const defaults = {
5255
model: null,
@@ -62,24 +65,21 @@ export default function find(options: UseFindOptions): UseFindData {
6265
options
6366
)
6467

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)
6970

7071
if (provided) {
7172
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
8273
}
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
8383
}
8484

8585
const state = reactive<UseFindState>({
@@ -95,9 +95,8 @@ export default function find(options: UseFindOptions): UseFindData {
9595
const computes = {
9696
// The find getter
9797
items: computed<any[]>(() => {
98-
const getterParams: Params = isRef(params)
99-
? Object.assign({}, params.value)
100-
: { params }
98+
const getterParams = unwrapParams(params)
99+
101100
if (getterParams.paginate) {
102101
const serviceState = model.store.state[model.servicePath]
103102
const { defaultSkip, defaultLimit } = serviceState.pagination
@@ -125,8 +124,8 @@ export default function find(options: UseFindOptions): UseFindData {
125124
servicePath: computed<string>(() => model.servicePath)
126125
}
127126

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)
130129
if (queryWhen.value && !state.isLocal) {
131130
state.isPending = true
132131
state.haveBeenRequested = true
@@ -151,7 +150,7 @@ export default function find(options: UseFindOptions): UseFindData {
151150
return find(params)
152151
}
153152
}
154-
function findProxy<T>(params?: Params): T {
153+
function findProxy<T>(params?: Params | Ref<Params>): T {
155154
const paramsToUse = getFetchParams(params)
156155

157156
if (paramsToUse && paramsToUse.debounce) {

test/use/find.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,65 @@ describe('use/find', function() {
106106
assert(qid.value === 'default')
107107
})
108108

109+
it('returns correct default data even when params is not reactive', function() {
110+
const { Instrument } = makeContext()
111+
112+
const instrumentsData = useFind({
113+
model: Instrument,
114+
params: {
115+
query: {},
116+
paginate: false
117+
}
118+
})
119+
120+
const {
121+
debounceTime,
122+
error,
123+
haveBeenRequested,
124+
haveLoaded,
125+
isPending,
126+
isLocal,
127+
items,
128+
latestQuery,
129+
paginationData,
130+
qid
131+
} = instrumentsData
132+
133+
assert(isRef(debounceTime))
134+
assert(debounceTime.value === null)
135+
136+
assert(isRef(error))
137+
assert(error.value === null)
138+
139+
assert(isRef(haveBeenRequested))
140+
assert(haveBeenRequested.value === true)
141+
142+
assert(isRef(haveLoaded))
143+
assert(haveLoaded.value === false)
144+
145+
assert(isRef(isPending))
146+
assert(isPending.value === true)
147+
148+
assert(isRef(isLocal))
149+
assert(isLocal.value === false)
150+
151+
assert(isRef(items))
152+
assert(Array.isArray(items.value))
153+
assert(items.value.length === 0)
154+
155+
assert(isRef(latestQuery))
156+
assert(latestQuery.value === null)
157+
158+
assert(isRef(paginationData))
159+
assert.deepStrictEqual(paginationData.value, {
160+
defaultLimit: null,
161+
defaultSkip: null
162+
})
163+
164+
assert(isRef(qid))
165+
assert(qid.value === 'default')
166+
})
167+
109168
it('allows passing {lazy:true} to not query immediately', function() {
110169
const { Instrument } = makeContext()
111170

0 commit comments

Comments
 (0)