-
Notifications
You must be signed in to change notification settings - Fork 21
docs(openapi-generator): Refactor documentation to Diátaxis Reference format #1038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --- | ||
| sidebar_position: 2 | ||
| --- | ||
|
|
||
| # Command-line Interface | ||
|
|
||
| ## Overview | ||
|
|
||
| The `openapi-generator` CLI tool converts your `@api-ts/io-ts-http` `apiSpec` definition | ||
| into an OpenAPI 3.0 specification. When you run this tool, it reads a TypeScript file | ||
| containing your API definition and outputs the OpenAPI specification to stdout. | ||
|
|
||
| ## Usage Syntax | ||
|
|
||
| ```shell | ||
| openapi-generator [OPTIONS] [FLAGS] <file> | ||
| ``` | ||
|
|
||
| ## Arguments | ||
|
|
||
| - `<file>`: (Required) Path to the TypeScript file containing the exported `apiSpec` | ||
| definition. | ||
|
|
||
| ## Options | ||
|
|
||
| - `--name`, `-n <string>`: Specifies the API name in the generated specification. | ||
| - `--version`, `-v <string>`: Specifies the API version in the generated OpenAPI | ||
| specification. If an `@version` JSDoc tag is present on the `apiSpec` export, that | ||
| value takes precedence. | ||
| - `--codec-file`, `-c <string>`: Path to a JavaScript configuration file defining | ||
| schemas for custom or external io-ts codecs. See | ||
| [Defining custom codec schemas](./configuration#defining-custom-codec-schemas) for | ||
| details. | ||
|
|
||
| ## Flags | ||
|
|
||
| - `--internal`, `-i`: Includes routes marked with the `@private` JSDoc tag in the | ||
| generated output. By default, private routes are excluded. | ||
| - `--help`, `-h`: Displays the help message describing arguments, options, and flags. | ||
|
|
||
| ## Examples | ||
|
|
||
| You can generate an OpenAPI specification and save it to a file: | ||
|
|
||
| ```shell | ||
| npx openapi-generator src/index.ts > openapi-spec.json | ||
| ``` | ||
|
|
||
| You can specify API name, version, and custom codec definitions: | ||
|
|
||
| ```shell | ||
| npx openapi-generator --name "My Service API" --version "2.1.0" --codec-file ./custom-codecs.js src/api.ts | ||
| ``` |
142 changes: 142 additions & 0 deletions
142
website/docs/reference/openapi-generator/configuration.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| # Configuration | ||
|
|
||
| ## Overview | ||
|
|
||
| You need to configure the generator to: | ||
|
|
||
| - Correctly interpret `io-ts` types from external packages. | ||
| - Define OpenAPI schemas for custom `io-ts` codecs that can't be automatically derived | ||
| from the Abstract Syntax Tree (AST). | ||
|
|
||
| ## Preparing External Types Packages | ||
|
|
||
| To process `io-ts` types imported from other npm packages, ensure the following in the | ||
| external package's `package.json`: | ||
|
|
||
| 1. Include source code in the published npm bundle by adding the source directory to the | ||
| `files` array: | ||
|
|
||
| ```json | ||
| // package.json of the external types package | ||
| { | ||
| "name": "my-external-types", | ||
| "version": "1.0.0", | ||
| "files": [ | ||
| "dist/", | ||
| "src/" // Include source code | ||
| ], | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "source": "src/index.ts" // Add this field | ||
| // ... rest of package.json | ||
| } | ||
| ``` | ||
|
|
||
| 2. Specify the source entry point using the `source` field (for example, | ||
| `"source": "src/index.ts"`). | ||
|
|
||
| ## Defining Custom Codec Schemas | ||
|
|
||
| For custom `io-ts` codecs (such as those using `new t.Type(...)` or complex types not | ||
| directly supported), you must define schemas manually using one of these methods: | ||
|
|
||
| ### Method 1: Via `openapi-gen.config.js` (Recommended For Type Authors) | ||
|
|
||
| You can define schemas directly within the package that declares the custom codecs: | ||
|
|
||
| 1. Create a file named `openapi-gen.config.js` in the root of the types package. | ||
|
|
||
| 2. Update the package's `package.json` to include: | ||
|
|
||
| - The `customCodecFile` field pointing to this file. | ||
| - The config file in the `files` array. | ||
|
|
||
| ```json | ||
| // package.json of the types package defining custom codecs | ||
| { | ||
| "name": "my-custom-codec-package", | ||
| // ... | ||
| "files": [ | ||
| "dist/", | ||
| "src/", | ||
| "openapi-gen.config.js" // Include the config file | ||
| ], | ||
| "customCodecFile": "openapi-gen.config.js" // Point to the file | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| 3. Structure the `openapi-gen.config.js` file as follows: | ||
|
|
||
| ```javascript | ||
| // openapi-gen.config.js | ||
| module.exports = (E) => { | ||
| return { | ||
| // Key matches the exported codec name (e.g., export const MyCustomString = ...) | ||
| MyCustomString: () => | ||
| E.right({ | ||
| type: 'string', | ||
| format: 'custom-format', | ||
| description: 'A custom string type definition', | ||
| }), | ||
| AnotherCustomType: () => | ||
| E.right({ | ||
| type: 'object', | ||
| properties: { | ||
| /* ... */ | ||
| }, | ||
| }), | ||
| // ... other custom codec definitions | ||
| }; | ||
| }; | ||
| ``` | ||
|
|
||
| The exported function receives the `fp-ts/Either` namespace (`E`) as an argument. You | ||
| should return an object where: | ||
|
|
||
| - Keys are the exported names of your custom codecs. | ||
| - Values are functions that return `E.right()` with an OpenAPI schema object. | ||
|
|
||
| ### Method 2: Via `--codec-file` Option (For Consumers) | ||
|
|
||
| You can define schemas in a configuration file within your project and pass the file | ||
| path via the `--codec-file` option: | ||
|
|
||
| 1. Create a JavaScript file (for example, `custom-codecs.js`). | ||
|
|
||
| 2. Structure the file similarly to Method 1, but group definitions by package: | ||
|
|
||
| ```javascript | ||
| // custom-codecs.js | ||
| module.exports = (E) => { | ||
| return { | ||
| 'io-ts-bigint': { | ||
| // Package name | ||
| BigIntFromString: () => E.right({ type: 'string', format: 'bigint' }), | ||
| NonZeroBigIntFromString: () => | ||
| E.right({ type: 'string', format: 'bigint' /* constraints */ }), | ||
| // ... other codecs from 'io-ts-bigint' | ||
| }, | ||
| 'my-other-custom-package': { | ||
| // Another package | ||
| SomeType: () => E.right({ type: 'number', format: 'float' }), | ||
| }, | ||
| // ... other packages | ||
| }; | ||
| }; | ||
| ``` | ||
|
|
||
| In this structure: | ||
|
|
||
| - Keys of the top-level object are package names. | ||
| - Values are objects that map codec names to their schema definitions. | ||
|
|
||
| 3. Run the generator with the `--codec-file` option: | ||
|
|
||
| ```shell | ||
| npx openapi-generator --codec-file ./custom-codecs.js src/index.ts | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| sidebar_position: 1 | ||
| --- | ||
|
|
||
| # OpenAPI Generator | ||
|
|
||
| This section provides technical reference for the `@api-ts/openapi-generator` | ||
| command-line utility. The documentation covers: | ||
|
|
||
| - CLI usage and options. | ||
| - Configuration settings. | ||
| - Supported `io-ts` primitives. | ||
| - JSDoc annotation system. | ||
|
|
||
| You can use this reference to generate OpenAPI 3.0 specifications from your | ||
| `@api-ts/io-ts-http` API definitions. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.