Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit c28502b

Browse files
committed
Rename model methods and export aliases for easier usage. Add documentation
1 parent eed28ea commit c28502b

File tree

10 files changed

+90
-39
lines changed

10 files changed

+90
-39
lines changed

README.md

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,64 @@
55
[![NPM downloads][npm-downloads-image]][npm-downloads-url] [![License][license-image]][license-url]
66

77

8-
# Mocks-server administration API client.
8+
# Mocks-server administration api client
99

10-
This package contains methods for administrating the mocks-server (using the plugin-admin-api RESTful API).
10+
This package contains methods for administrating the mocks-server _(through the [@mocks-server/plugin-admin-api](https://github.com/mocks-server/plugin-admin-api) RESTful API)_.
1111

12-
It can be used both from Browsers or from Node.js.
12+
Built using [@data-provider](https://github.com/data-provider), it can be used in Node.js, browsers, and is also compatible with @data-provider connectors, such as [@data-provider/connector-react](https://github.com/data-provider/connector-react), so can be easily integrated with frameworks.
1313

14-
## Usage
14+
## Usage with promises
15+
16+
All methods described in the [Api](#api) (expect the `config` method) return Promises when executed:
1517

1618
```js
17-
import { setBaseUrl, changeDelay } from "@mocks-server/admin-api-client";
19+
import { about, settings } from "@mocks-server/admin-api-client";
20+
21+
const example = async () => {
22+
const { version } = await about.read();
23+
console.log(`Current plugin-admin-api version is ${version}`);
24+
25+
const currentSettings = await settings.read();
26+
console.log("Current mocks-server settings are", currentSettings);
27+
28+
await settings.update({
29+
behavior: "user-super-admin",
30+
delay: 1000
31+
});
32+
console.log("Behavior and delay changed");
33+
};
34+
35+
example();
36+
```
37+
38+
## Usage with data-provider
1839

19-
setBaseUrl("http://localhost:3100");
40+
Exported properties `about`, `settings`, `behaviors`, `behaviorsModel`, `fixtures` and `fixturesModel` are [@data-provider/axios](https://github.com/data-provider/axios) providers, so can be used to define @data-provider Selectors. Methods can also be connected to frameworks using @data-provider connectors, such as [@data-provider/connector-react](https://github.com/data-provider/connector-react).
41+
42+
## Api
43+
44+
* `about.read()` - Returns info about mocks-server/plugin-admin-api, such as current version.
45+
* `settings.read()` - Returns current @mocks-server settings.
46+
* `settings.update(settingsObject)` - Updates @mocks-server settings. A settings object has to be provided. Read the [@mocks-server configuration documentation](https://www.mocks-server.org/docs/configuration-options) for further info.
47+
* `behaviors.read()` - Returns collection of available behaviors.
48+
* `behavior(behaviorName).read()` - Returns an specific behavior.
49+
* `behaviorsModel.byName(behaviorName).read()` - Returns an specific behavior.
50+
* `fixtures.read()` - Returns collection of available fixtures.
51+
* `fixture(fixtureId).read()` - Returns an specific fixture.
52+
* `fixturesModel.byId(fixtureId).read()` - Returns an specific fixture.
53+
54+
## Configuration
55+
56+
By default, the client is configured to request to http://localhost:3100/admin, based in the [default options of @mocks-server](https://www.mocks-server.org/docs/configuration-options)
57+
58+
You can change both the base url of the "@mocks-server", and the base api path of the "@mocks-server/plugin-admin-api" using the `config` method:
59+
60+
```js
61+
import { config } from "@mocks-server/admin-api-client";
2062

21-
changeDelay(1000).then(() => {
22-
console.log("Delay setting changed!");
63+
config({
64+
apiPath: "/foo-admin",
65+
baseUrl: "http://my-mocks-server:3000"
2366
});
2467
```
2568

index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
import configuration from "./src/config";
1+
export * from "./src/config";
22
export * from "./src/providers";
3-
4-
export const config = configuration;

src/config.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { instances } from "@data-provider/core";
2+
import { DEFAULT_BASE_PATH } from "@mocks-server/admin-api-paths";
23
import TAG from "./tag";
34

45
const DEFAULT_OPTIONS = {
5-
apiPath: "/admin",
6+
apiPath: DEFAULT_BASE_PATH,
67
baseUrl: "http://localhost:3100"
78
};
89

9-
const config = options => {
10+
export const config = options => {
1011
const finalOptions = {
1112
...DEFAULT_OPTIONS,
1213
...options
@@ -18,5 +19,3 @@ const config = options => {
1819
};
1920

2021
config();
21-
22-
export default config;

src/providers.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export const settings = new Api(SETTINGS, {
1616
tags: [TAG]
1717
});
1818

19-
export const behaviorsCollection = new Api(BEHAVIORS, {
19+
export const behaviors = new Api(BEHAVIORS, {
2020
defaultValue: [],
21-
uuid: "behaviors-collection",
21+
uuid: "behaviors",
2222
tags: [TAG]
2323
});
2424

@@ -29,16 +29,20 @@ export const behaviorsModel = new Api(`${BEHAVIORS}/:name`, {
2929
});
3030

3131
behaviorsModel.addCustomQuery({
32-
findByName: name => ({
32+
byName: name => ({
3333
urlParams: {
3434
name
3535
}
3636
})
3737
});
3838

39-
export const fixturesCollection = new Api(FIXTURES, {
39+
export const behavior = name => {
40+
return behaviorsModel.byName(name);
41+
};
42+
43+
export const fixtures = new Api(FIXTURES, {
4044
defaultValue: [],
41-
uuid: "fixtures-collection",
45+
uuid: "fixtures",
4246
tags: [TAG]
4347
});
4448

@@ -49,9 +53,13 @@ export const fixturesModel = new Api(`${FIXTURES}/:id`, {
4953
});
5054

5155
fixturesModel.addCustomQuery({
52-
findById: id => ({
56+
byId: id => ({
5357
urlParams: {
5458
id
5559
}
5660
})
5761
});
62+
63+
export const fixture = id => {
64+
return fixturesModel.byId(id);
65+
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { connect } from "@data-provider/connector-react";
2-
import { behaviorsCollection } from "mocks-server-admin-api-client";
2+
import { behaviors } from "mocks-server-admin-api-client";
33

44
import BehaviorsView from "./BehaviorsView";
55

66
const BehaviorsController = connect(() => ({
7-
behaviors: behaviorsCollection.read.getters.value
7+
behaviors: behaviors.read.getters.value
88
}))(BehaviorsView);
99

1010
export default BehaviorsController;

test-e2e/browser/react-app/src/modules/current-behavior/CurrentBehaviorController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const currentBehavior = new Selector(
99
{
1010
provider: behaviorsModel,
1111
query: (query, prevResults) => {
12-
return behaviorsModel.customQueries.findByName(prevResults[0].behavior);
12+
return behaviorsModel.customQueries.byName(prevResults[0].behavior);
1313
}
1414
},
1515
(settingsResults, behaviorResult) => {
@@ -25,7 +25,7 @@ const currentFixture = new Selector(
2525
{
2626
provider: fixturesModel,
2727
query: (query, prevResults) => {
28-
return fixturesModel.customQueries.findById(
28+
return fixturesModel.customQueries.byId(
2929
prevResults[0].fixtures[prevResults[0].fixtures.length - 1]
3030
);
3131
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { connect } from "@data-provider/connector-react";
2-
import { fixturesCollection } from "mocks-server-admin-api-client";
2+
import { fixtures } from "mocks-server-admin-api-client";
33

44
import FixturesView from "./FixturesView";
55

66
const FixturesController = connect(() => ({
7-
fixtures: fixturesCollection.read.getters.value
7+
fixtures: fixtures.read.getters.value
88
}))(FixturesView);
99

1010
export default FixturesController;

test-e2e/browser/vanilla-app/public/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ var loadSettings = function() {
7676
};
7777

7878
var loadBehaviors = function() {
79-
return adminApiClient.behaviorsCollection.read().then(function(behaviorsCollection) {
79+
return adminApiClient.behaviors.read().then(function(behaviorsCollection) {
8080
$behaviorsContainer.empty();
8181
behaviorsCollection.forEach(function(behavior) {
8282
$behaviorsContainer.append(
@@ -89,7 +89,7 @@ var loadBehaviors = function() {
8989
};
9090

9191
var loadFixtures = function() {
92-
return adminApiClient.fixturesCollection.read().then(function(fixturesCollection) {
92+
return adminApiClient.fixtures.read().then(function(fixturesCollection) {
9393
$fixturesContainer.empty();
9494
fixturesCollection.forEach(function(fixture) {
9595
$fixturesContainer.append(

test-e2e/nodejs/es6/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const {
22
about,
3-
behaviorsCollection,
4-
behaviorsModel,
5-
fixturesCollection,
6-
fixturesModel,
3+
behaviors,
4+
behavior,
5+
fixtures,
6+
fixture,
77
settings
88
} = require("../../../dist/index.cjs");
99

@@ -12,19 +12,19 @@ const readAbout = () => {
1212
};
1313

1414
const readBehaviors = () => {
15-
return behaviorsCollection.read();
15+
return behaviors.read();
1616
};
1717

1818
const readBehavior = name => {
19-
return behaviorsModel.findByName(name).read();
19+
return behavior(name).read();
2020
};
2121

2222
const readFixtures = () => {
23-
return fixturesCollection.read();
23+
return fixtures.read();
2424
};
2525

2626
const readFixture = id => {
27-
return fixturesModel.findById(id).read();
27+
return fixture(id).read();
2828
};
2929

3030
const readSettings = () => {

test/src/config.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import sinon from "sinon";
22
import { instances } from "@data-provider/core";
3+
import { DEFAULT_BASE_PATH } from "@mocks-server/admin-api-paths";
34

4-
import config from "../../src/config";
5+
import { config } from "../../src/config";
56
import TAG from "../../src/tag";
67

78
describe("config method", () => {
@@ -22,7 +23,9 @@ describe("config method", () => {
2223
config({
2324
baseUrl: BASE_URL
2425
});
25-
expect(instances.getByTag(TAG).config.getCall(0).args[0].baseUrl).toEqual(`${BASE_URL}/admin`);
26+
expect(instances.getByTag(TAG).config.getCall(0).args[0].baseUrl).toEqual(
27+
`${BASE_URL}${DEFAULT_BASE_PATH}`
28+
);
2629
});
2730

2831
it("should set apiPath in admin-api-client tagged providers", () => {

0 commit comments

Comments
 (0)