-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugins.js
80 lines (75 loc) · 2.22 KB
/
plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { handleAction, combineActions } from 'redux-actions';
import { MANAGEMENT_API_URL } from 'config';
import { normalize, arrayOf } from 'normalizr';
import { invoke } from 'helpers/api';
import { createUrl } from 'helpers/url';
import { pluginsSchema } from 'schema';
export const fetchPlugins = (apiId, options) => invoke({
endpoint: createUrl(`${MANAGEMENT_API_URL}/apis/${apiId}/plugins`, options),
method: 'GET',
headers: {
'content-type': 'application/json',
},
types: ['plugins/FETCH_LIST_REQUEST', {
type: 'plugins/FETCH_LIST_SUCCESS',
payload: (action, state, res) => res.json().then(
json => normalize(json.data, arrayOf(pluginsSchema))
),
}, 'plugins/FETCH_LIST_FAILURE'],
});
export const bindPlugin = (apiId, body, options) => invoke({
endpoint: createUrl(`${MANAGEMENT_API_URL}/apis/${apiId}/plugins/${body.name}`, options),
method: 'PUT',
headers: {
'content-type': 'application/json',
},
body: {
plugin: body,
},
types: ['plugins/BIND_REQUEST', {
type: 'plugins/BIND_SUCCESS',
payload: (action, state, res) => res.json().then(
json => normalize(json.data, arrayOf(pluginsSchema))
),
}, 'plugins/BIND_FAILURE'],
});
export const updatePlugin = (apiId, name, body, options) => invoke({
endpoint: createUrl(`${MANAGEMENT_API_URL}/apis/${apiId}/plugins/${name}`, options),
method: 'PUT',
headers: {
'content-type': 'application/json',
},
body: {
plugin: body,
},
types: ['plugins/UPDATE_REQUEST', {
type: 'plugins/UPDATE_SUCCESS',
payload: (action, state, res) => res.json().then(
json => normalize(json.data, pluginsSchema)
),
}, 'plugins/UPDATE_FAILURE'],
});
export const unbindPlugin = (apiId, name, options) => invoke({
endpoint: createUrl(`${MANAGEMENT_API_URL}/apis/${apiId}/plugins/${name}`, options),
method: 'DELETE',
headers: {
'content-type': 'application/json',
},
types: [
'plugins/DELETE_REQUEST',
'plugins/DELETE_SUCCESS',
'plugins/DELETE_FAILURE',
],
});
export default handleAction(
combineActions(
'plugins/FETCH_LIST_SUCCESS',
'plugins/BIND_SUCCESS',
'plugins/UPDATE_SUCCESS'
),
(state, action) => ({
...state,
...action.payload.entities.plugins,
}),
{}
);