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

tf-request

v0.2.0

Published

request模块

Downloads

13

Readme

请求模块

// 引入
import request, { setCsrfUrl, setCsrf } from "tf-request";

// 使用
request('url地址', {
  body: String | Object, // 传给后端的参数
  dataType: 'jsonp',  // 后端返回的数据格式, 一般不传, 目前只支持传入jsonp
  type: '', // 前端传给后端的数据格式, 一般不传, 默认form类型, 只支持传入json
  method: 'get | post',  // 请求类型, 默认post

  hasCsrf: Boolean | Fucntion(url, options),  // 是否需要先发csrf请求, 默认false
  csrfUrl: String,  // 设置csrf请求地址
  csrf(data, csrfType) { // 可选, 返回请求需要拼接的csrf参数, 默认为:
    return { csrfType, csrfToken: data.data }
  }

  interceptRequest(data) { // 请求发送前做处理, 可选参数
    // ...对data做处理, 有下面2种返回数据方式: 

    // 方式1: 直接返回处理后的data
    return data;

    // 方式2: 返回一个Promise
    return new Promise(resolve => {
      setTimeout(() => { // 模拟异步请求
          console.log(data)
          // data.body  = data.body + '&c=ccc'  // 如果是post请求方式, 请拼接到body上
          data.url = data.url + '?x=xxxx' // 如果是get请求方式, 请拼接到url上
          resolve(data) // 返回数据
      }, 1000)
    })
  },

  interceptResponse(data) { //  拦截返回的请求做处理, 可选参数
    // 对data做处理
    return data // 一定要返回数据
  }

  // ps: 如果是jsonp, options还支持参数: timeout, jsonpCallbackFunction, jsonpCallback等参数
  //     文档地址: https://www.npmjs.com/package/fetch-jsonp
})
.then(data => {
  console.log(data) // 返回的数据
})
.catch(e) {
  console.log(e) // 错误信息
}

// cors请求
request('https://cnodejs.org/api/v1/topics', {
    method: 'get',
    mode: 'cors',
    // credentials: 'include',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: 'a=aa'
}).then(function(response) {
    console.log(response);
});


setCsrfUrl('url地址')
setCsrf(true)