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

netop

v0.0.3

Published

Promise based HTTP client for the browser

Downloads

10

Readme

Netop

version downloads

Netop 是一个基于 promise 的 HTTP 库,目前可用在浏览器端。

安装

$ npm i netop applet --save
<script src="//unpkg.com/applet"></script>
<script src="//unpkg.com/netop"></script>

Features

  • 基于 fetch 实现
  • 支持 Promise API
  • 拦截请求和响应
  • 客户端支持防御 XSRF

netop([init])

是对 Netop 类的简单封装,方便调用。

简单使用

netop({url: '', method: ''}).send().then(function(response) {
  // response 是原生 Response 的实例
  // 你可以在这里验证 headers
  response.headers.has('X-Respond-Token');
  
  // 获取响应的 JSON 数据
  response.json().then(function(json) {});
  // 如果需要多次使用,可以 clone 这个 response
  response.clone().json().then(function(json) {});
  response.clone().text().then(function(text) {});
  response.clone().blob().then(function(blob) {});
}).catch(function(err) {
  // 错误处理,可能存在下面3个属性
  // err.config - 配置信息
  // err.request - 请求实例
  // err.response - 响应实例
})

精细控制流程

netop()// 返回 Netop 类的实例
  .use(function(config, next) {})// 使用配置拦截器
  .with('/path/to')// 拦截器类型分水岭
  .use(function(request, next) {})// 请求拦截器
  .send()// 发起请求(在其后面设置拦截器,会触发异常终止程序)
  .then(function(response) {})// 处理响应数据
  .catch(function(err) {});// 捕获异常(来自拦截器或请求触发的)

Netop

constructor([init])

  • init

    • url <string> | <Function> 可选,定义我们想要fetch的资源。可以是下面两者之一:

      1. 一个直接包含我们希望fetch的资源的URL的USVString;
      2. 一个 Request 对象.
    • body <any> 可选,任何你想加到请求中的body,可以是 Blob, BufferSource, FormData, URLSearchParams, 或 USVString对象

      需要注意的是 GETHEAD 请求没有 body(若指定了值,会在创建请求时触发异常)。

    • cache <string> 可选,请求中想要使用的缓存模式,具体说明查看 https://developer.mozilla.org/zh-CN/docs/Web/API/Request/cache, 有效值:defaultno-storereloadno-cacheforce-cacheonly-if-cached

    • redirect <string> 对重定向处理的模式。在Chrome中,Chrome 47 之前的版本默认值为 manual , 自Chrome 47起,默认值为 follow。有效值:followerrormanual

    • credentials <string> 可选,想要在请求中使用的credentials,浏览器默认值应该为 omit。 但在Chrome中,Chrome 47 之前的版本默认值为 same-origin,自Chrome 47起,默认值为 include。 自 Firefox 43后, 若URL有 credentials,Request() 会抛出TypeError, 例如 http://user:[email protected]。 根据规范,三个有效值: omitsame-origin**、**include`。 。

    • headers <any> 可选,任何你想加到请求中的头。

    • integrity <string> 可选,包括请求的 subresource integrity 值。 e.g. sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=

    • method <string> 可选,请求方法,Netop做了内部校验,七个有效值: GETDELETEPOSTPUTPATCHHEADOPTIONS

    • mode <string> 可选,请求的模式, 浏览器默认值应该为 cors。但在Chrome中,Chrome 47 之前的版本默认值为 no-cors , 自Chrome 47起,默认值为 same-origin。有效值:same-originno-corscorsnavigate

    • referrer <string> 可选,一个指定了 no-referrerclient 或一个 URL的 USVString。浏览器的默认值是 client.

    • auth <Object> 可选,Basic Authorization。

      • username <string> 可选,用户名。
      • password <string> 可选,密码。
    • xsrf <Object>

      • cookieName <string> 可选,保存在cookie中的值
      • headerName <string> 可选,将保存在cookie中的值映射到请求体的headers上面
    • validity <boolean> 可选,默认值 true,值为true时,校验 RequestInit 参数;只有通过校验,才会被用于请求。

    • validateStatus <Function> 可选,验证响应实例的状态码是否有效。

    • adapter <Function> 可选,发起请求的函数,默认值是 window.fetch

use(...fns): this

使用拦截器(也可以称作中间件),具体参考中间件框架 applet

  • fns <Function[]>

with(url): this

该方法用于区分 use 的拦截器类型,即 use 的拦截器用于处理 config 还是 request。

  • url <string> | <Function> 可选,请求路径。
netop({})
  // 使用配置拦截器
  .use(function(config, next) {
    // 修改配置信息
    config.mode = 'no-cors';
    config.integrity = 'sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=';
    return next();
  })
  .with('/user?id=1234')// 拦截器类型分水岭
  .use(function(request, next) {
    // request 是 Request 的实例
    if (request.headers.get('x-access-token') !== 'cached access token') {
      throw new Error('Token失效');
    }
    return next();
  })
  .send()// 发起请求)
  .then(response => response.json())
  .then(function(result = {}) {
    if (result.code !== 0) {
      throw new Error(result.message || 'error message');
    } 
  })
  .catch(function(err) {});// 捕获异常

send([body]): Promise

发送数据到服务器,调用该方法后,就不能够再使用拦截器了; 该方法返回一个 Promise 实例;

  • body <any> 可选,提交的数据

netop.all(...promises)

  • Array<Promise<any>> promises

netop.spread(callback)

  • function callback

License

MIT © 2018, Maofeng Zhang