@acrool/react-fetcher 0.0.2-alpha.2 → 0.0.2-alpha.4

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 CHANGED
@@ -1,11 +1,13 @@
1
1
  # Acrool React Fetcher
2
2
 
3
+ Acrool React Fetcher is a solution for API integration and Auth state management in React projects. It helps you easily manage tokens, make API requests, perform GraphQL queries, and handle authentication flows.
4
+
3
5
  <a href="https://acrool-react-fetcher.pages.dev/" title="Acrool React Fetcher - This is a block function for React development loading block">
4
6
  <img src="https://raw.githubusercontent.com/acrool/acrool-react-fetcher/main/example/public/og.webp" alt="Acrool React Fetcher Logo"/>
5
7
  </a>
6
8
 
7
9
  <p align="center">
8
- This is a toast message function for React development notifications
10
+ A solution for API integration and token management in React projects
9
11
  </p>
10
12
 
11
13
  <div align="center">
@@ -20,80 +22,108 @@
20
22
  </div>
21
23
 
22
24
 
23
- `^1.1.0 support react >=18.0.0 <20.0.0`
24
-
25
-
26
-
27
25
  ## Features
28
26
 
29
- - Supports queue block list
30
- - Plug and unplug using `@acrool/react-portal` and `framer-motion`
27
+ - Token state management and custom refresh mechanism
28
+ - GraphQL query support and custom fetcher
29
+ - Seamless integration with Redux Toolkit Query
30
+ - Flexible provider composition
31
+ - Easy to test and simulate login/logout/token invalidation scenarios
31
32
 
32
- ## Install
33
+ ## Installation
33
34
 
34
35
  ```bash
35
36
  yarn add @acrool/react-fetcher
36
37
  ```
37
38
 
39
+ ## Quick Start
38
40
 
39
- ## Usage
41
+ ### 1. Import styles
40
42
 
41
- add in your index.tsx
42
- ```tst
43
+ Add the following to your entry file (e.g. `index.tsx`):
44
+
45
+ ```ts
43
46
  import "@acrool/react-fetcher/dist/index.css";
44
47
  ```
45
48
 
46
- add in your App.tsx
49
+ ### 2. Provider structure
50
+
51
+ Wrap your app with `AuthStateProvider` and `AxiosClientProvider`. It is recommended to use `AppFetcherProvider` to automatically wrap all necessary providers:
47
52
 
48
53
  ```tsx
49
- import {BlockPortal} from "@acrool/react-fetcher";
50
-
51
- const App = () => {
52
- return (
53
- <div>
54
- <BaseUsed/>
55
- <BlockPortal
56
- isVisibleQueueKey={false}
57
- loader={<Loader/>}
58
- defaultMessage="Loading..."
59
- />
60
- </div>
61
- );
62
- };
54
+ import AppFetcherProvider from '@/library/acrool-react-fetcher';
55
+
56
+ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
57
+ <AppFetcherProvider>
58
+ <App />
59
+ </AppFetcherProvider>
60
+ );
61
+ ```
62
+
63
+ ### 3. Create baseApi (GraphQL query)
64
+
65
+ For Redux Toolkit Query, create `baseApi.ts`:
66
+
67
+ ```ts
68
+ import { createGraphQLFetcher } from '@acrool/react-fetcher';
69
+ import { createApi } from '@reduxjs/toolkit/query/react';
70
+ import { axiosInstance } from '@/library/acrool-react-fetcher';
71
+
72
+ export const baseApi = createApi({
73
+ reducerPath: 'api',
74
+ baseQuery: async (query, api, extraOptions) => {
75
+ // Token handling and refresh are managed automatically
76
+ const data = await createGraphQLFetcher(axiosInstance, query.document)(query.args);
77
+ return { data };
78
+ },
79
+ endpoints: () => ({}),
80
+ });
63
81
  ```
64
82
 
65
- then in your page
83
+ ### 4. Use Auth state and API in your pages
84
+
85
+ #### Get and update tokens
66
86
 
67
87
  ```tsx
