@feardread/feature-factory 1.1.6 → 1.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feardread/feature-factory",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "rollup -c",
@@ -0,0 +1,80 @@
1
+ import axios from "axios";
2
+ import qs from "qs";
3
+ import cache from "../cache/cache.js";
4
+
5
+
6
+
7
+
8
+ const InstanceFactory = ( 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;
53
+ }
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
+ }
68
+ }
69
+ return Promise.reject(error);
70
+ }
71
+ );
72
+
73
+ return instance;
74
+ }
75
+
76
+
77
+
78
+
79
+
80
+ export default InstanceFactory;
@@ -1,5 +1,5 @@
1
1
  import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
2
- import API from "../api/api.js";
2
+ import InstanceFactory from "../api/factory.js";
3
3
 
4
4
  const StateFactory = (entity, data = null) => ({
5
5
  [entity]: data,
@@ -8,30 +8,33 @@ const StateFactory = (entity, data = null) => ({
8
8
  error: null
9
9
  });
10
10
 
11
- const FeatureFactory = (name, endpoint, options = {}) => {
12
- const { initialData, service } = options;
13
- const apiurl = `${name}/${endpoint}`;
11
+ const FeatureFactory = (sliceName, endpoint, options = {}) => {
12
+ const apiEndpoint = `${sliceName}/${endpoint}`;
13
+ const apiInstance = InstanceFactory({
14
+ API_BASE_URL: 'http://fear.master.com:4000/fear/api',
15
+ JWT_TOKEN: 'fear-x-token'
16
+ });
14
17
 
15
18
  const fetch = createAsyncThunk(
16
- apiurl,
19
+ apiEndpoint,
17
20
  async (_, { rejectWithValue }) => {
18
- return await API.get(apiurl)
21
+ return await apiInstance.get(apiEndpoint)
19
22
  .then((response) => {
20
23
  if (response.data && response.data.success) {
21
24
  return response.data.result;
22
25
  }
26
+ return response.data;
23
27
  })
24
28
  .catch((error) => {
25
- return rejectWithValue('Error :: ', error);
29
+ return rejectWithValue('Error :: ', error.message);
26
30
  })
27
31
  }
28
- )
32
+ );
29
33
 
30
- const fetchOne = async (id) => {
31
- return createAsyncThunk(
32
- `${name}/${id}`,
33
- async (_, { rejectWithValue }) => {
34
- return await API.get(`${name}/${id}`)
34
+ const fetchOne = createAsyncThunk(
35
+ `${sliceName}/one`,
36
+ async (id, { rejectWithValue }) => {
37
+ return await apiInstance.get(`${sliceName}/${id}`)
35
38
  .then((response) => {
36
39
  if (response.data && response.data.success) {
37
40
  return response.data.result;
@@ -40,33 +43,30 @@ const FeatureFactory = (name, endpoint, options = {}) => {
40
43
  .catch((error) => {
41
44
  return rejectWithValue('Error :: ', error);
42
45
  })
43
- }
44
- )
45
- }
46
+ }
47
+ );
46
48
 
47
- const search = async (query) => {
48
- return createAsyncThunk(
49
- `${name}/search?${query}`,
50
- async (_, { rejectWithValue }) => {
51
- return await API.get(`${name}/search?${query}`)
49
+ const search = createAsyncThunk(
50
+ `${sliceName}/search`,
51
+ async (query, { rejectWithValue }) => {
52
+ return await apiInstance.get(`${sliceName}/search?${query}`)
52
53
  .then((response) => {
53
54
  if (response.data && response.data.success) {
54
55
  return response.data.result;
55
56
  }
56
57
  })
57
58
  .catch((error) => {
58
- return rejectWithValue('Error :: ', error);
59
+ return rejectWithValue('Error :: ', error.message);
59
60
  })
60
- }
61
- )
62
- }
61
+ }
62
+ );
63
63
 
64
64
  const slice = createSlice({
65
- name,
66
- initialState: StateFactory(name, initialData),
65
+ sliceName,
66
+ initialState: StateFactory(sliceName, initialData),
67
67
  reducers: {
68
68
  clearData: (state) => {
69
- state[name] = null;
69
+ state[sliceName] = null;
70
70
  }
71
71
  },
72
72
  extraReducers: (builder) => {
@@ -77,25 +77,51 @@ const FeatureFactory = (name, endpoint, options = {}) => {
77
77
  .addCase(fetch.fulfilled, (state, action) => {
78
78
  state.loading = false;
79
79
  state.success = true;
80
- state[name] = action.payload;
80
+ state[sliceName] = action.payload;
81
81
  })
82
82
  .addCase(fetch.rejected, (state, action) => {
83
83
  state.error = action.error;
84
84
  state.loading = false;
85
85
  state.success = false;
86
86
  })
87
+ .addCase(fetchOne.pending, (state) => {
88
+ state.loading = true;
89
+ })
90
+ .addCase(fetchOne.fulfilled, (state, action) => {
91
+ state.loading = false;
92
+ state.success = true;
93
+ state[sliceName] = action.payload;
94
+ })
95
+ .addCase(fetchOne.rejected, (state, action) => {
96
+ state.error = action.error;
97
+ state.loading = false;
98
+ state.success = false;
99
+ })
100
+ .addCase(search.pending, (state) => {
101
+ state.loading = true;
102
+ })
103
+ .addCase(search.fulfilled, (state, action) => {
104
+ state.loading = false;
105
+ state.success = true;
106
+ state[sliceName] = action.payload;
107
+ })
108
+ .addCase(search.rejected, (state, action) => {
109
+ state.error = action.error;
110
+ state.loading = false;
111
+ state.success = false;
112
+ })
87
113
  }
88
114
  })
89
115
 
90
116
  return {
91
- API,
92
117
  reducer: slice.reducer,
93
118
  actions: slice.actions,
119
+ instance: apiInstance,
94
120
  asyncActions: {
95
121
  fetch,
96
122
  fetchOne,
97
123
  search,
98
- services: (service) ? service : {}
124
+ service: (options.service) ? options.service : null
99
125
  },
100
126
 
101
127
  }
package/src/index.js CHANGED
@@ -1,2 +1,5 @@
1
1
 
2
2
  export { default as FeatureFactory } from "./factory/factory.js";
3
+
4
+ export { default as InstanceFactory } from "./api/factory.js";
5
+
package/src/api/api.js DELETED
@@ -1,72 +0,0 @@
1
- import axios from "axios";
2
- import qs from "qs";
3
- import cache from "../cache/cache.js";
4
-
5
-
6
- const API_BASE_URL = (process.env.NODE_ENV === "production")
7
- ? "http://fear.master.com:4000/fear/api/"
8
- : "http://localhost:4000/fear/api/";
9
-
10
- const ACCESS_TOKEN_NAME = (process.env.JWT_TOKEN)
11
- ? process.env.JWT_TOKEN
12
- : "x-token";
13
-
14
- const instance = axios.create({
15
- baseURL: `${API_BASE_URL}`,
16
- headers: {
17
- Accept: "application/json",
18
- "Content-Type": "application/json",
19
- },
20
- paramsSerializer: (params) => {
21
- return qs.stringify(params, { indices: false });
22
- },
23
- credentials: true
24
- //httpsAgent: new https.Agent({ rejectUnauthorized: false })
25
- });
26
-
27
- instance.interceptors.request.use(
28
- (config) => {
29
- const isAuth = cache.local.get("auth") ? cache.local.get("auth") : null;
30
- let token = isAuth !== null ? isAuth.token : "";
31
-
32
- config.headers = {
33
- Authorization: `Bearer ${token}`,
34
- [ACCESS_TOKEN_NAME]: token
35
- };
36
-
37
- return config;
38
- },
39
- (error) => { Promise.reject(error) }
40
- );
41
-
42
- instance.interceptors.response.use(
43
- (response) => {
44
- console.log("API RES :: ", response);
45
- const messages = response.data.message;
46
-
47
- if (response.status === 200 || 203) {
48
- return response;
49
- }
50
- if (messages) return Promise.reject({ messages: [messages] });
51
-
52
- return Promise.reject({ messages: ["got errors"] });
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);
60
- }
61
- if (error.response.status === 500) {
62
- return Promise.reject(error.response);
63
- }
64
- }
65
- return Promise.reject(error);
66
- }
67
- );
68
-
69
-
70
- export const API = instance;
71
-
72
- export default API;