Custom Plugins
OpenAPI TypeScript’s plugin system allows you to extend code generation with custom functionality. You can create plugins that generate validators, transformers, SDK helpers, or any custom output from your OpenAPI specifications.Understanding the Plugin System
Plugins in OpenAPI TypeScript are self-contained modules that process the OpenAPI specification and generate code. The plugin system provides:- Type-safe configuration with TypeScript
- Access to the parsed OpenAPI spec through the intermediate representation (IR)
- Code generation utilities via the TypeScript DSL
- Hook system for controlling which resources are processed
- Dependency management between plugins
Plugin Architecture
A plugin consists of three main components:- Configuration Types - Define user-facing and resolved configuration
- Plugin Definition - Export the plugin metadata and handler
- Handler Function - Process the specification and generate code
Basic Plugin Structure
Implementing the Handler
The handler function receives the plugin instance and processes the OpenAPI specification:Processing OpenAPI Resources
Theplugin.forEach() method iterates over different OpenAPI resources:
Real-World Example: Custom Client Plugin
Here’s a complete example based on the test suite showing how to create a custom HTTP client plugin:client/plugin.ts
Using Your Custom Plugin
openapi-ts.config.ts
Advanced Features
Using Hooks
Hooks allow you to control which resources are processed:Plugin Dependencies
Declare dependencies on other plugins:Plugin Tags
Use tags to influence plugin ordering and resolution:client- HTTP client pluginsmocker- Mock data generationsdk- SDK generationtransformer- Data transformationvalidator- Schema validation
Custom Resolvers
Resolvers control how specific schema constructs are processed:File Organization
A complete plugin typically has this structure:Best Practices
@org/plugin-name or use descriptive namesz{{name}} for validators, {{name}}Schema for schemasNext Steps
Programmatic Usage
Learn how to use the
createClient API programmaticallyWatch Mode
Configure watch mode for automatic regeneration