@@ -8,7 +8,7 @@ import { getId } from '../utils'
8
8
9
9
export default function makeServiceActions ( service ) {
10
10
const serviceActions = {
11
- find ( { commit, dispatch } , params ) {
11
+ async find ( { commit, dispatch } , params ) {
12
12
params = params || { }
13
13
params = fastCopy ( params )
14
14
@@ -23,7 +23,7 @@ export default function makeServiceActions(service) {
23
23
// Two query syntaxes are supported, since actions only receive one argument.
24
24
// 1. Just pass the id: `get(1)`
25
25
// 2. Pass arguments as an array: `get([null, params])`
26
- get ( { state, getters, commit, dispatch } , args ) {
26
+ async get ( { state, getters, commit, dispatch } , args ) {
27
27
let id
28
28
let params
29
29
let skipRequestIfExists
@@ -49,8 +49,8 @@ export default function makeServiceActions(service) {
49
49
commit ( 'setPending' , 'get' )
50
50
return service
51
51
. get ( id , params )
52
- . then ( item => {
53
- dispatch ( 'addOrUpdate' , item )
52
+ . then ( async item => {
53
+ await dispatch ( 'addOrUpdate' , item )
54
54
commit ( 'unsetPending' , 'get' )
55
55
return state . keyedById [ id ]
56
56
} )
@@ -62,15 +62,14 @@ export default function makeServiceActions(service) {
62
62
}
63
63
64
64
// If the records is already in store, return it
65
- const existedItem = getters . get ( id , params )
66
- if ( existedItem ) {
67
- if ( ! skipRequestIfExists ) getFromRemote ( )
68
- return Promise . resolve ( existedItem )
65
+ const existingItem = getters . get ( id , params )
66
+ if ( existingItem && skipRequestIfExists ) {
67
+ return Promise . resolve ( existingItem )
69
68
}
70
69
return getFromRemote ( )
71
70
} ,
72
71
73
- create ( { commit, dispatch, state } , dataOrArray ) {
72
+ async create ( { commit, dispatch, state } , dataOrArray ) {
74
73
const { idField, tempIdField } = state
75
74
let data
76
75
let params
@@ -97,9 +96,9 @@ export default function makeServiceActions(service) {
97
96
98
97
return service
99
98
. create ( data , params )
100
- . then ( response => {
99
+ . then ( async response => {
101
100
if ( Array . isArray ( response ) ) {
102
- dispatch ( 'addOrUpdateList' , response )
101
+ await dispatch ( 'addOrUpdateList' , response )
103
102
response = response . map ( item => {
104
103
const id = getId ( item , idField )
105
104
@@ -108,7 +107,7 @@ export default function makeServiceActions(service) {
108
107
} else {
109
108
const id = getId ( response , idField )
110
109
111
- dispatch ( 'addOrUpdate' , response )
110
+ await dispatch ( 'addOrUpdate' , response )
112
111
113
112
response = state . keyedById [ id ]
114
113
}
@@ -123,16 +122,16 @@ export default function makeServiceActions(service) {
123
122
} )
124
123
} ,
125
124
126
- update ( { commit, dispatch, state } , [ id , data , params ] ) {
125
+ async update ( { commit, dispatch, state } , [ id , data , params ] ) {
127
126
128
127
commit ( 'setPending' , 'update' )
129
128
130
129
params = fastCopy ( params )
131
130
132
131
return service
133
132
. update ( id , data , params )
134
- . then ( item => {
135
- dispatch ( 'addOrUpdate' , item )
133
+ . then ( async item => {
134
+ await dispatch ( 'addOrUpdate' , item )
136
135
commit ( 'unsetPending' , 'update' )
137
136
return state . keyedById [ id ]
138
137
} )
@@ -143,7 +142,7 @@ export default function makeServiceActions(service) {
143
142
} )
144
143
} ,
145
144
146
- patch ( { commit, dispatch, state } , [ id , data , params ] ) {
145
+ async patch ( { commit, dispatch, state } , [ id , data , params ] ) {
147
146
commit ( 'setPending' , 'patch' )
148
147
149
148
params = fastCopy ( params )
@@ -154,8 +153,8 @@ export default function makeServiceActions(service) {
154
153
155
154
return service
156
155
. patch ( id , data , params )
157
- . then ( item => {
158
- dispatch ( 'addOrUpdate' , item )
156
+ . then ( async item => {
157
+ await dispatch ( 'addOrUpdate' , item )
159
158
commit ( 'unsetPending' , 'patch' )
160
159
return state . keyedById [ id ]
161
160
} )
@@ -166,7 +165,7 @@ export default function makeServiceActions(service) {
166
165
} )
167
166
} ,
168
167
169
- remove ( { commit } , idOrArray ) {
168
+ async remove ( { commit } , idOrArray ) {
170
169
let id
171
170
let params
172
171
@@ -206,11 +205,11 @@ export default function makeServiceActions(service) {
206
205
* Feathers client. The client modifies the params object.
207
206
* @param response
208
207
*/
209
- handleFindResponse ( { state, commit, dispatch } , { params, response } ) {
208
+ async handleFindResponse ( { state, commit, dispatch } , { params, response } ) {
210
209
const { qid = 'default' , query } = params
211
210
const { idField } = state
212
211
213
- dispatch ( 'addOrUpdateList' , response )
212
+ await dispatch ( 'addOrUpdateList' , response )
214
213
commit ( 'unsetPending' , 'find' )
215
214
216
215
const mapItemFromState = item => {
@@ -232,17 +231,22 @@ export default function makeServiceActions(service) {
232
231
: ( response = mappedFromState )
233
232
}
234
233
235
- dispatch ( 'afterFind' , response )
234
+ response = await await dispatch ( 'afterFind' , response )
236
235
237
236
return response
238
237
} ,
239
- handleFindError ( { commit } , { params, error } ) {
238
+
239
+ async handleFindError ( { commit } , { params, error } ) {
240
240
commit ( 'setError' , { method : 'find' , params, error } )
241
241
commit ( 'unsetPending' , 'find' )
242
242
return Promise . reject ( error )
243
243
} ,
244
- afterFind ( ) { } ,
245
- addOrUpdateList ( { state, commit } , response ) {
244
+
245
+ async afterFind ( { } , response ) {
246
+ return response
247
+ } ,
248
+
249
+ async addOrUpdateList ( { state, commit } , response ) {
246
250
const list = response . data || response
247
251
const isPaginated = response . hasOwnProperty ( 'total' )
248
252
const toAdd = [ ]
@@ -282,8 +286,11 @@ export default function makeServiceActions(service) {
282
286
283
287
commit ( 'addItems' , toAdd )
284
288
commit ( 'updateItems' , toUpdate )
289
+
290
+ return response
285
291
} ,
286
- addOrUpdate ( { state, commit } , item ) {
292
+
293
+ async addOrUpdate ( { state, commit } , item ) {
287
294
const { idField } = state
288
295
let id = getId ( item , idField )
289
296
let existingItem = state . keyedById [ id ]
@@ -300,8 +307,12 @@ export default function makeServiceActions(service) {
300
307
if ( isIdOk ) {
301
308
existingItem ? commit ( 'updateItem' , item ) : commit ( 'addItem' , item )
302
309
}
310
+ return item
303
311
}
304
312
}
313
+ /**
314
+ * Only add a method to the store if the service actually has that same method.
315
+ */
305
316
Object . keys ( serviceActions ) . map ( method => {
306
317
if ( service [ method ] && typeof service [ method ] === 'function' ) {
307
318
actions [ method ] = serviceActions [ method ]
0 commit comments