You can develop your own plugins for the Mocks Server to provide more interfaces, add more ways of defining fixtures, etc.
It is recommended that plugins are published with the "mocks-server-plugin-[name]" name format in order to facilitate the search. Plugins should also contain the "mocks-server-plugin" tag in the
Plugins should contain four main methods, which will receive the instance of the Mocks Server core and a second argument with extra methods explicitly created for each different plugin. Please read the programmatic usage chapter to know how to interact with the core.
This method will be called for registering the plugin during the Mocks Server initialization, before
options have been initialized.
Here you should register your own custom
options using the
core.addSetting method, register your own custom express routers using the
core.addRouter method, etc.
You should never access here to the
core.settings methods, are they are not still ready in this phase, which was designed with the intention of letting the plugins to add their own settings.
If you define your plugin as a Class, the
constructorwill be equivalent to defining a
registermethod. If you define your plugin as a function, it will be called during the plugins registration, so you could also omit the
This method will be called when Mocks Server settings are ready. Here you already can access to the
core.settings to get the user options, and act in consequence. Here you should also add your listeners to the core events, such as
When this method is called, the Mocks Server is already started and listening to requests, and the files watcher is observing for changes too.
This method will be called when the Mocks Server stop method is called. Here you should stop all the plugin processes.
Plugins should also contain a
displayNameproperty or getter, which will be used by the core for debugging purposes.
Apart of the
core instance containing all methods and getters described in the programmatic usage chapter, plugins will receive methods explicitly created for each plugin instance as a second argument. This object contains next methods:
loadMocks(definitions)- Loads "behaviors" and "fixtures" definitions. Each time this method is called, all previously loaded behaviors and fixtures will be replaced by the new ones, but only those added by this plugin. Definitions loaded by the core or by other plugins will remain.
Here you have an example of how a plugin is defined. Consult the Mocks Server programmatic usage chapter for further info:
The methods can be defined in a plain
object, as methods of a
Class or even using a
function returning an object containing them.
Next examples show how each format should be defined: