@btravstack/oauth-client

中文文档
0.58.0发布于 2026/11/08MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 6764

A TokenSource port for @btravstack/core: OAuth 2.0 client credentials and RFC 8693 token exchange, so a service calls another as itself or for a subject — cached per audience until shortly before expiry, one fetch shared by concurrent callers, and a stati

原始使用文档
文档目录4 节
  1. A worked example
  2. Options
  3. What it decides, and what it does not
  4. License

@btravstack/oauth-client

A TokenSource port for @btravstack/core: a bearer token so a service calls another as itself or for a subject, over client credentials or RFC 8693 token exchange — kept per audience until shortly before it expires, one fetch shared by concurrent callers, and a static adapter for tests.

📖 Documentation · API Reference

pnpm add @btravstack/oauth-client @btravstack/core @btravstack/config \
  @btravstack/di openid-client unthrown

Every dependency is a peer. Node >=22.12: openid-client is ESM-only, so the CommonJS build relies on require(esm).

A worked example

A port that needs a token for the inventory service, and the composition that provides one:

import {
  TokenSource,
  oauthClient,
  type TokenIssuerUnavailable,
  type TokenRefused,
} from "@btravstack/oauth-client";

class InventoryAuth extends Port("ReadmeInventoryAuth")<{
  readonly header: () => AsyncResult<string, TokenRefused | TokenIssuerUnavailable>;
}> {}

export const inventoryAuth = Provider(InventoryAuth)({
  inject: { tokens: TokenSource },
  sync: ({ tokens }) => ({
    header: () =>
      tokens
        .token({ audience: "https://inventory.internal", scope: "inventory:read" })
        .map(({ bearer }) => `Bearer ${bearer}`),
  }),
});

export const Gateway = Module("ReadmeGateway")({
  imports: [oauthClient()],
  provides: [inventoryAuth],
  exports: [InventoryAuth],
});

Putting the token on an oRPC client's Authorization header, without a Promise leaving the Result, is Call another service as itself.

Options

OptionWhereWhat it is
OAUTH_ISSUERenvironment, pinned by issuerthe authorization server — required
OAUTH_CLIENT_IDenvironment, pinned by clientIdthis service's client — required
OAUTH_CLIENT_SECRETenvironment, pinned by clientSecretits secret, from a Kubernetes Secret — required
allowInsecureIssueroauthClient({ … })accept an http: issuer that is not loopback (default false)
portoauthClient({ … })distinct source port for a separate client identity
variablePrefixoauthClient({ … })issuer and client variable prefix (default OAUTH)
clockoauthClient({ … })what a token's lifetime is measured against

The full table — defaults, semantics and the reasoning — lives on the reference page, which is this list's one detailed home.

What it decides, and what it does not

It decides that discovery is lazy, so an issuer down at deploy fails the calls that need a token rather than the boot; that a token is reused until a minute before it expires, with fixed FIFO capacity so subject turnover stays bounded; that a refusal (TokenRefused, with the issuer's error code) and an outage (TokenIssuerUnavailable) are two typed errors; that every fetch is reported to Observers; and that an oauth-client health check follows the newest-started fetch per audience and scope, even when responses finish out of order. Each request is snapshotted at entry so caller mutation cannot change its grant or poison another credential's cache.

It does not decide what a failure means for your call — retrying belongs to your transport. On behalf of a subject, token({ audience, onBehalfOf: { token, tokenType, actor? } }) uses RFC 8693 exchange. The issuer must authorize delegation and mint the act claim; an actor credential is supplied when it requires one. The reasoning is in AGENTS.md.

License

MIT

版本与发布记录

@btravstack/oauth-client 共发布 38 个版本,首次发布于 2026/10/12;过去 12 个月发布了 38 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.58.0latest2026/11/08今天
0.57.02026/11/071 天前
0.56.12026/11/062 天前
0.56.02026/11/062 天前
0.55.12026/11/035 天前
0.55.02026/11/035 天前

关于 @btravstack/oauth-client 的常见问题

@btravstack/oauth-client 是什么?

A TokenSource port for @btravstack/core: OAuth 2.0 client credentials and RFC 8693 token exchange, so a service calls another as itself or for a subject — cached per audience until shortly before expiry, one fetch shared by concurrent callers, and a stati

如何安装 @btravstack/oauth-client?

在项目目录运行 npm install @btravstack/oauth-client,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:unthrown(^5.0.0)、openid-client(^6)、@btravstack/di(^0.58.0)、@btravstack/core(^0.58.0)、@btravstack/config(^0.58.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@btravstack/oauth-client 支持 TypeScript 吗?

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

@btravstack/oauth-client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@btravstack/oauth-client 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@btravstack/oauth-client 可以商用吗?

@btravstack/oauth-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@btravstack/oauth-client 的下载量有多少?

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

@btravstack/oauth-client 还在维护吗?

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