@feardread/feature-factory 1.1.2
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/.env +0 -0
- package/dist/bundle.min.js +1 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
- package/rollup.config.mjs +44 -0
- package/src/api/api.js +72 -0
- package/src/cache/cache.js +71 -0
- package/src/factory/factory.js +81 -0
- package/src/index.js +2 -0
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@feardread/feature-factory",
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "rollup -c",
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "Garrett Haptonstall (FearDread)",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@reduxjs/toolkit": "^2.2.7",
|
|
13
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
14
|
+
"axios": "^1.7.7",
|
|
15
|
+
"react": "^18.3.1",
|
|
16
|
+
"react-dom": "^18.3.1",
|
|
17
|
+
"react-redux": "^9.1.2",
|
|
18
|
+
"react-toastify": "^10.0.5",
|
|
19
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
20
|
+
"rollup-plugin-polyfill-node": "^0.13.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@babel/preset-env": "^7.25.4",
|
|
24
|
+
"@babel/preset-react": "^7.24.7",
|
|
25
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
26
|
+
"@rollup/plugin-commonjs": "^28.0.0",
|
|
27
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
28
|
+
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
29
|
+
"rollup": "^4.22.4",
|
|
30
|
+
"rollup-plugin-postcss": "^4.0.2"
|
|
31
|
+
},
|
|
32
|
+
"description": ""
|
|
33
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import terser from '@rollup/plugin-terser';
|
|
2
|
+
import resolve from "@rollup/plugin-node-resolve";
|
|
3
|
+
import commonjs from "@rollup/plugin-commonjs";
|
|
4
|
+
import json from "@rollup/plugin-json";
|
|
5
|
+
import polyfill from "rollup-plugin-polyfill-node";
|
|
6
|
+
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
|
|
7
|
+
|
|
8
|
+
import pkg from "./package.json" assert { type: 'json' };
|
|
9
|
+
|
|
10
|
+
export default [
|
|
11
|
+
{
|
|
12
|
+
input: 'src/index.js',
|
|
13
|
+
output: [
|
|
14
|
+
{
|
|
15
|
+
file: 'dist/index.js',
|
|
16
|
+
format: 'cjs',
|
|
17
|
+
exports: 'named',
|
|
18
|
+
sourcemap: true,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
file: 'dist/index.esm.js',
|
|
22
|
+
format: "esm",
|
|
23
|
+
exports: 'named',
|
|
24
|
+
sourcemap: true,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
file: 'dist/bundle.min.js',
|
|
28
|
+
format: 'iife',
|
|
29
|
+
name: 'version',
|
|
30
|
+
plugins: [terser()]
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
plugins: [
|
|
34
|
+
peerDepsExternal(),
|
|
35
|
+
resolve({
|
|
36
|
+
browser: true,
|
|
37
|
+
preferBuiltins: false,
|
|
38
|
+
}),
|
|
39
|
+
commonjs(),
|
|
40
|
+
json(),
|
|
41
|
+
terser()
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
];
|
package/src/api/api.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
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;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
// cache
|
|
4
|
+
const cache = (options = {}) => {
|
|
5
|
+
var engine = options.type == 'local' ? 'localStorage' : 'sessionStorage';
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
check: () => {
|
|
9
|
+
if (!window[engine]) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return true;
|
|
13
|
+
},
|
|
14
|
+
set: (key, value) => {
|
|
15
|
+
if (!key) throw Error('Error:> Invalid key');
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
window[engine].setItem(key, JSON.stringify(value));
|
|
19
|
+
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error(`Error setting item ${key}:`, error);
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
},
|
|
26
|
+
get: (key) => {
|
|
27
|
+
try {
|
|
28
|
+
if ( key !== "undefined") {
|
|
29
|
+
const data = window[engine].getItem(key);
|
|
30
|
+
return data ? JSON.parse(data) : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(`Error getting item ${key}:`, error);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
remove: function (key) {
|
|
40
|
+
window[engine].removeItem(key);
|
|
41
|
+
},
|
|
42
|
+
clear: () => {
|
|
43
|
+
window[engine].clear();
|
|
44
|
+
},
|
|
45
|
+
keys: () => {
|
|
46
|
+
return Object.keys(window[engine]);
|
|
47
|
+
},
|
|
48
|
+
has: (key) => {
|
|
49
|
+
return window[engine].getItem(key) !== null;
|
|
50
|
+
},
|
|
51
|
+
/*
|
|
52
|
+
_extend: () => {
|
|
53
|
+
const destination = typeof arguments[0] === 'object' ? arguments[0] : {};
|
|
54
|
+
|
|
55
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
56
|
+
if (arguments[i] && typeof arguments[i] === 'object') {
|
|
57
|
+
for (var property in arguments[i])
|
|
58
|
+
destination[property] = arguments[i][property];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return destination;
|
|
63
|
+
}
|
|
64
|
+
*/
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
cache.local = cache({type: 'local'});
|
|
69
|
+
cache.session = cache({type: 'session'});
|
|
70
|
+
|
|
71
|
+
export default cache;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
|
2
|
+
import API from "../api/api.js";
|
|
3
|
+
|
|
4
|
+
const StateFactory = (entity, data = null) => ({
|
|
5
|
+
[entity]: data,
|
|
6
|
+
loading: false,
|
|
7
|
+
success: false,
|
|
8
|
+
error: null
|
|
9
|
+
});
|
|
10
|
+
const FeatureFactory = (name, endpoint, options = {}) => {
|
|
11
|
+
const { initialData, transform } = options;
|
|
12
|
+
const apiurl = `${name}/${endpoint}`;
|
|
13
|
+
|
|
14
|
+
const fetch = createAsyncThunk(
|
|
15
|
+
apiurl,
|
|
16
|
+
async (_, { rejectWithValue }) => {
|
|
17
|
+
return await API.get(apiurl)
|
|
18
|
+
.then((response) => {
|
|
19
|
+
console.log('factory resp = ', response);
|
|
20
|
+
if (response.data && response.data.success) {
|
|
21
|
+
return response.data.result;
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
.catch((error) => {
|
|
25
|
+
return rejectWithValue('Error :: ', error);
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const fetchOne = (id) => {
|
|
31
|
+
return createAsyncThunk(
|
|
32
|
+
`${name}/${id}`,
|
|
33
|
+
async (_, { rejectWithValue }) => {
|
|
34
|
+
return await API.get(`${name}/${id}`)
|
|
35
|
+
.then((response) => {
|
|
36
|
+
console.log('factory resp = ', response);
|
|
37
|
+
if (response.data && response.data.success) {
|
|
38
|
+
return response.data.result;
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
.catch((error) => {
|
|
42
|
+
return rejectWithValue('Error :: ', error);
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const slice = createSlice({
|
|
49
|
+
name,
|
|
50
|
+
initialState: StateFactory(name, initialData),
|
|
51
|
+
reducers: {},
|
|
52
|
+
extraReducers: (builder) => {
|
|
53
|
+
builder
|
|
54
|
+
.addCase(fetch.pending, (state) => {
|
|
55
|
+
state.loading = true;
|
|
56
|
+
})
|
|
57
|
+
.addCase(fetch.fulfilled, (state, action) => {
|
|
58
|
+
state.loading = false;
|
|
59
|
+
state.success = true;
|
|
60
|
+
state[name] = action.payload;
|
|
61
|
+
})
|
|
62
|
+
.addCase(fetch.rejected, (state, action) => {
|
|
63
|
+
state.error = action.error;
|
|
64
|
+
state.loading = false;
|
|
65
|
+
state.success = false;
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
reducer: slice.reducer,
|
|
72
|
+
actions: slice.actions,
|
|
73
|
+
asyncActions: {
|
|
74
|
+
fetch,
|
|
75
|
+
fetchOne
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export default FeatureFactory;
|
package/src/index.js
ADDED