Gathering detailed insights and metrics for @nabh/ts-api-extractor
Gathering detailed insights and metrics for @nabh/ts-api-extractor
Gathering detailed insights and metrics for @nabh/ts-api-extractor
Gathering detailed insights and metrics for @nabh/ts-api-extractor
Generates API metadata from Typescript type definitions
npm install @nabh/ts-api-extractor
Typescript
Module System
Node Version
NPM Version
TypeScript (89.59%)
JavaScript (10.41%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
21 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 26, 2022
Latest Version
1.2.6
Package Id
@nabh/ts-api-extractor@1.2.6
Unpacked Size
288.14 kB
Size
42.72 kB
File Count
176
NPM Version
8.5.5
Node Version
16.14.2
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
ts-api-extractor
is a utility for extracting metadata about the types exported by a Typescript library and creating API documentation. The extracted metadata can be used for a variety of use cases such as automated CLI creation and aspect oriented programming.
ts-api-extractor
relies on @microsoft/api-documenter, @microsoft/api-extractor, and @microsoft/api-extractor-model for implementing most of its functionality. Two key reasons for implementing ts-api-extractor
package are:
ts-api-extractor
does the parsing for you to create a more fine-grained specification of the API.ts-api-extractor
combines the two steps in one file-less operation.You can consider using @microsoft/* packages directly if your use case does not require the additional benefits provided by ts-api-extractor
.
Install globally or locally
npm i [-g] @nabh/ts-api-extractor
ts-api-extractor
provides the command ts-api-extractor
to extract API metadata. If you install it globally, you can invoke the command as shown below.
> ts-api-extractor [--out <output-file-path>] [<pkg-name-or-dir]
Invoking the command without any arguments takes the current directory as the package root directory and prints the type information on the console. You can optionally provide the output file path and/or the package name/root directory.
ts-api-extractor
provides the command ts-documenter
that can generate Markdown documentation for a list of packages. You can use the command as shown below.
1> ts-documenter [--out <output-dir>] <pkg-name-or-dir1> <pkg-name-or-dir2> ...
You can also install ts-api-extractor
locally and use it in your Javascript code.
1const { APIExtractor } = require("@nabh/ts-api-extractor"); 2const apiDefs = APIExtractor.extract("test_package"); 3 4// Print API metadata object 5console.log(JSON.stringify(apiDefs, null, 2)); 6 7// Generate Markdown documentation in apidocs directory 8APIExtractor.document(["test_package"], "apidocs")
ts-api-extractor
reads the package.json
file for the input Typescript package as a starting point. You can either directly specify the package root folder or specify the package name. If you provide the package name, ts-api-extractor
attempts to find the package installation directory by recursively searching current and parent directories for node_modules/\<package\>
folder. Once it is able to locate the package.json
file, it uses the types
field as the source of Typescript type definitions.
The exported types are transformed into an hierarchy of metadata objects starting from an instance of class APIMetadata
. Please see https://pdabke.github.io/ts-api-extractor/ts-api-extractor.html for full specification of the metadata schema. As an example, see the serialized JSON object that represents types exported by a test package.
1// JSON produced by serializing instance of APIMetadata object 2{ 3 "package": { 4 "name": "test_package", 5 "version": "1.0.0", 6 "path": "/user/test/test_package" 7 }, 8 "metadata": { 9 "alphanumeric": { 10 "name": "alphanumeric", 11 "kind": "type-alias", 12 "comment": "A type that can be either a string or a number", 13 "types": [ 14 { 15 "type": "string" 16 }, 17 { 18 "type": "number" 19 } 20 ] 21 }, 22 "DocBaseClass": { 23 "name": "DocBaseClass", 24 "kind": "class", 25 "comment": "Example base class", 26 "constructors": [ 27 { 28 "name": "constructor", 29 "comment": "The simple constructor for <code>DocBaseClass</code>", 30 "params": [] 31 }, 32 { 33 "name": "constructor", 34 "comment": "The overloaded constructor for <code>DocBaseClass</code>", 35 "params": [ 36 { 37 "name": "x", 38 "type": "number" 39 } 40 ] 41 } 42 ], 43 "methods": [], 44 "properties": [], 45 "implements": [] 46 }, 47 "Fruits": { 48 "name": "Fruits", 49 "kind": "enum", 50 "members": [ 51 { 52 "key": "BANANA", 53 "comment": "Subject", 54 "value": "\"Banana\"" 55 }, 56 { 57 "key": "MANGO", 58 "comment": "King of fruits", 59 "value": "\"Mango\"" 60 } 61 ] 62 }, 63 "IDocInterface1": { 64 "name": "IDocInterface1", 65 "kind": "interface", 66 "comment": "", 67 "methods": [], 68 "properties": [ 69 { 70 "name": "regularProperty", 71 "type": "SystemEvent", 72 "package": "test_package", 73 "comment": "Does something" 74 } 75 ] 76 } 77 }
ts-api-extractor
recognizes four types available in Typescript:
enum, type (type alias), class, and interface. The following sections
provide examples of source/generated metadata for each of these types.
1export enum Fruits { 2 /** 3 * King of fruits 4 */ 5 MANGO = "Mango", 6 7 /** 8 * Subject 9 */ 10 BANANA = "Banana" 11}
1{ 2 "Fruits": { 3 "name": "Fruits", 4 "members": [ 5 { 6 "key": "BANANA", 7 "comment": "Subject", 8 "value": "\"Banana\"" 9 }, 10 { 11 "key": "MANGO", 12 "comment": "King of fruits", 13 "value": "\"Mango\"" 14 } 15 ], 16 "kind": "enum" 17 } 18}
1/** 2 * Type that maps to another type 3 */ 4export type targetType = TypeAliasSource 5 6/** 7 * Type alias with string literals 8 */ 9export type threeNumberStrings = "one" | "two" | "three"; 10
1{ 2 "targetType": { 3 "name": "targetType", 4 "kind": "type-alias", 5 "comment": "Type that maps to another type", 6 "types": [ 7 { 8 "type": "TypeAliasSource", 9 "package": "test_package" 10 } 11 ] 12 }, 13 "threeNumberStrings": { 14 "name": "threeNumberStrings", 15 "kind": "type-alias", 16 "comment": "Type alias with string literals", 17 "types": [ 18 "\"one\"", 19 "\"two\"", 20 "\"three\"" 21 ], 22 "isAllLiterals": true 23 } 24}
1export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInterface2 { 2 /** 3 * An internal class constructor. 4 * @internal 5 */ 6 public constructor(name: string) { 7 super(); 8 } 9 10 public get readonlyProperty(): string { 11 return 'hello'; 12 } 13 14 public get writeableProperty(): string { 15 return 'hello'; 16 } 17 public set writeableProperty(value: string) {} 18 19 /** 20 * This is a regular property that happens to use the SystemEvent type. 21 */ 22 public regularProperty: SystemEvent; 23 24 /** 25 * Returns the sum of two numbers. 26 * 27 * @remarks 28 * This illustrates usage of the `@example` block tag. 29 * 30 * @param x - the first number to add 31 * @param y - the second number to add 32 * @returns the sum of the two numbers 33 * 34 * @example 35 * Here's a simple example: 36 * ``` 37 * // Prints "2": 38 * console.log(DocClass1.sumWithExample(1,1)); 39 * ``` 40 * @example 41 * Here's an example with negative numbers: 42 * ``` 43 * // Prints "0": 44 * console.log(DocClass1.sumWithExample(1,-1)); 45 * ``` 46 */ 47 public static sumWithExample(x: number, y: number): number { 48 return x + y; 49 } 50}
1{ 2 "DocClass1": { 3 "name": "DocClass1", 4 "kind": "class", 5 "comment": "This is an example class.", 6 "constructors": [], 7 "methods": [ 8 { 9 "name": "sumWithExample", 10 "isStatic": true, 11 "returns": { 12 "type": "number", 13 "comment": " the sum of the two numbers" 14 }, 15 "comment": "Returns the sum of two numbers.", 16 "params": [ 17 { 18 "name": "x", 19 "type": "number", 20 "comment": "the first number to add" 21 }, 22 { 23 "name": "y", 24 "type": "number", 25 "comment": "the second number to add" 26 } 27 ] 28 } 29 ], 30 "properties": [ 31 { 32 "name": "readonlyProperty", 33 "type": "string", 34 "isReadOnly": true 35 }, 36 { 37 "name": "regularProperty", 38 "type": "SystemEvent", 39 "package": "test_package", 40 "comment": "This is a regular property that happens to use the SystemEvent type." 41 }, 42 { 43 "name": "writeableProperty", 44 "type": "string" 45 } 46 ], 47 "extends": { 48 "package": "test_package", 49 "type": "DocBaseClass" 50 }, 51 "implements": [ 52 { 53 "type": "IDocInterface1", 54 "package": "test_package" 55 }, 56 { 57 "type": "IDocInterface2", 58 "package": "test_package" 59 } 60 ] 61 } 62} 63
1/** 2 * @public 3 * {@docCategory DocBaseClass} 4 */ 5export interface IDocInterface1 { 6 /** 7 * Does something 8 */ 9 regularProperty: SystemEvent; 10} 11 12/** 13 * @public 14 * {@docCategory DocBaseClass} 15 * @deprecated 16 */ 17export interface IDocInterface2 extends IDocInterface1 { 18 /** 19 * @deprecated Use `otherThing()` instead. 20 */ 21 deprecatedExample(): void; 22} 23
1{ 2 "IDocInterface1": { 3 "name": "IDocInterface1", 4 "kind": "interface", 5 "comment": "", 6 "methods": [], 7 "properties": [ 8 { 9 "name": "regularProperty", 10 "type": "SystemEvent", 11 "package": "test_package", 12 "comment": "Does something" 13 } 14 ] 15 }, 16 "IDocInterface2": { 17 "name": "IDocInterface2", 18 "kind": "interface", 19 "isDeprecated": true, 20 "extends": [ 21 { 22 "type": "IDocInterface1", 23 "package": "test_package" 24 } 25 ], 26 "comment": "", 27 "methods": [ 28 { 29 "name": "deprecatedExample", 30 "isDeprecated": " Use <code>otherThing()</code> instead.", 31 "returns": { 32 "type": "void" 33 }, 34 "comment": "", 35 "params": [] 36 } 37 ], 38 "properties": [] 39 } 40}
No vulnerabilities found.
No security vulnerabilities found.