Strapi plugin logo for Nova APIs Mocker

Nova APIs Mocker

On demand, mock all responses from your Strapi APIs.

thumbnail for Nova APIs Mocker
Logo NovaGaïa

Nova APIs Mocker

by NovaGaïa.

On demand, mock all responses from your Strapi APIs.

CHANGELOG

Get Started

Why this plugin

This Strapi plugin was originally made to answer one of my problems when I develop for Gatsby. In order not to have GraphQL errors when fields / components / DynamicZone are not filled at all, it is necessary to create or force the creation of a Schema by Gatsby which identifies these fields. To do this, on the Gatsby side I use the plugin gatsby-plugin-schema-snapshot (1). But I have to fill at least 1 item for each Collection types with all the possibilities filled and the same for Single Types : it's tedious and generate errors, especially when we get snapshots from prod regularly.

That's why I created this plugin, so that it replaces, from time to time, the API responses by mocks that autogenerate with all the possibilities.

I advise you to use strapi-plugin-populate-deep in parallel, so you don't have to declare all the populate=* on the Gatsby side. I was largely inspired by his plugin and I thank him for his work and for being an inspiration 🙏

I was inspirad by this other plugin strapi-plugin-transformer, so I want to thank him too.


Great changes:

  • v1.4.0 - You can select the APIs to mock directely from the Plugin screen BREAKING CHANGE
  • v1.3.10 - You can access an API to access the state of this plugin and a Gatsby plugin is avalaible.

Read the state from an API and use the Gatsby compagnion plugin

(1) Since v1.3.10, you can access an API to access the state of this plugin.
When calling ${strapiURL}/nova-datas-mocker/isMockEnabled you can read the state of the switch Auto Mock Datas and get {mockEnabled: true|false}. This API is accessible without any authentification.
If your are working with Gatsby, you can use the Gatsby plugin gatsby-plugin-strapi-datas-mocker who can be directely connected to this plugin and generate/update your Schema.

More informations here

⚠️ WARNING ⚠️

  • In order for the plugin to return mocked data, at least 1 entry must be created in Strapi for each API. Enter only your required fields.
  • This plugin is automatically deactivated in production.
  • I decided, for collection, to sent only one response in an Array. More is not needed.
  • Your modifications in controllers are added to Mocked response without inteligence. It may breaks, fix my code and open a PR or open an issue and I'll try to fix.

Note

As you modify the data returned by the API and the Strapi Admin uses these same APIs to display and modify the data, as long as the plugin is activated, you could have errors when displaying the Collection Types and the Single Types.

1. Installation

Copy the following code and run from your terminal

1
npm i nova-datas-mocker

or

1
yarn nova-datas-mocker

2. Configuration

I. In plugins.js

The configuration is done in the plugin configuration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ./config/plugins.js
// if change, run `npm run build --clean`
module.exports = ({ env }) => ({
  // ...
  'nova-datas-mocker': {
    enabled: true,
    config: {
      defaultDepth: 5, // default 5
      consoleLog: false, // default false
      customFields: { 'plugin::permalinks.permalink': `string` }, // default {}
      imageNameToUse: 'ExistingImageInMediaLibraryToUse.xxx', // default ''
    },
  },
  // ...
});
VariableDescriptionTypeDefault value
defaultDepthIndicate the deep of the genration of Mocking datasInt5
consoleLogEnabled or not the verbous logBooleanfalse
customFieldsObject specifying which data type (Date, integer, Json, etc.) a CustomField should returnObject{}
apisToMockfrom v1.4.0 You can select them directely from screenObjectdeprecated
imageNameToUseAn exiting image in your Strapi Media Library (gatsby-source-strapi need it)String''

customFields must return one of those data types :

  • string
  • text
  • richtext
  • media
  • json
  • email
  • password
  • uid
  • boolean
  • float
  • integer
  • biginteger
  • enumeration
  • datetime
  • date
  • time

The data is mostly generated by @faker-js/faker or copied from real data (Markdown and Media for example).

II. In configuration screen, in Strapi

In the Strapi admin, you have a screen to enable/disable the mock functionality. You can olso add image to all mocked richtext.

Nova Datas Mocker Configuration Open it in Plugin > Nova APIs Mocker

In the screen you have only a Toogle allowing to activate/deactivate the mock of the API datas and an other to add an image in all Markdown fields.

III. Read the plugin status with API

A least, v1.3.10 of this plugin is required.

Note: On those routes, no credentials are required.

a. Get if the plugin is enabled with API

You can call and get plugin status with this route /nova-datas-mocker/isMockEnabled.

The returned object is:

1
2
3
4
5
{
  "mockEnabled": false|true,
  "addImageInRichtextEnabled": false|true,
  "api::*": false|true
}

b. Read if the plugin is configured with API

You can call and get plugin status with this route /nova-datas-mocker/configPlugin.

The returned object is:

1
2
3
4
5
6
7
8
{
  "defaultDepth": 6,
  "consoleLog": true,
  "customFields": {
    "plugin::permalinks.permalink": "string"
  },
  "imageNameToUse": "main_BCG_7099addefe.jpg"
}

c. Use the Gatsby plugin helper

To be more productive, you can add the Gatsby plugin gatsby-plugin-strapi-datas-mocker to your Gatsby project. After configure it, it will generate/update your Schema automaticaly.


Buy Me A Coffee


Mocking image used

The image is self hosted in this repository.

Photo de Anita Austvika sur Unsplash

Install now

npm install nova-datas-mocker

STATS

1 GitHub star23 weekly downloads

Last updated

142 days ago

Strapi Version

4.0.0 and above

Author

github profile image for NovaGaïa
NovaGaïa

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.