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

lzy-http

v1.0.0

Published

Downloads

4

Readme

请求示例

创建实例 http.ts

import createAxios from 'lzy-http'

//
const http = createAxios({
  authenticationScheme: '',
  urlPrefix: '',
  timeout: 10 * 1000,
  headers: {
    'Content-Type': ContentTypeEnum.JSON,
    ignoreCancelToken: true
  },
  transform: {
    // 请求拦截器处理
    requestInterceptors: () => {}, // 无需修改
    // 后台状态码异常拦截
    handlerAbnormalCode: () => {} // 需自行配置,参照下方示例
    httpStatusCodeInterceptor: () => {},
    successCallback: () => {}
  },
  // 配置项,下面的选项都可以在独立的接口请求中覆盖
  requestOptions: {
    // 默认将prefix 添加到url
    joinPrefix: true,
    // 是否返回原生响应头 比如:需要获取响应头时使用该属性
    isReturnNativeResponse: false,
    // 需要对返回数据进行处理
    isTransformResponse: true,
    // post请求的时候添加参数到url
    joinParamsToUrl: false,
    // 格式化提交参数时间
    formatDate: true,
    // 接口地址
    apiUrl: '',
    //  是否加入时间戳
    joinTime: true,
    // 忽略重复请求
    ignoreCancelToken: true,
    // 是否携带token
    withToken: true
  }
})

export default http
// http 是创建的实例
import http from 'http'
import type { AxiosRequestConfig }  from 'axios

const requestConfig: AxiosRequestConfig = {}

http.get(
  {
    url: '',
    ...
  },
  {
    // 是否返回原生响应头 比如:需要获取响应头时使用该属性
    isReturnNativeResponse: false,
    // 需要对返回数据进行处理
    isTransformResponse: true,
    ...
  }
)

异常状态码拦截处理示例

const handlerAbnormalCode = (
  code: '103107' | '103104' | '103105' | '103109' | '103102'
): void => {
  switch (code) {
    case '103107':
      createModal(
        '登录提示',
        '该用户已在其他地方登录, 确定强制登录吗?',
        async () => {
          await userStore.reLogin()
          location.reload()
        }
      )
      break
    case '103104':
      createModal(
        '登录提示',
        '该用户已在其他地方登录, 确定重新登录吗?',
        async () => {
          removeToken()
          location.reload()
        }
      )
      break
    case '103105':
      createModal(
        '过期提示',
        '该用户密码已过期, 确定跳转修改密码页吗?',
        async () => {
          // 不允许浏览器后退
          router.replace('/resetPass')
        }
      )
      break
    case '103109':
      createModal(
        '变更提示',
        '该用户权限已变更, 确定跳转登录页吗?',
        async () => {
          removeToken()
          location.reload()
        }
      )
      break
    case '103102':
      errorTip('登录已失效,请重新登录!')
      setTimeout(() => {
        removeToken()
        location.reload()
      }, 1000)
      break
    default:
  }
}

请求拦截

const requestInterceptors = config => {
  // 请求之前处理config
  const token = getToken()
  token && (config.headers.Authorization = 'Bearer ' + token)
  return config
}

原生请求异常处理

  • code: 400,401,403,404,405,408,500,501,502,503,504,505.'Network Error','ECONNABORTED','ABNORMAL'
  • msg: 消息内容
const httpStatusCodeInterceptor = (code, msg) => {}

处理数据返回

const successCallback = res => {
  const { code, msg, data: result } = res.data
  const hasSuccess = data && Reflect.has(data, 'code') && code === '000000'
  return {
    hasSuccess,
    result,
    code,
    msg
  }
}