# @datocms/cma-client-analysis 中文文档

规范地址：https://npmcn.com/package/%40datocms/cma-client-analysis/

版本：6\.1\.0
npm 元数据更新时间：2026-08-31T08:06:42.367Z
中文译文更新时间：2026-11-11T15:34:17.570Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40datocms/cma-client-analysis
原文对照：https://npmcn.com/package/%40datocms/cma-client-analysis/?lang=original
项目主页：https://github.com/datocms/js-rest-api-clients/tree/main/packages/cma-client-analysis#readme
源码仓库：https://github.com/datocms/js-rest-api-clients

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

- 包体积（解压后）：102 kB，35 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@datocms/cma-client-node ^6\.1\.0，@datocms/rest-api-reference ^6\.1\.0，typescript \>=5\.0\.0
- 首次发布：2026-04-27T12:58:51.819Z
- 最新版本发布：2026-08-31T08:06:42.141Z
- 近 30 天下载量：86364（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：29（近 12 个月 29 个）

## 中文简介

用于@datocms/cma-客户端-节点客户端类的基于脚本的静态分析工具。 提取方法签名、引用类型和端点映射.

## 安装

```sh
npm install @datocms/cma-client-analysis
```

也可以使用：`pnpm add @datocms/cma-client-analysis`、`yarn add @datocms/cma-client-analysis`、`bun add @datocms/cma-client-analysis`

## README

[![Node.js CI](https://github.com/datocms/js-rest-api-clients/actions/workflows/node.js.yml/badge.svg)](https://github.com/datocms/js-rest-api-clients/actions/workflows/node.js.yml)

# DatoCMS CMA 客户端分析

静态分析设施 `@datocms/cma-client-node`,以 TypeScript 编译器 API 为主. 检查 `Client` 类可以提取方法签名,解析引用类型,并生成即时到发TypeScript片段——用于生成 API 参考道克,AI 工具,或 CMA 客户端表面的任何内向.

## 安装

```bash
npm install @datocms/cma-client-analysis @datocms/cma-client-node @datocms/rest-api-reference typescript
```

`@datocms/cma-client-node`, (中文(简体) ), `@datocms/rest-api-reference`,以及 `typescript` 属于对等的依赖性 —— 包的内观, 无论哪个版本安装在您的树上, 所以输出总是与您实际运行的客户端同步 .

## 使用量

```typescript
import {
  getCmaClientProgram,
  extractAllMethodNames,
  extractMethodSignature,
  extractResourcesEndpointMethods,
  extractTypeDependencies,
} from '@datocms/cma-client-analysis';

// Build a TypeScript program over @datocms/cma-client-node's .d.ts entry.
// Expensive (~hundreds of ms): build once, thread through every extract* call.
const { program, checker, clientClass } = getCmaClientProgram();

// All method names available on a resource.
extractAllMethodNames(checker, clientClass, 'items');
// → ['list', 'find', 'create', 'rawList', 'rawFind', 'rawCreate', ...]

// Full signature for one method, including referenced type symbols.
const signature = extractMethodSignature(checker, clientClass, 'items', 'create');
// → { methodName, parameters, returnType, referencedTypeSymbols, ... }

// All client methods that implement a given REST endpoint, matched by docUrl.
import { findResourcesEntityByNamespace, findResourcesEndpointByRel, parseResourcesSchema } from '@datocms/rest-api-reference';
import resourcesJson from '@datocms/cma-client/resources.json';

const resourcesSchema = parseResourcesSchema(resourcesJson);
const entity = findResourcesEntityByNamespace(resourcesSchema, 'menuItems');
const endpoint = findResourcesEndpointByRel(entity!, 'create');

const methods = extractResourcesEndpointMethods(checker, clientClass, endpoint!);
// → [
//     { name: 'create',    functionDefinition: 'create(body: MenuItemCreateSchema): Promise<MenuItem>',                  referencedTypes: Set { ... } },
//     { name: 'rawCreate', functionDefinition: 'rawCreate(body: MenuItemCreateSchema): Promise<MenuItemCreateTargetSchema>', referencedTypes: Set { ... } },
//   ]

// Inline TypeScript declarations for a set of referenced types, walking up to a depth.
const { expandedTypes, notExpandedTypes } = extractTypeDependencies(
  checker,
  program,
  Array.from(signature!.referencedTypeSymbols.keys()),
  signature!.referencedTypeSymbols,
  { maxDepth: 2 },
);
```

## 缓存

`getCmaClientProgram()` 实值 _没有_ 被缓存——每个呼叫都会重建程序. 成本( 分析全部客户端) `.d.ts` 图)是非三角性的,因此:

-   **一发式进程** (CLI,脚本):启动时拨打一次,将结果接通.
-   **长期运行的进程** (服务器,MCP):在你自己的回忆中包好电话.

这使得终生决定权掌握在消费者手中,而不是将其钉入模块级状态.

## 常见问题

### @datocms/cma-client-analysis 是什么？

用于@datocms/cma-客户端-节点客户端类的基于脚本的静态分析工具。 提取方法签名、引用类型和端点映射\.

### 如何安装 @datocms/cma-client-analysis？

在项目目录运行 npm install @datocms/cma-client-analysis，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@datocms/cma-client-node（^6\.1\.0）、@datocms/rest-api-reference（^6\.1\.0）、typescript（\>=5\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @datocms/cma-client-analysis 支持 TypeScript 吗？

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

### @datocms/cma-client-analysis 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @datocms/cma-client-analysis 可以商用吗？

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

### @datocms/cma-client-analysis 的下载量有多少？

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

### @datocms/cma-client-analysis 还在维护吗？

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

