npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-mate

v1.1.1

Published

A powerful tool to mock any API based on OpenAPI specs

Downloads

183

Readme

Mock Mate

Mock Mate is a powerful and flexible API mocking tool that generates API mocks from OpenAPI specifications (supports both YAML and JSON formats). It allows developers to easily simulate API endpoints for testing and development purposes. With built-in customization options, users can update mock responses dynamically based on request properties such as the request body.

Features

  • Automatic API Mock Generation: Automatically generate API mocks from OpenAPI specs (YAML/JSON).
  • Supports Multiple Formats: Works with both YAML and JSON OpenAPI specs.
  • Dynamic Response Updates: Customize API responses on the fly via an update endpoint.
  • Conditional Response Matching: Define rules to send specific responses based on request body values.
  • Flexible and Extensible: Easily mock any endpoint and customize default responses for your tests.
  • Ideal for Development and Testing: Simulate real API behavior in local environments or CI pipelines.

Installation

To install Mock Mate, first clone the repository or add it as a dependency in your project.

Using npm

npm install mock-mate

Usage

1. Add an OpenAPI Spec

Mock Mate can use either YAML or JSON OpenAPI specs to generate mocks. Place your OpenAPI spec file in your project.

Example OpenAPI spec (YAML):

openapi: 3.0.0
info:
  title: Mock Mate API
  version: 1.0.0
paths:
  /Consent:
    post:
      summary: Create a consent
      responses:
        '200':
          description: Consent created
          content:
            application/json:
              example:
                ver: '2.0.0'
                txnid: '123456789'
                timestamp: '2023-06-26T11:39:57.153Z'
                ConsentHandle: '654024c8-29c8-11e8-8868-0289437bf331'
        '400':
          description: Bad Request
          content:
            application/json:
              example:
                code: 400
                msg: 'Bad Request'

2. Initialize and Start Mock Mate

In your project, you can use the getMockMate function to load the OpenAPI spec file and initialize the mock server.

import { getMockMate } from 'mock-mate';

// Path to your OpenAPI spec file
const filePath = './path-to-your-openapi-spec.yaml';

// Initialize the Mock Mate instance
const mockMate = getMockMate(filePath);

// Start the mock server
mockMate.start();

This will start the mock server with the API paths and endpoints defined in the OpenAPI spec.

3. Dynamic Response Matching

You can update mock responses dynamically, including the ability to define conditions based on the request body. When a request matches the defined conditions, the corresponding response will be returned.

For example, to return a 409 Conflict when the txnid field in the request body equals "conflict-id":

mockMate.updateMockConfig(
  '/Consent',
  'post',
  409,
  { code: 409, msg: 'Conflict: Consent already exists' },
  [{ field: 'txnid', value: 'conflict-id' }]
);

In this case, when a POST /Consent request has { "txnid": "conflict-id" } in the request body, a 409 Conflict response will be sent.

4. Example cURL for Updating Mock Responses

You can dynamically update mock responses by sending a request to /mock/update. Here’s an example:

curl -X POST http://localhost:3000/mock/update -H "Content-Type: application/json" -d '{
  "path": "/Consent",
  "method": "POST",
  "statusCode": 409,
  "responseBody": {
    "code": 409,
    "msg": "Conflict: Consent already exists"
  },
  "conditions": [
    {
      "field": "txnid",
      "value": "conflict-id"
    }
  ]
}'

This will ensure that a POST /Consent request with "txnid": "conflict-id" in the request body will return a 409 Conflict response.

5. Docker Support

Mock Mate also includes Docker support for easy deployment in testing environments. You can build the Docker image and run the mock server as a container.

1. Build the Docker Image

docker build -t mock-mate:latest .

2. Run the Docker Container

docker run -d -p 3000:3000 mock-mate:latest

The mock server will now be available on http://localhost:3000.

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you'd like to contribute to Mock Mate.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About Us

This tool is developed and maintained by S25Digital.