@cparra/apex-reflection

中文文档
4.0.0发布于 2026/07/05ISCTypeScriptES Module零依赖近 30 天下载 4万

Provides tools for reflecting Apex code, the language used in Salesforce development.

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Contributing
  4. Tests
  5. JSON serialization
  6. Parsing
  7. Typescript

Apex Reflection

Provides basic reflection for the Apex programming language.

Installation

npm i @cparra/apex-reflection

Usage

This library exposes a single function that handles parsing the body of an Apex top level type (class, interface, or enum) and returns the result.

import { reflect } from "@cparra/apex-reflection";

const classBody = "public with sharing class ExampleClass {}";
const response = reflect(classBody);

If you wish to parse an Apex type that comes from a file, you can read the file contents and use that as the source to reflect

import * as fs from 'fs';
import {reflect} from '@cparra/apex-reflection';

const path = './MyClass.cls';
const rawFile = fs.readFileSync(path);
const response = reflect(rawFile.toString());

The reflect function returns a ReflectionResult which contains either the results of the parsed Type (which will either be a ClassMirror, an InterfaceMirror, or an EnumMirror) or a ParsingError if the passed in body was not parsed successfully, with a message indicating where the error occurred.

Contributing

Even though this library is exposed as a Node.js library, the project's source code is written in Dart. The source can be found in the lib/src directory of the repository.

This package ships the Dart code compiled to a WebAssembly module (dist/node.wasm) together with its JS loader (dist/node.mjs), loaded lazily by the thin TypeScript wrapper in index.mts. Node.js 22 or newer is required (the module uses WasmGC). To rebuild the module after changing the Dart source, run from the repository root:

dart compile wasm lib/src/node/node.dart -o js/node.wasm

npm run build compiles the TypeScript wrapper and copies the wasm module and its loader into dist/.

Tests

Both the Dart source code and the packaged WebAssembly module must be tested.

The Dart tests live in the repository's test directory. The Dart source code must have unit tests testing each individual Dart file as well as end-to-end tests that verify the overall parsing functionality.

The JS tests live in __tests__. These are end-to-end tests that run against the built package (dist/), so they also guard against publishing a stale WebAssembly artifact. Run them with npm test.

JSON serialization

The reflection operation outputs a JSON representation of the Apex type, which is then deserialized on the JS side to return typed objects.

Serialization is handled through the json_serializable package, which helps automatically create the round-trip code for serialization and de-serialization.

When changing any of the model classes with serialization support, to re-build the serialization code run

dart run build_runner build

Parsing

Parsing is implemented with handwritten petitparser grammars. The Apex grammar parses only the declaration structure needed for reflection (bodies are skipped as balanced blocks), and the Apexdoc grammar is composed into it, so doc comments are parsed in the same single pass. See the repository README for details.

Typescript

This library provides its own TS type definition.

版本与发布记录

@cparra/apex-reflection 共发布 129 个版本,首次发布于 2021/09/28;过去 12 个月发布了 39 个版本。

版本发布日期距今
4.0.0latest2026/07/054 个月前
4.0.0-beta.6beta2026/07/054 个月前
4.0.0-beta.52026/07/054 个月前
4.0.0-beta.42026/07/044 个月前
4.0.0-beta.32026/07/044 个月前
4.0.0-beta.22026/07/044 个月前

其他发布通道:alpha → 2.17.0-alpha.0,dev → 3.0.0-dev.20260614084225,beta → 4.0.0-beta.6。

关于 @cparra/apex-reflection 的常见问题

@cparra/apex-reflection 是什么?

Provides tools for reflecting Apex code, the language used in Salesforce development.

如何安装 @cparra/apex-reflection?

在项目目录运行 npm install @cparra/apex-reflection,或使用 pnpm add / yarn add / bun add。

@cparra/apex-reflection 支持 TypeScript 吗?

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

@cparra/apex-reflection 支持 ESM 还是 CommonJS?

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

@cparra/apex-reflection 需要什么版本的 Node.js?

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

@cparra/apex-reflection 可以商用吗?

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

@cparra/apex-reflection 的下载量有多少?

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

@cparra/apex-reflection 还在维护吗?

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