Skip to content

Commit 68871d6

Browse files
committed
Readme
1 parent f92d930 commit 68871d6

File tree

3 files changed

+189
-4
lines changed

3 files changed

+189
-4
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ trim_trailing_whitespace = true
77

88
[*.md]
99
trim_trailing_whitespace = false
10+
indent_style = space
1011

1112
[output.txt]
1213
insert_final_newline = false

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Kristoffer K.
3+
Copyright (c) 2025 Michał Dudak.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 187 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,193 @@
1-
# Typescript API extractor
1+
# TypeScript API Extractor
22

3-
A utility for extracting API description from [TypeScript](https://www.npmjs.com/package/typescript) definitions using the TypeScript Compiler API.
3+
[![npm version](https://badge.fury.io/js/typescript-api-extractor.svg)](https://badge.fury.io/js/typescript-api-extractor)
4+
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
46

5-
This project was started as a fork of [typescript-to-proptypes](https://github.com/merceyz/typescript-to-proptypes) created by [Kristoffer K.](https://github.com/merceyz).
7+
A utility for extracting API descriptions from [TypeScript](https://www.npmjs.com/package/typescript) definitions using the TypeScript Compiler API. This tool analyzes TypeScript source code and generates structured metadata about exported functions, components, interfaces, types, and more.
8+
9+
## Features
10+
11+
- 🔍 **Extract API information** from TypeScript source files
12+
- ⚛️ **React component analysis** with prop types and documentation
13+
- 🏷️ **Type definitions** including interfaces, enums, and type aliases
14+
- 📝 **JSDoc comments** parsing and extraction
15+
- 🔗 **Reference resolution** for complex type relationships
16+
- 🎯 **Selective parsing** of specific files or entire projects
17+
18+
## Installation
19+
20+
```bash
21+
npm install typescript-api-extractor
22+
```
23+
24+
or with yarn:
25+
26+
```bash
27+
yarn add typescript-api-extractor
28+
```
29+
30+
or with pnpm:
31+
32+
```bash
33+
pnpm add typescript-api-extractor
34+
```
35+
36+
## Usage
37+
38+
```typescript
39+
import ts from 'typescript';
40+
import { loadConfig, parseFromProgram, type ModuleNode } from 'typescript-api-extractor';
41+
42+
// Load TypeScript configuration
43+
const config = loadConfig('./tsconfig.json');
44+
const program = ts.createProgram(config.fileNames, config.options);
45+
46+
// Parse all files in the project
47+
for (const file of config.fileNames) {
48+
try {
49+
const moduleInfo: ModuleNode = parseFromProgram(file, program);
50+
console.log(`Extracted API from ${file}:`, moduleInfo);
51+
} catch (error) {
52+
console.error(`Failed to parse ${file}:`, error);
53+
}
54+
}
55+
```
56+
57+
## API Reference
58+
59+
### `loadConfig(tsConfigPath: string)`
60+
61+
Loads and parses a TypeScript configuration file.
62+
63+
- **Parameters:**
64+
- `tsConfigPath`: Path to the `tsconfig.json` file
65+
- **Returns:** `{ options: ts.CompilerOptions, fileNames: string[] }`
66+
67+
### `parseFile(filePath: string, options: ts.CompilerOptions, parserOptions?: ParserOptions)`
68+
69+
Parses a single TypeScript file and returns the extracted API information.
70+
71+
- **Parameters:**
72+
- `filePath`: Path to the TypeScript file to parse
73+
- `options`: TypeScript compiler options
74+
- `parserOptions`: Optional parser configuration
75+
- **Returns:** `ModuleNode`
76+
77+
### `parseFromProgram(filePath: string, program: ts.Program, parserOptions?: ParserOptions)`
78+
79+
Parses a file from an existing TypeScript program for better performance when parsing multiple files.
80+
81+
- **Parameters:**
82+
- `filePath`: Path to the file to parse
83+
- `program`: TypeScript program instance
84+
- `parserOptions`: Optional parser configuration
85+
- **Returns:** `ModuleNode`
86+
87+
## Configuration Options
88+
89+
The parser accepts optional configuration through the `ParserOptions` interface:
90+
91+
```typescript
92+
interface ParserOptions {
93+
includePrivateMembers?: boolean;
94+
followReferences?: boolean;
95+
maxDepth?: number;
96+
}
97+
```
98+
99+
## Output Format
100+
101+
The parser returns a `ModuleNode` object with the following structure:
102+
103+
```typescript
104+
interface ModuleNode {
105+
name: string;
106+
exports: ExportNode[];
107+
}
108+
109+
interface ExportNode {
110+
name: string;
111+
type: TypeNode;
112+
documentation?: DocumentationNode;
113+
}
114+
```
115+
116+
`TypeNode` represents a TypeScript type. There are multiple classes of types. See the contents of the `src/models/types` directory to discover them.
117+
118+
### Example Output
119+
120+
For a React component like this:
121+
122+
```typescript
123+
interface Props {
124+
/** The title to display */
125+
title: string;
126+
/** Whether the component is disabled */
127+
disabled?: boolean;
128+
}
129+
130+
export function MyComponent(props: Props) {
131+
return <div>{props.title}</div>;
132+
}
133+
```
134+
135+
The extractor would produce:
136+
137+
```json
138+
{
139+
"name": "MyComponent",
140+
"exports": [
141+
{
142+
"name": "MyComponent",
143+
"type": {
144+
"kind": "component",
145+
"name": "MyComponent",
146+
"props": [
147+
{
148+
"name": "title",
149+
"type": {
150+
"kind": "intrinsic",
151+
"intrinsic": "string"
152+
},
153+
"optional": false,
154+
"documentation": {
155+
"description": "The title to display"
156+
}
157+
},
158+
{
159+
"name": "disabled",
160+
"type": {
161+
"kind": "intrinsic",
162+
"intrinsic": "boolean"
163+
},
164+
"optional": true,
165+
"documentation": {
166+
"description": "Whether the component is disabled"
167+
}
168+
}
169+
]
170+
}
171+
}
172+
]
173+
}
174+
```
175+
176+
## Requirements
177+
178+
- **Node.js**: >= 18.0.0
179+
- **TypeScript**: ^5.8 (peer dependency)
180+
181+
Make sure you have TypeScript installed in your project:
182+
183+
```bash
184+
npm install typescript
185+
```
6186

7187
## License
8188

9189
This project is licensed under the terms of the [MIT license](/LICENSE).
190+
191+
## Acknowledgments
192+
193+
This project was started as a fork of [typescript-to-proptypes](https://github.com/merceyz/typescript-to-proptypes) created by [Kristoffer K.](https://github.com/merceyz).

0 commit comments

Comments
 (0)