Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access to state and getters in MutationAction #379

Merged
merged 5 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ coverage/
# NodeJS
node_modules/

# Build
/dist

# Vuepress
/docs/.vuepress/dist/

# Cache
/.cache/
/.rpt2_cache/
/.rpt2_cache/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
If `(beta)` or `(alpha)` is marked in front of any release, it can be
installed as `npm install vuex-module-decorators@beta` (or alpha similar).

## 1.1.1
- fix deployment issues when installing directly from git

## 1.1.0
- add access to state and getters in MutationAction

## 1.0.0

### 0.17.0
Expand Down
551 changes: 551 additions & 0 deletions dist/cjs/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/cjs/index.js.map

Large diffs are not rendered by default.

541 changes: 541 additions & 0 deletions dist/esm/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/esm/index.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions dist/types/action.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Parameters that can be passed to the @Action decorator
*/
export interface ActionDecoratorParams {
commit?: string;
rawError?: boolean;
root?: boolean;
}
export declare function Action<T, R>(target: T, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => R>): void;
export declare function Action<T>(params: ActionDecoratorParams): MethodDecorator;
4 changes: 4 additions & 0 deletions dist/types/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const config: IConfig;
export interface IConfig {
rawError?: boolean;
}
12 changes: 12 additions & 0 deletions dist/types/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Takes the properties on object from parameter source and adds them to the object
* parameter target
* @param {object} target Object to have properties copied onto from y
* @param {object} source Object with properties to be copied to x
*/
export declare function addPropertiesToObject(target: any, source: any): void;
/**
* Returns a namespaced name of the module to be used as a store getter
* @param module
*/
export declare function getModuleName(module: any): string;
6 changes: 6 additions & 0 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { VuexModule, getModule } from './vuexmodule';
export { Module } from './module';
export { Action } from './action';
export { Mutation } from './mutation';
export { MutationAction } from './mutationaction';
export { config } from './config';
4 changes: 4 additions & 0 deletions dist/types/module/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Module as Mod } from 'vuex';
import { ModuleOptions } from '../moduleoptions';
export declare function Module<S>(module: Function & Mod<S, any>): void;
export declare function Module<S>(options: ModuleOptions): ClassDecorator;
2 changes: 2 additions & 0 deletions dist/types/module/stateFactory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Module as Mod } from 'vuex';
export declare function stateFactory<S>(module: Function & Mod<S, any>): S;
6 changes: 6 additions & 0 deletions dist/types/module/staticGenerators.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Module as Mod } from 'vuex';
import { DynamicModuleOptions } from '../moduleoptions';
export declare function staticStateGenerator<S extends Object>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
export declare function staticGetterGenerator<S>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
export declare function staticMutationGenerator<S>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
export declare function staticActionGenerators<S>(module: Function & Mod<S, any>, modOpt: DynamicModuleOptions, statics: any): void;
45 changes: 45 additions & 0 deletions dist/types/moduleoptions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Store } from 'vuex';
/**
* Options to pass to the @Module decorator
*/
export interface StaticModuleOptions {
/**
* name of module, if being namespaced
*/
name?: string;
/**
* whether or not the module is namespaced
*/
namespaced?: boolean;
/**
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean;
}
export interface DynamicModuleOptions {
/**
* If this is a dynamic module (added to store after store is created)
*/
dynamic: true;
/**
* The store into which this module will be injected
*/
store: Store<any>;
/**
* name of module, compulsory for dynamic modules
*/
name: string;
/**
* If this is enabled it will preserve the state when loading the module
*/
preserveState?: boolean;
/**
* whether or not the module is namespaced
*/
namespaced?: boolean;
/**
* Whether to generate a plain state object, or a state factory for the module
*/
stateFactory?: boolean;
}
export declare type ModuleOptions = StaticModuleOptions | DynamicModuleOptions;
1 change: 1 addition & 0 deletions dist/types/mutation.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function Mutation<T extends Object, R>(target: T, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => R>): void;
9 changes: 9 additions & 0 deletions dist/types/mutationaction.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface MutationActionParams<M> {
mutate?: (keyof Partial<M>)[];
rawError?: boolean;
root?: boolean;
}
export declare function MutationAction<K, T extends K>(target: {
[k in keyof T]: T[k] | null;
}, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<K>>): void;
export declare function MutationAction<T>(params: MutationActionParams<T>): (target: T, key: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<T>>) => void;
22 changes: 22 additions & 0 deletions dist/types/vuexmodule.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ActionTree, GetterTree, Module as Mod, ModuleTree, MutationTree, Store, ActionContext } from 'vuex';
export declare class VuexModule<S = ThisType<any>, R = any> implements Mod<S, R> {
static namespaced?: boolean;
static state?: any | (() => any);
static getters?: GetterTree<any, any>;
static actions?: ActionTree<any, any>;
static mutations?: MutationTree<any>;
static modules?: ModuleTree<any>;
modules?: ModuleTree<any>;
namespaced?: boolean;
getters?: GetterTree<S, R>;
state?: S | (() => S);
mutations?: MutationTree<S>;
actions?: ActionTree<S, R>;
context: ActionContext<S, R>;
constructor(module: Mod<S, any>);
}
declare type ConstructorOf<C> = {
new (...args: any[]): C;
};
export declare function getModule<M extends VuexModule>(moduleClass: ConstructorOf<M>, store?: Store<any>): M;
export {};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vuex-module-decorators",
"version": "1.0.1",
"version": "1.1.1",
"description": "Decorators to make class-like Vuex modules",
"main": "dist/cjs/index.js",
"types": "dist/types/index.d.ts",
Expand Down Expand Up @@ -41,7 +41,7 @@
},
"peerDependencies": {
"vue": ">=2",
"vuex": "3"
"vuex": ">=3"
},
"homepage": "https://github.com/championswimmer/vuex-module-decorators#readme",
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion src/mutationaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Action as Act, ActionContext, Module as Mod, Mutation as Mut, Payload, Store } from 'vuex'
import { addPropertiesToObject } from './helpers'

export interface MutationActionParams<M> {
mutate?: (keyof Partial<M>)[]
Expand Down Expand Up @@ -26,7 +27,10 @@ function mutationActionDecoratorFactory<T extends Object>(params: MutationAction
payload: Payload
) {
try {
const actionPayload = await mutactFunction.call(context, payload)
const thisObj = { context }
addPropertiesToObject(thisObj, context.state)
addPropertiesToObject(thisObj, context.getters)
const actionPayload = await mutactFunction.call(thisObj, payload)
context.commit(key as string, actionPayload)
} catch (e) {
if (params.rawError) {
Expand Down
12 changes: 12 additions & 0 deletions test/mutationaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class MyModule extends VuexModule {

return {count: newcount}
}

@MutationAction({ mutate: ['count'] })
async incrementCount() {
return { count: this.count + 1 }
}
}

const store = new Vuex.Store({
Expand Down Expand Up @@ -75,4 +80,11 @@ describe('dispatching moduleaction works', () => {
await store.dispatch('changeFruit', 'Guava')
expect(store.state.mm.fruit).to.equal('Guava')
})

it('can access state', async function() {
await store.dispatch('updateCount', 0)
expect(store.state.mm.count).to.equal(0)
await store.dispatch('incrementCount')
expect(store.state.mm.count).to.equal(1)
})
})