Skip to content

Commit 38b5681

Browse files
docs(openapi-generator): Refactor documentation to Diátaxis Reference format
This commit significantly refactors the documentation for the `@api-ts/openapi-generator` package to align with the Reference section principles of the Diátaxis framework. The goal is to provide users with a clear, structured, and authoritative technical description suitable for direct consultation during development. **Key Changes:** * Restructured the entire README.md into a dedicated Reference section, replacing the previous narrative format. * Organized content into logical subsections for improved navigability and focus: * **Command-Line Interface (CLI):** Provides a technical overview, usage syntax, and detailed descriptions of the `<file>` argument, options (`--name`, `--version`, `--codec-file`), and flags (`--internal`, `--help`), along with concrete usage examples. * **Configuration:** Clearly documents the requirements for preparing external types packages (specifying `files` and `source` in the external package's `package.json`). Details the two distinct methods for defining custom codec schemas: 1. Via `openapi-gen.config.js` within the types package (recommended for type authors), including file naming, `package.json` requirements (`customCodecFile`, `files`), and file structure. 2. Via the `--codec-file` CLI option for consumers, specifying the required file structure (grouping by package name). * **Supported `io-ts` Primitives:** Includes a definitive list of `io-ts` primitives and combinators that the generator can automatically interpret to produce OpenAPI schemas. * **JSDoc Annotations:** Exhaustively documents the supported JSDoc tags used to enrich the generated OpenAPI specification, categorized by: * *Endpoint Annotations* (applied to `httpRoute` variables): Covers Summary, Description, `@operationId`, `@tag`, `@private`, `@unstable`, `@example`, and the handling of unknown tags (`x-unknown-tags`), explaining their direct mapping to OpenAPI Operation Object fields (like `summary`, `description`, `operationId`, `tags`, `x-internal`, `x-unstable`, `example`, `x-unknown-tags`). * *Schema Annotations* (applied to `io-ts` types/fields): Details how to add descriptions via JSDoc text and lists all supported standard OpenAPI tags (`@default`, `@example`, `@minLength`, `@maximum`, `@pattern`, `@format`, etc.) and custom tags (`@private`, `@deprecated`), explaining their effect on the resulting OpenAPI Schema Object or Property (like `description`, `default`, `minLength`, `deprecated`, `x-internal`). Includes comprehensive examples demonstrating tag usage. * Removed introductory explanations and narrative prose, focusing strictly on factual, technical descriptions of the tool's features, configuration requirements, and behavior based on inputs (like JSDoc tags). **Impact:** This restructuring provides developers using `@api-ts/openapi-generator` with significantly improved technical reference documentation. The information is now presented in a consistent, predictable, and easily searchable format, adhering to the Diátaxis framework's guidelines for effective reference material. This makes it easier for users to find the specific technical details they need regarding CLI usage, configuration, supported types, and JSDoc annotation capabilities while working with the generator. chore: apply feedback
1 parent 58b65fb commit 38b5681

File tree

6 files changed

+512
-0
lines changed

6 files changed

+512
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Command-line interface
6+
7+
## Overview
8+
9+
The `openapi-generator` CLI tool converts your `@api-ts/io-ts-http` `apiSpec` definition
10+
into an OpenAPI 3.0 specification. When you run this tool, it reads a TypeScript file
11+
containing your API definition and outputs the OpenAPI specification to stdout.
12+
13+
## Usage syntax
14+
15+
```shell
16+
openapi-generator [OPTIONS] [FLAGS] <file>
17+
```
18+
19+
## Arguments
20+
21+
- `<file>`: (Required) Path to the TypeScript file containing the exported `apiSpec`
22+
definition.
23+
24+
## Options
25+
26+
- `--name`, `-n <string>`: Specifies the API name in the generated specification.
27+
- `--version`, `-v <string>`: Specifies the API version in the generated OpenAPI
28+
specification. If an `@version` JSDoc tag is present on the `apiSpec` export, that
29+
value takes precedence.
30+
- `--codec-file`, `-c <string>`: Path to a JavaScript configuration file defining
31+
schemas for custom or external io-ts codecs. See
32+
[Defining custom codec schemas](./configuration#defining-custom-codec-schemas) for
33+
details.
34+
35+
## Flags
36+
37+
- `--internal`, `-i`: Includes routes marked with the `@private` JSDoc tag in the
38+
generated output. By default, private routes are excluded.
39+
- `--help`, `-h`: Displays the help message describing arguments, options, and flags.
40+
41+
## Examples
42+
43+
You can generate an OpenAPI specification and save it to a file:
44+
45+
```shell
46+
npx openapi-generator src/index.ts > openapi-spec.json
47+
```
48+
49+
You can specify API name, version, and custom codec definitions:
50+
51+
```shell
52+
npx openapi-generator --name "My Service API" --version "2.1.0" --codec-file ./custom-codecs.js src/api.ts
53+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# Configuration
6+
7+
## Overview
8+
9+
You need to configure the generator to:
10+
11+
- Correctly interpret `io-ts` types from external packages.
12+
- Define OpenAPI schemas for custom `io-ts` codecs that can't be automatically derived
13+
from the Abstract Syntax Tree (AST).
14+
15+
## Preparing external types packages
16+
17+
To process `io-ts` types imported from other npm packages, ensure the following in the
18+
external package's `package.json`:
19+
20+
1. Include source code in the published npm bundle by adding the source directory to the
21+
`files` array:
22+
23+
```json
24+
// package.json of the external types package
25+
{
26+
"name": "my-external-types",
27+
"version": "1.0.0",
28+
"files": [
29+
"dist/",
30+
"src/" // Include source code
31+
],
32+
"main": "dist/index.js",
33+
"types": "dist/index.d.ts",
34+
"source": "src/index.ts" // Add this field
35+
// ... rest of package.json
36+
}
37+
```
38+
39+
2. Specify the source entry point using the `source` field (for example,
40+
`"source": "src/index.ts"`)
41+
42+
## Defining custom codec schemas
43+
44+
For custom `io-ts` codecs (such as those using `new t.Type(...)` or complex types not
45+
directly supported), you must define schemas manually using one of these methods:
46+
47+
### Method 1: Via openapi-gen.config.js (recommended for type authors)
48+
49+
You can define schemas directly within the package that declares the custom codecs:
50+
51+
1. Create a file named `openapi-gen.config.js` in the root of the types package
52+
53+
2. Update the package's `package.json` to include:
54+
55+
- The `customCodecFile` field pointing to this file
56+
- The config file in the `files` array
57+
58+
```json
59+
// package.json of the types package defining custom codecs
60+
{
61+
"name": "my-custom-codec-package",
62+
// ...
63+
"files": [
64+
"dist/",
65+
"src/",
66+
"openapi-gen.config.js" // Include the config file
67+
],
68+
"customCodecFile": "openapi-gen.config.js" // Point to the file
69+
// ...
70+
}
71+
```
72+
73+
3. Structure the `openapi-gen.config.js` file as follows:
74+
75+
```javascript
76+
// openapi-gen.config.js
77+
module.exports = (E) => {
78+
return {
79+
// Key matches the exported codec name (e.g., export const MyCustomString = ...)
80+
MyCustomString: () =>
81+
E.right({
82+
type: 'string',
83+
format: 'custom-format',
84+
description: 'A custom string type definition',
85+
}),
86+
AnotherCustomType: () =>
87+
E.right({
88+
type: 'object',
89+
properties: {
90+
/* ... */
91+
},
92+
}),
93+
// ... other custom codec definitions
94+
};
95+
};
96+
```
97+
98+
The exported function receives the `fp-ts/Either` namespace (`E`) as an argument. You
99+
should return an object where:
100+
101+
- Keys are the exported names of your custom codecs
102+
- Values are functions that return `E.right()` with an OpenAPI schema object
103+
104+
### Method 2: Via --codec-file option (for consumers)
105+
106+
You can define schemas in a configuration file within your project and pass the file
107+
path via the `--codec-file` option:
108+
109+
1. Create a JavaScript file (for example, `custom-codecs.js`)
110+
111+
2. Structure the file similarly to Method 1, but group definitions by package:
112+
113+
```javascript
114+
// custom-codecs.js
115+
module.exports = (E) => {
116+
return {
117+
'io-ts-bigint': {
118+
// Package name
119+
BigIntFromString: () => E.right({ type: 'string', format: 'bigint' }),
120+
NonZeroBigIntFromString: () =>
121+
E.right({ type: 'string', format: 'bigint' /* constraints */ }),
122+
// ... other codecs from 'io-ts-bigint'
123+
},
124+
'my-other-custom-package': {
125+
// Another package
126+
SomeType: () => E.right({ type: 'number', format: 'float' }),
127+
},
128+
// ... other packages
129+
};
130+
};
131+
```
132+
133+
In this structure:
134+
135+
- Keys of the top-level object are package names
136+
- Values are objects that map codec names to their schema definitions
137+
138+
3. Run the generator with the `--codec-file` option:
139+
140+
```shell
141+
npx openapi-generator --codec-file ./custom-codecs.js src/index.ts
142+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# OpenAPI generator
6+
7+
This section provides technical reference for the `@api-ts/openapi-generator`
8+
command-line utility. The documentation covers:
9+
10+
- CLI usage and options
11+
- Configuration settings
12+
- Supported `io-ts` primitives
13+
- JSDoc annotation system
14+
15+
You can use this reference to generate OpenAPI specifications from your
16+
`@api-ts/io-ts-http` API definitions.

0 commit comments

Comments
 (0)