What is @microsoft/api-extractor-model?
@microsoft/api-extractor-model is a library that provides tools for analyzing and processing API metadata. It is primarily used to work with API reports generated by API Extractor, enabling developers to programmatically inspect and manipulate API data.
What are @microsoft/api-extractor-model's main functionalities?
Loading API Reports
This feature allows you to load an API report JSON file and inspect its contents. The code sample demonstrates how to load an API report and print the package name.
const { ApiModel } = require('@microsoft/api-extractor-model');
const apiModel = new ApiModel();
const apiPackage = apiModel.loadPackage('path/to/api-report.json');
console.log(apiPackage.name);
Inspecting API Members
This feature allows you to inspect the members of an API package. The code sample demonstrates how to iterate over the members of an API package and print their display names.
const { ApiModel } = require('@microsoft/api-extractor-model');
const apiModel = new ApiModel();
const apiPackage = apiModel.loadPackage('path/to/api-report.json');
apiPackage.members.forEach(member => {
console.log(member.displayName);
});
Comparing API Reports
This feature allows you to compare two API reports to identify changes. The code sample demonstrates how to load two API reports, compare them, and print the changes.
const { ApiModel } = require('@microsoft/api-extractor-model');
const apiModel = new ApiModel();
const oldApiPackage = apiModel.loadPackage('path/to/old-api-report.json');
const newApiPackage = apiModel.loadPackage('path/to/new-api-report.json');
const changes = oldApiPackage.compareWith(newApiPackage);
changes.forEach(change => {
console.log(change.kind, change.name);
});
Other packages similar to @microsoft/api-extractor-model
typedoc
TypeDoc is a documentation generator for TypeScript projects. It generates API documentation directly from the source code, similar to how @microsoft/api-extractor-model works with API reports. However, TypeDoc focuses more on generating HTML documentation rather than providing tools for programmatic inspection and manipulation of API data.
api-documenter
API Documenter is another tool from Microsoft that works in conjunction with API Extractor. It generates documentation in various formats (Markdown, HTML) from API reports. While it shares some functionality with @microsoft/api-extractor-model, it is more focused on documentation generation rather than API data manipulation.
ts-morph
ts-morph is a TypeScript compiler API wrapper that simplifies working with TypeScript AST (Abstract Syntax Tree). It allows for programmatic inspection and manipulation of TypeScript code, similar to how @microsoft/api-extractor-model allows for inspection and manipulation of API reports. However, ts-morph operates at the source code level rather than the API metadata level.
Use this library to read and write *.api.json files as defined by the API Extractor tool.
These files are used to generate a documentation website for your TypeScript package. The files store the
API signatures and doc comments that were extracted from your package.
API documentation for this package: https://api.rushstack.io/pages/api-extractor-model/
Example Usage
The following code sample shows how to load example.api.json
, which would be generated by API Extractor
when it analyzes a hypothetical NPM package called example
:
import { ApiModel, ApiPackage } from '@microsoft/api-extractor-model';
const apiModel: ApiModel = new ApiModel();
const apiPackage: ApiPackage = apiModel.loadPackage('example.api.json');
for (const member of apiPackage.members) {
console.log(member.displayName);
}
The ApiModel
is acts as a container for various packages that are loaded and operated on as a group.
For example, a documentation tool may need to resolve @link
references across different packages.
In this case we would load the various packages into the ApiModel
, and then use
the ApiModel.resolveDeclarationReference()
to resolve the @link
targets.
The data structure forms a tree of various classes that start with the Api
prefix. The nesting hierarchy
might look like this:
- ApiModel
- ApiPackage
- ApiEntryPoint
- ApiClass
- ApiMethod
- ApiProperty
- ApiEnum
- ApiEnumMember
- ApiInterface
- ApiMethodSignature
- ApiPropertySignature
- ApiNamespace
- (ApiClass, ApiEnum, ApiInterface, ...)
You can use the ApiItem.members
property to traverse this tree.
Note that the non-abstract classes (e.g. ApiClass
, ApiEnum
, ApiInterface
, etc.) use
TypeScript "mixin" functions (e.g. ApiDeclaredItem
, ApiItemContainerMixin
, etc.) to add various
features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
the function that generates a subclass, an interface that describes the members of the subclass, and
a namespace containing static members of the class.
For a complete project that uses these APIs to generate an API reference web site,
see the @microsoft/api-documenter source code.
Links
API Extractor is part of the Rush Stack family of projects.