On demand, mock all responses from your Strapi APIs.
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.
(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
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.
Copy the following code and run from your terminal
1npm i nova-datas-mocker
or
1yarn nova-datas-mocker
plugins.js
The configuration is done in the plugin configuration.
1// ./config/plugins.js
2// if change, run `npm run build --clean`
3module.exports = ({ env }) => ({
4 // ...
5 'nova-datas-mocker': {
6 enabled: true,
7 config: {
8 defaultDepth: 5, // default 5
9 consoleLog: false, // default false
10 customFields: { 'plugin::permalinks.permalink': `string` }, // default {}
11 imageNameToUse: 'ExistingImageInMediaLibraryToUse.xxx', // default ''
12 },
13 },
14 // ...
15});
Variable | Description | Type | Default value |
---|---|---|---|
defaultDepth | Indicate the deep of the genration of Mocking datas | Int | 5 |
consoleLog | Enabled or not the verbous log | Boolean | false |
customFields | Object specifying which data type (Date, integer, Json, etc.) a CustomField should return | Object | {} |
apisToMock | from v1.4.0 You can select them directely from screen | Object | deprecated |
imageNameToUse | An 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).
In the Strapi admin, you have a screen to enable/disable the mock functionality. You can olso add image to all mocked richtext.
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.
A least, v1.3.10 of this plugin is required.
Note: On those routes, no credentials are required.
You can call and get plugin status with this route /nova-datas-mocker/isMockEnabled
.
The returned object is:
1{
2 "mockEnabled": false|true,
3 "addImageInRichtextEnabled": false|true,
4 "api::*": false|true
5}
You can call and get plugin status with this route /nova-datas-mocker/configPlugin
.
The returned object is:
1{
2 "defaultDepth": 6,
3 "consoleLog": true,
4 "customFields": {
5 "plugin::permalinks.permalink": "string"
6 },
7 "imageNameToUse": "main_BCG_7099addefe.jpg"
8}
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.
The image is self hosted in this repository.
Photo de Anita Austvika sur Unsplash
npm install nova-datas-mocker
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.