# @authzed/authzed-node 中文文档

规范地址：https://npmcn.com/package/%40authzed/authzed-node/

版本：1\.6\.1
npm 元数据更新时间：2025-10-06T16:36:50.402Z
中文译文更新时间：2026-11-07T06:16:53.458Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40authzed/authzed-node
原文对照：https://npmcn.com/package/%40authzed/authzed-node/?lang=original
项目主页：https://github.com/authzed/authzed-node#readme
源码仓库：https://github.com/authzed/authzed-node

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：5.6 MB，164 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：4 个
- 首次发布：2021-07-30T23:03:56.575Z
- 最新版本发布：2025-10-06T16:36:50.248Z
- 近 30 天下载量：1888849（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：39（近 12 个月 0 个）

## 中文简介

节点验证客户端

## 安装

```sh
npm install @authzed/authzed-node
```

也可以使用：`pnpm add @authzed/authzed-node`、`yarn add @authzed/authzed-node`、`bun add @authzed/authzed-node`

## README

# Authzed 节点JS 客户端

[![npm version](https://img.shields.io/npm/v/@authzed/authzed-node.svg?style=flat)](https://www.npmjs.com/package/@authzed/authzed-node) [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) [![Build Status](https://github.com/authzed/authzed-node/workflows/authzed-node-ci/badge.svg)](https://github.com/authzed/authzed-node/actions) [![Mailing List](https://img.shields.io/badge/email-google%20groups-4285F4)](https://groups.google.com/g/authzed-oss) [![Discord Server](https://img.shields.io/discord/844600078504951838?color=7289da&logo=discord "Discord Server")](https://discord.gg/jTysUaxXzM) [![Twitter](https://img.shields.io/twitter/follow/authzed?color=%23179CF0&logo=twitter&style=flat-square)](https://twitter.com/authzed)

这个寄存器存放了Authzed的NodeJS客户端库.

[认证](https://authzed.com/) 是一个存储、计算和验证您的应用程序权限的数据库和服务.

开发者创建了一个模拟其权限要求的策略,并使用一个客户端库,比如这个策略,将策略应用于数据库,将数据插入数据库,并查询数据以高效地检查其应用程序中的权限.

支持客户端 API 版本 :

-   [单曲](https://docs.authzed.com/reference/api#authzedapiv1)

您可以在 API 上查找更多信息 [经认证的API参考文件](https://docs.authzed.com/reference/api)。 。 。 . 此外,Protobuf API文件可在以下网址上找到: [Buf 注册 Authzed API 存储器](https://buf.build/authzed/api/docs/main)。 。 。 .

见 [捐款额](https://npmcn.com/package/%40authzed/authzed-node/CONTRIBUTING.md) 说明如何贡献和完成共同任务,例如建设项目和进行测试.

## 开始

我们强烈建议: **[保护您的第一个应用程序](https://docs.authzed.com/guides/first-app)** 指南,以学习将应用程序与Authzed合并的最新最佳做法.

如果您对特定版本的API的示例感兴趣,请在数据库中的各自文件夹中找到 [示例目录](https://npmcn.com/examples)。 。 。 .

## 基本用途

### 安装

项目通过下列方式包装和分发: [国家预防机制](https://www.npmjs.com/package/@authzed/authzed-node)。 。 。 .

如果您正在使用典型的 npm 工具链,安装库的命令是:

```sh
npm i @authzed/authzed-node
```

### 初始化客户端

连接和制作API呼叫所需的一切信息都位于一个与API版本相适应的模块中.

您必须提供自己的API Token 从\[认证仪表板\]取而代之 `t_your_token_here_1234567deadbeef` 实例如下:

```js
import { v1 } from '@authzed/authzed-node';
// if your endpoint is localhost
// const client = v1.NewClient('t_your_token_here_1234567deadbeef', 'localhost:50051', v1.ClientSecurity.INSECURE_LOCALHOST_ALLOWED);
const client = v1.NewClient('t_your_token_here_1234567deadbeef');
```

或使用自定义证书权限, 加载 CA 证书并传递文件引用到 `NewClientWithCustomCert`。 。 。 .

```js
import { v1 } from '@authzed/authzed-node';
import fs from 'fs';

const endpoint = 'localhost:50051';
const cert = fs.readFileSync('path/to/cert.pem');
const client = v1.NewClientWithCustomCert('t_your_token_here_1234567deadbeef', endpoint, cert);
```

### 执行 API 呼叫

因为这些类型的动词,我们建议自己写出功能/方法,从现有的应用模型中创建出这些类型.

这个 `create` 生成类上的方法将属性作为输入并默认其空值的未指定属性。 这使得您可以创建请求信件,例如只指定相关的字段,并留下可选字段为空.

```js
import { v1 } from '@authzed/authzed-node';

const client = v1.NewClient('token')

// Create the relationship between the resource and the user.
const firstPost = v1.ObjectReference.create({
    objectType: "blog/post",
    objectId: "1",
});

// Create the user reference.
const emilia = v1.ObjectReference.create({
    objectType: "blog/user",
    objectId: "emilia",
});

// Create the subject reference using the user reference
const subject = v1.SubjectReference.create({
    object: emilia,
});

const checkPermissionRequest = v1.CheckPermissionRequest.create({
    resource: firstPost,
    permission: "read",
    subject,
});

client.checkPermission(checkPermissionRequest, (err, response) => {
    console.log(response);
    console.log(err);
});
```

### 承诺(同步/等待)支助

客户端的每一种可用方法都有相关的承诺式方法来代替回调,可以在 `.promises` 客户端上的财产.

```js
import { v1 } from '@authzed/authzed-node';

const client = v1.NewClient('token');
const { promises: promiseClient } = client; // access client.promises

const checkPermissionRequest = /** from above **/;

const result = await promiseClient.checkPermission(checkPermissionRequest);
```

用于溪流回流方法,包括 `client.readRelationships()`, (中文(简体) ), `client.lookupResources()` 财务报告和审定财务报表 `client.lookupSubjects()`,承诺式方法会在溪流关闭后产生一系列响应对象.

```js
import { v1 } from '@authzed/authzed-node';

const client = v1.NewClient('token');
const { promises: promiseClient } = client; // access client.promises

const results = await promiseClient.readRelationships(/** req **/);
console.log(results[0]); // first ReadRelationship result
```

## 写入洞穴

在写一个关系时,我们发现你需要使用 页:1 `Struct` 对象捆绑在一起 `authzed-node`,而不是从 页:1 `google-protobuf` 软件包。 如果你没有使用 `authzed-node` 版本中, 写上下文会默默地失败,关系也不会反映上下文.

一个例子是:

```js
import { protobuf } from '@authzed/authzed-node';
const { Struct } = protobuf;

const writeRequest = WriteRelationshipsRequest.create({
updates: [
  RelationshipUpdate.create({
    relationship: Relationship.create({
      resource: resource,
      relation: "caveated_viewer",
      subject: SubjectReference.create({
        object: testUser,
      }),
      optionalCaveat: ContextualizedCaveat.create({
        caveatName: "has_special_attribute",
        context: Struct.fromJson({
          special: true,
        }),
      }),
    }),
    operation: RelationshipUpdate_Operation.CREATE,
  }),
],
});

const response = await client.writeRelationships(writeRequest);
```

## 所需资源

支持的Node.js版本:18,20,22

最小类型脚本版本 3.8

## 常见问题

### @authzed/authzed-node 是什么？

节点验证客户端

### 如何安装 @authzed/authzed-node？

在项目目录运行 npm install @authzed/authzed-node，或使用 pnpm add / yarn add / bun add。

### @authzed/authzed-node 支持 TypeScript 吗？

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

### @authzed/authzed-node 支持 ESM 还是 CommonJS？

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

### @authzed/authzed-node 可以商用吗？

@authzed/authzed-node 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @authzed/authzed-node 的下载量有多少？

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

### @authzed/authzed-node 还在维护吗？

最新版本 1\.6\.1 发布于 2025/10/07（1\.1 年前）；至今共发布 39 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/07/31。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

