|
| 1 | +--- |
| 2 | +id: javascript |
| 3 | +title: JavaScript API |
| 4 | +description: Mocks Server JavaScript API |
| 5 | +keywords: |
| 6 | + - mocks server |
| 7 | + - programmatic |
| 8 | + - api |
| 9 | + - core |
| 10 | + - methods |
| 11 | + - properties |
| 12 | + - getters |
| 13 | + - advanced usage |
| 14 | + - JavaScript |
| 15 | + - js |
| 16 | + - node |
| 17 | + - nodejs |
| 18 | +--- |
| 19 | + |
| 20 | +```mdx-code-block |
| 21 | +import DocCardList from '@theme/DocCardList'; |
| 22 | +import {useCurrentSidebarCategory} from '@docusaurus/theme-common'; |
| 23 | +``` |
| 24 | + |
| 25 | +## Preface |
| 26 | + |
| 27 | +Mocks Server provides its `core` instance to plugins, middlewares and other system elements. It contains methods allowing to configure it, start or stop it, listen to its events, etc. Using it also enables to tap into, modify, or extend its internal behavior. |
| 28 | + |
| 29 | +:::caution |
| 30 | +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. |
| 31 | +::: |
| 32 | + |
| 33 | +## Constructor |
| 34 | + |
| 35 | +__`new Core([config], [advancedOptions])`__: Creates a new Mocks Server core instance. Read the [Javascript integrations](../integrations/javascript.md) for further info. |
| 36 | + * `config` _(Object)_: Any of [Mocks Server core options](../configuration/options.md#core-options) or [plugins options](../configuration/options.md#plugin-options). Command line arguments, environment variables and configuration file options would override the values defined here. |
| 37 | + * `advancedOptions` _(Object)_: |
| 38 | + * `pkg` _(Object)_: Useful when creating custom distributions. It allows to define a different package name and version to check for updates. For example, the `@mocks-server/main` distribution provides its name and version to the core using this parameter, so when the core checks for updates, it uses these properties instead of its own. |
| 39 | + * `name` _(String)_: Package name |
| 40 | + * `version` _(String)_: Current package version |
| 41 | + |
| 42 | +```js |
| 43 | +const Core = require("@mocks-server/core"); |
| 44 | + |
| 45 | +// highlight-next-line |
| 46 | +const core = new Core({ |
| 47 | + server: { |
| 48 | + port: 3500 |
| 49 | + }, |
| 50 | +}); |
| 51 | + |
| 52 | +// highlight-next-line |
| 53 | +core.init().then(async () => { |
| 54 | + // highlight-start |
| 55 | + await core.start(); |
| 56 | + await core.stop(); |
| 57 | + // highlight-end |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +:::warning |
| 62 | +The `Core` constructor is exported only by the `@mocks-server/core` package, and it doesn't include any pre-installed plugins, so you should [install them by yourself](../plugins/installation.md) if you choose this method for starting Mocks Server programmatically. Read the [next section](#creating-an-instance-with-pre-installed-plugins) for an alternative method of creating a core instance using the `@mocks-server/main` package, which includes pre-installed plugins and other optimal configuration to start it programmatically. |
| 63 | +::: |
| 64 | + |
| 65 | +### Creating an instance with pre-installed plugins |
| 66 | + |
| 67 | +The `@mocks-server/main` distribution includes some plugins providing useful integrations, such as the [Admin Api Plugin](../integrations/rest-api.md), etc. If you create a new server instance using the `@mocks-server/core` package as in the example above, you would have to [install your desired plugins](../plugins/installation.md) by your own (which may be useful to [create your own distribution](../integrations/javascript.md#creating-your-own-distribution), for example). |
| 68 | + |
| 69 | +Note also that the [default configuration](../configuration/options.md) of `@mocks-server/core` is intended to be used in CLI, so it tries to load files, read arguments and environment variables, etc. which might not be ideal to start it programmatically in Jest tests, for example. |
| 70 | + |
| 71 | +But it is also possible to create an instance with all `@mocks-server/main` distribution plugins and configuration optimized for a programmatic usage using the next function exported by the library: |
| 72 | + |
| 73 | +__`createServer([config])`__: Returns a new core instance. The `config` provided is merged with the default package config, which includes some pre-installed plugins and optimized configuration for a programmatic usage: |
| 74 | + * It disables the files load. The scaffold is not created and routes and collections must have to be loaded using the JavaScript API. |
| 75 | + * It disables loading configuration from file, environment variables or arguments. So, possible conflicts are avoided and all configuration has to be provided programmatically. |
| 76 | + * It disables the interactive CLI plugin. |
| 77 | + |
| 78 | +```js |
| 79 | +const { createServer } = require("@mocks-server/main"); |
| 80 | +const { routes, collections } = require("./fixtures"); |
| 81 | + |
| 82 | +// highlight-next-line |
| 83 | +const core = createServer({ |
| 84 | + server: { |
| 85 | + port: 3500 |
| 86 | + }, |
| 87 | +}); |
| 88 | + |
| 89 | +// highlight-next-line |
| 90 | +core.init().then(async () => { |
| 91 | + // highlight-start |
| 92 | + const { loadRoutes, loadCollections } = core.mock.createLoaders(); |
| 93 | + loadRoutes(routes); |
| 94 | + loadCollections(collections); |
| 95 | + |
| 96 | + await core.start(); |
| 97 | + // highlight-end |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +### Other ways of accessing to the core instance |
| 102 | + |
| 103 | +Apart from creating your own `core` instance programmatically, you can also use it from other system elements, because it is passed as an argument to them. Some elements to which the core instance is passed are: |
| 104 | + |
| 105 | +* __Plugins__: A plugin receives the core instance on its constructor and in all of its standardized methods. Read [plugins development](../plugins/development.md) for further info. |
| 106 | +* __Variant handlers__: A variant handler receives the core instance on its constructor. Read [variant handlers development](../variant-handlers/development.md) for further info. |
| 107 | + * __middleware variants__: The core is passed from the `middleware` variant handler to the middleware functions defined in that type of variants. So, it can be used directly in Express middlewares. Read the [middleware variant chapter](../usage/variants/middleware.md) for further info. |
| 108 | + |
| 109 | +## API |
| 110 | + |
| 111 | +### init() |
| 112 | + |
| 113 | +__`core.init([config])`__: Register plugins, initialize options and prepare all other internal dependencies needed to start the server. Returns a promise. Accepts next arguments: |
| 114 | + * `config` _(Object)_: Any of [Mocks Server options](../configuration/options.md#core-options) or [plugin options](../configuration/options.md#plugin-options). If provided, the config passed in the [constructor](#constructor) would be merged with this one. Command line arguments, environment variables and configuration file options would override the values defined here. Options are internally available using the [`core.config` API](./javascript/config.md) once they are initialized. |
| 115 | + |
| 116 | +### start() |
| 117 | + |
| 118 | +__`core.start()`__: Start the server and plugins. Returns a promise. It calls to the `init` method internally if it was not done before. |
| 119 | + |
| 120 | +### stop() |
| 121 | + |
| 122 | +__`core.stop()`__: Stop the server and plugins. Returns a promise. |
| 123 | + |
| 124 | +## Children objects APIs |
| 125 | + |
| 126 | +```mdx-code-block |
| 127 | +<DocCardList items={useCurrentSidebarCategory().items}/> |
| 128 | +``` |
0 commit comments