@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feardread/feature-factory",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "rollup -c",
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
- 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);
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 (error.response.status === 500) {
62
- return Promise.reject(error.response);
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
- return Promise.reject(error);
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;
@@ -8,30 +8,34 @@ const StateFactory = (entity, data = null) => ({
8
8
  error: null
9
9
  });
10
10
 
11
- const FeatureFactory = (name, endpoint, options = {}) => {
11
+ const FeatureFactory = (sliceName, endpoint, options = {}) => {
12
12
  const { initialData, service } = options;
13
- const apiurl = `${name}/${endpoint}`;
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
- apiurl,
20
+ apiEndpoint,
17
21
  async (_, { rejectWithValue }) => {
18
- return await API.get(apiurl)
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 = async (id) => {
31
- return createAsyncThunk(
32
- `${name}/${id}`,
33
- async (_, { rejectWithValue }) => {
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 = async (query) => {
48
- return createAsyncThunk(
49
- `${name}/search?${query}`,
50
- async (_, { rejectWithValue }) => {
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
- name,
66
- initialState: StateFactory(name, initialData),
66
+ sliceName,
67
+ initialState: StateFactory(sliceName, initialData),
67
68
  reducers: {
68
69
  clearData: (state) => {
69
- state[name] = null;
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[name] = action.payload;
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[name] = action.payload;
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[name] = action.payload;
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
- services: (service) ? service : {}
125
+ serviceActions: (service) ? service : {}
125
126
  },
126
127
 
127
128
  }