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-async-module

v0.0.14

Published

Example git project that is used for typescript libraries as a starter pack

Downloads

9

Readme

vuex-async-module

NPM version Coveralls github Build Status Standard Version Maintainability Test Coverage

What is this about ??

  • The idea and mainly code in this project have come from this article by @lachlanmiller_52885.
  • Shortly it's about how to reduce vuex boilerplate with "call — pending — success — failure pattern".
  • Each module provide each state of vuex as a API including data, status of request and some hook function to customize your state.
  • For more information take a look at our example.

Dependencies

  • vue
  • vuex
  • axios

Installation

Install via npm

npm install vuex-async-module

Install via yarn

yarn add vuex-async-module

Usage

createVuexAsyncModule

Create vuex store by using our "createVuexAsyncModule" function. When vuex store was created you don't need to know or write anything inside vuex, But all you need to know is about all functions we provided for you to get the job done easily.

import Vue from 'vue'
import Vuex from 'vuex'
import { createVuexAsyncModule } from 'vuex-async-module'

Vue.use(Vuex)

export const store = new Vuex.Store({
  modules: {
    Info: {
      ...createVuexAsyncModule('info')
    }
  }
})

State

This is module state created by createVuexAsyncModule

  state: {
      data: null, // data from AJAX call, You can customize this by using beforeSave callback (look at next section)
      pending: false, // loading status (true = loading, false = done)
      statusCode: 0, // response status code
  },

mapGetters

One getter available for each module and it represent a state itself. The name of getter is module name then follow by "State" for example module "info" and getter will be "infoState"

<template>
  <h1>{{infoState.something}}</h1>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  computed: {
    ...mapGetters(['infoState'])
  }
}
</script>

mapActions

One action available for each module and it can be customize for any endpoint you need. The name of action is "request" follow by module name then "Async" for example module "info" and action will be "requestInfoAsync" Once you call action with provided endpoint the function will save reponsed data on state immediately. Note that axiosConfig is the same config from axios libary itself.

<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      // after this called all response data will be saved as module state automaticly.
      this.requestInfoAsync({
        axiosConfig: {
          url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
        }
      })
    }
  }
}
</script>
  • Customize data before it save to module state.
<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      this.requestInfoAsync({
        axiosConfig: {
          url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
        },
        beforeSave: (data, state) => {
          // data = reponse data when our request already success
          // state = last data state

          // filter then return to save in module state (last state will be gone by this)
          return data.map(item => item.name)
        }
      })
    }
  }
}
</script>
  • Handle action with promise using then, catch
<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      this.requestInfoAsync({
        axiosConfig: {
          url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>
  • Handle action with promise using await [2]
<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      try {
        await this.requestInfoAsync({
          axiosConfig: {
            url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
          }
        })
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>
  • Handle action with callback
<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      this.requestInfoAsync({
        axiosConfig: {
          url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
        },
        onSuccess: (res) => {
          console.log(res)
        },
        onError: (err) => {
          console.log(err)
        }
      })
    }
  }
}
</script>

mapGetters

When data from AJAX call saved to state then you can map getter to sync your data to the template. One getter available for each module and it represent a state itself. The name of getter is module name then follow by "State" for example module "info" and getter will be "infoState"

<template>
  <div v-if="infoState.pending === false">
    <h1>{{infoState.data.something}}</h1>
  </div>
  <div v-else>
    Loading...
  </div>
</template>

import {mapGetters} from 'vuex'

export default {
  computed: {
    ...mapGetters(['infoState'])
  }
}

Example

We provided a lot of way using our libary including.

  • Create vuex store (easiest way) [Click]
  • Create vuex store by grouping module (by code **Compiled as one module) [Click]
  • Create vuex store by grouping module (by module **Compiled as seperated modules) [Click]
  • How to handle action using promise and callback [Click]

Unit Test [For Contributors]

If you clone this repo and did some changes do not forget to update the test file then..

Test via npm

npm run test

Test via yarn

yarn test