Skip to content

Commit 213090f

Browse files
committed
Add params support to FeathersVuexGet
1 parent 02c2c52 commit 213090f

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

src/FeathersVuexGet.ts

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
12
export default {
23
props: {
34
/**
@@ -22,6 +23,29 @@ export default {
2223
type: Object,
2324
default: null
2425
},
26+
/**
27+
* If a separate query is desired to fetch data, use fetchQuery
28+
* The watchers are automatically updated, so you don't have to write 'fetchQuery.propName'
29+
*/
30+
fetchQuery: {
31+
type: Object
32+
},
33+
/**
34+
* Can be used in place of the `query` prop to provide more params. Only params.query is
35+
* passed to the getter.
36+
*/
37+
params: {
38+
type: Object,
39+
default: null
40+
},
41+
/**
42+
* Can be used in place of the `fetchQuery` prop to provide more params. Only params.query is
43+
* passed to the getter.
44+
*/
45+
fetchParams: {
46+
type: Object,
47+
default: null
48+
},
2549
/**
2650
* When `queryWhen` evaluates to false, no API request will be made.
2751
*/
@@ -34,13 +58,6 @@ export default {
3458
type: [Number, String],
3559
default: null
3660
},
37-
/**
38-
* If a separate query is desired to fetch data, use fetchQuery
39-
* The watchers are automatically updated, so you don't have to write 'fetchQuery.propName'
40-
*/
41-
fetchQuery: {
42-
type: Object
43-
},
4461
/**
4562
* Specify which properties in the query to watch and re-trigger API requests.
4663
*/
@@ -76,13 +93,17 @@ export default {
7693
computed: {
7794
item() {
7895
const getArgs = this.getArgs(this.query)
79-
8096
if (this.id) {
81-
return (
82-
this.$store.getters[`${this.service}/get`](
83-
getArgs.length === 1 ? this.id : getArgs
84-
) || null
85-
)
97+
if (getArgs.length === 1) {
98+
return this.$store.getters[`${this.service}/get`](this.id) || null
99+
} else {
100+
const args = [this.id]
101+
const query = getArgs[1].query
102+
if (query) {
103+
args.push(query)
104+
}
105+
return this.$store.getters[`${this.service}/get`](args) || null
106+
}
86107
} else {
87108
return null
88109
}
@@ -97,12 +118,14 @@ export default {
97118
methods: {
98119
getArgs(queryToUse) {
99120
const query = queryToUse || this.fetchQuery || this.query
100-
const getArgs = [this.id]
121+
const params = this.fetchParams || this.params
101122

102-
if (query && Object.keys(query).length > 0) {
123+
const getArgs = [this.id]
124+
if (params) {
125+
getArgs.push(params)
126+
} else if (query && Object.keys(query).length > 0) {
103127
getArgs.push({ query })
104128
}
105-
106129
return getArgs
107130
},
108131
getData() {
@@ -121,8 +144,9 @@ export default {
121144
`${this.service}/get`,
122145
getArgs.length === 1 ? this.id : getArgs
123146
)
124-
.then(() => {
147+
.then(response => {
125148
this.isGetPending = false
149+
return response
126150
})
127151
}
128152
}
@@ -131,17 +155,15 @@ export default {
131155
if (this.local || this.id === 'new') {
132156
return
133157
} else if (
134-
this.id !== null &&
135-
this.id !== undefined &&
136-
!this.query &&
137-
!this.fetchQuery
158+
this.fetchQuery ||
159+
this.query ||
160+
this.params ||
161+
(this.id !== null && this.id !== undefined)
138162
) {
139163
return this.getData()
140164
} else {
141165
// eslint-disable-next-line no-console
142-
console.log(
143-
`No query and no id provided, so no data will be fetched.`
144-
)
166+
console.log(`No query and no id provided, so no data will be fetched.`)
145167
}
146168
}
147169
},
@@ -153,7 +175,7 @@ export default {
153175
}
154176
if (!this.$store.state[this.service]) {
155177
throw new Error(
156-
`The '${ this.service }' plugin is not registered with feathers-vuex`
178+
`The '${this.service}' plugin is not registered with feathers-vuex`
157179
)
158180
}
159181

@@ -162,6 +184,7 @@ export default {
162184
if (
163185
this.fetchQuery ||
164186
this.query ||
187+
this.params ||
165188
(this.id !== null && this.id !== undefined)
166189
) {
167190
watch.forEach(prop => {

0 commit comments

Comments
 (0)