@syntropiq/openapi-codegen v1.0.0
OpenAPI Stub Generator
A TypeScript tool that generates Cloudflare Workers-compatible API stubs and types from OpenAPI YAML specifications.
Features
- š Cloudflare Workers Native: Generates code specifically optimized for Cloudflare Workers
- š Complete Type Generation: Converts OpenAPI schemas to comprehensive TypeScript interfaces
- š Modular Architecture: Creates separate workers for each route group
- ā” Zero Dependencies: Generated code runs without external runtime dependencies
- š ļø Configurable: Support for custom API endpoints and authentication
- šÆ AI-Ready: Optional AI enhancement for business logic (when API key provided)
Installation
npm install
npm run build
Usage
Basic Usage
- Place your OpenAPI YAML file in the
examples/
directory - Configure your settings in
src/config/index.ts
- Run the generator:
npm run generate
Advanced Configuration
Edit src/config/index.ts
:
export const config = {
outputDir: './generated',
apiKey: process.env.OPENAI_API_KEY, // Optional: for AI enhancement
apiUrl: 'https://api.openai.com/v1', // Optional: custom AI endpoint
generateCloudflareWorkers: true,
enableTypeGeneration: true,
};
Generated Output
The tool generates:
types.ts
: Complete TypeScript type definitionsapi-client.ts
: Cloudflare Workers-compatible API clientworker.ts
: Main router that delegates to route-specific workers*_worker.ts
: Individual workers for each route group (audio, chat, etc.)
Generated Code Structure
generated/
āāā types.ts # TypeScript interfaces
āāā api-client.ts # API client with fetch methods
āāā worker.ts # Main router worker
āāā audio_worker.ts # Audio-related endpoints
āāā chat_worker.ts # Chat completions
āāā assistants_worker.ts # AI assistants
āāā ... # Additional route workers
Deployment
Cloudflare Workers
- Copy generated files to your Workers project
- Set environment variables:
API_KEY=your_api_key API_BASE_URL=https://api.openai.com/v1
- Deploy using Wrangler:
wrangler deploy
Example Worker Usage
// Import the main worker
import worker from './generated/worker';
export default worker;
Installation
Clone the repository:
git clone <repository-url>
Navigate to the project directory:
cd openapi-stub-generator
Install the dependencies:
npm install
Usage
Configure the API endpoint and key in
src/config/index.ts
.Place your OpenAPI YAML file in the
examples
directory.Run the generator:
npm run generate
Check the
dist
directory for the generated stubs and types.
Example
Refer to examples/basic-usage.ts
for a demonstration of how to use the stub generator tool with a sample OpenAPI YAML file.
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.
License
This project is licensed under the MIT License. See the LICENSE file for details.
2 months ago