@scalar/mock-server

v0.2.64
OpenAPI mock server server
scalar openapi swagger cli

Scalar Mock Server

Version Downloads License Discord

A powerful Node.js server that generates and returns realistic mock data based on OpenAPI specifications. Ideal for API development, testing, and prototyping.

Features

  • Automatically creates endpoints from your OpenAPI/Swagger document
  • Generates realistic sample data matching your schema definitions
  • Supports Swagger 2.0, OpenAPI 3.0 and 3.1 specifications
  • Customizable response handling and data generation
  • Perfect for frontend development without an actual backend

Quickstart

It’s part of our Scalar CLI, sou can boot it literllay in seconds:

npx @scalar/cli mock openapi.json --watch

Installation

For more advanced use cases or finer control over the mock server, you can use the package directly in your Node.js application:

npm install @scalar/mock-server

Usage

import { serve } from '@hono/node-server'
import { createMockServer } from '@scalar/mock-server'

// Your OpenAPI specification
const specification = {
  openapi: '3.1.0',
  info: {
    title: 'Hello World',
    version: '1.0.0',
  },
  paths: {
    '/foobar': {
      get: {
        responses: {
          '200': {
            description: 'OK',
            content: {
              'application/json': {
                example: {
                  foo: 'bar',
                },
              },
            },
          },
        },
      },
    },
  },
}

// Create the mocked routes
const app = await createMockServer({
  specification,
  onRequest({ context, operation }) {
    console.log(context.req.method, context.req.path)
  },
})

// Start the server
serve(
  {
    fetch: app.fetch,
    port: 3000,
  },
  (info) => {
    console.log(`Listening on http://localhost:${info.port}`)
  },
)

Community

We are API nerds. You too? Let’s chat on Discord: https://discord.gg/scalar

License

The source code in this repository is licensed under MIT.

npm i @scalar/mock-server

Metadata

  • MIT
  • >=18
  • Scalar
  • released 10/20/2024

Downloads