vuex-crud
vuex-crud copied to clipboard
react to request success/error with actions
First of all, Id like to say this is a great little library, great work!
My only issue is that we can only react to request errors/successes with custom mutations. I think it would be much more helpful if we could react with actions instead, giving us more flexibility, with access to rootState, ability to dispatch further actions etc ...
Although it would be a breaking change it seems a relatively straightforward one. For example, for the onFetchListSuccess and onFetchListError you could change
if (only.includes('FETCH_LIST')) {
Object.assign(crudActions, {
/**
* GET /api/<resourceName>
*
* Fetch list of resources.
*/
fetchList({ commit }, { config, customUrl, customUrlFnArgs = [] } = {}) {
commit('fetchListStart');
return client.get(urlGetter({ customUrl, customUrlFnArgs }), config)
.then((res) => {
const parsedResponse = parseList(res);
commit('fetchListSuccess', parsedResponse);
return parsedResponse;
})
.catch((err) => {
const parsedError = parseError(err);
commit('fetchListError', parsedError);
return Promise.reject(parsedError);
});
}
});
}
to
if (only.includes('FETCH_LIST')) {
Object.assign(crudActions, {
/**
* GET /api/<resourceName>
*
* Fetch list of resources.
*/
fetchList({ commit, dispatch }, { config, customUrl, customUrlFnArgs = [] } = {}) {
commit('fetchListStart');
return client.get(urlGetter({ customUrl, customUrlFnArgs }), config)
.then((res) => {
const parsedResponse = parseList(res);
dispatch('fetchListSuccess', parsedResponse);
return parsedResponse;
})
.catch((err) => {
const parsedError = parseError(err);
dispatch('fetchListError', parsedError);
return Promise.reject(parsedError);
});
}
});
}
I think that would work but forgive me if I'm missing something and this feature cant be implemented. If it can I do think it will make the library a lot more flexible/powerful.
Thanks, Connor