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

v0.2.1

Published

a helper utility to simplify the usage of REST api with vuex.

Downloads

2

Readme

vuex-sugar

一个帮助简化使用 Vuex 发起 HTTP(REST API)请求的工具。支持 Vuex 2,默认使用流行的 HTTP 客户端axios发起请求。

vuex-sugar是一个旨在使应用程序副作用(即数据获取等异步事件和访问浏览器缓存等不正确的事物)更易于管理,执行效率更高的库,功能强大且完全可自定义。

如果你打算发起通过vuex来发起一个 HTTP API(尤其是 REST API)请求,你会发现这需要几个重复的步骤。你需要编写一个action来发起请求,然后编写一个mutation来改变state。或者你想要在一些地方集中处理请求,或者执行一些回调。本库能够帮助你生成一个 store,你只需按照简单的形式设置,它就会自动帮你生成 state、mutations、actions,然后自动处理请求并改变 state。

vuex-sugar并不是一个 Vuex plugin,仅仅只是一个帮助简化生成 Store 对象的工具,你可以对你任何不满意的地方进行重写。

English Document

用法

// step 1
import VuexSugar from 'vuex-sugar';

// step 2
const posts = VuexSugar({
    baseURL: '/v1/client',
    state: { posts: [] }
})
    // step 3
    .get({
        action: 'getPost', // action name
        property: 'post', // state property name
        path: ({ id }) => `/posts/${id}`
    })
    .get({
        action: 'listPosts',
        property: 'posts',
        path: '/posts'
    })
    .post({
        action: 'updatePost',
        property: 'post',
        path: ({ id }) => `/posts/${id}`
    })
    // step 4
    .getStore(); // create store object

// step 5
export const store = new Vuex.Store(posts);

然后你可以在vue组件中 dispatch a action,并且获得 HTTP 请求返回的数据。

// dispatch action
this.listPosts() // with mapActions

// get state
...mapState(['posts'])

实际上,上面的代码自动生成了如下代码。

{
   namespaced: false,
   state: {
       pending: {
           posts: false,
           post: false
       },
       error: {
           posts: null,
           post: null
       },
       posts: [],
       post: null
   },
   mutations: {
       LIST_POSTS: Function,
       LIST_POSTS_SUCCEEDED: Function,
       LIST_POSTS_FAILED: Function,
       GET_POST: Function,
       GET_POST_SUCCEEDED: Function,
       GET_POST_FAILED: Function,
       UPDATE_POST: Function,
       UPDATE_POST_SUCCEEDED: Function,
       UPDATE_POST_FAILED: Function
   },

   actions: {
       listPosts: Function,
       getPost: Function,
       updatePost: Function
   }
}

Oh, Yeah。更多用法参考官方文档

开发步骤

# install dependencies
yarn

# build for production with minification
yarn run build

# run eslint and fix code
npm run lint

# run all tests
yarn run test

修改历史

see CHANGELOG.md

致谢

本库的灵感来自于redux-sagavuex-rest-api

For a detailed explanation on how things work, contact us [email protected].