Mocks Server Documentation

website·Indexed 32 days ago

https://mocks-server.javierbrea.com/docs/overview/

Documentation for Mocks Server v4.0.0, a tool for creating mock APIs using route variants, collections, and plugins. It includes guides on installation, configuration, and JavaScript/REST APIs, as well as integrations with Cypress, Docker, and OpenAPI.

Tokens
31.7K
Snippets
162
Records
247
Agent score
55%

What's inside Mocks Server

  1. Overview of the core.logger API

    The core.logger object is used for printing and storing logs. It supports namespaces, allowing developers to create labeled loggers. Each namespace can have its own log level and a separate logs store. When the core is passed to a plugin, the logger object provided is automatically a namespace labeled with the plugin ID to facilitate log tracing.
  2. Overview of Mocks Server OpenAPI plugin

    The OpenAPI plugin for Mocks Server (preinstalled in the main distribution) automatically creates routes and collections from OpenAPI 3.0 and 3.1 documents. It generates routes based on the paths property and creates route variants based on response examples or status codes without content.
  3. Understand the Mocks Server project structure

    When started for the first time, Mocks Server creates a default scaffold. The server monitors the mocks folder for changes to provide immediate API updates via hot reloading.
    project-root/
    ├── mocks/
    │   ├── routes/        <- Route definitions
    │   │   ├── common.js
    │   │   └── users.js
    │   └── collections.json <- Collection definitions
    └── mocks.config.js    <- Server configuration
  4. Interact with the Mocks Server Administration API

    The Mocks Server main distribution includes the admin-api plugin, which provides a REST API to interact with the server while it is running. By default, the administration API runs on port 3110 on a separate server from the API mock.
  5. Extend Mocks Server behavior using plugins

    Plugins allow developers to modify or extend the internal behavior of Mocks Server by accessing the core API. Common use cases include programmatically adding routes and collections, implementing new Variant Handlers, adding custom Express routers, reading files from the /mocks folder with hot reloading support, and creating new integration tools or interfaces.
  6. Update Mocks Server core concepts for v4

    In v4, the concept of 'mocks' has been renamed to 'collections'. This affects documentation, APIs, logs, and file naming. Specifically, the mocks/mocks.[js|json] file must be renamed to mocks/collections.[js|json]. Additionally, the routesVariants property within mock definitions is renamed to routes.
  7. Understand Mocks Server versioning strategy

    Mocks Server uses different versioning schemes depending on the artifact:

    • NPM Packages: Adhere to Semantic Versioning 2.0 (SemVer). Each package is versioned independently. In the monorepo, releases are generally tagged based on the @mocks-server/main package version.
    • Docker Images: Versioned based on the version of the Mocks Server NPM package they execute. If a Docker-only fix is required without a package change, only the patch version is incremented.
    • Documentation: Versioned according to the @mocks-server/main package. Minor or major releases of the main package trigger a documentation version.
  8. Understand how Variant Handlers process route variants

    In Mocks Server, route variants do not contain the logic for sending responses. Instead, each variant has a type property that specifies which Variant Handler is responsible for receiving the variant's options and executing the corresponding action. This architecture allows for the extension of the server by adding custom Variant Handlers to support new response formats or behaviors.
  9. Access the Mocks Server Administration REST API

    The Mocks Server main distribution comes with the admin-api plugin preinstalled, which provides a REST API to interact with the server while it is running.
  10. Understand Mocks Server core concepts

    Mocks Server relies on three primary concepts to simulate API behavior:

    • Routes: Define the URL and HTTP method of an API resource. Supports wildcards to simulate one or many real API resources.
    • Variants: Each route can have multiple variants. A variant defines a specific response to send, a middleware to execute, or a URL to proxy the request.
    • Collections: A group of route variants that define the overall state of the API mock. Collections can extend other collections, allowing for easy switching between different API behavior sets.
  11. Understand Mocks Server core concepts

    Mocks Server uses three primary concepts to simulate and control API behavior:

    • Routes: Define the URL and HTTP method of an API resource. Supports wildcards to allow a single route to simulate one or multiple real API resources.
    • Variants: Contained within routes. A variant defines the specific action to take for a request, such as sending a predefined response, executing middleware, or proxying the request to another URL. You can dynamically switch which variant is active for a route.
    • Collections: A group of route variants that define the overall state of the mocked API. Collections can extend other collections, allowing you to switch the entire API behavior by changing the active collection.
  12. Manage API states with Collections

    Collections allow you to group specific responses for different routes to simulate distinct API states. You can switch the active collection at any time to change the overall API behavior. Collections can also extend other collections to reduce maintenance overhead.