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

spider_req

v1.4.0

Published

## 支持 - 请求地址统一包装 - 统一处理响应 - 统一处理错误 - 定制不同平台配置项 - 挂载至指定对象

Downloads

10

Readme

通用请求工具

支持

  • 请求地址统一包装
  • 统一处理响应
  • 统一处理错误
  • 定制不同平台配置项
  • 挂载至指定对象

语法

以包装 uni-app 请求api 为例;

方式1:作为对象导出,自行决定存放位置。

import initializer from 'spider_req';
// 初始化实例
const options = {
  host: 'https://my-server-host.com',
  layout: '/api/v1/',
  extraData: {token: 'ASDFGHJKL'},
  beforeSuccess: (res)=>{
      console.log('成功拦截器,成功之前统一调用')
  } 
};

const myRequest = initializer(uni.request,options);

// 使用
// 示例1:获取商品列表
myRequest({
  url:'/products',
  method: 'GET',
  data: {
    someData:'someData'
  },
  success: (res)=>{
    console.log('响应成功了')
  } 
});

// 根据提供的 options 上面的请求会执行如下操作:
// 1、请求地址包裹为:
// "https://my-server-host.com/api/v1/products"
// 2、请求数据包裹为:
// { someData:'someData', token:'ASDFGHJKL' }
// 3、请求的响应将会被拦截函数拦截,执行完毕后才会执行success函数。即控制台输出为:
// '成功拦截器,成功之前统一调用'
// '响应成功了'

方式2:挂在至指定对象。


 import initializer from 'spider_req';

 // 仅需要将初始化器中传入需要挂载的instance即可。 
 initializer(uni.request,options,uni); // 在第三个参数传入挂载点,这里以 uni 对象为例。

 // 使用:
 
 uni.req({
   url:'/products',
   method: 'GET',
   data: {
     someData:'someData'
   },
   success: (res)=>{
     console.log('响应成功了')
   } 
 });

初始化器中可选的options

  • extraData : 要追加到请求参数中的固定参数。
  • beforeSuccess : 成功前钩子函数。
  • beforeFail : 失败前钩子函数。
  • beforeSend : 发送前钩子函数。
  • sendPreHandler : 发送前预处理。将会发送该函数 return 出来的值。
  • receivePreHandler : 接受时预处理。工作原理同上。
  • appendDataFrom : 所有请求追加参数的函数,每次发送都会调用该函数,该函数返回值将会被携带在请求中。适用于动态固定参数。
  • resetSchema : 重置语法。
  • handlerDelay : 所有处理函数延迟执行。单位毫秒。
  • indices : 默认false。即get请求参数被序列化时是否带数组索引。

可选的重置语法以及默认值

{
  "url": "url",
  "data": "data",
  "header": "header",
  "method": "method",
  "dataType": "dataType",
  "responseType": "responseType",
  "sslVerify": "sslVerify",
  "success": "success",
  "fail": "fail",
  "complete": "complete"
}

每次请求可以接受的额外参数

  • skipBeforeFail : 该请求跳过全局的beforeFail。
  • skipBeforeSend : 原理同上。
  • realSuccess : 逻辑上的请求成功函数。即相应status < 400 时调用。常规的success是网络通了就视为成功。
  • statusFail : 逻辑上的请求失败函数。即相应的status >= 400 时调用。

重新初始化

重新初始化会自动销毁原对象(仅适用于以挂载点的方式初始化),然后用新的配置进行初始化。