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-functional

v1.1.1

Published

Functional-style accessors for the Vuex store

Downloads

77

Readme

Vuex-Functional

Codacy Badge CodeFactor Snyk

This TypeScript library provides a set of functions that reveal the committers, dispatchers, getters, state, and modules of Vuex stores in the Vue-Composition API. The goal of this project is to provide TypeScript hinting with low-overhead and zero configuration.

Browser Support

This library should work in any browser that supports ES6. It does, however, use JavaScript Proxies — IE11 is not supported, and there are no plans to support it in the future.

Installation

npm i vuex-functional

Usage

This section explains how to extract content from the store. For the following examples, this is the store file:

const storeState = {
  string: "string",
  number: 1
};

export const options = {
  state: () => ({ ...storeState }),
  getters: {
    get_string: (state: typeof storeState) => state.string,
    get_number: (state: typeof storeState) => state.number
  },
  mutations: {
    SET_STRING: (state: typeof storeState, payload: { string: string }) =>
      (state.string = payload.string),
    SET_NUMBER: (state: typeof storeState, payload: { number: number }) =>
      (state.number = payload.number),
    NO_PARAM: () => void 0
  },
  actions: {
    action_payload: async (ctx, payload: { string: string }) => {
      ctx.commit("SET_STRING", payload);
    },
    no_payload: async () => true
  },
  modules: {
    bar: {
      namespaced: true,
      state: { bar: "bar", number: 1 },
      mutations: {
        BAR_SET_STRING(this: VStore<any>, state, payload: string) {
          state.bar = payload;
          commits<typeof options>(this).SET_STRING({
            string: payload
          });
        }
      }
    }
  }
};

Get the Store object

Before the Store object can be retrieved, it must first be injected into the Vue application:

import { createApp } from "vue"
import { createStore } from "vuex";

import { options } from "path/to/my/store";
import App from "path/to/App.vue";

createApp(App).use(createStore(options)).mount("#app");

Previous versions of this library supported Vue.extend and the Vue 2.0-compatible Composition API. They are no longer supported.

import { defineComponent } from "vue";
import { getters, store } from "vuex-functional";
import { options } from "path/to/my/store"; //< The options used to create the store

export default defineComponent({
  setup: (_, ctx) => {
    // Extracts the store
    const $store = store<typeof options>(ctx);

    //
    // Your code goes here
    //
  }
});

Store state and getters

State and getters behave exactly the same way they would if you were to access them directly.

import { getters, state, store } from "vuex-functional";
import { options } from "path/to/my/store";

// ...

const $store = store<typeof options>(ctx);
const $state = state($store);
const $getters = getters($store);

$state.number; // 1
$getters.get_number; // 1

Also...

$store.state.number; // 1
$store.getters.get_number; // 1

Store actions and mutations

Actions and mutations now use Proxies to forward the action or mutation name on to the store. If you didn't define a payload in the function, then the payload is either null or left blank; otherwise, TypeScript will automatically detect the payload type and insert it into the tooltip.

import { actions, mutations, state, store } from "vuex-functional";
import { options } from "path/to/my/store";

// ...

const $store = store<typeof options>(ctx);
const $actions = actions($store);
const $mutations = mutations($store);

$mutations.SET_NUMBER({ number: 9 });
await $actions.no_payload(); // `true`

state($store).number; // 9

Store modules

Modules can also be extracted, and passed back into the other functions to access their methods and state:

import { actions, getters, modules, mutations, state, store } from "vuex-functional";
import { options } from "path/to/my/store";

// ...

const $store = store<typeof options>(ctx);

const $barModule = modules($store).bar;

// This mutation forwards to the main store
mutations($barModule).BAR_SET_STRING("hello, world!");

state($store).string; // "hello, world!"
state($barModule).bar; // "hello, world!"

All accessors at once

import { makeAccessors, store } from "vuex-functional";
import { options } from "path/to/my/store";

// ...

const $store = store<typeof options>(ctx);

const { commit: $commit, dispatch: $dispatch, state: $state, getters: $getters } = makeAccessors($store);

//
// Your code here
//