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-with-http-calls

v1.0.2

Published

A helper function to automatically create vuex modules with http calls as actions.

Downloads

3

Readme

NPM npm

vuex-module-with-http-calls

A helper function to automatically create vuex modules with http calls as actions.

Install

with yarn:

yarn add vuex-module-with-http-calls

with npm:

npm install --save vuex-module-with-http-calls

Usage

Let's suppose that we want to define a vuex module called profile. We can, for instance, create a file file profile-vuex-module.js and put some content inside:

import { withHttpCalls } from 'vuex-module-with-http-calls'

const baseURL = 'https://example.com/api'

const httpCalls = [
  // results in an action `DeleteUserProfile` that calls
  // `GET https://example.com/api/DeleteUserProfile`
  // notice that GET is the default method
  { url: 'DeleteUserProfile' },
  // results in an action `DeleteUserProfile` that calls 
  // `DELETE https://example.com/api/DeleteUserProfile`
  { url: 'DeleteUserProfile', method: 'delete' },
  // results in a named action `getProfile` that calls 
  // `POST https://example.com/api/GetUserProfile`
  // the result of the call will be stored in `state.profile` property
  // you can call the generated action with some argument
  //    getProfile({ uid: "137faa4c-e8d7-4a98-8945-37dd7fe21af8" })
  {
    name: 'getProfile',
    url: 'GetUserProfile',
    method: 'post',
    resultToStateField: 'profile',
    onSuccess: data => console.log('request went fine, result:', data),
    onError: {
      // keys are the http status response
      '401': error => console.log('Unauthorized', error),
      '500': error => console.log('internal server error', error)
    }
  }
]

// you can either provide state, getters, mutations and extra actions
// or choose not to, in the last case empty objects will be created and used
const profileModule = withHttpCalls({
  baseURL,
  httpCalls
})

export default profileModule

Notice that the action arguments will be passed as the data property to the underlying axios.request method while performing the http call. If no name property is passed then the url is used as the action name. The httpCalls examples are organized in an ascending complexity way here to point out the many options we may pass but the only required field is the url one. All the actions created to perform the http calls are marked as async.

Then you can later register the vuex module profileModule by doing:

import profileModule from './profile-vuex-module'
...
// here `store` is the `vuex` store instance
// register the module dynamically
store.registerModule('profile', profileModule)
...

or

// or at store creation time
...
const store = new Vuex.Store({
  modules: {
    profile: profileModule
  }
})
...

somewhere in your code.

Default content of the module

The module by default has:

  • namespaced = true.
  • state = { requesting: false, token: '' }. The requesting field will be set to true when a http call starts and false when completed and it can be used to display loaders while requesting, for example.
  • getters = {}.
  • A mutation set which just update the correspondent key in the state with the passed value.
  • An action setToken which can be used to set the authentication jwt token header, see next section.

Any additional state, gettters, actions and mutations provided to the withHttpCalls function:

const profileModule = withHttpCalls({
  baseURL,
  httpCalls,
  state,
  getters,
  mutations,
  actions,
  namespaced: true // or false as needed
})

will be merged with these.

Authentication

You can configure to send an Authorization: Bearer token header on each http request by setting the token in the module state using the built in setToken action. Let's assume that somewhere in your app you authenticate against your authentication server and you get a valid JWT token in exchange, then you can dispacth the setToken action, for instance from another vuex module action body, by doing:

store.dispatch('profile/setToken', { token: 'ey...' }, { root: true })

From now on your module request will have the Authorization: Bearer ey... header.

Sponsors

Kenkou GmbH