@forgepack/request 1.0.4 → 1.0.6
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/README.md +217 -86
- package/dist/api/client.d.ts +17 -10
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +18 -6
- package/dist/hooks/AuthContext.d.ts +6 -6
- package/dist/hooks/AuthContext.d.ts.map +1 -1
- package/dist/hooks/AuthContext.js +1 -1
- package/dist/hooks/AuthProvider.d.ts +34 -17
- package/dist/hooks/AuthProvider.d.ts.map +1 -1
- package/dist/hooks/AuthProvider.js +26 -15
- package/dist/hooks/useAuth.d.ts +33 -12
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +30 -12
- package/dist/hooks/useRequest.d.ts +37 -17
- package/dist/hooks/useRequest.d.ts.map +1 -1
- package/dist/hooks/useRequest.js +36 -13
- package/dist/index.d.ts +159 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +159 -14
- package/dist/services/api.d.ts +37 -15
- package/dist/services/api.d.ts.map +1 -1
- package/dist/services/api.js +40 -14
- package/dist/services/auth.d.ts +76 -19
- package/dist/services/auth.d.ts.map +1 -1
- package/dist/services/auth.js +94 -22
- package/dist/services/crud.d.ts +115 -62
- package/dist/services/crud.d.ts.map +1 -1
- package/dist/services/crud.js +132 -85
- package/dist/services/token.d.ts +83 -26
- package/dist/services/token.d.ts.map +1 -1
- package/dist/services/token.js +141 -52
- package/dist/types/auth.d.ts +20 -20
- package/dist/types/auth.d.ts.map +1 -1
- package/dist/types/error.d.ts +3 -3
- package/dist/types/error.d.ts.map +1 -1
- package/dist/types/request.d.ts +8 -8
- package/dist/types/request.d.ts.map +1 -1
- package/dist/types/response.d.ts +15 -15
- package/dist/types/response.d.ts.map +1 -1
- package/dist/types/token.d.ts +15 -15
- package/dist/types/token.d.ts.map +1 -1
- package/dist/utils/constants.d.ts +7 -7
- package/dist/utils/constants.js +7 -7
- package/package.json +70 -56
- package/dist/api/api.d.ts +0 -8
- package/dist/api/api.d.ts.map +0 -1
- package/dist/api/api.js +0 -35
- package/dist/component/auth.d.ts +0 -8
- package/dist/component/auth.d.ts.map +0 -1
- package/dist/component/auth.js +0 -9
- package/dist/component/errorMessage.d.ts +0 -6
- package/dist/component/errorMessage.d.ts.map +0 -1
- package/dist/component/errorMessage.js +0 -7
- package/dist/component/request.d.ts +0 -13
- package/dist/component/request.d.ts.map +0 -1
- package/dist/component/request.js +0 -12
- package/dist/component/response.d.ts +0 -13
- package/dist/component/response.d.ts.map +0 -1
- package/dist/component/response.js +0 -13
- package/dist/component/token.d.ts +0 -32
- package/dist/component/token.d.ts.map +0 -1
- package/dist/component/token.js +0 -16
- package/dist/hook/useProvider.d.ts +0 -13
- package/dist/hook/useProvider.d.ts.map +0 -1
- package/dist/hook/useProvider.js +0 -51
- package/dist/hook/useRequest.d.ts +0 -11
- package/dist/hook/useRequest.d.ts.map +0 -1
- package/dist/hook/useRequest.js +0 -38
- package/dist/hook/useRequireAuth.d.ts +0 -4
- package/dist/hook/useRequireAuth.d.ts.map +0 -1
- package/dist/hook/useRequireAuth.js +0 -21
- package/dist/service/crud.d.ts +0 -15
- package/dist/service/crud.d.ts.map +0 -1
- package/dist/service/crud.js +0 -116
- package/dist/service/fetchPage.d.ts +0 -5
- package/dist/service/fetchPage.d.ts.map +0 -1
- package/dist/service/fetchPage.js +0 -22
- package/dist/service/token.d.ts +0 -10
- package/dist/service/token.d.ts.map +0 -1
- package/dist/service/token.js +0 -93
package/dist/hooks/useAuth.d.ts
CHANGED
|
@@ -1,33 +1,54 @@
|
|
|
1
1
|
import { AuthContextType } from './AuthContext';
|
|
2
2
|
/**
|
|
3
|
-
* Hook
|
|
3
|
+
* Hook to access authentication context
|
|
4
4
|
*
|
|
5
|
-
* @returns
|
|
5
|
+
* @returns {AuthContextType} Authentication context with state and methods
|
|
6
|
+
* @throws {Error} If used outside of AuthProvider
|
|
6
7
|
*
|
|
7
8
|
* @example
|
|
8
9
|
* ```typescript
|
|
9
|
-
*
|
|
10
|
+
* function ProfilePage() {
|
|
11
|
+
* const { isAuthenticated, loginUser, logoutUser, role } = useAuth()
|
|
12
|
+
* if (!isAuthenticated) {
|
|
13
|
+
* return <p>Not authenticated</p>
|
|
14
|
+
* }
|
|
15
|
+
* return <div>User profile</div>
|
|
16
|
+
* }
|
|
10
17
|
* ```
|
|
11
18
|
*/
|
|
12
19
|
export declare const useAuth: () => AuthContextType;
|
|
13
20
|
/**
|
|
14
|
-
*
|
|
21
|
+
* Properties of the RequireAuth component
|
|
22
|
+
*/
|
|
23
|
+
export type RequireAuthProps = {
|
|
24
|
+
/** Array of roles allowed to access the route */
|
|
25
|
+
allowedRoles: string[];
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Route protection component that verifies authentication and authorization based on roles
|
|
15
29
|
*
|
|
16
|
-
*
|
|
17
|
-
* -
|
|
18
|
-
* -
|
|
19
|
-
* -
|
|
30
|
+
* Behavior:
|
|
31
|
+
* - If user has allowed role: renders content (Outlet)
|
|
32
|
+
* - If authenticated but without permission: redirects to /notAllowed
|
|
33
|
+
* - If not authenticated: redirects to /login
|
|
20
34
|
*
|
|
21
|
-
* @param props -
|
|
22
|
-
* @param props.allowedRoles - Array
|
|
23
|
-
* @returns
|
|
35
|
+
* @param {RequireAuthProps} props - Component properties
|
|
36
|
+
* @param {string[]} props.allowedRoles - Array of roles that have access to the route
|
|
37
|
+
* @returns {JSX.Element} Conditional navigation component
|
|
24
38
|
*
|
|
25
39
|
* @example
|
|
26
40
|
* ```tsx
|
|
41
|
+
* // Protecting admin route
|
|
27
42
|
* <Route path="/admin" element={<RequireAuth allowedRoles={['ADMIN']} />}>
|
|
28
43
|
* <Route index element={<AdminDashboard />} />
|
|
44
|
+
* <Route path="users" element={<UserManagement />} />
|
|
45
|
+
* </Route>
|
|
46
|
+
*
|
|
47
|
+
* // Multiple roles
|
|
48
|
+
* <Route path="/moderator" element={<RequireAuth allowedRoles={['ADMIN', 'MODERATOR']} />}>
|
|
49
|
+
* <Route index element={<ModeratorPanel />} />
|
|
29
50
|
* </Route>
|
|
30
51
|
* ```
|
|
31
52
|
*/
|
|
32
|
-
export declare const RequireAuth: ({ allowedRoles }:
|
|
53
|
+
export declare const RequireAuth: ({ allowedRoles }: RequireAuthProps) => import("react/jsx-runtime").JSX.Element;
|
|
33
54
|
//# sourceMappingURL=useAuth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAe,eAAe,EAAE,MAAM,eAAe,CAAA;AAE5D
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/hooks/useAuth.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAe,eAAe,EAAE,MAAM,eAAe,CAAA;AAE5D;;;;;;;;;;;;;;;;GAgBG;AAEH,eAAO,MAAM,OAAO,QAAO,eAM1B,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC3B,iDAAiD;IACjD,YAAY,EAAE,MAAM,EAAE,CAAA;CACzB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,eAAO,MAAM,WAAW,GAAI,kBAAkB,gBAAgB,4CAW7D,CAAA"}
|
package/dist/hooks/useAuth.js
CHANGED
|
@@ -6,35 +6,53 @@ const react_1 = require("react");
|
|
|
6
6
|
const react_router_dom_1 = require("react-router-dom");
|
|
7
7
|
const AuthContext_1 = require("./AuthContext");
|
|
8
8
|
/**
|
|
9
|
-
* Hook
|
|
9
|
+
* Hook to access authentication context
|
|
10
10
|
*
|
|
11
|
-
* @returns
|
|
11
|
+
* @returns {AuthContextType} Authentication context with state and methods
|
|
12
|
+
* @throws {Error} If used outside of AuthProvider
|
|
12
13
|
*
|
|
13
14
|
* @example
|
|
14
15
|
* ```typescript
|
|
15
|
-
*
|
|
16
|
+
* function ProfilePage() {
|
|
17
|
+
* const { isAuthenticated, loginUser, logoutUser, role } = useAuth()
|
|
18
|
+
* if (!isAuthenticated) {
|
|
19
|
+
* return <p>Not authenticated</p>
|
|
20
|
+
* }
|
|
21
|
+
* return <div>User profile</div>
|
|
22
|
+
* }
|
|
16
23
|
* ```
|
|
17
24
|
*/
|
|
18
25
|
const useAuth = () => {
|
|
19
|
-
|
|
26
|
+
const context = (0, react_1.useContext)(AuthContext_1.AuthContext);
|
|
27
|
+
if (!context) {
|
|
28
|
+
throw new Error('useAuth must be used within an AuthProvider');
|
|
29
|
+
}
|
|
30
|
+
return context;
|
|
20
31
|
};
|
|
21
32
|
exports.useAuth = useAuth;
|
|
22
33
|
/**
|
|
23
|
-
*
|
|
34
|
+
* Route protection component that verifies authentication and authorization based on roles
|
|
24
35
|
*
|
|
25
|
-
*
|
|
26
|
-
* -
|
|
27
|
-
* -
|
|
28
|
-
* -
|
|
36
|
+
* Behavior:
|
|
37
|
+
* - If user has allowed role: renders content (Outlet)
|
|
38
|
+
* - If authenticated but without permission: redirects to /notAllowed
|
|
39
|
+
* - If not authenticated: redirects to /login
|
|
29
40
|
*
|
|
30
|
-
* @param props -
|
|
31
|
-
* @param props.allowedRoles - Array
|
|
32
|
-
* @returns
|
|
41
|
+
* @param {RequireAuthProps} props - Component properties
|
|
42
|
+
* @param {string[]} props.allowedRoles - Array of roles that have access to the route
|
|
43
|
+
* @returns {JSX.Element} Conditional navigation component
|
|
33
44
|
*
|
|
34
45
|
* @example
|
|
35
46
|
* ```tsx
|
|
47
|
+
* // Protecting admin route
|
|
36
48
|
* <Route path="/admin" element={<RequireAuth allowedRoles={['ADMIN']} />}>
|
|
37
49
|
* <Route index element={<AdminDashboard />} />
|
|
50
|
+
* <Route path="users" element={<UserManagement />} />
|
|
51
|
+
* </Route>
|
|
52
|
+
*
|
|
53
|
+
* // Multiple roles
|
|
54
|
+
* <Route path="/moderator" element={<RequireAuth allowedRoles={['ADMIN', 'MODERATOR']} />}>
|
|
55
|
+
* <Route index element={<ModeratorPanel />} />
|
|
38
56
|
* </Route>
|
|
39
57
|
* ```
|
|
40
58
|
*/
|
|
@@ -3,35 +3,55 @@ import { Page } from '../types/response';
|
|
|
3
3
|
import { Search } from '../types/request';
|
|
4
4
|
import { AxiosInstance } from 'axios';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Return type of the useRequest hook
|
|
7
|
+
*/
|
|
8
|
+
export type UseRequestReturn = {
|
|
9
|
+
/** Paginated response data from the API */
|
|
10
|
+
response: Page;
|
|
11
|
+
/** Array of error messages if request failed */
|
|
12
|
+
error: ErrorMessage[];
|
|
13
|
+
/** Loading state indicator */
|
|
14
|
+
loading: boolean;
|
|
15
|
+
/** Function to manually trigger a new request */
|
|
16
|
+
request: () => Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* React hook for managing paginated HTTP requests with automatic state management
|
|
7
20
|
*
|
|
8
|
-
*
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
11
|
-
* -
|
|
12
|
-
* -
|
|
21
|
+
* Features:
|
|
22
|
+
* - Manages loading, data and error states automatically
|
|
23
|
+
* - Supports pagination and search/filtering
|
|
24
|
+
* - Automatic cancellation of pending requests on unmount or parameter change
|
|
25
|
+
* - Automatic re-execution when endpoint or search parameters change
|
|
26
|
+
* - Request deduplication via AbortController
|
|
13
27
|
*
|
|
14
|
-
* @param api -
|
|
15
|
-
* @param endpoint -
|
|
16
|
-
* @param search -
|
|
17
|
-
* @
|
|
28
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
29
|
+
* @param {string} endpoint - API endpoint path (without leading slash, e.g., 'users' or 'posts')
|
|
30
|
+
* @param {Search} [search] - Optional search and pagination parameters
|
|
31
|
+
* @param {number} [search.page] - Page number (zero-indexed)
|
|
32
|
+
* @param {number} [search.size] - Number of items per page
|
|
33
|
+
* @param {string} [search.value] - Search term for filtering results
|
|
34
|
+
* @returns {UseRequestReturn} Object containing response data, error state, loading indicator and manual request function
|
|
18
35
|
*
|
|
19
36
|
* @example
|
|
20
37
|
* ```typescript
|
|
38
|
+
* // Basic usage with pagination
|
|
21
39
|
* const { response, error, loading, request } = useRequest(
|
|
22
40
|
* apiClient,
|
|
23
41
|
* 'users',
|
|
42
|
+
* { page: 0, size: 10 }
|
|
43
|
+
* )
|
|
44
|
+
*
|
|
45
|
+
* // Access paginated data
|
|
46
|
+
* const { response, loading } = useRequest(
|
|
47
|
+
* apiClient,
|
|
48
|
+
* 'posts',
|
|
24
49
|
* { page: 0, size: 10, value: 'search term' }
|
|
25
50
|
* )
|
|
26
51
|
*
|
|
27
|
-
* // Re-
|
|
52
|
+
* // Re-execute manually to refresh data
|
|
28
53
|
* await request()
|
|
29
54
|
* ```
|
|
30
55
|
*/
|
|
31
|
-
export declare const useRequest: (api: AxiosInstance, endpoint: string, search?: Search) =>
|
|
32
|
-
response: Page<unknown>;
|
|
33
|
-
error: ErrorMessage[];
|
|
34
|
-
loading: boolean;
|
|
35
|
-
request: () => Promise<void>;
|
|
36
|
-
};
|
|
56
|
+
export declare const useRequest: (api: AxiosInstance, endpoint: string, search?: Search) => UseRequestReturn;
|
|
37
57
|
//# sourceMappingURL=useRequest.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useRequest.d.ts","sourceRoot":"","sources":["../../src/hooks/useRequest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAE7C,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAEzC,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAErC
|
|
1
|
+
{"version":3,"file":"useRequest.d.ts","sourceRoot":"","sources":["../../src/hooks/useRequest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAE7C,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAEzC,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAErC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC3B,2CAA2C;IAC3C,QAAQ,EAAE,IAAI,CAAA;IACd,gDAAgD;IAChD,KAAK,EAAE,YAAY,EAAE,CAAA;IACrB,8BAA8B;IAC9B,OAAO,EAAE,OAAO,CAAA;IAChB,iDAAiD;IACjD,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC/B,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,eAAO,MAAM,UAAU,GAAI,KAAK,aAAa,EAAE,UAAU,MAAM,EAAE,SAAS,MAAM,KAAG,gBAyClF,CAAA"}
|
package/dist/hooks/useRequest.js
CHANGED
|
@@ -5,28 +5,40 @@ const react_1 = require("react");
|
|
|
5
5
|
const constants_1 = require("../utils/constants");
|
|
6
6
|
const api_1 = require("../services/api");
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* React hook for managing paginated HTTP requests with automatic state management
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
* -
|
|
12
|
-
* -
|
|
13
|
-
* -
|
|
14
|
-
* -
|
|
10
|
+
* Features:
|
|
11
|
+
* - Manages loading, data and error states automatically
|
|
12
|
+
* - Supports pagination and search/filtering
|
|
13
|
+
* - Automatic cancellation of pending requests on unmount or parameter change
|
|
14
|
+
* - Automatic re-execution when endpoint or search parameters change
|
|
15
|
+
* - Request deduplication via AbortController
|
|
15
16
|
*
|
|
16
|
-
* @param api -
|
|
17
|
-
* @param endpoint -
|
|
18
|
-
* @param search -
|
|
19
|
-
* @
|
|
17
|
+
* @param {AxiosInstance} api - Configured Axios instance with authentication
|
|
18
|
+
* @param {string} endpoint - API endpoint path (without leading slash, e.g., 'users' or 'posts')
|
|
19
|
+
* @param {Search} [search] - Optional search and pagination parameters
|
|
20
|
+
* @param {number} [search.page] - Page number (zero-indexed)
|
|
21
|
+
* @param {number} [search.size] - Number of items per page
|
|
22
|
+
* @param {string} [search.value] - Search term for filtering results
|
|
23
|
+
* @returns {UseRequestReturn} Object containing response data, error state, loading indicator and manual request function
|
|
20
24
|
*
|
|
21
25
|
* @example
|
|
22
26
|
* ```typescript
|
|
27
|
+
* // Basic usage with pagination
|
|
23
28
|
* const { response, error, loading, request } = useRequest(
|
|
24
29
|
* apiClient,
|
|
25
30
|
* 'users',
|
|
31
|
+
* { page: 0, size: 10 }
|
|
32
|
+
* )
|
|
33
|
+
*
|
|
34
|
+
* // Access paginated data
|
|
35
|
+
* const { response, loading } = useRequest(
|
|
36
|
+
* apiClient,
|
|
37
|
+
* 'posts',
|
|
26
38
|
* { page: 0, size: 10, value: 'search term' }
|
|
27
39
|
* )
|
|
28
40
|
*
|
|
29
|
-
* // Re-
|
|
41
|
+
* // Re-execute manually to refresh data
|
|
30
42
|
* await request()
|
|
31
43
|
* ```
|
|
32
44
|
*/
|
|
@@ -35,8 +47,14 @@ const useRequest = (api, endpoint, search) => {
|
|
|
35
47
|
const [error, setError] = (0, react_1.useState)([constants_1.initialErrorMessage]);
|
|
36
48
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
37
49
|
const abortControllerRef = (0, react_1.useRef)(null);
|
|
50
|
+
/**
|
|
51
|
+
* Executes the API request with automatic cancellation of previous pending requests
|
|
52
|
+
*
|
|
53
|
+
* @throws {ErrorMessage} When the request fails
|
|
54
|
+
*/
|
|
38
55
|
const request = (0, react_1.useCallback)(async () => {
|
|
39
56
|
var _a;
|
|
57
|
+
/** Cancel any ongoing request */
|
|
40
58
|
(_a = abortControllerRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
41
59
|
const controller = new AbortController();
|
|
42
60
|
abortControllerRef.current = controller;
|
|
@@ -47,15 +65,20 @@ const useRequest = (api, endpoint, search) => {
|
|
|
47
65
|
setResponse(data);
|
|
48
66
|
}
|
|
49
67
|
catch (requestError) {
|
|
50
|
-
|
|
68
|
+
/** Ignore abort errors */
|
|
69
|
+
if (requestError.name !== 'AbortError') {
|
|
70
|
+
setError([requestError]);
|
|
71
|
+
}
|
|
51
72
|
}
|
|
52
73
|
finally {
|
|
53
74
|
setLoading(false);
|
|
54
75
|
abortControllerRef.current = null;
|
|
55
76
|
}
|
|
56
|
-
}, [endpoint, search]);
|
|
77
|
+
}, [api, endpoint, search]);
|
|
78
|
+
/** Automatically execute request on mount and when dependencies change */
|
|
57
79
|
(0, react_1.useEffect)(() => {
|
|
58
80
|
request();
|
|
81
|
+
/** Cleanup: cancel request on unmount or dependency change */
|
|
59
82
|
return () => { var _a; return (_a = abortControllerRef.current) === null || _a === void 0 ? void 0 : _a.abort(); };
|
|
60
83
|
}, [request]);
|
|
61
84
|
return { response, error, loading, request };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,29 +1,179 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
2
|
+
* @forgepack/request - Complete HTTP client with JWT authentication for React
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
* @module @forgepack/request
|
|
6
|
+
*
|
|
7
|
+
* @description
|
|
8
|
+
* Production-ready solution for HTTP requests and JWT authentication in React applications
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* **Quick Start**
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { createApiClient, AuthProvider, useAuth, useRequest } from '@forgepack/request'
|
|
14
|
+
*
|
|
15
|
+
* // 1. Create API client
|
|
16
|
+
* const api = createApiClient({
|
|
17
|
+
* baseURL: 'https://api.example.com',
|
|
18
|
+
* onUnauthorized: () => window.location.href = '/login'
|
|
19
|
+
* })
|
|
20
|
+
*
|
|
21
|
+
* // 2. Wrap app with AuthProvider
|
|
22
|
+
* function App() {
|
|
23
|
+
* return (
|
|
24
|
+
* <AuthProvider api={api}>
|
|
25
|
+
* <Router>
|
|
26
|
+
* <Routes />
|
|
27
|
+
* </Router>
|
|
28
|
+
* </AuthProvider>
|
|
29
|
+
* )
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* // 3. Use authentication
|
|
33
|
+
* function LoginPage() {
|
|
34
|
+
* const { loginUser } = useAuth()
|
|
35
|
+
*
|
|
36
|
+
* const handleLogin = async (credentials) => {
|
|
37
|
+
* const result = await loginUser(credentials)
|
|
38
|
+
* if (result.success) {
|
|
39
|
+
* navigate('/dashboard')
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* return <LoginForm onSubmit={handleLogin} />
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* // 4. Fetch data with hooks
|
|
47
|
+
* function UsersPage() {
|
|
48
|
+
* const { response, loading, error } = useRequest(
|
|
49
|
+
* api,
|
|
50
|
+
* 'users',
|
|
51
|
+
* { page: 0, size: 10 }
|
|
52
|
+
* )
|
|
53
|
+
*
|
|
54
|
+
* if (loading) return <Spinner />
|
|
55
|
+
* if (error[0]?.message) return <Error message={error[0].message} />
|
|
56
|
+
*
|
|
57
|
+
* return <UserList users={response.content} />
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* // 5. Protect routes
|
|
61
|
+
* <Route path="/admin" element={<RequireAuth allowedRoles={['ADMIN']} />}>
|
|
62
|
+
* <Route index element={<AdminDashboard />} />
|
|
63
|
+
* </Route>
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @see {@link https://forgepack.dev/packages/request | Complete Documentation}
|
|
67
|
+
* @see {@link https://github.com/forgepack/request | GitHub Repository}
|
|
68
|
+
* @see {@link https://www.npmjs.com/package/@forgepack/request | NPM Package}
|
|
69
|
+
*
|
|
70
|
+
* @fileoverview React package for managing HTTP requests with JWT authentication
|
|
71
|
+
* @author Marcelo Gadelha {@link https://github.com/gadelhati}
|
|
72
|
+
* @version 1.0.6
|
|
5
73
|
* @license Apache License 2.0
|
|
6
74
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* -
|
|
12
|
-
* -
|
|
75
|
+
* @remarks
|
|
76
|
+
* This package requires React 16.8+ (hooks support) and axios as peer dependencies
|
|
77
|
+
*
|
|
78
|
+
* Complete solution for JWT authentication in React applications:
|
|
79
|
+
* - Automatic JWT authentication with Axios interceptors
|
|
80
|
+
* - React hooks for requests and state management (useAuth, useRequest)
|
|
81
|
+
* - Authentication and authorization components (AuthProvider, RequireAuth)
|
|
82
|
+
* - Standardized CRUD operations with error handling
|
|
83
|
+
* - Token management with automatic expiration checks
|
|
84
|
+
* - Pagination and search support for API requests
|
|
85
|
+
* - Request cancellation via AbortController
|
|
86
|
+
* - TypeScript support with comprehensive type definitions
|
|
87
|
+
*/
|
|
88
|
+
/**
|
|
89
|
+
* API client creation and configuration utilities
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { createApiClient } from '@forgepack/request'
|
|
94
|
+
*
|
|
95
|
+
* const api = createApiClient({
|
|
96
|
+
* baseURL: process.env.REACT_APP_API_URL,
|
|
97
|
+
* onUnauthorized: () => navigate('/login'),
|
|
98
|
+
* onForbidden: () => toast.error('Access denied')
|
|
99
|
+
* })
|
|
100
|
+
* ```
|
|
13
101
|
*/
|
|
14
102
|
export * from './api/client';
|
|
103
|
+
/**
|
|
104
|
+
* Type definitions for authentication, errors, requests, responses and tokens
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* import type { Auth, LoginCredentials, ErrorMessage, Page } from '@forgepack/request'
|
|
109
|
+
*
|
|
110
|
+
* const credentials: LoginCredentials = {
|
|
111
|
+
* username: 'user@example.com',
|
|
112
|
+
* password: 'password123'
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
15
116
|
export * from './types/auth';
|
|
16
117
|
export * from './types/error';
|
|
17
118
|
export * from './types/request';
|
|
18
119
|
export * from './types/response';
|
|
19
120
|
export * from './types/token';
|
|
121
|
+
/**
|
|
122
|
+
* React hooks for authentication and request management
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* import { AuthProvider, useAuth, useRequest } from '@forgepack/request'
|
|
127
|
+
*
|
|
128
|
+
* // Authentication hook
|
|
129
|
+
* const { isAuthenticated, loginUser, logoutUser, role } = useAuth()
|
|
130
|
+
*
|
|
131
|
+
* // Request hook with pagination
|
|
132
|
+
* const { response, loading, error, request } = useRequest(
|
|
133
|
+
* api,
|
|
134
|
+
* 'posts',
|
|
135
|
+
* { page: 0, size: 20, sort: { key: 'createdAt', order: 'desc' } }
|
|
136
|
+
* )
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
20
139
|
export * from './hooks/AuthContext';
|
|
21
140
|
export * from './hooks/AuthProvider';
|
|
22
141
|
export * from './hooks/useAuth';
|
|
23
142
|
export * from './hooks/useRequest';
|
|
143
|
+
/**
|
|
144
|
+
* Service layer for API operations, authentication, CRUD and token management
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* import { login, create, retrieve, update, remove, fetchPage } from '@forgepack/request'
|
|
149
|
+
*
|
|
150
|
+
* // Authentication
|
|
151
|
+
* const result = await login(api, '/auth/login', credentials)
|
|
152
|
+
*
|
|
153
|
+
* // CRUD operations
|
|
154
|
+
* const newUser = await create(api, 'users', userData)
|
|
155
|
+
* const users = await retrieve(api, 'users', { page: 0, size: 10 })
|
|
156
|
+
* const updated = await update(api, 'users', { id: 1, name: 'New Name' })
|
|
157
|
+
* await remove(api, 'users', '1')
|
|
158
|
+
*
|
|
159
|
+
* // Paginated fetch
|
|
160
|
+
* const page = await fetchPage(api, 'posts', { page: 0, size: 20 })
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
24
163
|
export * from './services/api';
|
|
25
164
|
export * from './services/auth';
|
|
26
165
|
export * from './services/crud';
|
|
27
166
|
export * from './services/token';
|
|
167
|
+
/**
|
|
168
|
+
* Utility constants and default values
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* import { initialAuth, initialPage, initialErrorMessage } from '@forgepack/request'
|
|
173
|
+
*
|
|
174
|
+
* const [auth, setAuth] = useState(initialAuth)
|
|
175
|
+
* const [page, setPage] = useState(initialPage)
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
28
178
|
export * from './utils/constants';
|
|
29
179
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsFG;AAEH;;;;;;;;;;;;;GAaG;AACH,cAAc,cAAc,CAAA;AAE5B;;;;;;;;;;;;GAYG;AACH,cAAc,cAAc,CAAA;AAC5B,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,eAAe,CAAA;AAE7B;;;;;;;;;;;;;;;;;GAiBG;AACH,cAAc,qBAAqB,CAAA;AACnC,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,oBAAoB,CAAA;AAElC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAEhC;;;;;;;;;;GAUG;AACH,cAAc,mBAAmB,CAAA"}
|