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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vuex-api-request

v2.13.2

Published

減少 api 請求重複代碼,開心開發 :)

Readme

Vuex Api Request

減少 api 請求重複代碼,開心開發 :)

安裝

$ yarn add vuex-api-request

API Document

Http

使用請求 Instance,我們使用 Axios 套件,相關設定請看 Axios。

import Vue from 'vue'
import {Http} from 'vuex-api-request'

const axiosConfig = {
  baseURL: 'http://localhoat:3002',
}

// 將請求 Instance 綁定在 Vue 上
Vue.use(Http(axiosConfig))

在 js 檔案中使用

import Vue from 'vue'

Vue.http.get('/posts')

在 vue 檔案中使用

export default {
  methods: {
    getPosts() {
      this.$http.get('/posts')
    },
  }
}

LocalStoragePlugin

localStorage 與 vuex 做綁定,當 vuex 的狀態會跟 localStorage 同步,可使用來儲存使用者 token 等資料

// store.js
import Vue from 'vue'
import Vuex from 'vuex'
import auth from './modules/auth'
import {LocalStoragePlugin} from 'vuex-api-request'

Vue.use(Vuex)

const authLocalStoragePlugin = LocalStoragePlugin({
  storageKey: 'paradise-soft',
  vuexModule: ['auth', auth], // [moduleName, module]
})

export default new Vuex.Store({
  state: {},
  mutations: {},
  actions: {},
  getters: {},
  modules: {
    auth,
  },
  plugins: [authLocalStoragePlugin],
})

Auth

當我們使用 LocalStoragePlugin 跟 vuex 做綁硬後,我們就可以記住使用者的登入狀態,我們可以從 localStorage 或 vuex module state 取得 token 建立帶有 token 的請求

import Vue from 'vue'
import store from '@/store'
import {Auth} from 'vuex-api-request'

const axiosConfig = {
  baseURL: '/api',
}

Vue.use(
  Auth({
    axiosConfig,
    headerBinding: {
      Authorization: [store, (state) => state.auth.accessToken]
    },
  })
)

在 js 檔案中使用

import Vue from 'vue'

Vue.auth.get('/posts')

在 vue 檔案中使用

export default {
  methods: {
    getPosts() {
      this.$auth.get('/posts')
    },
  }
}

VuexApiRequest

Step 1 - 安裝 VuexApiRequest 與 store 做綁定,及加入 VuexApiRequest mixin

// main.js
import Vue from 'vue'
import store from 'store'
import VuexApiRequest from 'vuex-api-request'

Vue.use(VuexApiRequest(store))

Step 2 - 建立 watch

import VuexApiRequest from 'vuex-api-request'

const watch = VuexApiRequest.createWatch({
  response: (res) => res.data,
  error: (err) => err,
  errorHandler: (context, err) => {
    if (err.status === 401) context.dispatch('auth/logout')
  },
})

// vuex posts module
const actions = {
  async getPosts(context, payload) {
    const res = await watch(context, 'getPosts')(fetchPosts(payload))
  },
}

在 Vue 檔取得狀態

<template>
  <div>
    {{$api('getPosts').pedding}}
    {{$api('getPosts').error}}
  </div>
</template>

export default {
  computed: {
    getPostsPedding() {
      return this.$api('getPosts').pedding
    },
    getPostsError() {
      return this.$api('getPosts').error
    }
  }
}

queryStringMixin

import queryStringMixin from '@/mixins/queryStringMixin'

export default {
  mixins: [queryStringMixin],
  data() {
    return {
      filters: this.queryStringAssign({
        page: {type: Number, default: 1},
        per_page: {type: Number, default: 10}
      })
    }
  },
  watch: {
    filters: {
      handler(val) {
        this.getPosts(this.queryStringReplace(val))
      },
      immediate: true,
      deep: true,
    }
  }
}