axiomate 1.0.2 → 1.0.3
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.
Potentially problematic release.
This version of axiomate might be problematic. Click here for more details.
- package/package.json +1 -1
- package/src/_store.js +0 -6
- package/src/createApi.js +13 -17
- package/src/interceptors.js +4 -4
- package/src/useApi.js +6 -6
package/package.json
CHANGED
package/src/_store.js
CHANGED
package/src/createApi.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
const axios = require("axios");
|
|
2
2
|
const { _store } = require("./_store");
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Token getter — developer set karta hai apna logic
|
|
6
|
-
*/
|
|
7
4
|
let _getToken = () => localStorage.getItem("token");
|
|
8
5
|
|
|
9
6
|
/**
|
|
10
|
-
* setTokenGetter —
|
|
11
|
-
* @param {Function} fn - function
|
|
7
|
+
* setTokenGetter — Developer provider there function to get token
|
|
8
|
+
* @param {Function} fn - function who return the token string
|
|
12
9
|
*
|
|
13
10
|
* @example
|
|
14
11
|
* setTokenGetter(() => store.getState().auth.token);
|
|
@@ -21,13 +18,13 @@ const setTokenGetter = (fn) => {
|
|
|
21
18
|
* createApi — library ka core function
|
|
22
19
|
*
|
|
23
20
|
* @param {Object} config
|
|
24
|
-
* @param {string} config.baseUrl - Backend
|
|
25
|
-
* @param {Object} config.endpoints -
|
|
21
|
+
* @param {string} config.baseUrl - Backend base URL
|
|
22
|
+
* @param {Object} config.endpoints - All endpoints map
|
|
26
23
|
* @param {Object} [config.headers] - Extra default headers
|
|
27
24
|
* @param {number} [config.timeout] - Request timeout ms (default: 10000)
|
|
28
|
-
* @param {boolean} [config.autoToken] - Auto Bearer token
|
|
25
|
+
* @param {boolean} [config.autoToken] - Auto Bearer token (default: true)
|
|
29
26
|
*
|
|
30
|
-
* @returns {Object}
|
|
27
|
+
* @returns {Object}
|
|
31
28
|
*
|
|
32
29
|
* @example
|
|
33
30
|
* const api = createApi({
|
|
@@ -54,7 +51,7 @@ const createApi = (config) => {
|
|
|
54
51
|
console.warn("[axiomate] No endpoints defined.");
|
|
55
52
|
}
|
|
56
53
|
|
|
57
|
-
// Axios instance
|
|
54
|
+
// Create Axios instance
|
|
58
55
|
_store.instance = axios.create({
|
|
59
56
|
baseURL: baseUrl,
|
|
60
57
|
timeout,
|
|
@@ -75,7 +72,7 @@ const createApi = (config) => {
|
|
|
75
72
|
}
|
|
76
73
|
}
|
|
77
74
|
|
|
78
|
-
|
|
75
|
+
|
|
79
76
|
let finalConfig = axiosConfig;
|
|
80
77
|
for (const fn of _store.interceptors.request) {
|
|
81
78
|
finalConfig = fn(finalConfig) || finalConfig;
|
|
@@ -96,12 +93,11 @@ const createApi = (config) => {
|
|
|
96
93
|
return finalResponse;
|
|
97
94
|
},
|
|
98
95
|
(error) => {
|
|
99
|
-
|
|
96
|
+
|
|
100
97
|
for (const fn of _store.interceptors.error) {
|
|
101
98
|
fn(error);
|
|
102
99
|
}
|
|
103
100
|
|
|
104
|
-
// Meaningful error message banao
|
|
105
101
|
const status = error?.response?.status;
|
|
106
102
|
const message =
|
|
107
103
|
error?.response?.data?.message ||
|
|
@@ -118,7 +114,7 @@ const createApi = (config) => {
|
|
|
118
114
|
}
|
|
119
115
|
);
|
|
120
116
|
|
|
121
|
-
// ───
|
|
117
|
+
// ─── Convert endpoints map to functions ─────────────────────────────────
|
|
122
118
|
const api = {};
|
|
123
119
|
|
|
124
120
|
for (const [name, endpoint] of Object.entries(endpoints)) {
|
|
@@ -132,9 +128,9 @@ const createApi = (config) => {
|
|
|
132
128
|
/**
|
|
133
129
|
* Generated endpoint function
|
|
134
130
|
*
|
|
135
|
-
* @param {Object} [data]
|
|
136
|
-
* @param {Object} [params]
|
|
137
|
-
* @param {Object} [extraConfig]
|
|
131
|
+
* @param {Object} [data]
|
|
132
|
+
* @param {Object} [params]
|
|
133
|
+
* @param {Object} [extraConfig]
|
|
138
134
|
*/
|
|
139
135
|
api[name] = (data = {}, params = {}, extraConfig = {}) => {
|
|
140
136
|
const upperMethod = method.toUpperCase();
|
package/src/interceptors.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* interceptors.js
|
|
3
|
-
*
|
|
3
|
+
* Developers can register their custom interceptors here
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
const { _store } = require("./_store");
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* addRequestInterceptor
|
|
10
|
-
*
|
|
10
|
+
* This function runs before every request is sent
|
|
11
11
|
*
|
|
12
12
|
* @param {Function} fn - (axiosConfig) => axiosConfig
|
|
13
13
|
*
|
|
@@ -23,7 +23,7 @@ const addRequestInterceptor = (fn) => {
|
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* addResponseInterceptor
|
|
26
|
-
*
|
|
26
|
+
* This function runs after every successful response is received
|
|
27
27
|
*
|
|
28
28
|
* @param {Function} fn - (response) => response
|
|
29
29
|
*
|
|
@@ -39,7 +39,7 @@ const addResponseInterceptor = (fn) => {
|
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* addErrorInterceptor
|
|
42
|
-
*
|
|
42
|
+
* This function runs when any request fails
|
|
43
43
|
*
|
|
44
44
|
* @param {Function} fn - (error) => void
|
|
45
45
|
*
|
package/src/useApi.js
CHANGED
|
@@ -2,13 +2,13 @@ const { useState, useEffect, useCallback, useRef } = require("react");
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* useApi — React hook for API calls
|
|
5
|
-
* Loading, error, data
|
|
5
|
+
* Loading, error, data states are all automatic!
|
|
6
6
|
*
|
|
7
|
-
* @param {Function} apiFn - api.getUser
|
|
7
|
+
* @param {Function} apiFn - api.getUser like function
|
|
8
8
|
* @param {Object} [options]
|
|
9
|
-
* @param {any} [options.params] -
|
|
10
|
-
* @param {boolean} [options.immediate] -
|
|
11
|
-
* @param {any} [options.initialData] -
|
|
9
|
+
* @param {any} [options.params] - Parameters to pass to the function
|
|
10
|
+
* @param {boolean} [options.immediate] - Call immediately on mount (default: true)
|
|
11
|
+
* @param {any} [options.initialData] - Initial value for data
|
|
12
12
|
*
|
|
13
13
|
* @returns {{ data, loading, error, execute, reset }}
|
|
14
14
|
*
|
|
@@ -34,7 +34,7 @@ const useApi = (apiFn, options = {}) => {
|
|
|
34
34
|
const [loading, setLoading] = useState(immediate);
|
|
35
35
|
const [error, setError] = useState(null);
|
|
36
36
|
|
|
37
|
-
// Latest params ref — stale closure
|
|
37
|
+
// Latest params ref — to avoid stale closure
|
|
38
38
|
const paramsRef = useRef(params);
|
|
39
39
|
paramsRef.current = params;
|
|
40
40
|
|