Skip to content

Commit 3c5f52a

Browse files
authored
Merge pull request #1038 from BitGo/docs/refactor-openapi-generator-docs
docs(openapi-generator): Refactor documentation to Diátaxis Reference format
2 parents 58b65fb + a0399b6 commit 3c5f52a

File tree

6 files changed

+512
-0
lines changed

6 files changed

+512
-0
lines changed
Lines changed: 53 additions & 0 deletions
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+
```
Lines changed: 142 additions & 0 deletions
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+
```
Lines changed: 16 additions & 0 deletions
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 3.0 specifications from your
16+
`@api-ts/io-ts-http` API definitions.

0 commit comments

Comments
 (0)