Skip to content

Commit

Permalink
added commit/dispatch to axios interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
reed-jones committed Feb 13, 2020
1 parent 2aeb5a8 commit eff58d9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ and starting with version v0.1.0, this project adheres to [Semantic Versioning](
### Added
- response()->vuex() and response()->phase() now behave the same. `->phase()` is preferred.
- `Vuex::lazyLoad($namespace, $key)` has been introduced so now ModuleLoaders can easily be lazy loaded without modification
- `Vuex::commit($mutation, $value)` has been added, however it is not yet included in the response to the front end
- `Vuex::dispatch($action, $value)` has been added, however it is not yet included in the response to the front end
- `Vuex::commit($mutation, $value)` has been added
- `Vuex::dispatch($action, $value)` has been added
- Added actions/mutations to axios interceptors

### Deprecated
- `response()->vuex()` is deprecated, and response()->phase() is the preferred, however there are currently no plans to remove it.
Expand Down
20 changes: 15 additions & 5 deletions packages/@phased/state/lib/committer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosInstance, AxiosResponse } from "axios";
import { InitializedVuexStore, VuexModule, VuexStore } from "@phased/state"
import { InitializedVuexStore, VuexModule, VuexStore } from "@phased/state";

export const VuexcellentAutoCommitter = (
axios: AxiosInstance,
Expand All @@ -23,7 +23,7 @@ export const VuexcellentAutoCommitter = (
*
* @param {Object} data_2 server supplied state for mutations
*/
const commitData = (
const autoCommitData = (
{
store,
_state,
Expand Down Expand Up @@ -102,12 +102,22 @@ const autoMutateInterceptor = (
(response: AxiosResponse) => {
if (response.data.$vuex) {
try {
// grab state & modules, if existing
commitData({ store, _state, mutator }, response.data.$vuex);
// grab state & modules, if existing & auto-commit
autoCommitData({ store, _state, mutator }, response.data.$vuex);

// user specified mutations
(response.data.$vuex.mutations || []).forEach(
([mutation, value]: [string, any?]) => store.commit(mutation, value)
);

// user specified actions
(response.data.$vuex.actions || []).forEach(
([action, value]: [string, any?]) => store.dispatch(action, value)
);
} catch (err) {
console.error(err);
console.error(
`[Vuexcellent] An error occurred during the auto commit process.\nYour vuex state may not be what you expected.`
`[@phased/state] An error occurred during the auto commit process.\nYour vuex state may not be what you expected.`
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@phased/state/lib/objectMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const recursiveMerge = (
});
} else {
console.warn(
`[Vuexcellent] The server side data does not match client side expectations.
`[@phased/state] The server side data does not match client side expectations.
Server: ${JSON.stringify(source)}.
Client: ${JSON.stringify(target)}`
);
Expand Down
3 changes: 2 additions & 1 deletion packages/@phased/types/lib/phased__state/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ declare module '@phased/state' {
}

export interface InitializedVuexStore {
commit(mutation: string, data?: any): void
commit(mutation: string, data?: any): void,
dispatch(action: string, data?: any): void
}

export interface VuexcellentOptions {
Expand Down

0 comments on commit eff58d9

Please sign in to comment.