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

Commit a14bf26

Browse files
committed
feat: reject promises in case of response status different to 2xx
1 parent 3f40f5a commit a14bf26

File tree

7 files changed

+364
-149
lines changed

7 files changed

+364
-149
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"env": {
3-
"node": true
3+
"node": true,
4+
"es6": true
45
},
56
"parserOptions": {
67
"ecmaVersion": "2018",

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [unreleased]
88
### Added
9+
- feat: Expose one different method for each action.
10+
- feat: Reject promises in case of status code different to 2xx
911
### Changed
1012
- feat: Update mocks-server dependencies to v2 beta versions and adapt routes and tests.
1113
### Fixed
1214
### Removed
15+
- feat: Make private entities
16+
### Breaking change
17+
- Removed previous entity-based methods
1318

1419
## [2.1.0] - 2020-12-26
1520

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export { config } from "./src/entities";
12
export * from "./src/methods";

src/entities.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import fetch from "cross-fetch";
2+
3+
import {
4+
DEFAULT_BASE_PATH,
5+
SETTINGS,
6+
ABOUT,
7+
ALERTS,
8+
MOCKS,
9+
ROUTES,
10+
ROUTES_VARIANTS,
11+
MOCK_CUSTOM_ROUTES_VARIANTS,
12+
LEGACY,
13+
BEHAVIORS,
14+
FIXTURES,
15+
} from "@mocks-server/admin-api-paths";
16+
17+
const DEFAULT_OPTIONS = {
18+
apiPath: DEFAULT_BASE_PATH,
19+
baseUrl: "http://localhost:3100",
20+
};
21+
22+
let configuration = {
23+
...DEFAULT_OPTIONS,
24+
};
25+
26+
function handleResponse(res) {
27+
if (res.status > 199 && res.status < 300) {
28+
return res.json().catch(() => Promise.resolve());
29+
}
30+
return res.json().then((data) => {
31+
return Promise.reject(new Error(data.message));
32+
});
33+
}
34+
35+
export function config(options) {
36+
configuration = {
37+
...configuration,
38+
...options,
39+
};
40+
}
41+
42+
class Fetcher {
43+
constructor(url, id) {
44+
this._url = url;
45+
this._id = id ? `/${encodeURIComponent(id)}` : "";
46+
}
47+
48+
get url() {
49+
return `${configuration.baseUrl}${configuration.apiPath}${this._url}${this._id}`;
50+
}
51+
52+
_read() {
53+
return fetch(this.url).then(handleResponse);
54+
}
55+
56+
_patch(data) {
57+
return fetch(this.url, {
58+
method: "PATCH",
59+
body: JSON.stringify(data),
60+
headers: {
61+
"Content-Type": "application/json",
62+
},
63+
}).then(handleResponse);
64+
}
65+
66+
_delete() {
67+
return fetch(this.url, {
68+
method: "DELETE",
69+
}).then(handleResponse);
70+
}
71+
72+
_create(data) {
73+
return fetch(this.url, {
74+
method: "POST",
75+
body: JSON.stringify(data),
76+
headers: {
77+
"Content-Type": "application/json",
78+
},
79+
}).then(handleResponse);
80+
}
81+
82+
read() {
83+
return this._read();
84+
}
85+
86+
update(data) {
87+
return this._patch(data);
88+
}
89+
90+
delete() {
91+
return this._delete();
92+
}
93+
94+
create(data) {
95+
return this._create(data);
96+
}
97+
}
98+
99+
export const about = new Fetcher(ABOUT);
100+
101+
export const settings = new Fetcher(SETTINGS);
102+
103+
export const alerts = new Fetcher(ALERTS);
104+
105+
export const alert = (id) => {
106+
return new Fetcher(ALERTS, id);
107+
};
108+
109+
export const mocks = new Fetcher(MOCKS);
110+
111+
export const mock = (id) => {
112+
return new Fetcher(MOCKS, id);
113+
};
114+
115+
export const routes = new Fetcher(ROUTES);
116+
117+
export const route = (id) => {
118+
return new Fetcher(ROUTES, id);
119+
};
120+
121+
export const routesVariants = new Fetcher(ROUTES_VARIANTS);
122+
123+
export const routeVariant = (id) => {
124+
return new Fetcher(ROUTES_VARIANTS, id);
125+
};
126+
127+
export const mockCustomRoutesVariants = new Fetcher(MOCK_CUSTOM_ROUTES_VARIANTS);
128+
129+
// legacy methods
130+
131+
export const behaviors = new Fetcher(`${LEGACY}/${BEHAVIORS}`);
132+
133+
export const behavior = (name) => {
134+
return new Fetcher(`${LEGACY}/${BEHAVIORS}`, name);
135+
};
136+
137+
export const fixtures = new Fetcher(`${LEGACY}/${FIXTURES}`);
138+
139+
export const fixture = (id) => {
140+
return new Fetcher(`${LEGACY}/${FIXTURES}`, id);
141+
};

src/methods.js

Lines changed: 92 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,93 @@
1-
import fetch from "cross-fetch";
2-
31
import {
4-
DEFAULT_BASE_PATH,
5-
SETTINGS,
6-
ABOUT,
7-
ALERTS,
8-
/* MOCKS,
9-
ROUTES,
10-
ROUTES_VARIANTS,
11-
MOCK_CUSTOM_ROUTES_VARIANTS, */
12-
LEGACY,
13-
BEHAVIORS,
14-
FIXTURES,
15-
} from "@mocks-server/admin-api-paths";
16-
17-
const DEFAULT_OPTIONS = {
18-
apiPath: DEFAULT_BASE_PATH,
19-
baseUrl: "http://localhost:3100",
20-
};
21-
22-
let configuration = {
23-
...DEFAULT_OPTIONS,
24-
};
25-
26-
export const config = (options) => {
27-
configuration = {
28-
...configuration,
29-
...options,
30-
};
31-
};
32-
33-
class Fetcher {
34-
constructor(url, id) {
35-
this._url = url;
36-
this._id = id ? `/${encodeURIComponent(id)}` : "";
37-
}
38-
39-
get url() {
40-
return `${configuration.baseUrl}${configuration.apiPath}${this._url}${this._id}`;
41-
}
42-
43-
_read() {
44-
return fetch(this.url).then((res) => res.json());
45-
}
46-
47-
_patch(data) {
48-
return fetch(this.url, {
49-
method: "PATCH",
50-
body: JSON.stringify(data),
51-
headers: {
52-
"Content-Type": "application/json",
53-
},
54-
});
55-
}
56-
57-
read() {
58-
return this._read();
59-
}
60-
61-
update(data) {
62-
return this._patch(data);
63-
}
64-
}
65-
66-
export const about = new Fetcher(ABOUT);
67-
68-
export const settings = new Fetcher(SETTINGS);
69-
70-
export const behaviors = new Fetcher(`${LEGACY}/${BEHAVIORS}`);
71-
72-
export const behavior = (name) => {
73-
return new Fetcher(`${LEGACY}/${BEHAVIORS}`, name);
74-
};
75-
76-
export const fixtures = new Fetcher(`${LEGACY}/${FIXTURES}`);
77-
78-
export const fixture = (id) => {
79-
return new Fetcher(`${LEGACY}/${FIXTURES}`, id);
80-
};
81-
82-
export const alerts = new Fetcher(ALERTS);
83-
84-
export const alert = (id) => {
85-
return new Fetcher(ALERTS, id);
86-
};
2+
about,
3+
behaviors,
4+
behavior,
5+
fixtures,
6+
fixture,
7+
settings,
8+
alerts,
9+
alert,
10+
mocks,
11+
mock,
12+
routes,
13+
route,
14+
routesVariants,
15+
routeVariant,
16+
mockCustomRoutesVariants,
17+
} from "./entities";
18+
19+
export function readAbout() {
20+
return about.read();
21+
}
22+
23+
export function readSettings() {
24+
return settings.read();
25+
}
26+
27+
export function updateSettings(newSettings) {
28+
return settings.update(newSettings);
29+
}
30+
31+
export function readAlerts() {
32+
return alerts.read();
33+
}
34+
35+
export function readAlert(id) {
36+
return alert(id).read();
37+
}
38+
39+
export function readMocks() {
40+
return mocks.read();
41+
}
42+
43+
export function readMock(id) {
44+
return mock(id).read();
45+
}
46+
47+
export function readRoutes() {
48+
return routes.read();
49+
}
50+
51+
export function readRoute(id) {
52+
return route(id).read();
53+
}
54+
55+
export function readRoutesVariants() {
56+
return routesVariants.read();
57+
}
58+
59+
export function readRouteVariant(id) {
60+
return routeVariant(id).read();
61+
}
62+
63+
export function readMockCustomRoutesVariants() {
64+
return mockCustomRoutesVariants.read();
65+
}
66+
67+
export function addMockCustomRouteVariant(id) {
68+
return mockCustomRoutesVariants.create({
69+
id,
70+
});
71+
}
72+
73+
export function restoreMockRoutesVariants() {
74+
return mockCustomRoutesVariants.delete();
75+
}
76+
77+
// legacy
78+
79+
export function readBehaviors() {
80+
return behaviors.read();
81+
}
82+
83+
export function readBehavior(name) {
84+
return behavior(name).read();
85+
}
86+
87+
export function readFixtures() {
88+
return fixtures.read();
89+
}
90+
91+
export function readFixture(id) {
92+
return fixture(id).read();
93+
}

0 commit comments

Comments
 (0)