Skip to main content

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:
  1. Configuration Types - Define user-facing and resolved configuration
  2. Plugin Definition - Export the plugin metadata and handler
  3. 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

The plugin.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:
Available tags:
  • client - HTTP client plugins
  • mocker - Mock data generation
  • sdk - SDK generation
  • transformer - Data transformation
  • validator - Schema validation

Custom Resolvers

Resolvers control how specific schema constructs are processed:

File Organization

A complete plugin typically has this structure:

Best Practices

Next Steps

Programmatic Usage

Learn how to use the createClient API programmatically

Watch Mode

Configure watch mode for automatic regeneration