@relia-fe/core 0.0.1 → 0.0.2
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/dist/api/index.d.mts +317 -0
- package/dist/api/index.d.ts +317 -0
- package/dist/api/index.js +749 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/index.mjs +700 -0
- package/dist/api/index.mjs.map +1 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +723 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +699 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +30 -3
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosInstance } from 'axios';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { PropsWithChildren } from 'react';
|
|
5
|
+
import * as swr__internal from 'swr/_internal';
|
|
6
|
+
import * as swr from 'swr';
|
|
7
|
+
import { SWRConfiguration } from 'swr';
|
|
8
|
+
import * as swr_mutation from 'swr/mutation';
|
|
9
|
+
import { SWRMutationConfiguration } from 'swr/mutation';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* API 客户端类型定义
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
interface BaseResponse<T> {
|
|
16
|
+
datetime?: string;
|
|
17
|
+
success: boolean;
|
|
18
|
+
code: number;
|
|
19
|
+
message?: string;
|
|
20
|
+
data?: T;
|
|
21
|
+
}
|
|
22
|
+
interface PaginationReq {
|
|
23
|
+
page?: number;
|
|
24
|
+
size?: number;
|
|
25
|
+
}
|
|
26
|
+
interface PaginationResp<T> {
|
|
27
|
+
items: T[];
|
|
28
|
+
total: number;
|
|
29
|
+
current: number;
|
|
30
|
+
size: number;
|
|
31
|
+
}
|
|
32
|
+
/** 认证上下文类型 */
|
|
33
|
+
type AuthType = "wallet-connect" | "token-refresh" | "request-init";
|
|
34
|
+
/** 认证结果(成功时返回) */
|
|
35
|
+
interface AuthResult {
|
|
36
|
+
success: true;
|
|
37
|
+
token: string;
|
|
38
|
+
}
|
|
39
|
+
/** 认证状态 */
|
|
40
|
+
interface AuthState {
|
|
41
|
+
status: "idle" | "authenticating" | "authenticated" | "unauthenticated";
|
|
42
|
+
address?: string;
|
|
43
|
+
error?: AuthError;
|
|
44
|
+
}
|
|
45
|
+
/** 状态变化监听器 */
|
|
46
|
+
type AuthStateListener = (state: AuthState) => void;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Token 存储服务接口
|
|
50
|
+
*/
|
|
51
|
+
interface ITokenStorageService {
|
|
52
|
+
setToken(token: string | undefined, address?: string): void;
|
|
53
|
+
getToken(address?: string): string | null;
|
|
54
|
+
subscribe(listener: () => void): () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 默认 Token 存储服务实现(localStorage)
|
|
58
|
+
* 支持多地址 token 存储
|
|
59
|
+
*/
|
|
60
|
+
declare class TokenStorageService implements ITokenStorageService {
|
|
61
|
+
private listeners;
|
|
62
|
+
private boundHandleStorage;
|
|
63
|
+
setToken(token: string | undefined, address?: string): void;
|
|
64
|
+
getToken(address?: string): string | null;
|
|
65
|
+
private getMap;
|
|
66
|
+
private saveMap;
|
|
67
|
+
subscribe(listener: () => void): () => void;
|
|
68
|
+
}
|
|
69
|
+
/** 默认单例 */
|
|
70
|
+
declare const defaultTokenStorageService: TokenStorageService;
|
|
71
|
+
|
|
72
|
+
type RefreshTokenFn = (context: AuthType) => Promise<AuthResult>;
|
|
73
|
+
interface AuthServiceConfig {
|
|
74
|
+
refreshToken: RefreshTokenFn;
|
|
75
|
+
tokenStorageService?: ITokenStorageService;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Authentication service with concurrency control, token storage, and state management
|
|
79
|
+
*/
|
|
80
|
+
declare class AuthService {
|
|
81
|
+
private refreshToken;
|
|
82
|
+
private tokenStorageService;
|
|
83
|
+
private authPromise;
|
|
84
|
+
private state;
|
|
85
|
+
private listeners;
|
|
86
|
+
constructor(config: AuthServiceConfig);
|
|
87
|
+
/**
|
|
88
|
+
* Authenticate with concurrency control
|
|
89
|
+
* Multiple concurrent calls will share the same Promise
|
|
90
|
+
* Stores token on success
|
|
91
|
+
*/
|
|
92
|
+
authenticate(context: AuthType): Promise<AuthResult>;
|
|
93
|
+
/**
|
|
94
|
+
* 重置内部认证状态(私有方法)
|
|
95
|
+
*/
|
|
96
|
+
private reset;
|
|
97
|
+
/**
|
|
98
|
+
* Get current token for the current wallet address
|
|
99
|
+
*/
|
|
100
|
+
getToken(): string | null;
|
|
101
|
+
/**
|
|
102
|
+
* 检查地址是否有有效 token(私有方法)
|
|
103
|
+
*/
|
|
104
|
+
private isAddressAuthenticated;
|
|
105
|
+
/**
|
|
106
|
+
* Logout: clear token for current address and update state
|
|
107
|
+
*/
|
|
108
|
+
logout(): void;
|
|
109
|
+
/**
|
|
110
|
+
* Get current authentication state
|
|
111
|
+
*/
|
|
112
|
+
getState(): AuthState;
|
|
113
|
+
/**
|
|
114
|
+
* Subscribe to state changes
|
|
115
|
+
*/
|
|
116
|
+
subscribe(listener: AuthStateListener): () => void;
|
|
117
|
+
/**
|
|
118
|
+
* Handle wallet address changes
|
|
119
|
+
* Note: Wallet disconnection should be handled externally by calling logout()
|
|
120
|
+
*/
|
|
121
|
+
handleWalletChange(address: string | undefined): void;
|
|
122
|
+
/**
|
|
123
|
+
* 处理 StorageChange 事件(用于多选项卡同步)
|
|
124
|
+
* 当 localStorage 在另一个选项卡中发生更改时调用
|
|
125
|
+
*/
|
|
126
|
+
private handleStorageChange;
|
|
127
|
+
/**
|
|
128
|
+
* 更新状态并通知监听者
|
|
129
|
+
*/
|
|
130
|
+
private setState;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
declare const isBrowser: boolean;
|
|
134
|
+
interface BaseRequestConfig {
|
|
135
|
+
baseURL: string;
|
|
136
|
+
headers?: Record<string, string>;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* 创建不带认证的基础请求实例
|
|
140
|
+
*/
|
|
141
|
+
declare const createBaseRequest: (config: BaseRequestConfig) => AxiosInstance;
|
|
142
|
+
/**
|
|
143
|
+
* 获取或创建默认基础请求实例
|
|
144
|
+
*/
|
|
145
|
+
declare const getBaseRequest: () => AxiosInstance;
|
|
146
|
+
/**
|
|
147
|
+
* 配置默认基础请求实例
|
|
148
|
+
*/
|
|
149
|
+
declare const configureBaseRequest: (config: BaseRequestConfig) => void;
|
|
150
|
+
interface CreateAuthRequestConfig {
|
|
151
|
+
authService: AuthService;
|
|
152
|
+
axiosConfig?: AxiosRequestConfig;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* 创建带鉴权 Axios 实例
|
|
156
|
+
*/
|
|
157
|
+
declare const createAuthRequest: (config: CreateAuthRequestConfig) => AxiosInstance;
|
|
158
|
+
|
|
159
|
+
type FetchIdentity<Resp, Req> = ResourceIdentity<Resp, Req> | MutationIdentity<Resp, Req>;
|
|
160
|
+
type ResourceIdentity<Resp = unknown, Req = unknown> = {
|
|
161
|
+
uri: string;
|
|
162
|
+
method?: "get";
|
|
163
|
+
host?: string;
|
|
164
|
+
noAuth?: boolean;
|
|
165
|
+
};
|
|
166
|
+
type MutationIdentity<Resp = unknown, Req = unknown> = {
|
|
167
|
+
uri: string;
|
|
168
|
+
method?: "post" | "delete";
|
|
169
|
+
host?: string;
|
|
170
|
+
noAuth?: boolean;
|
|
171
|
+
};
|
|
172
|
+
declare function defineFetch<Resp, Req>(uri: string, method?: "get" | "post" | "delete", option?: {
|
|
173
|
+
host?: string;
|
|
174
|
+
noAuth?: boolean;
|
|
175
|
+
}): FetchIdentity<Resp, Req>;
|
|
176
|
+
declare function defineResource<Resp, Req = undefined>(uri: string, option?: {
|
|
177
|
+
host?: string;
|
|
178
|
+
noAuth?: boolean;
|
|
179
|
+
}): ResourceIdentity<Resp, Req>;
|
|
180
|
+
declare function defineResourceList<Resp, Req = undefined>(uri: string, option?: {
|
|
181
|
+
host?: string;
|
|
182
|
+
noAuth?: boolean;
|
|
183
|
+
}): ResourceIdentity<PaginationResp<Resp>, Req & PaginationReq>;
|
|
184
|
+
declare function defineMutation<Resp, Req>(uri: string, option?: {
|
|
185
|
+
host?: string;
|
|
186
|
+
noAuth?: boolean;
|
|
187
|
+
method?: "post" | "delete";
|
|
188
|
+
}): MutationIdentity<Resp, Req>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 认证错误类型枚举
|
|
192
|
+
*/
|
|
193
|
+
declare const enum AuthErrorType {
|
|
194
|
+
/** 认证被取消 */
|
|
195
|
+
CANCELLED = "cancelled",
|
|
196
|
+
/** 地址在认证过程中改变 */
|
|
197
|
+
ADDRESS_CHANGED = "address_changed",
|
|
198
|
+
/** 未知错误 */
|
|
199
|
+
UNKNOWN = "unknown"
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* 通用认证错误类
|
|
203
|
+
*/
|
|
204
|
+
declare class AuthError<T extends string = string> extends Error {
|
|
205
|
+
readonly errorType: AuthErrorType | T;
|
|
206
|
+
constructor(message: string, errorType: AuthErrorType | T);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
type AuthStatus = "idle" | "authenticating" | "authenticated" | "unauthenticated";
|
|
210
|
+
interface AuthProviderInstanceConfig {
|
|
211
|
+
authService: AuthService;
|
|
212
|
+
authRequest: AxiosInstance;
|
|
213
|
+
address?: string;
|
|
214
|
+
}
|
|
215
|
+
interface AuthProviderFactoryConfig {
|
|
216
|
+
refreshTokenFn: (context: AuthType) => Promise<AuthResult>;
|
|
217
|
+
axiosConfig?: AxiosRequestConfig;
|
|
218
|
+
tokenStorageService?: ITokenStorageService;
|
|
219
|
+
address?: string;
|
|
220
|
+
}
|
|
221
|
+
/** 联合类型:支持两种配置方式 */
|
|
222
|
+
type AuthProviderConfig = AuthProviderInstanceConfig | AuthProviderFactoryConfig;
|
|
223
|
+
interface UseAuthResult {
|
|
224
|
+
status: AuthStatus;
|
|
225
|
+
error?: AuthError;
|
|
226
|
+
address?: string;
|
|
227
|
+
isAuthenticated: boolean;
|
|
228
|
+
isAuthenticating: boolean;
|
|
229
|
+
authenticate: () => Promise<void>;
|
|
230
|
+
logout: () => void;
|
|
231
|
+
}
|
|
232
|
+
declare const AuthProvider: ({ children, ...config }: PropsWithChildren<AuthProviderConfig>) => react_jsx_runtime.JSX.Element;
|
|
233
|
+
|
|
234
|
+
declare const useAuth: () => UseAuthResult;
|
|
235
|
+
|
|
236
|
+
type AuthChangeType = {
|
|
237
|
+
type: "authenticated";
|
|
238
|
+
} | {
|
|
239
|
+
type: "unauthenticated";
|
|
240
|
+
error?: AuthError;
|
|
241
|
+
} | {
|
|
242
|
+
type: "error";
|
|
243
|
+
error: AuthError;
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* 监听认证状态变化,通知调用方
|
|
247
|
+
* @param onChange 状态变化时的回调函数
|
|
248
|
+
*/
|
|
249
|
+
declare function useAuthChange(onChange: (change: AuthChangeType) => void): void;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Hook for accessing the authenticated axios instance.
|
|
253
|
+
* This hook is for module internal use only and should NOT be exported from api/index.ts
|
|
254
|
+
*/
|
|
255
|
+
declare const useAuthRequest: () => AxiosInstance;
|
|
256
|
+
|
|
257
|
+
declare const useResource: <Resp, Req>(requestIdentity?: ResourceIdentity<Resp, Req> | null, params?: Req, option?: SWRConfiguration<BaseResponse<Resp>>) => {
|
|
258
|
+
response: BaseResponse<Resp> | undefined;
|
|
259
|
+
data: Resp | undefined;
|
|
260
|
+
error: any;
|
|
261
|
+
mutate: swr.KeyedMutator<BaseResponse<Resp>>;
|
|
262
|
+
isValidating: boolean;
|
|
263
|
+
isLoading: swr__internal.IsLoadingResponse<Data, Config>;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
declare const useResourceList: <RespItem, Req extends PaginationReq>(requestIdentity?: ResourceIdentity<PaginationResp<RespItem>, Req> | null, params?: Req, option?: SWRConfiguration<BaseResponse<PaginationResp<RespItem>>> & {
|
|
267
|
+
page?: number;
|
|
268
|
+
size?: number;
|
|
269
|
+
pageMapping?: (pageInfo: {
|
|
270
|
+
page: number;
|
|
271
|
+
size: number;
|
|
272
|
+
}) => Record<string, number>;
|
|
273
|
+
}) => {
|
|
274
|
+
response: BaseResponse<PaginationResp<RespItem>> | undefined;
|
|
275
|
+
list: RespItem[] | undefined;
|
|
276
|
+
pagination: {
|
|
277
|
+
count: number;
|
|
278
|
+
page: number;
|
|
279
|
+
size: number;
|
|
280
|
+
setPage: react.Dispatch<react.SetStateAction<number>>;
|
|
281
|
+
setSize: react.Dispatch<react.SetStateAction<number>>;
|
|
282
|
+
};
|
|
283
|
+
error: any;
|
|
284
|
+
mutate: swr.KeyedMutator<BaseResponse<PaginationResp<RespItem>>>;
|
|
285
|
+
isValidating: boolean;
|
|
286
|
+
isLoading: swr__internal.IsLoadingResponse<Data, Config>;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
declare const useMutation: <Resp, Req>(key: MutationIdentity<Resp, Req>, option?: SWRMutationConfiguration<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req>) => {
|
|
290
|
+
error: Error | undefined;
|
|
291
|
+
response: BaseResponse<Resp> | undefined;
|
|
292
|
+
data: Resp | undefined;
|
|
293
|
+
isMutating: boolean;
|
|
294
|
+
trigger: [Req] extends [never] ? swr_mutation.TriggerWithoutArgs<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req> : (undefined extends Req ? true : false) extends true ? swr_mutation.TriggerWithOptionsArgs<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req> : swr_mutation.TriggerWithArgs<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req>;
|
|
295
|
+
reset: () => void;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
type FetcherSingleParams<Resp, Req> = [
|
|
299
|
+
FetchIdentity<Resp, Req>,
|
|
300
|
+
Req | undefined
|
|
301
|
+
];
|
|
302
|
+
type FetcherBatchParams = Array<[FetchIdentity<unknown, unknown>, unknown]>;
|
|
303
|
+
/**
|
|
304
|
+
* 创建 SWR fetcher(支持单个和批量请求)
|
|
305
|
+
*/
|
|
306
|
+
declare const createFetcherInterceptor: (authRequest: AxiosInstance) => {
|
|
307
|
+
<Resp, Req>(params: FetcherSingleParams<Resp, Req>): Promise<BaseResponse<Resp>>;
|
|
308
|
+
(params: FetcherBatchParams): Promise<BaseResponse<unknown>[]>;
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* 创建 SWR mutation fetcher
|
|
312
|
+
*/
|
|
313
|
+
declare const createFetcherMutation: (authRequest: AxiosInstance) => <Resp, Req>(requestIdentity: FetchIdentity<Resp, Req>, { arg }: Readonly<{
|
|
314
|
+
arg: Req;
|
|
315
|
+
}>) => Promise<BaseResponse<Resp>>;
|
|
316
|
+
|
|
317
|
+
export { type AuthChangeType, AuthError, AuthProvider, type AuthProviderConfig, type AuthResult, AuthService, type AuthServiceConfig, type AuthStatus, type AuthType, type BaseRequestConfig, type BaseResponse, type FetchIdentity, type ITokenStorageService, type MutationIdentity, type PaginationReq, type PaginationResp, type ResourceIdentity, TokenStorageService, type UseAuthResult, configureBaseRequest, createAuthRequest, createBaseRequest, createFetcherInterceptor, createFetcherMutation, defaultTokenStorageService, defineFetch, defineMutation, defineResource, defineResourceList, getBaseRequest, isBrowser, useAuth, useAuthChange, useAuthRequest, useMutation, useResource, useResourceList };
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosInstance } from 'axios';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { PropsWithChildren } from 'react';
|
|
5
|
+
import * as swr__internal from 'swr/_internal';
|
|
6
|
+
import * as swr from 'swr';
|
|
7
|
+
import { SWRConfiguration } from 'swr';
|
|
8
|
+
import * as swr_mutation from 'swr/mutation';
|
|
9
|
+
import { SWRMutationConfiguration } from 'swr/mutation';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* API 客户端类型定义
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
interface BaseResponse<T> {
|
|
16
|
+
datetime?: string;
|
|
17
|
+
success: boolean;
|
|
18
|
+
code: number;
|
|
19
|
+
message?: string;
|
|
20
|
+
data?: T;
|
|
21
|
+
}
|
|
22
|
+
interface PaginationReq {
|
|
23
|
+
page?: number;
|
|
24
|
+
size?: number;
|
|
25
|
+
}
|
|
26
|
+
interface PaginationResp<T> {
|
|
27
|
+
items: T[];
|
|
28
|
+
total: number;
|
|
29
|
+
current: number;
|
|
30
|
+
size: number;
|
|
31
|
+
}
|
|
32
|
+
/** 认证上下文类型 */
|
|
33
|
+
type AuthType = "wallet-connect" | "token-refresh" | "request-init";
|
|
34
|
+
/** 认证结果(成功时返回) */
|
|
35
|
+
interface AuthResult {
|
|
36
|
+
success: true;
|
|
37
|
+
token: string;
|
|
38
|
+
}
|
|
39
|
+
/** 认证状态 */
|
|
40
|
+
interface AuthState {
|
|
41
|
+
status: "idle" | "authenticating" | "authenticated" | "unauthenticated";
|
|
42
|
+
address?: string;
|
|
43
|
+
error?: AuthError;
|
|
44
|
+
}
|
|
45
|
+
/** 状态变化监听器 */
|
|
46
|
+
type AuthStateListener = (state: AuthState) => void;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Token 存储服务接口
|
|
50
|
+
*/
|
|
51
|
+
interface ITokenStorageService {
|
|
52
|
+
setToken(token: string | undefined, address?: string): void;
|
|
53
|
+
getToken(address?: string): string | null;
|
|
54
|
+
subscribe(listener: () => void): () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 默认 Token 存储服务实现(localStorage)
|
|
58
|
+
* 支持多地址 token 存储
|
|
59
|
+
*/
|
|
60
|
+
declare class TokenStorageService implements ITokenStorageService {
|
|
61
|
+
private listeners;
|
|
62
|
+
private boundHandleStorage;
|
|
63
|
+
setToken(token: string | undefined, address?: string): void;
|
|
64
|
+
getToken(address?: string): string | null;
|
|
65
|
+
private getMap;
|
|
66
|
+
private saveMap;
|
|
67
|
+
subscribe(listener: () => void): () => void;
|
|
68
|
+
}
|
|
69
|
+
/** 默认单例 */
|
|
70
|
+
declare const defaultTokenStorageService: TokenStorageService;
|
|
71
|
+
|
|
72
|
+
type RefreshTokenFn = (context: AuthType) => Promise<AuthResult>;
|
|
73
|
+
interface AuthServiceConfig {
|
|
74
|
+
refreshToken: RefreshTokenFn;
|
|
75
|
+
tokenStorageService?: ITokenStorageService;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Authentication service with concurrency control, token storage, and state management
|
|
79
|
+
*/
|
|
80
|
+
declare class AuthService {
|
|
81
|
+
private refreshToken;
|
|
82
|
+
private tokenStorageService;
|
|
83
|
+
private authPromise;
|
|
84
|
+
private state;
|
|
85
|
+
private listeners;
|
|
86
|
+
constructor(config: AuthServiceConfig);
|
|
87
|
+
/**
|
|
88
|
+
* Authenticate with concurrency control
|
|
89
|
+
* Multiple concurrent calls will share the same Promise
|
|
90
|
+
* Stores token on success
|
|
91
|
+
*/
|
|
92
|
+
authenticate(context: AuthType): Promise<AuthResult>;
|
|
93
|
+
/**
|
|
94
|
+
* 重置内部认证状态(私有方法)
|
|
95
|
+
*/
|
|
96
|
+
private reset;
|
|
97
|
+
/**
|
|
98
|
+
* Get current token for the current wallet address
|
|
99
|
+
*/
|
|
100
|
+
getToken(): string | null;
|
|
101
|
+
/**
|
|
102
|
+
* 检查地址是否有有效 token(私有方法)
|
|
103
|
+
*/
|
|
104
|
+
private isAddressAuthenticated;
|
|
105
|
+
/**
|
|
106
|
+
* Logout: clear token for current address and update state
|
|
107
|
+
*/
|
|
108
|
+
logout(): void;
|
|
109
|
+
/**
|
|
110
|
+
* Get current authentication state
|
|
111
|
+
*/
|
|
112
|
+
getState(): AuthState;
|
|
113
|
+
/**
|
|
114
|
+
* Subscribe to state changes
|
|
115
|
+
*/
|
|
116
|
+
subscribe(listener: AuthStateListener): () => void;
|
|
117
|
+
/**
|
|
118
|
+
* Handle wallet address changes
|
|
119
|
+
* Note: Wallet disconnection should be handled externally by calling logout()
|
|
120
|
+
*/
|
|
121
|
+
handleWalletChange(address: string | undefined): void;
|
|
122
|
+
/**
|
|
123
|
+
* 处理 StorageChange 事件(用于多选项卡同步)
|
|
124
|
+
* 当 localStorage 在另一个选项卡中发生更改时调用
|
|
125
|
+
*/
|
|
126
|
+
private handleStorageChange;
|
|
127
|
+
/**
|
|
128
|
+
* 更新状态并通知监听者
|
|
129
|
+
*/
|
|
130
|
+
private setState;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
declare const isBrowser: boolean;
|
|
134
|
+
interface BaseRequestConfig {
|
|
135
|
+
baseURL: string;
|
|
136
|
+
headers?: Record<string, string>;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* 创建不带认证的基础请求实例
|
|
140
|
+
*/
|
|
141
|
+
declare const createBaseRequest: (config: BaseRequestConfig) => AxiosInstance;
|
|
142
|
+
/**
|
|
143
|
+
* 获取或创建默认基础请求实例
|
|
144
|
+
*/
|
|
145
|
+
declare const getBaseRequest: () => AxiosInstance;
|
|
146
|
+
/**
|
|
147
|
+
* 配置默认基础请求实例
|
|
148
|
+
*/
|
|
149
|
+
declare const configureBaseRequest: (config: BaseRequestConfig) => void;
|
|
150
|
+
interface CreateAuthRequestConfig {
|
|
151
|
+
authService: AuthService;
|
|
152
|
+
axiosConfig?: AxiosRequestConfig;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* 创建带鉴权 Axios 实例
|
|
156
|
+
*/
|
|
157
|
+
declare const createAuthRequest: (config: CreateAuthRequestConfig) => AxiosInstance;
|
|
158
|
+
|
|
159
|
+
type FetchIdentity<Resp, Req> = ResourceIdentity<Resp, Req> | MutationIdentity<Resp, Req>;
|
|
160
|
+
type ResourceIdentity<Resp = unknown, Req = unknown> = {
|
|
161
|
+
uri: string;
|
|
162
|
+
method?: "get";
|
|
163
|
+
host?: string;
|
|
164
|
+
noAuth?: boolean;
|
|
165
|
+
};
|
|
166
|
+
type MutationIdentity<Resp = unknown, Req = unknown> = {
|
|
167
|
+
uri: string;
|
|
168
|
+
method?: "post" | "delete";
|
|
169
|
+
host?: string;
|
|
170
|
+
noAuth?: boolean;
|
|
171
|
+
};
|
|
172
|
+
declare function defineFetch<Resp, Req>(uri: string, method?: "get" | "post" | "delete", option?: {
|
|
173
|
+
host?: string;
|
|
174
|
+
noAuth?: boolean;
|
|
175
|
+
}): FetchIdentity<Resp, Req>;
|
|
176
|
+
declare function defineResource<Resp, Req = undefined>(uri: string, option?: {
|
|
177
|
+
host?: string;
|
|
178
|
+
noAuth?: boolean;
|
|
179
|
+
}): ResourceIdentity<Resp, Req>;
|
|
180
|
+
declare function defineResourceList<Resp, Req = undefined>(uri: string, option?: {
|
|
181
|
+
host?: string;
|
|
182
|
+
noAuth?: boolean;
|
|
183
|
+
}): ResourceIdentity<PaginationResp<Resp>, Req & PaginationReq>;
|
|
184
|
+
declare function defineMutation<Resp, Req>(uri: string, option?: {
|
|
185
|
+
host?: string;
|
|
186
|
+
noAuth?: boolean;
|
|
187
|
+
method?: "post" | "delete";
|
|
188
|
+
}): MutationIdentity<Resp, Req>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 认证错误类型枚举
|
|
192
|
+
*/
|
|
193
|
+
declare const enum AuthErrorType {
|
|
194
|
+
/** 认证被取消 */
|
|
195
|
+
CANCELLED = "cancelled",
|
|
196
|
+
/** 地址在认证过程中改变 */
|
|
197
|
+
ADDRESS_CHANGED = "address_changed",
|
|
198
|
+
/** 未知错误 */
|
|
199
|
+
UNKNOWN = "unknown"
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* 通用认证错误类
|
|
203
|
+
*/
|
|
204
|
+
declare class AuthError<T extends string = string> extends Error {
|
|
205
|
+
readonly errorType: AuthErrorType | T;
|
|
206
|
+
constructor(message: string, errorType: AuthErrorType | T);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
type AuthStatus = "idle" | "authenticating" | "authenticated" | "unauthenticated";
|
|
210
|
+
interface AuthProviderInstanceConfig {
|
|
211
|
+
authService: AuthService;
|
|
212
|
+
authRequest: AxiosInstance;
|
|
213
|
+
address?: string;
|
|
214
|
+
}
|
|
215
|
+
interface AuthProviderFactoryConfig {
|
|
216
|
+
refreshTokenFn: (context: AuthType) => Promise<AuthResult>;
|
|
217
|
+
axiosConfig?: AxiosRequestConfig;
|
|
218
|
+
tokenStorageService?: ITokenStorageService;
|
|
219
|
+
address?: string;
|
|
220
|
+
}
|
|
221
|
+
/** 联合类型:支持两种配置方式 */
|
|
222
|
+
type AuthProviderConfig = AuthProviderInstanceConfig | AuthProviderFactoryConfig;
|
|
223
|
+
interface UseAuthResult {
|
|
224
|
+
status: AuthStatus;
|
|
225
|
+
error?: AuthError;
|
|
226
|
+
address?: string;
|
|
227
|
+
isAuthenticated: boolean;
|
|
228
|
+
isAuthenticating: boolean;
|
|
229
|
+
authenticate: () => Promise<void>;
|
|
230
|
+
logout: () => void;
|
|
231
|
+
}
|
|
232
|
+
declare const AuthProvider: ({ children, ...config }: PropsWithChildren<AuthProviderConfig>) => react_jsx_runtime.JSX.Element;
|
|
233
|
+
|
|
234
|
+
declare const useAuth: () => UseAuthResult;
|
|
235
|
+
|
|
236
|
+
type AuthChangeType = {
|
|
237
|
+
type: "authenticated";
|
|
238
|
+
} | {
|
|
239
|
+
type: "unauthenticated";
|
|
240
|
+
error?: AuthError;
|
|
241
|
+
} | {
|
|
242
|
+
type: "error";
|
|
243
|
+
error: AuthError;
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* 监听认证状态变化,通知调用方
|
|
247
|
+
* @param onChange 状态变化时的回调函数
|
|
248
|
+
*/
|
|
249
|
+
declare function useAuthChange(onChange: (change: AuthChangeType) => void): void;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Hook for accessing the authenticated axios instance.
|
|
253
|
+
* This hook is for module internal use only and should NOT be exported from api/index.ts
|
|
254
|
+
*/
|
|
255
|
+
declare const useAuthRequest: () => AxiosInstance;
|
|
256
|
+
|
|
257
|
+
declare const useResource: <Resp, Req>(requestIdentity?: ResourceIdentity<Resp, Req> | null, params?: Req, option?: SWRConfiguration<BaseResponse<Resp>>) => {
|
|
258
|
+
response: BaseResponse<Resp> | undefined;
|
|
259
|
+
data: Resp | undefined;
|
|
260
|
+
error: any;
|
|
261
|
+
mutate: swr.KeyedMutator<BaseResponse<Resp>>;
|
|
262
|
+
isValidating: boolean;
|
|
263
|
+
isLoading: swr__internal.IsLoadingResponse<Data, Config>;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
declare const useResourceList: <RespItem, Req extends PaginationReq>(requestIdentity?: ResourceIdentity<PaginationResp<RespItem>, Req> | null, params?: Req, option?: SWRConfiguration<BaseResponse<PaginationResp<RespItem>>> & {
|
|
267
|
+
page?: number;
|
|
268
|
+
size?: number;
|
|
269
|
+
pageMapping?: (pageInfo: {
|
|
270
|
+
page: number;
|
|
271
|
+
size: number;
|
|
272
|
+
}) => Record<string, number>;
|
|
273
|
+
}) => {
|
|
274
|
+
response: BaseResponse<PaginationResp<RespItem>> | undefined;
|
|
275
|
+
list: RespItem[] | undefined;
|
|
276
|
+
pagination: {
|
|
277
|
+
count: number;
|
|
278
|
+
page: number;
|
|
279
|
+
size: number;
|
|
280
|
+
setPage: react.Dispatch<react.SetStateAction<number>>;
|
|
281
|
+
setSize: react.Dispatch<react.SetStateAction<number>>;
|
|
282
|
+
};
|
|
283
|
+
error: any;
|
|
284
|
+
mutate: swr.KeyedMutator<BaseResponse<PaginationResp<RespItem>>>;
|
|
285
|
+
isValidating: boolean;
|
|
286
|
+
isLoading: swr__internal.IsLoadingResponse<Data, Config>;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
declare const useMutation: <Resp, Req>(key: MutationIdentity<Resp, Req>, option?: SWRMutationConfiguration<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req>) => {
|
|
290
|
+
error: Error | undefined;
|
|
291
|
+
response: BaseResponse<Resp> | undefined;
|
|
292
|
+
data: Resp | undefined;
|
|
293
|
+
isMutating: boolean;
|
|
294
|
+
trigger: [Req] extends [never] ? swr_mutation.TriggerWithoutArgs<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req> : (undefined extends Req ? true : false) extends true ? swr_mutation.TriggerWithOptionsArgs<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req> : swr_mutation.TriggerWithArgs<BaseResponse<Resp>, Error, MutationIdentity<Resp, Req>, Req>;
|
|
295
|
+
reset: () => void;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
type FetcherSingleParams<Resp, Req> = [
|
|
299
|
+
FetchIdentity<Resp, Req>,
|
|
300
|
+
Req | undefined
|
|
301
|
+
];
|
|
302
|
+
type FetcherBatchParams = Array<[FetchIdentity<unknown, unknown>, unknown]>;
|
|
303
|
+
/**
|
|
304
|
+
* 创建 SWR fetcher(支持单个和批量请求)
|
|
305
|
+
*/
|
|
306
|
+
declare const createFetcherInterceptor: (authRequest: AxiosInstance) => {
|
|
307
|
+
<Resp, Req>(params: FetcherSingleParams<Resp, Req>): Promise<BaseResponse<Resp>>;
|
|
308
|
+
(params: FetcherBatchParams): Promise<BaseResponse<unknown>[]>;
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* 创建 SWR mutation fetcher
|
|
312
|
+
*/
|
|
313
|
+
declare const createFetcherMutation: (authRequest: AxiosInstance) => <Resp, Req>(requestIdentity: FetchIdentity<Resp, Req>, { arg }: Readonly<{
|
|
314
|
+
arg: Req;
|
|
315
|
+
}>) => Promise<BaseResponse<Resp>>;
|
|
316
|
+
|
|
317
|
+
export { type AuthChangeType, AuthError, AuthProvider, type AuthProviderConfig, type AuthResult, AuthService, type AuthServiceConfig, type AuthStatus, type AuthType, type BaseRequestConfig, type BaseResponse, type FetchIdentity, type ITokenStorageService, type MutationIdentity, type PaginationReq, type PaginationResp, type ResourceIdentity, TokenStorageService, type UseAuthResult, configureBaseRequest, createAuthRequest, createBaseRequest, createFetcherInterceptor, createFetcherMutation, defaultTokenStorageService, defineFetch, defineMutation, defineResource, defineResourceList, getBaseRequest, isBrowser, useAuth, useAuthChange, useAuthRequest, useMutation, useResource, useResourceList };
|