Skip to main content
Version: 3.10.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.

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.

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.

danger

When the core is received in a plugin as a parameter, you must use core.alerts, but when you are creating your own core instance programmatically, then you must use core.alertsApi instead. This was made in v3.2 due to backward compatibility reasons, and it will be fixed in next major version.

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.

onChange()

core.alerts.onChange(callback): Add a callback to be executed when alerts change. Returns a function for removing the added callback.

  • 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!");
});

// 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. It adds a collection id to each item. For nested collections, the id is built with all parents ids and self id joined with :.

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.