示例项目
本文继续介绍“什么是 API 提取器?”页面的教程。建议从那里开始。
在本教程中,我们将考虑一个假设的 TypeScript 库项目。声明来自一个真实世界的 NPM 包 **@microsoft/sp-core-library**。它的主要入口点是 **src/index.ts**,其中包含以下导出语句
src/index.ts
export { default as Log } from './log/Log';
export { default as ILogHandler } from './log/ILogHandler';
这些定义来自三个其他源文件(为简洁起见,这里摘录了部分内容)
src/log/Log.ts
import DefaultLogHandler from './DefaultLogHandler';
import ILogHandler from './ILogHandler';
/**
* The Log class provides static methods for logging messages at different levels (verbose,
* info, warning, error) and with context information. Context information helps identify
* which component generated the messages and makes the messages useful and filterable.
* @public
*/
export default class Log {
private static _logHandler: ILogHandler = new DefaultLogHandler();
/**
* Configures the logger with a different target.
* @beta
*/
public static initialize(logHandler: ILogHandler): void {
Log._logHandler = logHandler;
}
/**
* Logs a verbose message
* @param source - the source from where the message is logged, e.g., the class name.
* The source provides context information for the logged message.
* If the source's length is more than 20, only the first 20 characters are kept.
* @param message - the message to be logged
* If the message's length is more than 100, only the first 100 characters are kept.
*/
public static verbose(source: string, message: string): void {
this._logHandler.verbose(source, message);
}
. . .
public static info(source: string, message: string): void {
this._logHandler.info(source, message);
}
. . .
public static warn(source: string, message: string): void {
this._logHandler.warn(source, message);
}
. . .
public static error(source: string, error: Error): void {
this._logHandler.error(source, error);
}
}
src/log/ILogHandler.ts
/**
* The redirectable implementation for the Log class.
* @beta
*/
export interface ILogHandler {
verbose(source: string, message: string): void;
info(source: string, message: string): void;
warn(source: string, message: string): void;
error(source: string, error: Error): void;
}
export default ILogHandler;
src/log/DefaultLogHandler.ts
(在本示例中为私有,因此其实现并不重要,留给你的想象)
让我们考察一下 API 提取器如何处理这些输入以生成 API 报告、d.ts 汇总和 API 文档输出...