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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@polyv/request-plugin-authorize-token

v2.8.1

Published

用途:请求令牌插件,使用后会在所有请求中插入令牌信息。

Readme

AuthorizeTokenRequestPlugin

用途:请求令牌插件,使用后会在所有请求中插入令牌信息。

实例化参数

| 参数名 | 用途 | 类型 | 默认值 | | ----------------------- | ---------------------- | ---------- | ------ | | useToken | 默认使用 token 插件 | boolean | true | | responseTokenField | 在响应中返回的令牌字段 | string | - | | getTokenInfo | 获取授权令牌信息 | function | - | | responseTokenCallback | 响应中获取令牌后的回调 | function | - |

请求选项

| 参数名 | 用途 | 类型 | 默认值 | | ----------- | ------------------- | ----------- | ------ | | useToken | 是否使用 token 授权 | boolean | true | | tokenInfo | 令牌信息 | TokenInfo | - |

令牌信息类型

TokenInfo 类型如下:

| 参数名 | 用途 | 类型 | 默认值 | | --------------- | ----------------------------- | ----------- | ------------------ | | token | 授权令牌 | string | - | | tokenMode | 令牌方式,仅在 request 中生效 | TokenMode | TokenMode.Params | | tokenField | 在请求中插入的令牌字段 | string | 'token' | | includeParams | 插入的 params | object | - |

使用方式

import { PolyvRequest } from '@polyv/request-core';
import { AuthorizeTokenRequestPlugin, TokenMode } from '@polyv/request-plugin-authorize-token';

// 方式一:直接传入令牌信息
const requester = new PolyvRequest({
  requestPlugins: [
    new AuthorizeTokenRequestPlugin({
      token: 'xxxxx',
      tokenMode: TokenMode.Header,
      tokenField: 'xAuthToken',
    }),
  ],
});

// 方式二(推荐):支持传入 getTokenInfo 方法获取,返回 TokenInfo 类型,优先级高于直接传入
const requester = new PolyvRequest({
  requestPlugins: [
    new AuthorizeTokenRequestPlugin({
      getTokenInfo: async options => {
        return {
          token: 'xxxxx',
          tokenMode: TokenMode.Header,
          tokenField: 'xAuthToken',
          // 额外插入到 params
          includeParams: {
            channelId: 'xxx',
          },
        };
      },
    }),
  ],
});

// 方式三:调用时传入 tokenInfo
requester.get(
  '/xxx',
  {},
  {
    tokenInfo: {
      token: 'xxxxx',
      tokenMode: TokenMode.Params,
      tokenField: 'xAuthToken',
    },
  },
);

// 如果你想在某个请求中,不处理令牌操作时
requester.get(
  '/xxxx',
  {},
  {
    useToken: false,
  },
);

TokenMode 区别

  • TokenMode.Params:通过地址参数入参令牌
  • TokenMode.Header:通过请求头插入令牌

自动获取响应头中的授权令牌

实例化插件时入参 responseTokenField,插件内部会自动获取接口响应中的令牌,并在请求时如果不传入 tokenInfo,则使用响应中的令牌,示例如下:

const requester = new PolyvRequest({
  requestPlugins: [
    new AuthorizeTokenRequestPlugin({
      responseTokenField: 'x-auth-token',
      responseTokenCallback: data => {
        console.log('从接口响应中获取到的令牌', data.token);
      },
    }),
  ],
});

此时在每次接口请求后,会从响应头中获取 x-auth-token 作为令牌并记录,后续接口中,如果没有入参令牌则以该令牌传入。