Rush StackShopBlogEvents
メイン コンテンツにスキップ

サンプル プロジェクト

この記事は「API Extractor とは」ページからのチュートリアルの続きです。最初にそこをお読みください。

このチュートリアルでは、仮説上の 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';

これらの定義は、他の 3 つのソース ファイル(ここでは簡便なために一部を抜粋)から来ています

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 Extractor がこれらの入力を処理し、API レポート、d.ts ロールアップ、API ドキュメント出力を生み出す方法を見ていきましょう...