From eff58d9889fe4d161c509d4d43e90f188b033e38 Mon Sep 17 00:00:00 2001 From: Reed Jones Date: Wed, 12 Feb 2020 23:15:02 -0400 Subject: [PATCH] added commit/dispatch to axios interceptor --- CHANGELOG.md | 5 +++-- packages/@phased/state/lib/committer.ts | 20 ++++++++++++++----- packages/@phased/state/lib/objectMerge.ts | 2 +- .../types/lib/phased__state/index.d.ts | 3 ++- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bf7b0c..9359fba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/packages/@phased/state/lib/committer.ts b/packages/@phased/state/lib/committer.ts index c9d9873..2b8bba1 100644 --- a/packages/@phased/state/lib/committer.ts +++ b/packages/@phased/state/lib/committer.ts @@ -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, @@ -23,7 +23,7 @@ export const VuexcellentAutoCommitter = ( * * @param {Object} data_2 server supplied state for mutations */ -const commitData = ( +const autoCommitData = ( { store, _state, @@ -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.` ); } } diff --git a/packages/@phased/state/lib/objectMerge.ts b/packages/@phased/state/lib/objectMerge.ts index 9f83284..c857348 100644 --- a/packages/@phased/state/lib/objectMerge.ts +++ b/packages/@phased/state/lib/objectMerge.ts @@ -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)}` ); diff --git a/packages/@phased/types/lib/phased__state/index.d.ts b/packages/@phased/types/lib/phased__state/index.d.ts index 8a84f3f..e2e8e1e 100644 --- a/packages/@phased/types/lib/phased__state/index.d.ts +++ b/packages/@phased/types/lib/phased__state/index.d.ts @@ -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 {