@feardread/feature-factory 1.1.5 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.min.js +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/api/api.js +68 -60
- package/src/factory/factory.js +32 -31
package/package.json
CHANGED
package/src/api/api.js
CHANGED
|
@@ -3,70 +3,78 @@ import qs from "qs";
|
|
|
3
3
|
import cache from "../cache/cache.js";
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
config
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
54
|
-
(error) => {
|
|
55
|
-
console.log("API ERROR :: ", error);
|
|
56
|
-
if (error.response) {
|
|
57
|
-
if (error.response.status === 401) {
|
|
58
|
-
cache.local.remove("auth");
|
|
59
|
-
return Promise.reject(error.response);
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const API = ( options ) => {
|
|
9
|
+
|
|
10
|
+
const API_BASE_URL = (options.API_BASE_URL)
|
|
11
|
+
? options.API_BASE_URL
|
|
12
|
+
: "http://fear.master.com:4000/fear/api/";
|
|
13
|
+
|
|
14
|
+
const ACCESS_TOKEN_NAME = (options.JWT_TOKEN)
|
|
15
|
+
? options.JWT_TOKEN
|
|
16
|
+
: "x-token";
|
|
17
|
+
|
|
18
|
+
const instance = axios.create({
|
|
19
|
+
baseURL: `${API_BASE_URL}`,
|
|
20
|
+
headers: {
|
|
21
|
+
Accept: "application/json",
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
},
|
|
24
|
+
paramsSerializer: (params) => {
|
|
25
|
+
return qs.stringify(params, { indices: false });
|
|
26
|
+
},
|
|
27
|
+
credentials: true
|
|
28
|
+
//httpsAgent: new https.Agent({ rejectUnauthorized: false })
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
instance.interceptors.request.use(
|
|
32
|
+
(config) => {
|
|
33
|
+
const isAuth = cache.local.get("auth") ? cache.local.get("auth") : null;
|
|
34
|
+
let token = isAuth !== null ? isAuth.token : "";
|
|
35
|
+
|
|
36
|
+
config.headers = {
|
|
37
|
+
Authorization: `Bearer ${token}`,
|
|
38
|
+
[ACCESS_TOKEN_NAME]: token
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
return config;
|
|
42
|
+
},
|
|
43
|
+
(error) => { Promise.reject(error) }
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
instance.interceptors.response.use(
|
|
47
|
+
(response) => {
|
|
48
|
+
console.log("API RES :: ", response);
|
|
49
|
+
const messages = response.data.message;
|
|
50
|
+
|
|
51
|
+
if (response.status === 200 || 203) {
|
|
52
|
+
return response;
|
|
60
53
|
}
|
|
61
|
-
if (
|
|
62
|
-
|
|
54
|
+
if (messages) return Promise.reject({ messages: [messages] });
|
|
55
|
+
|
|
56
|
+
return Promise.reject({ messages: ["got errors"] });
|
|
57
|
+
},
|
|
58
|
+
(error) => {
|
|
59
|
+
console.log("API ERROR :: ", error);
|
|
60
|
+
if (error.response) {
|
|
61
|
+
if (error.response.status === 401) {
|
|
62
|
+
cache.local.remove("auth");
|
|
63
|
+
return Promise.reject(error.response);
|
|
64
|
+
}
|
|
65
|
+
if (error.response.status === 500) {
|
|
66
|
+
return Promise.reject(error.response);
|
|
67
|
+
}
|
|
63
68
|
}
|
|
69
|
+
return Promise.reject(error);
|
|
64
70
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return instance;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
68
77
|
|
|
69
78
|
|
|
70
|
-
export const API = instance;
|
|
71
79
|
|
|
72
80
|
export default API;
|
package/src/factory/factory.js
CHANGED
|
@@ -8,30 +8,34 @@ const StateFactory = (entity, data = null) => ({
|
|
|
8
8
|
error: null
|
|
9
9
|
});
|
|
10
10
|
|
|
11
|
-
const FeatureFactory = (
|
|
11
|
+
const FeatureFactory = (sliceName, endpoint, options = {}) => {
|
|
12
12
|
const { initialData, service } = options;
|
|
13
|
-
const
|
|
13
|
+
const apiEndpoint = `${sliceName}/${endpoint}`;
|
|
14
|
+
const apiInstance = API({
|
|
15
|
+
API_BASE_URL: 'http://fear.master.com:4000/fear/api',
|
|
16
|
+
JWT_TOKEN: 'fear-x-token'
|
|
17
|
+
});
|
|
14
18
|
|
|
15
19
|
const fetch = createAsyncThunk(
|
|
16
|
-
|
|
20
|
+
apiEndpoint,
|
|
17
21
|
async (_, { rejectWithValue }) => {
|
|
18
|
-
return await
|
|
22
|
+
return await apiInstance.get(apiEndpoint)
|
|
19
23
|
.then((response) => {
|
|
20
24
|
if (response.data && response.data.success) {
|
|
21
25
|
return response.data.result;
|
|
22
26
|
}
|
|
27
|
+
return response.data;
|
|
23
28
|
})
|
|
24
29
|
.catch((error) => {
|
|
25
|
-
return rejectWithValue('Error :: ', error);
|
|
30
|
+
return rejectWithValue('Error :: ', error.message);
|
|
26
31
|
})
|
|
27
32
|
}
|
|
28
|
-
)
|
|
33
|
+
);
|
|
29
34
|
|
|
30
|
-
const fetchOne =
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return await API.get(`${name}/${id}`)
|
|
35
|
+
const fetchOne = createAsyncThunk(
|
|
36
|
+
`${name}/one`,
|
|
37
|
+
async (id, { rejectWithValue }) => {
|
|
38
|
+
return await apiInstance.get(`${sliceName}/${id}`)
|
|
35
39
|
.then((response) => {
|
|
36
40
|
if (response.data && response.data.success) {
|
|
37
41
|
return response.data.result;
|
|
@@ -40,33 +44,30 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
40
44
|
.catch((error) => {
|
|
41
45
|
return rejectWithValue('Error :: ', error);
|
|
42
46
|
})
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
47
|
+
}
|
|
48
|
+
);
|
|
46
49
|
|
|
47
|
-
const search =
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return await API.get(`${name}/search?${query}`)
|
|
50
|
+
const search = createAsyncThunk(
|
|
51
|
+
`${sliceName}/search`,
|
|
52
|
+
async (query, { rejectWithValue }) => {
|
|
53
|
+
return await apiInstance.get(`${sliceName}/search?${query}`)
|
|
52
54
|
.then((response) => {
|
|
53
55
|
if (response.data && response.data.success) {
|
|
54
56
|
return response.data.result;
|
|
55
57
|
}
|
|
56
58
|
})
|
|
57
59
|
.catch((error) => {
|
|
58
|
-
return rejectWithValue('Error :: ', error);
|
|
60
|
+
return rejectWithValue('Error :: ', error.message);
|
|
59
61
|
})
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
62
|
+
}
|
|
63
|
+
);
|
|
63
64
|
|
|
64
65
|
const slice = createSlice({
|
|
65
|
-
|
|
66
|
-
initialState: StateFactory(
|
|
66
|
+
sliceName,
|
|
67
|
+
initialState: StateFactory(sliceName, initialData),
|
|
67
68
|
reducers: {
|
|
68
69
|
clearData: (state) => {
|
|
69
|
-
state[
|
|
70
|
+
state[sliceName] = null;
|
|
70
71
|
}
|
|
71
72
|
},
|
|
72
73
|
extraReducers: (builder) => {
|
|
@@ -77,7 +78,7 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
77
78
|
.addCase(fetch.fulfilled, (state, action) => {
|
|
78
79
|
state.loading = false;
|
|
79
80
|
state.success = true;
|
|
80
|
-
state[
|
|
81
|
+
state[sliceName] = action.payload;
|
|
81
82
|
})
|
|
82
83
|
.addCase(fetch.rejected, (state, action) => {
|
|
83
84
|
state.error = action.error;
|
|
@@ -90,7 +91,7 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
90
91
|
.addCase(fetchOne.fulfilled, (state, action) => {
|
|
91
92
|
state.loading = false;
|
|
92
93
|
state.success = true;
|
|
93
|
-
state[
|
|
94
|
+
state[sliceName] = action.payload;
|
|
94
95
|
})
|
|
95
96
|
.addCase(fetchOne.rejected, (state, action) => {
|
|
96
97
|
state.error = action.error;
|
|
@@ -103,7 +104,7 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
103
104
|
.addCase(search.fulfilled, (state, action) => {
|
|
104
105
|
state.loading = false;
|
|
105
106
|
state.success = true;
|
|
106
|
-
state[
|
|
107
|
+
state[sliceName] = action.payload;
|
|
107
108
|
})
|
|
108
109
|
.addCase(search.rejected, (state, action) => {
|
|
109
110
|
state.error = action.error;
|
|
@@ -114,14 +115,14 @@ const FeatureFactory = (name, endpoint, options = {}) => {
|
|
|
114
115
|
})
|
|
115
116
|
|
|
116
117
|
return {
|
|
117
|
-
API,
|
|
118
|
+
API: API({ API_BASE_URL: 'http://fear.master.com:4000/fear/api' }),
|
|
118
119
|
reducer: slice.reducer,
|
|
119
120
|
actions: slice.actions,
|
|
120
121
|
asyncActions: {
|
|
121
122
|
fetch,
|
|
122
123
|
fetchOne,
|
|
123
124
|
search,
|
|
124
|
-
|
|
125
|
+
serviceActions: (service) ? service : {}
|
|
125
126
|
},
|
|
126
127
|
|
|
127
128
|
}
|