axiomate 1.0.1 → 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/README.md +17 -17
- 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/README.md
CHANGED
|
@@ -16,12 +16,12 @@ npm install axiomate axios
|
|
|
16
16
|
|
|
17
17
|
## Quick Start
|
|
18
18
|
|
|
19
|
-
### 1. Setup (
|
|
19
|
+
### 1. Setup (once — in `index.js` or `App.js`)
|
|
20
20
|
|
|
21
21
|
```js
|
|
22
22
|
import { setTokenGetter, addErrorInterceptor } from "axiomate";
|
|
23
23
|
|
|
24
|
-
//
|
|
24
|
+
// Tell axiomate where to get the auth token
|
|
25
25
|
setTokenGetter(() => localStorage.getItem("token"));
|
|
26
26
|
|
|
27
27
|
// Global error handling
|
|
@@ -32,7 +32,7 @@ addErrorInterceptor((error) => {
|
|
|
32
32
|
});
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
### 2.
|
|
35
|
+
### 2. Define your API (one file — `api.config.js`)
|
|
36
36
|
|
|
37
37
|
```js
|
|
38
38
|
import { createApi } from "axiomate";
|
|
@@ -48,7 +48,7 @@ export const api = createApi({
|
|
|
48
48
|
});
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
### 3.
|
|
51
|
+
### 3. Use anywhere
|
|
52
52
|
|
|
53
53
|
```js
|
|
54
54
|
import { api } from "./api.config";
|
|
@@ -84,20 +84,20 @@ function ProfilePage() {
|
|
|
84
84
|
|
|
85
85
|
| Option | Type | Required | Default | Description |
|
|
86
86
|
|--------|------|----------|---------|-------------|
|
|
87
|
-
| `baseUrl` | string | ✅ | — |
|
|
88
|
-
| `endpoints` | object | ✅ | — |
|
|
87
|
+
| `baseUrl` | string | ✅ | — | Your backend base URL |
|
|
88
|
+
| `endpoints` | object | ✅ | — | Map of all endpoints |
|
|
89
89
|
| `headers` | object | ❌ | `{}` | Extra default headers |
|
|
90
|
-
| `timeout` | number | ❌ | `10000` |
|
|
91
|
-
| `autoToken` | boolean | ❌ | `true` | Auto Bearer token
|
|
90
|
+
| `timeout` | number | ❌ | `10000` | Request timeout in ms |
|
|
91
|
+
| `autoToken` | boolean | ❌ | `true` | Auto-attach Bearer token |
|
|
92
92
|
|
|
93
93
|
### `setTokenGetter(fn)`
|
|
94
|
-
|
|
94
|
+
Define how axiomate should retrieve the auth token.
|
|
95
95
|
```js
|
|
96
96
|
setTokenGetter(() => store.getState().auth.token);
|
|
97
97
|
```
|
|
98
98
|
|
|
99
99
|
### `addRequestInterceptor(fn)`
|
|
100
|
-
|
|
100
|
+
Run a function before every outgoing request.
|
|
101
101
|
```js
|
|
102
102
|
addRequestInterceptor((config) => {
|
|
103
103
|
config.headers["X-App-Version"] = "2.0";
|
|
@@ -106,7 +106,7 @@ addRequestInterceptor((config) => {
|
|
|
106
106
|
```
|
|
107
107
|
|
|
108
108
|
### `addResponseInterceptor(fn)`
|
|
109
|
-
|
|
109
|
+
Run a function after every successful response.
|
|
110
110
|
```js
|
|
111
111
|
addResponseInterceptor((response) => {
|
|
112
112
|
console.log("Response:", response.config.url);
|
|
@@ -115,7 +115,7 @@ addResponseInterceptor((response) => {
|
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
### `addErrorInterceptor(fn)`
|
|
118
|
-
|
|
118
|
+
Handle failed requests globally.
|
|
119
119
|
```js
|
|
120
120
|
addErrorInterceptor((error) => {
|
|
121
121
|
if (error.status === 403) alert("Access denied!");
|
|
@@ -126,17 +126,17 @@ addErrorInterceptor((error) => {
|
|
|
126
126
|
|
|
127
127
|
| Option | Type | Default | Description |
|
|
128
128
|
|--------|------|---------|-------------|
|
|
129
|
-
| `params` | object | `{}` | Default params |
|
|
130
|
-
| `immediate` | boolean | `true` |
|
|
131
|
-
| `initialData` | any | `null` | Initial data
|
|
129
|
+
| `params` | object | `{}` | Default params passed to the function |
|
|
130
|
+
| `immediate` | boolean | `true` | Call automatically on mount |
|
|
131
|
+
| `initialData` | any | `null` | Initial value for `data` |
|
|
132
132
|
|
|
133
133
|
**Returns:** `{ data, loading, error, execute, reset }`
|
|
134
134
|
|
|
135
135
|
```jsx
|
|
136
|
-
// Auto call
|
|
136
|
+
// Auto call on mount
|
|
137
137
|
const { data, loading, error } = useApi(api.getUser);
|
|
138
138
|
|
|
139
|
-
// Manual call
|
|
139
|
+
// Manual call (e.g. on form submit)
|
|
140
140
|
const { execute, loading } = useApi(api.login, { immediate: false });
|
|
141
141
|
await execute({ username, password });
|
|
142
142
|
```
|
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
|
|