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

tebon_request

v1.0.0

Published

通用request方法

Downloads

1

Readme

tebon_request

基于XMLHttpRequest实现get和post请求,支持了promise返回。默认导出es module版本,需要iife格式版本直接引用 dist/request.min.js

Demo示例
前往GitLab

安装

方式一:通过import方式引入

npm install tebon_request
import request from 'tebon_request';
request({
  url: ''
})
  .then(res => {
    console.log('响应的数据', res);
  });

方式二:内联html文件

npm install tebon_request
# 安装 raw-loader,要求版本号为 0.5.1 版本
npm install [email protected]

在 html 文件内使用 <script> 标签内联引入

<script>${require('raw-loader!tebon_request/dist/request.min.js')}</script>
<script>
  request({
    url: ''
  })
    .then(function (res) {
      console.log('响应的数据', res);
    });
</script>

基本用法

import request from 'tebon_request';
Object.assign(request.defaults, {
  headers: {
    'X-Custom': 'tebon-appid'
  }
});

// config 配置信息
request.before = function (config) {
  // 在发送请求之前可以修改参数配置,比如:请求Header
  console.log('before拦截,发送请求前执行:', config);
};
// res 请求返回的数据
request.after = function (res) {
  // 对响应数据进行统一的拦截操作
  console.log('after拦截,对返回的数据统一进行拦截操作', res);
};
// 请求错误拦截,只能全局配置一个拦截
request.error = function (res) {
  // 对响应数据做点什么
  console.log('拦截,只能全局配置一个拦截,数据抛出异常前执行:', res);
};

request({
  url: '../data.json'
})
  .then(function (res) {
    console.log(res)
  }, function (err) {
    console.log('请求失败', err)
  })

配置

| 配置项 | 类型 |默认值 | 是否必填 | 说明 | | :---: | :---: |:---: | :---: | :---: | | url | String | 无 | 是 | 请求地址 | | type | String | 'GET' | 否 | 请求方法GET或者POST 等XMLHttpRequest的type类型 | | data | Object | 无 | 否 | 传输的数据 | | withCredentials | Boolean | false | 否 | 跨域需携带cookie可设置为true | | timeout | number | 15000毫秒 | 否 | 超时时间 | | headers | headers | 无 | 否 | 设置headers |

全局默认配置

request.defaults 建议通过Object.assign配置

拦截方法

request.before

添加请求拦截器,使用请看示例

request.after

添加响应拦截器,使用请看示例

request.error

请求错误拦截,只能全局配置一个拦截,数据抛出异常前执行