Skip to content

About

Generates Cloudflare Workers that mock APIs from OpenAPI schemas

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

OpenAPI โ†’ Cloudflare Worker Mock Generator

Convert any OpenAPI 3.x schema into a fully functional Cloudflare Worker mock API in seconds โ€” entirely in your browser.

License: MIT OpenAPI 3.x Cloudflare Workers

๐Ÿš€ Features

  • ๐ŸŒ 100% Client-Side - No server required, runs entirely in your browser
  • โšก Instant Generation - Paste schema, click generate, deploy
  • ๐ŸŽฏ Smart Fallbacks - Multiple mock data generation strategies
  • ๐Ÿ”’ Safe Code Output - Properly escaped, production-ready JavaScript
  • ๐ŸŽจ Dark Theme UI - Beautiful, modern interface
  • ๐Ÿ“ฆ Zero Dependencies - Pure HTML, CSS, and JavaScript
  • ๐Ÿ”ง Extensible - Modular codebase ready for customization

๐ŸŽฏ Use Cases

  • Frontend Development - Mock backends before API implementation
  • Testing & QA - Reliable, consistent test data
  • Demos & Prototypes - Quick API mocking for presentations
  • CI/CD Pipelines - Temporary mock endpoints for testing
  • API Documentation - Interactive examples with real responses

๐Ÿ› ๏ธ Quick Start

Option 1: Use Online (Recommended)

Simply open index.html in your browser - no installation needed!

Option 2: Local Development

# Clone the repository
git clone https://github.com/yourusername/openapi-worker-generator.git
cd openapi-worker-generator

# Open in browser
open index.html  # macOS
# or
start index.html  # Windows
# or
xdg-open index.html  # Linux

๐Ÿ“– How It Works

  1. Paste Schema - Add your OpenAPI 3.x JSON schema
  2. Select Fallback Mode - Choose how to generate mock data
  3. Generate - Click the button to create your worker
  4. Deploy - Copy or download worker.js and wrangler.toml

Deployment

# Install Wrangler (Cloudflare's CLI)
npm install -g wrangler

# Login to Cloudflare
wrangler login

# Deploy your worker
wrangler deploy

๐ŸŽจ Fallback Modes

The generator uses a smart example precedence system:

Example Priority (Highest to Lowest)

  1. Named examples: responses[status].content[type].examples[name].value
  2. Inline example: responses[status].content[type].example
  3. Schema example: responses[status].content[type].schema.example
  4. Selected fallback mode (see below)

Fallback Strategies

1. Basic Stub (Default)

{
  "ok": true,
  "mocked": true,
  "path": "/users"
}

Simple, reliable response when no schema is available.

2. Schema-Typed Stub

{
  "id": 0,
  "name": "string",
  "email": "user@example.com",
  "active": false
}

Generates typed mock data based on your schema definitions.

3. JSON-Schema-Faker Pre-generation

{
  "id": 847,
  "name": "xkpqwmfhzt",
  "email": "user529@example.com",
  "active": true
}

Creates realistic, randomized mock data with variety.

๐Ÿ“‹ Example OpenAPI Schema

{
  "openapi": "3.0.0",
  "info": {
    "title": "Users API",
    "version": "1.0.0"
  },
  "paths": {
    "/users": {
      "get": {
        "summary": "List all users",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "example": [
                  { "id": 1, "name": "Alice" },
                  { "id": 2, "name": "Bob" }
                ]
              }
            }
          }
        }
      }
    },
    "/users/{id}": {
      "get": {
        "summary": "Get user by ID",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "integer" }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": { "type": "integer" },
                    "name": { "type": "string" },
                    "email": { "type": "string", "format": "email" }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

๐Ÿ”ง Generated Worker Features

The generated Cloudflare Worker includes:

  • โœ… Regex-based routing - Fast path matching with parameter support
  • โœ… CORS headers - Pre-configured for cross-origin requests
  • โœ… OPTIONS handling - Automatic preflight request support
  • โœ… Content negotiation - Respects Content-Type from schema
  • โœ… Status codes - Uses correct HTTP status from your spec
  • โœ… 404 handling - Graceful fallback for unmatched routes
  • โœ… No runtime dependencies - Pure JavaScript, no external libs

๐Ÿ—๏ธ Architecture

openapi-worker-generator/
โ”œโ”€โ”€ index.html          # Main UI structure
โ”œโ”€โ”€ style.css           # Dark theme styling
โ”œโ”€โ”€ app.js              # Core generator logic
โ”‚   โ”œโ”€โ”€ Utilities       # Escaping, regex, type generation
โ”‚   โ”œโ”€โ”€ Parser          # OpenAPI schema parsing
โ”‚   โ”œโ”€โ”€ Generator       # Worker code generation
โ”‚   โ””โ”€โ”€ UI              # Event handlers, display
โ””โ”€โ”€ README.md           # This file

๐ŸŽฏ Supported OpenAPI Features

Feature Supported Notes
OpenAPI 3.0.x โœ… Full support
OpenAPI 3.1.x โœ… Full support
Path parameters โœ… {id} converted to regex
Query parameters โš ๏ธ Ignored (mocks don't validate)
Request bodies โš ๏ธ Ignored (mocks return examples)
Multiple responses โœ… Uses first 2xx or first available
Examples โœ… Named, inline, and schema examples
Content types โœ… Respects Content-Type header
Security โš ๏ธ Not validated (future feature)
Callbacks โŒ Not supported
Links โŒ Not supported

๐Ÿ”ฎ Roadmap

Future enhancements planned:

  • Authentication mocking - Basic/Bearer token validation
  • Delay simulation - x-mock-delay extension support
  • Query param overrides - ?__status=404&__example=error
  • TypeScript output - Optional TS worker generation
  • Multiple workers - Split by tag/path prefix
  • Request logging - Optional console/analytics integration
  • State simulation - POST/PUT/DELETE state tracking
  • Webhook forwarding - Proxy real requests to external URLs

๐Ÿค Contributing

Contributions are welcome! The codebase is designed to be modular and extensible.

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Inspired by the need for instant API mocking during frontend development
  • Built for developers who need reliable mock APIs without complex setup
  • Designed with Cloudflare Workers' simplicity and performance in mind

About

Generates Cloudflare Workers that mock APIs from OpenAPI schemas

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages