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

fshows-fetch

v2.0.1

Published

## 安装

Downloads

35

Readme

fshows-fetch 请求工具

安装

npm install fshows-fetch --save

使用场景

H5日志上报

  • axios 小程序日志上报
  • 微信小程序
    • wx.request
  • 支付宝小程序
    • my.request

签名参数 signParams

| 参数名称 | 类型 | 是否必填 | 描述 | 默认值 | | -------- | ------ | -------- | ------ | ------ | | appid | String | 选填 | 应用id | | | salt | String | 选填 | 盐 | | | version | String | 选填 | 版本号 | |

请求参数 reqOptions

| 参数名称 | 类型 | 是否必填 | 描述 | 默认值 | | ------------- | ------- | -------- | --------------- | ----------------- | | baseUrl | String | 必填 | 基本url | | baseUrlSuffix | String | 选填 | url 后缀,比如gateway默认空字符串 | | url | String | 必填 | 接口方法名 | | method | String | 选填 | 请求方式 | POST,可用值POST|GET | | timeout | Number | 选填 | 超时时间 | 不填默认使用全局的超时时间,10000 | | formatType | String | 选填 | 数据格式 | urlencode,可用值为json | formdata | urlencoded | | dataType | String | 选填 | dataType | json | | data | Object | 选填 | 请求数据 | | retryTimes | Number | 选填 | 请求次数 接口请求失败时自动重新请求 | 1 | | header | Object | 选填 | 请求头部 可在此设置请求头部请求格式、token | | sendRawData | Boolean | 选填 | 是否上传原始请求数据(为true时,常规请求接口,false,付呗方式请求接口) | false | | returnRawData | Boolean | 选填 | 不处理返回参数,否则返回res.data | false | | isMock | Boolean | 选填 | 是否mock | | switchGateway | Boolean | 选填 | 接口错误是否切换域名,设置了备用requerySite才会真正生效 true |

设置全局参数

setSignParams - 设置加盐密钥

import FsFetch from 'fshows-fetch'

// 方式一
const fsFetch = new FsFetch({
  appid:'xxxx',
  salt:'xxxx',
  version:'1.0.0'
})

// 方式二
const fsFetch = new FsFetch()
fsFetch.setSignParams({
  appid:'xxxx',
  salt:'xxxx',
  version:'1.0.0'
})

setTimeout - 设置超时时间

不设置,默认10000ms;改方法兼容旧版本 setAxiosTimeout 方法

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

fsFetch.setTimeout(12000)

setDefaultHeader - 设置默认Content-Type 不设置,默认urlencoded;支持入参json,formdata,urlencoded

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

fsFetch.setDefaultHeader ('urlencoded')

// json: { 'Content-Type': 'application/json;charset=UTF-8' },
// formdata: { 'Content-Type': 'multipart/formdata;charset=UTF-8' },
// urlencoded: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'

setMockUrl - 设置mock地址

全局设置了mock地址后,需要设置request方法的isMock: true才会生效

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

fsFetch.setMockUrl ('https://xxx')

// 需要在request传入isMock: true才会生效
// fsFetch.request({
//   ...
//   isMock: true
//   ...
// })

setInterceptor - 是否启用防重复请求

默认防重复点击开启,false 关闭

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

fsFetch.setInterceptor (false)

setRequerySite - 设置备用域名

支持单个域名或域名列表,[string | Array]

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

fsFetch.requerySite ('https://xxx')
// or
fsFetch.requerySite (['https://xxx1', 'https://xxx2'])

setSwitchGateway - 是否允许切换域名

  • 默认是允许切换域名的,如果设置了备用域名,当接口请求错误则自动切换为备用域名。
  • 如果该设置设置为false, 则禁用切换域名
import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

fsFetch.setSwitchGateway(false)

setDeleteKeyName - 校验参数需删除的参数名

默认删除signParams中的salt字段

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

fsFetch.setDeleteKeyName('xxx')

自定义处理方法

customHandleParams - 自定义请求参数处理方法

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

/**
 * 自定义请求参数处理方法
 * @param data reqOptions.data
 * @param signParams 加签
 * @param reqOptions 请求的参数
 */
fsFetch.customHandleParams = (data, signParams, reqOptions) => {

}

customJudgeSuccess - 用户自定义成功判断函数

默认(result.success || result.code === '00000')判断为接口成功

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

/**
 * 自定义请求参数处理方法
 * @param result httpResult
 */
fsFetch.customJudgeSuccess = (result) => {
  // return Boolean
}

请求回调方法

customErrorHandle - 错误回调

可在此上传报错日志

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

/**
 * 错误回调
 * @param error 返回的错误数据
 * @param reqData 处理过请求的数据
 * @param reqOptions 请求的参数
 * @param context { baseUrl?: 当前请求的baseUrl, useTimes: 当前请求错误次数 } 当前请求的一些状态
 */
fsFetch.customErrorHandle = (error, reqData, reqOptions, { baseUrl, useTimes }) => {

}

customSuccessHandle - 成功回调

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

/**
 * 成功回调
 * @param error 返回的错误数据
 * @param reqData 处理过请求的数据
 * @param reqOptions 请求的参数
 * @param context { baseUrl?: 当前请求的url, useTimes: 当前请求错误次数 } 当前请求的一些状态
 */
fsFetch.customErrorHandle = (result, reqData, reqOptions, { baseUrl, useTimes })=>{

}

customFinallyHandle - 请求结束回调

可以在这个里处理loading

import FsFetch from 'fshows-fetch'
const fsFetch = new FsFetch()

/**
 * complate回调
 * @param error 返回的错误数据
 * @param reqData 处理过请求的数据
 * @param reqOptions 请求的参数
 * @param context { url: 当前请求的url, useTimes: 当前请求错误次数 } 当前请求的一些状态
 */
fsFetch.customFinallyHandle = (result, reqData, reqOptions, { url, useTimes }) => {
	if (reqOptions.loading) {
    wx.hideLoading()
  }
}

// 需要在request传入loading
// fsFetch.request({
//   ...
//   loading: true
//   ...
// })