openapi-faker-mock
v1.0.1
Published
A library to generate mock responses from OpenAPI specifications
Maintainers
Readme
OpenAPI Faker Mock
A library to generate mock responses from OpenAPI specifications using JSON Schema Faker.
Installation
npm install openapi-faker-mockUsage
const { createMocker } = require('openapi-faker-mock');
// Load your OpenAPI specification
const openApiPath = './openapi.yaml';
// Create a new mocker function
const mocker = createMocker(openApiPath);
// Generate a mock response for a specific path and method
const response = mocker('/api/users', 'get');Features
- Generates realistic mock data based on OpenAPI schemas
- Supports all OpenAPI 3.0 and Swagger 2.0 data types
- Customizable response generation
- Path parameter matching
- Query parameter support
- Extended x-faker support with min/max parameters
API
createMocker
const mocker = createMocker(openApiPath);Parameters:
openApiPath: Path to the OpenAPI specification file
Returns: A mocker function that generates responses
mocker(path, method)
Generates a mock response for the specified path and HTTP method.
Parameters:
path: API endpoint pathmethod: HTTP method (get, post, put, delete, etc.)
Returns: Mock response object
Advanced Usage with x-faker
You can use the x-faker property to customize the generated data:
Basic usage
properties:
email:
type: string
format: email
x-faker: internet.emailAdvanced usage with min/max parameters
properties:
age:
type: integer
x-faker:
datatype.number:
min: 18
max: 65Example with complex x-faker options
components:
schemas:
Product:
type: object
properties:
id:
type: integer
x-faker:
datatype.number:
min: 1000
max: 9999
price:
type: number
format: float
x-faker:
datatype.float:
min: 9.99
max: 99.99
precision: 0.01For more detailed documentation on x-faker extensions, see docs/x-faker.md.
License
MIT
