Mocks-server administration api client
This package provides an API client for administrating Mocks Server through HTTP requests to the Admin API plugin.
Requests to the Mocks Server administration API are made using cross-fetch
, which makes this package compatible with browsers and Node.js environments, but, if you are going to build a browser application, you'll probably prefer to use the @mocks-server/admin-api-client-data-provider
package, which uses Data Provider, and works well with Redux, React, etc.
Install
npm install --save @mocks-server/admin-api-client
The UMD build is also available on unpkg. When UMD package is loaded, it creates a mocksServerAdminApiClient
global object containing all of the methods.
<script src="https://unpkg.com/@mocks-server/admin-api-paths/dist/index.umd.js"></script>
<script src="https://unpkg.com/@mocks-server/admin-api-client/dist/index.umd.js"></script>
The umd distribution is bundled with the cross-env
dependency, but requires the @mocks-server/admin-api-paths
dependency to be added separately.
Usage
All methods described in the Api return Promises when executed (except the configClient
method):
import { readAbout, readConfig, updateConfig } from "@mocks-server/admin-api-client";
const example = async () => {
const { version } = await readAbout();
console.log(`Current Admin API plugin version is ${versions.adminApi}`);
const currentConfig = await readConfig();
console.log("Current Mocks Server config is", JSON.stringify(currentConfig));
await updateConfig({
mock: {
collections: {
selected: "user-super-admin"
},
routes: {
delay: 1000
},
},
});
console.log("Collection and delay changed");
};
example();
Api
readAbout()
- Returns info about the Admin API plugin, such as current version.readConfig()
- Returns current configuration.updateConfig(configObject)
- Updates Mocks Server configuration. A configuration object has to be provided. Read the Mocks Server configuration docs for further info.readAlerts()
- Returns array of current alerts.readAlert(alertId)
- Returns an specific alert.readCollections()
- Returns available collections.readCollection(id)
- Returns a collection by ID.readRoutes()
- Returns available routes.readRoute(id)
- Returns a route by ID.readVariants()
- Returns available route variants.readVariant(id)
- Returns a route variant by ID.readCustomRouteVariants()
- Returns current custom route variants of the current collection.useRouteVariant(id)
- Sets a custom route variant to be used by current collection.restoreRouteVariants()
- Restore route variants to those defined in current collection.
Configuration
By default, the client is configured to request to http://127.0.0.1:3110/api
, based in the default options of Mocks Server
You can change both the the host and port of the administration API:
import { configClient } from "@mocks-server/admin-api-client";
configClient({
host: "localhost",
port: 3500
});
Release notes
Current major release is compatible only with @mocks-server/main
versions upper or equal than 3.6
. Use prior releases for lower versions. If you don't want to update to the latest major version of this package yet but you want to update @mocks-server/main
, you can also use any 5.x
version of this package with any @mocks-server/main@3.x
version.
Contributing
Contributors are welcome.
Please read the contributing guidelines and code of conduct.