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

@totebox/http

v0.16.0

Published

An ajax request util based on fetch api.

Downloads

13

Readme

@totebox/http

安装

npm i @totebox/http

使用

Polyfill

如果要兼容旧版本的浏览器,比如 IE,你需要在项目入口处导入 fetch 的 polyfill:

import 'whatwg-fetch'

安装 @totebox/http 时,已经包含了 whatwg-fetch 的依赖,您无需单独安装。

在 ES 模块系统中

import http from '@totebox/http';

const request = http.create(/* settings */);
request.get(/* REQUEST_URL */);

在浏览器中

<script src="./node_modules/@totebox/http/dist/http.js"></script>
<script>
  $totebox.http(/* REQUEST_URL */);
</script>

API

http.create( settings )

通过该方法返回的请求对象,使用各请求方法时,可共享请求配置

settings

...fetch options

可传入 fetch api 的 options 参数

timeout

请求超时,默认:0

interceptors.response( data )

你可以在这里对 data 做二次加工,但你应该始终返回一个处理后的数据对象。也可以返回 Promise,当你返回 rejection 状态时,会继续执行 interceptors.error 方法。

interceptors: {
  response(data) {
    if (data.status !== 0) {
      return Promise.reject(new Error(`${data.message}, # interceptors.response`));
    }

    return data.data;
  },
  ...
},
interceptors.error( error, statusText, url )

对异常消息的加工方法

error: 包含 message 属性的错误对象

statusText: 错误状态字符串,可能的值为:"timeout""abort"

你应该在 interceptors.error 中始终返回一个新的 Error 对象,因为 error 参数的 message 可能是只读的,比如 DOMException

interceptors: {
  error(err, statusText, url) {
    const message = `Request url: "${url}" failed, message: "${err.message}"${statusText
      ? `, status: "${statusText}"` : ''}`;
    return new Error(message);
  },
},

http.create 的返回对象

  • request.get( url [, data [, options ] ] )
  • request.post( url [, data [, options ] ] )
  • request.put( url [, data [, options ] ] )
  • request.patch( url [, data [, options ] ] )
  • request.delete( url [, data [, options ] ] )

每个请求方法,可以额外单独配置 options,它包含 fetch api 的 options 参数和 timeout,参考 http.create 方法的 settings 参数说明。

请求方法返回的 Promise 对象,同时包含了一个 abort 方法,用来取消(中止)请求:

...

if (req) {
  req.abort();
}
req = request.get(/* REQUEST_URL */);
const data = await req;

http( url, options [, interceptors] )

对 fetch 的底层请求逻辑封装,返回包含 abort 方法(如上)的 Promise 对象。

url

请求地址

options

fetch api 的 options 参数和 timeout,参考 http.create 方法的 settings 参数说明

interceptors

可选,参数 http.create 方法的 settings 参数说明

License

MIT © nicolaszhao