@conduit-client/jwt-manager

中文文档
3.34.0发布于 2026/11/05SEE LICENSE IN LICENSE.txtTypeScriptES Module近 30 天下载 4.3万

Luvio Next generic JWT manager

原始使用文档
文档目录9 节
  1. Installation
  2. Features
  3. Usage
  4. API Reference
  5. JwtManager
  6. JWT Repository
  7. JwtResolver
  8. Contributing
  9. License

JWT Manager

JWT Manager is a package that simplifies the handling of JWT (JSON Web Tokens) in your application. It abstracts the process of retrieving, storing, and refreshing tokens with a clean and straightforward API.

Installation

You can add JWT Manager to your project using npm:

npm install -client/jwt-manager

Features

  • Get and refresh JWTs easily with getJwt and refreshToken methods.
  • Handle token expiration with automatic refresh.
  • Utilizes JwtRepository for token storage and management.
  • Works with a JwtResolver to retrieve tokens.
  • Allows additional extra info along with the token.

Usage

Here is a basic example of using JWT Manager in a Node.js application:

const { JwtManager, JwtRepository, JwtResolver } = require('@conduit-client/jwt-manager');

type EncodedJwtClaims = {
  exp: number;
  username: string;
}
type ExtraInfo = {
  envBaseUri: string;
}
// Your JwtResolver implementation
const jwtResolver: JwtResolver<ExtraInfo> = {
  getJwt(): Promise<{ jwt: string; extraInfo: ExtraInfo }> {
    return fetch(); // resolves the jwt.
  }
};

// Your JwtRepository implementation
const jwtRepository = new JwtRepository<EncodedJwtClaims, ExtraInfo>(
  3, // notifies that the token will expire in 3 seconds
  120, // if exp claim is not provided, the token will expire in 120 seconds.
);

// Create JwtManager instance
const jwtManager = new JwtManager(jwtRepository, jwtResolver);

// Get a JWT
jwtManager.getJwt().then((jwt) => {
  console.log(jwt.token);  // Prints the JWT
  console.log(jwt.decodedInfo);  // Prints the JWT decoded information
  console.log(jwt.extraInfo);  // Prints the JWT extra information
});

Remember that you will need to provide your own JwtResolver implementation of the JwtResolver interface. The JwtResolver should provide a getJwt method that retrieves a new JWT (and optionally extra info) when needed.

API Reference

The package exports two main elements: JwtManager class, JwtRepository class and JwtResolver and JwtToken types.

JwtManager

The JwtManager class is the main class in the JWT Manager package.

It exposes the following methods:

  • getJwt(): Returns a JWT. If a token request is in progress, it returns the Promise of this request. If the current token is undefined or expired, it initiates a token refresh. Otherwise, it returns the current token.
  • refreshToken(): Refreshes a JWT. If a refresh request is already in progress, it returns the Promise of this request. Otherwise, it starts a new refresh request and returns its Promise.

JWT Repository

The JwtRepository class is a storage and management solution for JWT (JSON Web Tokens) within the JWT Manager package.

The class handles:

  • Setting and getting the current JWT.
  • Notifying observers when the JWT is nearing its expiration.
  • Removing the JWT.

Usage

const { JwtRepository } = require('jwt-manager');

// Create JwtRepository instance with optional parameters
const jwtRepository = new JwtRepository(limitInSeconds, defaultTokenTTLInSeconds, logger);

// Set a JWT with optional extra information
jwtRepository.setToken('myJWT', { extra: 'info' });

// Get the current JWT
const currentToken = jwtRepository.token;

// Subscribe to the token nearing its expiration
const unsubscribe = jwtRepository.subscribeToTokenNearExpiration((token) => {
  console.log(`Token is about to expire: ${token}`);
});

// To unsubscribe
unsubscribe();

// Remove the current JWT
jwtRepository.removeToken();

API

JwtRepository exposes the following methods:

  • constructor(limitInSeconds: number, defaultTokenTTLInSeconds: number, logger: Logger): The constructor takes optional parameters to customize its behavior. The limitInSeconds sets the time before the token's expiry to notify observers. The defaultTokenTTLInSeconds sets the default token expiry time in seconds if "exp" claim is not present in the token. logger is used for logging warnings and errors.

  • token: Returns the current JWT.

  • setToken(token: string, extraInfo?: ExtraInfo): Sets the current JWT with optional extra information. Returns an object of the set token.

  • removeToken(): Removes the current JWT.

  • subscribeToTokenNearExpiration(cb: (token: JwtToken<T, ExtraInfo>) => void): Subscribes to the token nearing its expiration. It returns a function that can be used to unsubscribe.

JwtResolver

The JwtResolver type is used to define the structure for JWT resolver instances. It contains a getJwt method that should return a Promise with a JWT and optionally extra information.

Contributing

We welcome contributions! Please see our contributing guide for more details.

License

see the LICENSE.txt file for details.

版本与发布记录

@conduit-client/jwt-manager 共发布 74 个版本,首次发布于 2025/11/01;过去 12 个月发布了 71 个版本。

版本发布日期距今
3.34.0latest2026/11/055 天前
3.33.02026/10/2912 天前
3.32.12026/10/2219 天前
3.32.02026/10/1526 天前
3.31.02026/10/091 个月前
3.30.12026/10/091 个月前

其他发布通道:patch → 3.26.1-dev3。

关于 @conduit-client/jwt-manager 的常见问题

@conduit-client/jwt-manager 是什么?

Luvio Next generic JWT manager

如何安装 @conduit-client/jwt-manager?

在项目目录运行 npm install @conduit-client/jwt-manager,或使用 pnpm add / yarn add / bun add。

@conduit-client/jwt-manager 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@conduit-client/jwt-manager 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@conduit-client/jwt-manager 可以商用吗?

@conduit-client/jwt-manager 使用 SEE LICENSE IN LICENSE.txt 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@conduit-client/jwt-manager 的下载量有多少?

根据 npm 官方下载统计,@conduit-client/jwt-manager 在 2026/10/11 – 2026/11/09 这 30 天内被下载 43,471 次,日均约 1,449 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@conduit-client/jwt-manager 还在维护吗?

最新版本 3.34.0 发布于 2026/11/05(5 天前);至今共发布 74 个版本,其中过去 12 个月发布了 71 个;首次发布于 2025/11/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。