@mocks-server/admin-api-client-data-provider

Client of @mocks-server/plugin-admin-api built with @data-provider

Usage no npm install needed!

<script type="module">
  import mocksServerAdminApiClientDataProvider from 'https://cdn.skypack.dev/@mocks-server/admin-api-client-data-provider';
</script>

README

Build status Coverage Status Quality Gate

Renovate Last commit Last release

NPM downloads License

Mocks-server administration api client built with @data-provider

This package contains methods for administrating mocks-server (through the @mocks-server/plugin-admin-api REST API).

Built using data-provider, it can be used in Node.js, browsers, and it is also compatible with other data-provider packages, such as @data-provider/react, so can be easily integrated with frameworks.

Installation

npm i --save redux @data-provider/core @data-provider/axios @mocks-server/admin-api-client-data-provider

Usage with promises

All methods described in the Api (expect the config method) return Promises when executed:

import { about, settings } from "@mocks-server/admin-api-client-data-provider";

const example = async () => {
  const { version } = await about.read();
  console.log(`Current plugin-admin-api version is ${version}`);

  const currentSettings = await settings.read();
  console.log("Current mocks-server settings are", currentSettings);

  await settings.update({
    mock: "user-super-admin",
    delay: 1000
  });
  console.log("Mock and delay changed");
};

example();

Usage with data-provider

Exported properties about, settings, mocks, mocksModel, routes, routesModel, routesVariants and routesVariantsModel are @data-provider/axios providers, so can be used to define @data-provider Selectors. Methods can also be connected to frameworks using another data-provider packages, such as @data-provider/react.

Api

  • about.read() - Returns info about mocks-server/plugin-admin-api, such as current version.
  • settings.read() - Returns current mocks-server settings.
  • settings.update(settingsObject) - Updates mocks-server settings. A settings object has to be provided. Read the mocks-server configuration documentation for further info.
  • alerts.read() - Returns collection of current alerts.
  • alert(id).read() - Returns a specific alert.
  • alertsModel.queries.byId(id).read() - Returns a specific alert.
  • mocks.read() - Returns collection of available mocks.
  • mock(mockId).read() - Returns a specific mock.
  • mocksModel.queries.byId(mockId).read() - Returns a specific mock.
  • routes.read() - Returns collection of available routes.
  • route(id).read() - Returns a specific route.
  • routes.queries.byId(id).read() - Returns an specific route.
  • routesVariants.read() - Returns collection of available routes variants.
  • routeVariant(id).read() - Returns a specific route variant.
  • routesVariants.queries.byId(id).read() - Returns an specific route variant.
  • customRouteVariants.read() - Returns collection of current custom route variants.
  • customRouteVariants.create(id) - Sets a specific route variant to be used by current mock.
  • customRouteVariants.delete() - Restore routes variants to those defined in current mock.

Legacy methods

Next methods correspond to Mocks Server legacy methods

  • behaviors.read() - Returns collection of available behaviors.
  • behavior(behaviorName).read() - Returns a specific behavior.
  • behaviorsModel.queries.byName(behaviorName).read() - Returns a specific behavior.
  • fixtures.read() - Returns collection of available fixtures.
  • fixture(id).read() - Returns a specific fixture.
  • fixturesModel.queries.byId(id).read() - Returns a specific fixture.

Configuration

By default, the client is configured to request to http://localhost:3100/admin, based in the default options of mocks-server

You can change both the base url of mocks-server, and the base api path of mocks-server/plugin-admin-api using the config method:

import { config } from "@mocks-server/admin-api-client-data-provider";

config({
  apiPath: "/foo-admin",
  baseUrl: "http://my-mocks-server:3000"
});

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.