68
- import {block} from '@acrool/react-fetcher';
69
- import {useEffect} from "react";
70
-
71
- const Example = () => {
72
-
73
- useEffect(() => {
74
- block.show();
75
-
76
- setTimeout(() => {
77
- block.hide();
78
- }, 3000)
79
- }, []);
80
-
81
- return (
82
- <div>
83
- sample page
84
- </div>
85
- );
88
+ import { useAuthState } from '@acrool/react-fetcher';
89
+
90
+ const { getTokens, updateTokens } = useAuthState();
91
+ ```
92
+
93
+ #### Send GraphQL query
94
+
95
+ ```tsx
96
+ const { data, refetch } = useGetBookmarkQuery({ variables: { bookmarkId: '1' } });
97
+ ```
98
+
99
+ #### Simulate token invalidation and refresh
100
+
101
+ ```tsx
102
+ const handleMockTokenInvalid = () => {
103
+ updateTokens(curr => ({
104
+ ...curr,
105
+ accessToken: 'mock-invalid-token',
106
+ }));
107
+ refetch();
86
108
  };
87
109
  ```
88
110
 
89
- - block.show
90
- - block.hide
111
+ #### Login/Logout
91
112
 
113
+ ```tsx
114
+ const login = useLogin();
115
+ const logout = useLogout();
116
+
117
+ await login({ variables: { input: { account, password } } });
118
+ logout();
119
+ ```
92
120
 
93
- There is also a example that you can play with it:
121
+ ### 5. More examples
94
122
 
95
- [![Play react-editext-example](https://raw.githubusercontent.com/acrool/acrool-react-fetcher/main/play-in-example-button.svg)](https://acrool-react-fetcher.pages.dev)
123
+ - The Dashboard page demonstrates how to operate token, API, and locale switching
124
+ - The Login page demonstrates login and error handling
96
125
 
126
+ ---
97
127
 
98
128
  ## License
99
129
 
@@ -1,21 +1,25 @@
1
- import { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
1
+ import { AxiosInstance } from 'axios';
2
2
  import { default as React } from 'react';
3
+ import { IRequestConfig } from '../fetchers/types';
3
4
  import { IResponseFirstError } from './types';
4
- export declare const AxiosClientContext: React.Context<AxiosInstance>;
5
- export declare const useAxiosClient: () => AxiosInstance;
5
+ export declare const AxiosClientContext: React.Context<AxiosInstance | null>;
6
+ export declare const useAxiosClient: () => null;
6
7
  interface IProps {
7
8
  children: React.ReactNode;
8
- i18nDict?: Record<string, Record<number, string>>;
9
- checkIsRefreshTokenRequest?: (config: InternalAxiosRequestConfig) => boolean;
10
- getLocale: () => string;
9
+ axiosInstance: AxiosInstance;
10
+ checkIsRefreshTokenRequest?: (config: IRequestConfig) => boolean;
11
+ locale?: string;
11
12
  onError?: (error: IResponseFirstError) => void;
13
+ authorizationPrefix?: string;
14
+ i18nDict?: Record<string, Record<number, string>>;
15
+ isDebug?: boolean;
12
16
  }
13
17
  /**
14
18
  * Axios Client 提供者
15
19
  * @param children
16
20
  * @param authTokensManager
17
- * @param getLocale
21
+ * @param locale
18
22
  * @param onError
19
23
  */
20
- declare const FetcherProvider: ({ children, getLocale, onError, i18nDict, checkIsRefreshTokenRequest, }: IProps) => import("react/jsx-runtime").JSX.Element;
24
+ declare const FetcherProvider: ({ children, axiosInstance, locale, onError, checkIsRefreshTokenRequest, authorizationPrefix, i18nDict, isDebug, }: IProps) => import("react/jsx-runtime").JSX.Element;
21
25
  export default FetcherProvider;