Skip to main content
Version: 4.0.0

core.alerts

Preface

The core.alerts object provides access to methods related to Mocks Server alerts. Use alerts to inform the user about deprecated methods or other warning messages, or about current errors. For example, when an error happens loading files, the server adds automatically an alert in order to let the user know about the error.

caution

Use only the API methods described in this docs. Use other methods under your own risk, and take into account that they may change in minor versions without considering it as a breaking change.

API

caution

When the core is passed to a plugin as a parameter, the alerts object is an alerts subcollection instead of the root alerts instance. The alerts subcollection is namespaced with the plugin id, so it is easy to trace where the alerts come from.

info

Here are described only some methods of the alerts API, for further info please read the @mocks-server/nested-collections docs, but take into account that in Mocks Server, the alerts set method is extended and supports passing a third error argument. Flat items are also different in Mocks Server.

onChange()

core.alerts.onChange(callback): Add a callback to be executed when alerts change. Returns a function for removing the added callback. The event will be triggered whenever an item in the current alerts collection or any of the descendent collections change. Changes in parent collections are ignored.

  • callback() (Function): Function to be executed whenever alerts change.
Example
// Add event listener and store the method allowing to remove it
const removeListener = core.alerts.onChange(() => {
console.log("Alerts have changed!");
console.log(core.alerts.flat);
});

// Remove event listener
removeListener();

set()

core.alerts.set(id, message, error): Adds an alert or modify it.

  • id (String): The id for the alert to be added or modified in case it already exists.
  • message (String): Message for the alert.
  • error (Error): Optional. Error causing the alert.

remove()

core.alerts.remove(id): Removes an alert.

  • id (String): Id of the alert to be removed.

clean()

core.alerts.clean(): Removes all alerts, including descendant collections.

collection()

core.alerts.collection(id): Allows to create a new subcollection of alerts or returns an already existent one. The returned collection will have all of the same methods described for alerts. It is useful to group alerts by its type. The context property of the alerts created in a child collection will include all parent collections ids joined with :, so the user can know the alert's "path".

flat

core.alerts.flat: Returns all collection items and descendent collection items in a flat array. For nested collections, the id of each item is built with all parents ids and self id joined with :

Example
console.log(core.alerts.flat);
/*
[
{
id: "mock:collections:load",
message: "No collections found",
error: null
}
]
*/

root

core.alerts.root: Getter returning the root alerts object. It is useful when trying to access to the root Mocks Server alerts from a plugin, but use it with caution because you will be accessing to all of the elements alerts, not only to those owned by the plugin.