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

@rhao/request-middleware-axios

v4.0.3

Published

Axios middleware of request.

Readme

@rhao/request-middleware-axios

搭建 Axios@rhao/request 桥梁的中间件。

安装

使用 npm

npm i vue @rhao/request @rhao/request-middleware-axios

使用 yarn or pnpm

pnpm add vue @rhao/request @rhao/request-middleware-axios

使用

创建 axios.ts

// utils/axios.ts
import Axios from 'axios'

export const axios = Axios.create({
  // ...
})

创建 useRequest.ts

// hooks/useRequest.ts
import type {
  BasicRequestHook,
  RequestFetcher,
  RequestOptions,
  RequestResult,
} from '@rhao/request'
import { RequestAxios } from '@rhao/request-middleware-vue'
import { AxiosResponse } from 'axios'

type FlattenAxiosResponse<T> = T extends AxiosResponse<infer D> ? D : T

// 自定义调用类型
interface UseRequest extends BasicRequestHook {
  <TData, TParams extends unknown[] = unknown[]>(
    fetcher: RequestFetcher<TData, TParams>,
    options?: RequestOptions<FlattenAxiosResponse<TData>, TParams>,
  ): RequestResult<FlattenAxiosResponse<TData>, TParams>
}

// 后端数据格式
interface DataFormat {
  success: boolean
  data: any
  message?: string
}

export const useRequest = createRequestHook({
  // 此处解析数据格式,仅返回响应的后端数据
  dataParser: (data: AxiosResponse) => {
    // 响应状态码错误
    if (data.status !== 200) throw new Error(data.statusText)

    // 解析后端格式
    const realData: DataFormat = data.data

    // 后端数据错误
    if (!realData.success) throw new Error(realData.message)

    // 返回解析后的数据
    return realData.data
  },
  middleware: [RequestAxios({ associativeCancel: true })]
}) as UseRequest

创建 api.ts

// apis/example.ts
import { axios } from 'utils/axios'

export interface Params {
  a: number
  b: string
}

export interface DataItem {
  a: number
  b: string
}

export const queryList = (params: Params) => axios.get<DataItem[]>('/api/example/list', { params })

使用 useRequest

import { useRequest } from 'hooks/useRequest'
import { queryList } from 'apis/example'

// data => Ref<DataItem[] | null> // 这里会自动对 AxiosResponse 进行拆包
// params => Ref<[Params]> // 会自动推导出 queryList 的入参
const { data, loading, error, params } = useRequest(queryList, {
  immediate: true,

  // 初次自动调用时的参数,设置 `immediate` 为 `true` 时有效
  // 类型会自动推导出 Params
  defaultParams: [{ a: 1, b: '123' }],

  // 这里可以传入 axios 的配置项,支持函数格式
  axiosConfig: {
    timeout: 10e3
  }
})