npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuex-module-accessor

v1.0.6

Published

a module accessor for vuex

Downloads

3

Readme

Vuex Module Accessor - Using Typescript to make Vuex type-safe

Managing the state of a large application without using types is an insane technical decision! We became aware of that in out team when out projects was getting larger and larger. So we decided to develop a wrapper to make sate management possible in a type-safe manner wihout loosing the benefits of Vuex.

License

MIT License

Installation

npm:

npm i vuex-module-accessor

yarn:

yarn add vuex-module-accessor

Creating a module

Define the state of your module in a POJO class:

class TestState {
	testValue: string = 'Module Accessor';
	count: number = 0;
}

and then define other parts of module by derivating a class from Module<State>:

import { Module } from 'vuex-module-accessor';
class TestModule extends Module<TestState> {
	constructor() {
		super(new TestState());
	}
}

Mutations

Every setter in the module class will be considered as a mutation. Also you can use mutation decorator to define non-setter mutations. Note that these are mutations for real, and so they will not have access to getters and actions.

import { Module, mutation } from 'vuex-module-accessor';
class TestModule extends Module<TestState> {
	constructor() {
		super(new TestState());
	}

	// Becomes a mutation, and so can alter the state
	set count(count: number) {
		this.state.count = count;
	}

	// Another way to define a mutation, when defining a setter does not make sense
	@mutation
	reset() {
		this.state.count = 0;
	}
}

Getters

All getters will be considered as module getters.

import { Module, mutation } from 'vuex-module-accessor';
class TestModule extends Module<TestState> {
	constructor() {
		super(new TestState());
	}

	get count(): number {
		return this.state.count;
	}
}

Actions

Methods without any decoration will be considered as actions. So they can read the state, access getters and call mutations.

import { Module } from 'vuex-module-accessor';
class TestModule extends Module<TestState> {
	constructor() {
		super(new TestState());
	}

	increase() {
		// Reading state and calling a mutation
		this.count = this.state.count + 1;
	}
	decrease() {
		this.count = this.state.count - 1;
	}
}

ModuleAccessor

ModuleAccessor will handle the conversion of the Module class to a typical Vuex Module. It also will provide a reference to ModuleWrapper, that will handle calling vuex operations in a type-safe manner. You will use it like this to define a module and have a reference to access it anywhere in your program.

// Defines a module based on TestModule with namespace /testStore/
export default new ModuleAccessor(new TestModule(), 'testStore/');

Registering the module

Nuxt.js

In a Nuxt.js project, you are already done, you just have to put the file that was described above inside store directory. Note that the path must be compatible with the namespace that you specify:

~/store/testStore.ts

export default new ModuleAccessor(new TestModule(), 'testStore/');

Vue

In a Vue project, you need to import accessor and register it as module, where you initialize Vuex store. ~/store/index.ts

import Vue from 'vue';
import Vuex from 'vuex';
// modules
import testStore from './testStore';

Vue.use(Vuex);

export default new Vuex.Store({
	state: {},
	mutations: {},
	actions: {},
	modules: { testStore: testStore.getModule() }
});

Accessing the module

You can use the exported ModuleWrapper in order to access the Vuex module with type-safety.
This will make interacting with Vuex feel like working with an ordinary class, and handles all the work needed to call the Vuex apis behind the scene.

ModuleAccessor.of()

accessor.of() : ModuleWrapper has a method, called of() that takes the store instance and returns the Module that can be used to access all module's features:

import test from '../store/testStore';
test.of(this.$store).state.testValue; // Returns the value of testValue in state

You can use a computed value to avoid repeating test.of(...):

import test, { TestModule } from '../store/testStore';
...
computed: {
    ...
		testStore(): TestModule {
			return test.of(this.$store);
		},
		testValue():string{
		    return this.testStore.state.testValue;
		}
	...
	}
...

typedMapState and typedMapGetters

This tool will provide type-safe versions of normal mapState and mapGetters apis, that will become handy:

import { typedMapState } from 'vuex-module-accessor';
import { TestModule } from '../store/testStore';
...
computed: {
    ...
		...typedMapState(test, {
			testValue: (state) => state.testValue,
		}),
		...typedMapGetters(test, {
			count: testModule => testModule.count,
		})
	...
	},
	mounted() {
		console.log(this.testValue); // Contain value of testValue state
		console.log(this.count); // Contain value of count getter
	}
...

Examples

Vue example

Nuxt.js example

Nuxt provider example

Acknowledgments

This project is made during development of MrBilit, the online travel agency. Mrbilit

Contribution

We have no plan for accepting contribution outside of our team on the project right now, until it becomes ready to be publicly developed. Anyway, we are ready to hear feedbacks from you to improve it!