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 🙏

© 2025 – Pkg Stats / Ryan Hefner

admin-axios-plus

v1.1.2

Published

axios扩展, 登录拦截【后置】、请求封装、过期刷新Token。

Readme

admin-axios-plus

介绍

axios 扩展,登录拦截【后置】、请求封装、过期刷新 Token。

安装

  npm i admin-axios-plus --save

说明

备注:

  1. 鉴权秘钥(获取getToken()的值)携带在请求头的Authorization字段中。
  2. 接口统一响应格式 { code: 0, msg: '' data: ... },code 用于状态判断。
  3. 登录拦截:当响应 code 值在unauthorizedStatusCodes数组中时,触发未授权处理。
  4. 过期刷新:当响应 code 值在refreshTokenStatusCodes数组中时,触发刷新 token 请求。

请求封装

  • getJSON(url: string, data?: any, config?: AxiosRequestConfig) : Promise
  • postJSON(url: string, data?: any, config?: AxiosRequestConfig) : Promise
  • putJSON(url: string, data?: any, config?: AxiosRequestConfig) : Promise
  • deleteJSON(url: string, data?: any, config?: AxiosRequestConfig) : Promise
  • uploadFile(url: string, file: any, params: object, onProgress?: (e: any) => void): Promise
  • downloadFile(url: string, data?: any, method = "get"): Promise

本地存储(Cookies)

  • Cookies: Cookies对象
  • getToken(key?: string): string | undefined
  • setToken(token: string, expires?: number, key?: string): undefined // 有效时长(秒)
  • removeToken(key?: string): void
export declare interface AxiosConfig {
  axiosDefaults: Omit<AxiosDefaults, "headers">; // Axios默认配置
  unauthorizedStatusCodes?: number[]; // 未授权状态码
  refreshTokenStatusCodes?: number[]; // 刷新token状态码
  unauthorizedHandler: () => void; // 未授权处理函数
  refreshTokenRequest?: () => Promise<void>; // 刷新token请求函数
}

使用

main.ts

import { createApp } from 'vue'
import { createPinia } from 'pinia'

import AxiosPlus from 'admin-axios-plus'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import router from './router'
import App from './App.vue'

import './assets/main.css'

const app = createApp(App)

app.use(createPinia())
app.use(router)

app.use(ElementPlus)

app.use(AxiosPlus, {
  axiosDefaults: {
    baseURL: "/api", // 基础URL
  },
  refreshTokenStatusCodes: [402],
  unauthorizedStatusCodes: [401],
  unauthorizedHandler: () => {
    removeToken();
    router.push("/login");
  },
  refreshTokenCallback: (res) => {
    // 请求刷新token后,写入新token
    setToken(res.data.data.token, res.data.data.expires);
  },
})

app.mount('#app')

example.vue

<template>
  <div class="example">
    <h1>This is an index page</h1>
  </div>
</template>

<script setup lang="ts">
import { getJSON, postJSON } from 'admin-axios-plus'

function onRequest() {
  getJSON('/tableColumn/list', { tableCode: 'user_role' }).then((res) => {
    console.log(res)
  })
}

function onLogin() {
  postJSON(
    '/login/pwd',
    {
      loginName: 'admin',
      loginPwd: 'admin123456',
    },
    { headers: { version: 2 } }
  ).then((res) => {
    console.log(res)
  })
}
</script>