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

nova-datas-mocker

v1.4.7

Published

On demand, mock all responses from your Strapi APIs.

Downloads

78

Readme

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

npm i nova-datas-mocker

or

yarn nova-datas-mocker

2. Configuration

I. In plugins.js

The configuration is done in the plugin configuration.

// ./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 ''
    },
  },
  // ...
});

| 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).

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:

{
  "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:

{
  "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.



Mocking image used

The image is self hosted in this repository.

Photo de Anita Austvika sur Unsplash