Skip to content

Commit 752b08f

Browse files
author
Jérémy
committed
fix(usefind): fix params wrapped in object in the computed items and refactoring
fix #413
1 parent 5991797 commit 752b08f

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

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

0 commit comments

Comments
 (0)