@acrool/react-fetcher 0.0.2-alpha.1 → 0.0.2-alpha.10
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 +79 -49
- package/dist/FetcherProvider/FetcherProvider.d.ts +14 -9
- package/dist/FetcherProvider/index.d.ts +2 -1
- package/dist/FetcherProvider/types.d.ts +2 -1
- package/dist/FetcherProvider/utils.d.ts +9 -4
- package/dist/acrool-react-fetcher.es.js +2240 -2180
- package/dist/{FetcherProvider/config.d.ts → config/i18nDict.d.ts} +0 -1
- package/dist/createAxios.d.ts +2 -0
- package/dist/fetchers/config.d.ts +1 -0
- package/dist/fetchers/createGraphQLFetcher/createGraphQLFetcher.d.ts +11 -0
- package/dist/fetchers/createGraphQLFetcher/index.d.ts +2 -0
- package/dist/fetchers/createGraphQLFetcher/types.d.ts +8 -0
- package/dist/fetchers/createRestFulFetcher/config.d.ts +5 -0
- package/dist/fetchers/createRestFulFetcher/createRestFulFetcher.d.ts +11 -0
- package/dist/fetchers/createRestFulFetcher/index.d.ts +3 -0
- package/dist/fetchers/createRestFulFetcher/types.d.ts +11 -0
- package/dist/fetchers/createRestFulFetcher/utils.d.ts +13 -0
- package/dist/fetchers/index.d.ts +3 -2
- package/dist/fetchers/types.d.ts +9 -7
- package/dist/index.d.ts +1 -0
- package/package.json +1 -1
- package/dist/fetchers/graphqlFetcher.d.ts +0 -4
- /package/dist/fetchers/{utils.d.ts → createGraphQLFetcher/utils.d.ts} +0 -0
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
|
-
|
|
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
|
-
-
|
|
30
|
-
-
|
|
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
|
-
##
|
|
33
|
+
## Installation
|
|
33
34
|
|
|
34
35
|
```bash
|
|
35
36
|
yarn add @acrool/react-fetcher
|
|
36
37
|
```
|
|
37
38
|
|
|
39
|
+
## Quick Start
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
### 1. Import styles
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
83
|
+
### 4. Use Auth state and API in your pages
|
|
84
|
+
|
|
85
|
+
#### Get and update tokens
|
|
66
86
|
|
|
67
87
|
```tsx
|
|
68
|
-
import {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
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
|
-
|
|
121
|
+
### 5. More examples
|
|
94
122
|
|
|
95
|
-
|
|
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,26 @@
|
|
|
1
|
-
import { AxiosInstance
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
2
|
import { default as React } from 'react';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
export declare const
|
|
3
|
+
import { IInternalRequestConfig } from '../fetchers/types';
|
|
4
|
+
import { IFormatResponseErrorReturn, TGetResponseFormatError } from './types';
|
|
5
|
+
export declare const AxiosClientContext: React.Context<AxiosInstance | null>;
|
|
6
|
+
export declare const useAxiosClient: () => null;
|
|
6
7
|
interface IProps {
|
|
7
8
|
children: React.ReactNode;
|
|
9
|
+
axiosInstance: AxiosInstance;
|
|
10
|
+
checkIsRefreshTokenRequest?: (config: IInternalRequestConfig) => boolean;
|
|
11
|
+
locale?: string;
|
|
12
|
+
getResponseFormatError?: TGetResponseFormatError;
|
|
13
|
+
onError?: (error: IFormatResponseErrorReturn) => void;
|
|
14
|
+
authorizationPrefix?: string;
|
|
8
15
|
i18nDict?: Record<string, Record<number, string>>;
|
|
9
|
-
|
|
10
|
-
getLocale: () => string;
|
|
11
|
-
onError?: (error: IResponseFirstError) => void;
|
|
16
|
+
isDebug?: boolean;
|
|
12
17
|
}
|
|
13
18
|
/**
|
|
14
19
|
* Axios Client 提供者
|
|
15
20
|
* @param children
|
|
16
21
|
* @param authTokensManager
|
|
17
|
-
* @param
|
|
22
|
+
* @param locale
|
|
18
23
|
* @param onError
|
|
19
24
|
*/
|
|
20
|
-
declare const FetcherProvider: ({ children,
|
|
25
|
+
declare const FetcherProvider: ({ children, axiosInstance, locale, getResponseFormatError, onError, checkIsRefreshTokenRequest, authorizationPrefix, i18nDict, isDebug, }: IProps) => import("react/jsx-runtime").JSX.Element;
|
|
21
26
|
export default FetcherProvider;
|
|
@@ -2,9 +2,10 @@ import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
|
2
2
|
export type TInterceptorRequest = (value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>;
|
|
3
3
|
export type TInterceptorResponseSuccess = (value: AxiosResponse<any>) => AxiosResponse<any> | Promise<AxiosResponse<any>>;
|
|
4
4
|
export type TInterceptorResponseError = (error: AxiosError<any>) => AxiosResponse<any> | Promise<AxiosResponse<any>>;
|
|
5
|
-
export interface
|
|
5
|
+
export interface IFormatResponseErrorReturn {
|
|
6
6
|
message: string;
|
|
7
7
|
code: string;
|
|
8
8
|
path?: string;
|
|
9
9
|
args?: any;
|
|
10
10
|
}
|
|
11
|
+
export type TGetResponseFormatError = (response?: AxiosResponse) => IFormatResponseErrorReturn;
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IResponseFirstError } from './types';
|
|
1
|
+
import { TGetResponseFormatError } from './types';
|
|
3
2
|
/**
|
|
4
|
-
* 返回 Axios 格式錯誤
|
|
3
|
+
* 返回 Axios 格式錯誤 With GraphQL
|
|
4
|
+
* 針對 GraphQL多錯誤格式
|
|
5
5
|
* @param response
|
|
6
6
|
*/
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const getGraphQLResponseFormatError: TGetResponseFormatError;
|
|
8
|
+
/**
|
|
9
|
+
* 返回 Axios 格式錯誤 with RestFul
|
|
10
|
+
* @param response
|
|
11
|
+
*/
|
|
12
|
+
export declare const getRestFulResponseFormatError: TGetResponseFormatError;
|