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

@pansy/request

v4.3.0

Published

基于 Axios 的统一网络请求和错误处理方案

Downloads

17

Readme

docs by dumi layout layout

🌈 前言

后端接口一般情况下区别请求是否成功主要为以下三种情况

  1. 完全按照 Http 状态码,[200 ~ 300) 表示成功,其他表示请求失败
  2. 始终保证请求返回 Http 状态码为 200,根据返回数据的中约定的 code 去判断请求是否成功
  3. 两者混用

为了满足第二种情况,对 axios 进行了改造,发布了 @pansy/axios,主要区别如下:

添加 validateDataStatus 配置项,类型定义如下

{
  // 根据请求返回的数据判定该请求是否成功
  // data 为后端返回的数据
  validateDataStatus: (data?: any) => { success: boolean; message?: string };
}

一个请求的响应包含以下信息 Axios 响应结构

interface AxiosResponse<T = any, D = any> {
  data: T;
  status: number;
  statusText: string;
  headers: RawAxiosResponseHeaders | AxiosResponseHeaders;
  config: InternalAxiosRequestConfig<D>;
  request?: any;
}

后端返回的数据格式一般情况下都包含以下类似字段

{
  /** 接口状态码 */
  code: number;
  /** 接口返回数据 */
  data: any;
  /** 接口报错信息 */
  message: string;
}

前端开发时,大多数情况下只需要消费上述数据格式中的 data,例如下面的示例中的 { name: 'test' }

{
  code: 0;
  data: { name: 'test' },
  message: 'success'
}

✨ 特性

  • 🌈 在 Axios 基础上进行了自己的封装,更加易用
  • 🎨 支持根据接口返回数据判定请求是否成功
  • 🛡 使用 TypeScript 开发,提供完整的类型定义文件

📦 安装

# npm install
npm install @pansy/request --save

# yarn install
yarn add @pansy/request

# pnpm install
pnpm i @pansy/request

🔨 示例

import { request, setConfig } from '@pansy/request';

// 需要初始化
setConfig({
  baseURL: 'https://api.pansy.com'
})

export async function fetchUser(params) {
  return request<{ name: string }>('/api/user', {
    method: 'GET',
    params,
  })
}

接口返回数据格式

enum ErrorShowType {
  /** 不做处理 */
  SILENT = 0,
  /** 全局提示 - 警告 */
  WARN_MESSAGE = 1,
  /** 全局提示 - 异常 */
  ERROR_MESSAGE = 2,
  /** 通知提醒 */
  NOTIFICATION = 3,
  /** 重定向 */
  REDIRECT = 9,
}

interface ResponseData<D = any> {
  /** 接口状态码 */
  code: number;
  /** 接口返回数据 */
  data: D,
  /** 接口报错信息 */
  message: string;
  /** 接口报错处理类型 */
  showType?: ErrorShowType;
  [key: string]: any;
}