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

@dreamjser/request-axios

v1.0.13

Published

a request lib create by axios

Downloads

27

Readme

使用教程

安装

  // npm安装
  npm i @dreamjser/request-axios

  // yarn安装
  yarn add @dreamjser/request-axios

    // pnpm安装
  pnpm add @dreamjser/request-axios

使用

import { getGlobalAxios, getAxios, AllType, OptionsGlobalType } from '@dreamjser/request-axios'

// 获取axios实例,设置全局的配置
const globalOpts: OptionsGlobalType = {
  timeout: 30000,
  baseURL: GLOBAL_CONFIG.BASE_URL,
}
const axiosInstance = getGlobalAxios(globalOpts)

// 获取axios实例
const axiosInstance = getAxios(opts: AllType, globalInstance)

getGlobalAxios(globalOpts)

globalOpts参数可以设置axios的一些全局配置,配置同axios,如下:

{
  // baseURL: 'http://api.xxx.com/'
  baseURL?: string
  // timeout: 15000
  timeout?: number
  // 上传进度回调
  onUploadProgress?: (progressEvent: AxiosProgressEvent) => void
  // 下载进度回调
  onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void
  // headers: {'content-type': 'xxx'}
  headers?: object
}

getAxios(opts, globalInstance)

opts参数可以设置axios的配置,配置同axios,同时新增了一些参数如下

{
  // 是否显示loading,默认true
  slient?:
  // 是否上传文件,默认false,为true则默认使用FormData数据
  isUpload?: boolean
  // 传参数据,不区分method
  data?: object
  // 是否显示公共错误提示,默认true,为false可以在调用时捕获异常做处理
  publicError?: boolean
  // 请求发起前hook
  requestHook?: (opts: AllType) => any
  // 请求返回数据hook
  responseHook?: (reslove: any, reject: any, data: any) => void
}

示例

import { getGlobalAxios, getAxios, AllType, OptionsGlobalType } from '@dreamjser/request-axios'
import { showLoading, hideLoading } from './loading'

const globalOpts: OptionsGlobalType = {
  timeout: 30000,
  baseURL: GLOBAL_CONFIG.BASE_URL,
}
const axiosInstance = getGlobalAxios(globalOpts)

const requestHook = (config: AllType) => {
  !config.slient && showLoading()
}

const responseHook = (reslove: any, reject: any, res: any) => {
  const { config, data } = res
  const { errorCode, errorMsg } = data

  !config.slint && setTimeout(hideLoading, 100)

  if (errorCode !== '0') {
    if (config.publicError) {
      App.interface.toast(errorMsg)
    } else {
      reject({
        errorCode,
        errorMsg,
      })
    }
    return
  }

  reslove(data)
}

const request = (opts: AllType) => {
  opts.requestHook = requestHook
  opts.responseHook = responseHook
  return getAxios(opts, axiosInstance).catch(() => {
    App.interface.toast('网络请求失败')
  })
}

export default request