yuand 1.0.0
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 +23 -0
- package/es/components/Table/index.d.ts +12 -0
- package/es/components/Table/index.js +326 -0
- package/es/components/Table/style.css +3 -0
- package/es/components/Table/types.d.ts +101 -0
- package/es/components/Table/types.js +1 -0
- package/es/components/Table/useTable.d.ts +3 -0
- package/es/components/Table/useTable.js +76 -0
- package/es/components/TableConfig/index.d.ts +11 -0
- package/es/components/TableConfig/index.js +19 -0
- package/es/components/TableUseFetch/index.d.ts +12 -0
- package/es/components/TableUseFetch/index.js +283 -0
- package/es/components/TableUseFetch/style.css +3 -0
- package/es/components/TableUseFetch/types.d.ts +100 -0
- package/es/components/TableUseFetch/types.js +1 -0
- package/es/components/TableUseFetch/useTable.d.ts +3 -0
- package/es/components/TableUseFetch/useTable.js +78 -0
- package/es/components/index.d.ts +3 -0
- package/es/components/index.js +3 -0
- package/es/fetch.d.ts +137 -0
- package/es/fetch.js +534 -0
- package/es/hooks/index.d.ts +4 -0
- package/es/hooks/index.js +4 -0
- package/es/hooks/useFetch.d.ts +17 -0
- package/es/hooks/useFetch.js +41 -0
- package/es/hooks/useMutation.d.ts +14 -0
- package/es/hooks/useMutation.js +26 -0
- package/es/hooks/useQuery.d.ts +14 -0
- package/es/hooks/useQuery.js +40 -0
- package/es/hooks/useX.d.ts +1 -0
- package/es/hooks/useX.js +13 -0
- package/es/index.d.ts +4 -0
- package/es/index.js +4 -0
- package/es/utils/index.d.ts +6 -0
- package/es/utils/index.js +23 -0
- package/es/utils/table.d.ts +15 -0
- package/es/utils/table.js +106 -0
- package/es/utils/util.d.ts +2 -0
- package/es/utils/util.js +6 -0
- package/lang/en_US.json +6 -0
- package/lang/zh_CN.json +6 -0
- package/lib/components/Table/index.d.ts +12 -0
- package/lib/components/Table/index.js +330 -0
- package/lib/components/Table/style.css +3 -0
- package/lib/components/Table/types.d.ts +101 -0
- package/lib/components/Table/types.js +17 -0
- package/lib/components/Table/useTable.d.ts +3 -0
- package/lib/components/Table/useTable.js +85 -0
- package/lib/components/TableConfig/index.d.ts +11 -0
- package/lib/components/TableConfig/index.js +55 -0
- package/lib/components/TableUseFetch/index.d.ts +12 -0
- package/lib/components/TableUseFetch/index.js +303 -0
- package/lib/components/TableUseFetch/style.css +3 -0
- package/lib/components/TableUseFetch/types.d.ts +100 -0
- package/lib/components/TableUseFetch/types.js +17 -0
- package/lib/components/TableUseFetch/useTable.d.ts +3 -0
- package/lib/components/TableUseFetch/useTable.js +86 -0
- package/lib/components/index.d.ts +3 -0
- package/lib/components/index.js +45 -0
- package/lib/fetch.d.ts +137 -0
- package/lib/fetch.js +366 -0
- package/lib/hooks/index.d.ts +4 -0
- package/lib/hooks/index.js +48 -0
- package/lib/hooks/useFetch.d.ts +17 -0
- package/lib/hooks/useFetch.js +62 -0
- package/lib/hooks/useMutation.d.ts +14 -0
- package/lib/hooks/useMutation.js +43 -0
- package/lib/hooks/useQuery.d.ts +14 -0
- package/lib/hooks/useQuery.js +57 -0
- package/lib/hooks/useX.d.ts +1 -0
- package/lib/hooks/useX.js +37 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +29 -0
- package/lib/utils/index.d.ts +6 -0
- package/lib/utils/index.js +60 -0
- package/lib/utils/table.d.ts +15 -0
- package/lib/utils/table.js +124 -0
- package/lib/utils/util.d.ts +2 -0
- package/lib/utils/util.js +32 -0
- package/package.json +46 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
|
|
15
|
+
// src/components/TableUseFetch/types.ts
|
|
16
|
+
var types_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/components/TableUseFetch/useTable.ts
|
|
20
|
+
var useTable_exports = {};
|
|
21
|
+
__export(useTable_exports, {
|
|
22
|
+
default: () => useTable_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(useTable_exports);
|
|
25
|
+
var import_antd = require("antd");
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_zustand = require("zustand");
|
|
28
|
+
var useTable = (options = {}) => {
|
|
29
|
+
const [_, update] = (0, import_react.useState)(0);
|
|
30
|
+
const [form] = import_antd.Form.useForm();
|
|
31
|
+
const tableRef = (0, import_react.useRef)(null);
|
|
32
|
+
if (!tableRef.current) {
|
|
33
|
+
const useStore = (0, import_zustand.create)((set) => ({
|
|
34
|
+
page: options.page ?? 1,
|
|
35
|
+
size: options.size ?? 10,
|
|
36
|
+
sorter: options.sorter || {},
|
|
37
|
+
search: {},
|
|
38
|
+
params: {
|
|
39
|
+
page: options.page ?? 1,
|
|
40
|
+
size: options.size ?? 10,
|
|
41
|
+
sorter: options.sorter || {},
|
|
42
|
+
form: {}
|
|
43
|
+
},
|
|
44
|
+
data: {},
|
|
45
|
+
ready: false,
|
|
46
|
+
setState(values = {}) {
|
|
47
|
+
set(values);
|
|
48
|
+
}
|
|
49
|
+
}));
|
|
50
|
+
tableRef.current = {
|
|
51
|
+
form,
|
|
52
|
+
useStore,
|
|
53
|
+
run() {
|
|
54
|
+
},
|
|
55
|
+
clear: () => {
|
|
56
|
+
useStore.setState({
|
|
57
|
+
data: {}
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
refresh: () => {
|
|
61
|
+
useStore.setState({
|
|
62
|
+
ready: true
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
reset: () => {
|
|
66
|
+
},
|
|
67
|
+
sortOrder(key) {
|
|
68
|
+
const sorter = useStore.getState().sorter;
|
|
69
|
+
if (sorter && sorter.field === key) {
|
|
70
|
+
return sorter.order;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
},
|
|
74
|
+
update: () => update((v) => v + 1)
|
|
75
|
+
};
|
|
76
|
+
useStore.subscribe((state, prevState) => {
|
|
77
|
+
var _a, _b;
|
|
78
|
+
if (state.sorter !== prevState.sorter && ((_a = state.sorter) == null ? void 0 : _a.order) !== ((_b = prevState.sorter) == null ? void 0 : _b.order)) {
|
|
79
|
+
tableRef.current = { ...tableRef.current };
|
|
80
|
+
update((v) => v + 1);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return [tableRef.current];
|
|
85
|
+
};
|
|
86
|
+
var useTable_default = useTable;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/components/index.tsx
|
|
30
|
+
var components_exports = {};
|
|
31
|
+
__export(components_exports, {
|
|
32
|
+
Table: () => import_Table.default,
|
|
33
|
+
TableConfig: () => import_TableConfig.default,
|
|
34
|
+
TableUseFetch: () => import_TableUseFetch.default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(components_exports);
|
|
37
|
+
var import_TableConfig = __toESM(require("./TableConfig"));
|
|
38
|
+
var import_Table = __toESM(require("./Table"));
|
|
39
|
+
var import_TableUseFetch = __toESM(require("./TableUseFetch"));
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
Table,
|
|
43
|
+
TableConfig,
|
|
44
|
+
TableUseFetch
|
|
45
|
+
});
|
package/lib/fetch.d.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
export declare const isObject: (oj: unknown) => boolean;
|
|
2
|
+
export declare const isFunction: (oj: unknown) => boolean;
|
|
3
|
+
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "get" | "post" | "put" | "delete" | "patch" | "head" | "options" | undefined;
|
|
4
|
+
export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
|
|
5
|
+
export type JsonObject = {
|
|
6
|
+
[key: string]: JsonValue;
|
|
7
|
+
};
|
|
8
|
+
export type JsonArray = JsonValue[];
|
|
9
|
+
export type RequestData = unknown;
|
|
10
|
+
export interface HttpClientConfig {
|
|
11
|
+
/** 请求的基础 URL */
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
/** 流数据响应头类型关键字 */
|
|
14
|
+
blobContentTypes?: readonly string[];
|
|
15
|
+
/** 请求头配置 */
|
|
16
|
+
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
17
|
+
/** 响应数据状态码的 key,默认: 'code' */
|
|
18
|
+
codeKey?: string;
|
|
19
|
+
/** 响应数据的 data key,默认: 'data' */
|
|
20
|
+
dataKey?: string;
|
|
21
|
+
/** 响应消息的 key,默认: 'message' */
|
|
22
|
+
messageKey?: string;
|
|
23
|
+
/** 是否默认返回 data 层数据,默认: false */
|
|
24
|
+
returnData?: boolean;
|
|
25
|
+
/** 默认请求方法,默认: 'GET' */
|
|
26
|
+
defaultMethod?: HttpMethod;
|
|
27
|
+
/** 请求超时时间(ms),默认: 30000 */
|
|
28
|
+
timeout?: number;
|
|
29
|
+
/** 状态码配置 */
|
|
30
|
+
codes?: {
|
|
31
|
+
/** 成功状态码列表 */
|
|
32
|
+
success?: readonly number[];
|
|
33
|
+
/** 需要登出的状态码列表 */
|
|
34
|
+
logout?: readonly number[];
|
|
35
|
+
/** 忽略错误的状态码列表 */
|
|
36
|
+
ignoreError?: readonly number[];
|
|
37
|
+
};
|
|
38
|
+
/** 错误处理回调 */
|
|
39
|
+
onError?: (error: HttpError) => void | Promise<void>;
|
|
40
|
+
/** 登出回调 */
|
|
41
|
+
onLogout?: (error: HttpError) => void | Promise<void>;
|
|
42
|
+
onSuccess?: (data: any) => void | Promise<void>;
|
|
43
|
+
/** 请求拦截器 */
|
|
44
|
+
requestInterceptor?: (url: string, options: RequestInit) => Promise<void> | void;
|
|
45
|
+
/** 响应拦截器 */
|
|
46
|
+
responseInterceptor?: <T>(response: HttpResponse<T>) => Promise<HttpResponse<T>> | HttpResponse<T>;
|
|
47
|
+
}
|
|
48
|
+
export interface RequestOptions extends Omit<RequestInit, "body" | "method"> {
|
|
49
|
+
/** 请求方法 */
|
|
50
|
+
method?: HttpMethod;
|
|
51
|
+
/** 请求体数据(对象会自动转为 JSON) */
|
|
52
|
+
json?: RequestData;
|
|
53
|
+
/** URL 查询参数 */
|
|
54
|
+
params?: Record<string, string | number | boolean | null | undefined>;
|
|
55
|
+
/** 是否忽略当前请求的错误,不触发 onError */
|
|
56
|
+
ignoreError?: boolean;
|
|
57
|
+
/** 是否返回 data 层数据 */
|
|
58
|
+
returnData?: boolean;
|
|
59
|
+
/** 请求超时时间(ms) */
|
|
60
|
+
timeout?: number;
|
|
61
|
+
/** 自定义错误回调(仅本次请求) */
|
|
62
|
+
onError?: (error: HttpError) => void;
|
|
63
|
+
/** 自定义登出回调(仅本次请求) */
|
|
64
|
+
onLogout?: (error: HttpError) => void;
|
|
65
|
+
/** 自定义成功回调(仅本次请求) */
|
|
66
|
+
onSuccess?: (data: any) => void;
|
|
67
|
+
}
|
|
68
|
+
export interface HttpResponse<T = any> {
|
|
69
|
+
[key: string]: any;
|
|
70
|
+
data?: T;
|
|
71
|
+
}
|
|
72
|
+
export interface BlobResponse {
|
|
73
|
+
data: Blob;
|
|
74
|
+
filename?: string;
|
|
75
|
+
response: Response;
|
|
76
|
+
}
|
|
77
|
+
export declare class HttpError extends Error {
|
|
78
|
+
code?: number;
|
|
79
|
+
response?: any;
|
|
80
|
+
statusCode?: number;
|
|
81
|
+
constructor(message: string, code?: number, response?: any, statusCode?: number);
|
|
82
|
+
}
|
|
83
|
+
export declare class HttpClient {
|
|
84
|
+
private configure;
|
|
85
|
+
constructor(configure?: HttpClientConfig);
|
|
86
|
+
/**
|
|
87
|
+
* 更新客户端配置
|
|
88
|
+
*/
|
|
89
|
+
config(configure: HttpClientConfig): void;
|
|
90
|
+
get<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
91
|
+
post<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
92
|
+
put<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
93
|
+
delete<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
94
|
+
patch<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
95
|
+
request<T = any>(url: string, options?: RequestOptions): Promise<T>;
|
|
96
|
+
/**
|
|
97
|
+
* 构建完整 URL
|
|
98
|
+
*/
|
|
99
|
+
private buildUrl;
|
|
100
|
+
/**
|
|
101
|
+
* 添加查询参数
|
|
102
|
+
*/
|
|
103
|
+
private appendParams;
|
|
104
|
+
/**
|
|
105
|
+
* 构建请求头
|
|
106
|
+
*/
|
|
107
|
+
private buildHeaders;
|
|
108
|
+
/**
|
|
109
|
+
* 带超时的 fetch 请求
|
|
110
|
+
*/
|
|
111
|
+
private fetchWithTimeout;
|
|
112
|
+
/**
|
|
113
|
+
* 处理响应数据
|
|
114
|
+
*/
|
|
115
|
+
private handleResponse;
|
|
116
|
+
/**
|
|
117
|
+
* 从响应头提取文件名
|
|
118
|
+
*/
|
|
119
|
+
private extractFilename;
|
|
120
|
+
/**
|
|
121
|
+
* 统一错误处理
|
|
122
|
+
*/
|
|
123
|
+
private handleError;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 下载文件(用于 Blob 响应)
|
|
127
|
+
* @example
|
|
128
|
+
* const response = await http.get('/api/download');
|
|
129
|
+
* downloadfile(response);
|
|
130
|
+
*/
|
|
131
|
+
export declare function downloadfile(response: BlobResponse | {
|
|
132
|
+
data: Blob;
|
|
133
|
+
filename?: string;
|
|
134
|
+
}): void;
|
|
135
|
+
export declare function createHttpClient(config?: HttpClientConfig): HttpClient;
|
|
136
|
+
export declare const rq: HttpClient;
|
|
137
|
+
export default HttpClient;
|
package/lib/fetch.js
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/fetch.ts
|
|
20
|
+
var fetch_exports = {};
|
|
21
|
+
__export(fetch_exports, {
|
|
22
|
+
HttpClient: () => HttpClient,
|
|
23
|
+
HttpError: () => HttpError,
|
|
24
|
+
createHttpClient: () => createHttpClient,
|
|
25
|
+
default: () => fetch_default,
|
|
26
|
+
downloadfile: () => downloadfile,
|
|
27
|
+
isFunction: () => isFunction,
|
|
28
|
+
isObject: () => isObject,
|
|
29
|
+
rq: () => rq
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(fetch_exports);
|
|
32
|
+
var isObject = (oj) => Object.prototype.toString.call(oj) === "[object Object]";
|
|
33
|
+
var isFunction = (oj) => Object.prototype.toString.call(oj) === "[object Function]";
|
|
34
|
+
var DEFAULT_SUCCESS_CODES = [200];
|
|
35
|
+
var DEFAULT_LOGOUT_CODES = [401, 403];
|
|
36
|
+
var DEFAULT_METHOD = "GET";
|
|
37
|
+
var DEFAULT_CODE_KEY = "code";
|
|
38
|
+
var DEFAULT_DATA_KEY = "data";
|
|
39
|
+
var DEFAULT_MESSAGE_KEY = "message";
|
|
40
|
+
var DEFAULT_CONTENT_TYPE = "application/json;charset=UTF-8";
|
|
41
|
+
var BLOB_CONTENT_TYPES = [
|
|
42
|
+
"stream",
|
|
43
|
+
"excel",
|
|
44
|
+
"download",
|
|
45
|
+
"blob",
|
|
46
|
+
"octet-stream"
|
|
47
|
+
];
|
|
48
|
+
var METHODS_WITHOUT_BODY = ["GET", "HEAD"];
|
|
49
|
+
var HttpError = class extends Error {
|
|
50
|
+
constructor(message, code, response, statusCode) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.code = code;
|
|
53
|
+
this.response = response;
|
|
54
|
+
this.statusCode = statusCode;
|
|
55
|
+
this.name = "HttpError";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var HttpClient = class {
|
|
59
|
+
constructor(configure = {}) {
|
|
60
|
+
var _a, _b, _c;
|
|
61
|
+
this.configure = {
|
|
62
|
+
baseUrl: configure.baseUrl ?? "",
|
|
63
|
+
blobContentTypes: configure.blobContentTypes ?? [...BLOB_CONTENT_TYPES],
|
|
64
|
+
headers: configure.headers,
|
|
65
|
+
codeKey: configure.codeKey ?? DEFAULT_CODE_KEY,
|
|
66
|
+
dataKey: configure.dataKey ?? DEFAULT_DATA_KEY,
|
|
67
|
+
messageKey: configure.messageKey ?? DEFAULT_MESSAGE_KEY,
|
|
68
|
+
returnData: configure.returnData ?? false,
|
|
69
|
+
defaultMethod: configure.defaultMethod ?? DEFAULT_METHOD,
|
|
70
|
+
timeout: configure.timeout ?? 3e4,
|
|
71
|
+
codes: {
|
|
72
|
+
success: ((_a = configure.codes) == null ? void 0 : _a.success) ?? [...DEFAULT_SUCCESS_CODES],
|
|
73
|
+
logout: ((_b = configure.codes) == null ? void 0 : _b.logout) ?? [...DEFAULT_LOGOUT_CODES],
|
|
74
|
+
ignoreError: ((_c = configure.codes) == null ? void 0 : _c.ignoreError) ?? []
|
|
75
|
+
},
|
|
76
|
+
onError: configure.onError,
|
|
77
|
+
onLogout: configure.onLogout,
|
|
78
|
+
onSuccess: configure.onSuccess,
|
|
79
|
+
requestInterceptor: configure.requestInterceptor,
|
|
80
|
+
responseInterceptor: configure.responseInterceptor
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 更新客户端配置
|
|
85
|
+
*/
|
|
86
|
+
config(configure) {
|
|
87
|
+
if (!isObject(configure)) {
|
|
88
|
+
throw new TypeError("Config must be an object");
|
|
89
|
+
}
|
|
90
|
+
this.configure = { ...this.configure, ...configure };
|
|
91
|
+
}
|
|
92
|
+
get(url, options) {
|
|
93
|
+
return this.request(url, { ...options, method: "GET" });
|
|
94
|
+
}
|
|
95
|
+
post(url, options) {
|
|
96
|
+
return this.request(url, { ...options, method: "POST" });
|
|
97
|
+
}
|
|
98
|
+
put(url, options) {
|
|
99
|
+
return this.request(url, { ...options, method: "PUT" });
|
|
100
|
+
}
|
|
101
|
+
delete(url, options) {
|
|
102
|
+
return this.request(url, { ...options, method: "DELETE" });
|
|
103
|
+
}
|
|
104
|
+
patch(url, options) {
|
|
105
|
+
return this.request(url, { ...options, method: "PATCH" });
|
|
106
|
+
}
|
|
107
|
+
async request(url, options = {}) {
|
|
108
|
+
const {
|
|
109
|
+
json,
|
|
110
|
+
params,
|
|
111
|
+
ignoreError = false,
|
|
112
|
+
returnData = this.configure.returnData,
|
|
113
|
+
timeout = this.configure.timeout,
|
|
114
|
+
onError,
|
|
115
|
+
onLogout,
|
|
116
|
+
onSuccess,
|
|
117
|
+
...restOptions
|
|
118
|
+
} = options;
|
|
119
|
+
const fetchOptions = { ...restOptions };
|
|
120
|
+
try {
|
|
121
|
+
let fullUrl = this.buildUrl(url);
|
|
122
|
+
if (params) {
|
|
123
|
+
fullUrl = this.appendParams(fullUrl, params);
|
|
124
|
+
}
|
|
125
|
+
const method = (options.method ?? this.configure.defaultMethod).toUpperCase();
|
|
126
|
+
fetchOptions.method = method;
|
|
127
|
+
fetchOptions.headers = await this.buildHeaders(options.headers);
|
|
128
|
+
if (json) {
|
|
129
|
+
if (METHODS_WITHOUT_BODY.includes(method)) {
|
|
130
|
+
if (isObject(json) && !(json instanceof FormData)) {
|
|
131
|
+
fullUrl = this.appendParams(fullUrl, json);
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
if (json instanceof FormData) {
|
|
135
|
+
fetchOptions.headers.delete("Content-Type");
|
|
136
|
+
fetchOptions.body = json;
|
|
137
|
+
} else {
|
|
138
|
+
fetchOptions.body = JSON.stringify(json);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (this.configure.requestInterceptor) {
|
|
143
|
+
await this.configure.requestInterceptor(fullUrl, fetchOptions);
|
|
144
|
+
}
|
|
145
|
+
const response = await this.fetchWithTimeout(
|
|
146
|
+
fullUrl,
|
|
147
|
+
fetchOptions,
|
|
148
|
+
timeout
|
|
149
|
+
);
|
|
150
|
+
if (!response.ok) {
|
|
151
|
+
throw new HttpError(
|
|
152
|
+
response.statusText || "Request failed",
|
|
153
|
+
void 0,
|
|
154
|
+
response,
|
|
155
|
+
response.status
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
const result = await this.handleResponse(
|
|
159
|
+
response,
|
|
160
|
+
returnData,
|
|
161
|
+
ignoreError,
|
|
162
|
+
onSuccess
|
|
163
|
+
);
|
|
164
|
+
if (this.configure.responseInterceptor && isObject(result)) {
|
|
165
|
+
return await this.configure.responseInterceptor(
|
|
166
|
+
result
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return this.handleError(error, ignoreError, onError, onLogout);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 构建完整 URL
|
|
176
|
+
*/
|
|
177
|
+
buildUrl(url) {
|
|
178
|
+
if (typeof url !== "string") {
|
|
179
|
+
throw new TypeError("URL must be a string");
|
|
180
|
+
}
|
|
181
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
182
|
+
return url;
|
|
183
|
+
}
|
|
184
|
+
const baseUrl = this.configure.baseUrl;
|
|
185
|
+
if (!baseUrl)
|
|
186
|
+
return url;
|
|
187
|
+
const normalizedBase = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
188
|
+
const normalizedUrl = url.startsWith("/") ? url : `/${url}`;
|
|
189
|
+
return normalizedBase + normalizedUrl;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* 添加查询参数
|
|
193
|
+
*/
|
|
194
|
+
appendParams(url, params) {
|
|
195
|
+
if (!isObject(params)) {
|
|
196
|
+
throw new TypeError("Params must be an object");
|
|
197
|
+
}
|
|
198
|
+
const searchParams = new URLSearchParams();
|
|
199
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
200
|
+
if (value !== void 0 && value !== null) {
|
|
201
|
+
searchParams.append(key, String(value));
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
const queryString = searchParams.toString();
|
|
205
|
+
if (!queryString)
|
|
206
|
+
return url;
|
|
207
|
+
const separator = url.includes("?") ? "&" : "?";
|
|
208
|
+
return `${url}${separator}${queryString}`;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* 构建请求头
|
|
212
|
+
*/
|
|
213
|
+
async buildHeaders(customHeaders) {
|
|
214
|
+
let baseHeaders;
|
|
215
|
+
const configureHeaders = this.configure.headers;
|
|
216
|
+
if (configureHeaders && isFunction(configureHeaders)) {
|
|
217
|
+
baseHeaders = await configureHeaders();
|
|
218
|
+
} else if (configureHeaders) {
|
|
219
|
+
baseHeaders = configureHeaders;
|
|
220
|
+
}
|
|
221
|
+
const headers = new Headers(baseHeaders);
|
|
222
|
+
if (customHeaders) {
|
|
223
|
+
const custom = new Headers(customHeaders);
|
|
224
|
+
custom.forEach((value, key) => {
|
|
225
|
+
headers.set(key, value);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
if (!headers.has("Content-Type")) {
|
|
229
|
+
headers.set("Content-Type", DEFAULT_CONTENT_TYPE);
|
|
230
|
+
}
|
|
231
|
+
return headers;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 带超时的 fetch 请求
|
|
235
|
+
*/
|
|
236
|
+
async fetchWithTimeout(url, options, timeout) {
|
|
237
|
+
const controller = new AbortController();
|
|
238
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
239
|
+
try {
|
|
240
|
+
const response = await fetch(url, {
|
|
241
|
+
...options,
|
|
242
|
+
signal: controller.signal
|
|
243
|
+
});
|
|
244
|
+
return response;
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (error.name === "AbortError") {
|
|
247
|
+
throw new HttpError(
|
|
248
|
+
`Request timeout after ${timeout}ms`,
|
|
249
|
+
void 0,
|
|
250
|
+
void 0,
|
|
251
|
+
408
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
throw error;
|
|
255
|
+
} finally {
|
|
256
|
+
clearTimeout(timeoutId);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* 处理响应数据
|
|
261
|
+
*/
|
|
262
|
+
async handleResponse(response, returnData, ignoreError, onSuccess) {
|
|
263
|
+
var _a;
|
|
264
|
+
const contentType = ((_a = response.headers.get("content-type")) == null ? void 0 : _a.toLowerCase()) ?? "";
|
|
265
|
+
const { codeKey, dataKey, messageKey, codes } = this.configure;
|
|
266
|
+
const successCodes = codes.success ?? DEFAULT_SUCCESS_CODES;
|
|
267
|
+
const logoutCodes = codes.logout ?? DEFAULT_LOGOUT_CODES;
|
|
268
|
+
const ignoreErrorCodes = codes.ignoreError ?? [];
|
|
269
|
+
if (this.configure.blobContentTypes.some((type) => contentType.includes(type))) {
|
|
270
|
+
const blob = await response.blob();
|
|
271
|
+
const filename = this.extractFilename(response.headers);
|
|
272
|
+
return {
|
|
273
|
+
[codeKey]: successCodes[0],
|
|
274
|
+
data: blob,
|
|
275
|
+
filename,
|
|
276
|
+
response
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
const data = await response.json();
|
|
280
|
+
if (!data.hasOwnProperty(codeKey)) {
|
|
281
|
+
return data;
|
|
282
|
+
}
|
|
283
|
+
const code = data[codeKey];
|
|
284
|
+
const message = data[messageKey];
|
|
285
|
+
if (successCodes.includes(code)) {
|
|
286
|
+
if (returnData && data.hasOwnProperty(dataKey)) {
|
|
287
|
+
return data[dataKey];
|
|
288
|
+
}
|
|
289
|
+
onSuccess == null ? void 0 : onSuccess(data);
|
|
290
|
+
return data;
|
|
291
|
+
}
|
|
292
|
+
if (ignoreError || ignoreErrorCodes.includes(code)) {
|
|
293
|
+
return data;
|
|
294
|
+
}
|
|
295
|
+
if (logoutCodes.includes(code)) {
|
|
296
|
+
throw new HttpError(message || "Unauthorized", code, data);
|
|
297
|
+
}
|
|
298
|
+
throw new HttpError(message || "Request failed", code, data);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* 从响应头提取文件名
|
|
302
|
+
*/
|
|
303
|
+
extractFilename(headers) {
|
|
304
|
+
const disposition = headers.get("content-disposition");
|
|
305
|
+
if (!disposition)
|
|
306
|
+
return void 0;
|
|
307
|
+
const matches = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
|
|
308
|
+
if (!(matches == null ? void 0 : matches[1]))
|
|
309
|
+
return void 0;
|
|
310
|
+
let filename = matches[1].replace(/['"]/g, "");
|
|
311
|
+
try {
|
|
312
|
+
filename = decodeURIComponent(filename);
|
|
313
|
+
} catch {
|
|
314
|
+
}
|
|
315
|
+
return filename;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* 统一错误处理
|
|
319
|
+
*/
|
|
320
|
+
handleError(error, ignoreError, customOnError, customOnLogout) {
|
|
321
|
+
var _a;
|
|
322
|
+
const httpError = error instanceof HttpError ? error : new HttpError(error.message || "Unknown error", void 0, error);
|
|
323
|
+
if (!ignoreError) {
|
|
324
|
+
const logoutCodes = ((_a = this.configure.codes) == null ? void 0 : _a.logout) ?? DEFAULT_LOGOUT_CODES;
|
|
325
|
+
if (httpError.code && logoutCodes.includes(httpError.code)) {
|
|
326
|
+
const logoutHandler = customOnLogout ?? this.configure.onLogout;
|
|
327
|
+
logoutHandler == null ? void 0 : logoutHandler(httpError);
|
|
328
|
+
}
|
|
329
|
+
const errorHandler = customOnError ?? this.configure.onError;
|
|
330
|
+
errorHandler == null ? void 0 : errorHandler(httpError);
|
|
331
|
+
}
|
|
332
|
+
throw httpError;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
function downloadfile(response) {
|
|
336
|
+
const { data, filename } = response;
|
|
337
|
+
if (!(data instanceof Blob)) {
|
|
338
|
+
throw new TypeError("Data must be a Blob");
|
|
339
|
+
}
|
|
340
|
+
const url = URL.createObjectURL(data);
|
|
341
|
+
const link = document.createElement("a");
|
|
342
|
+
link.href = url;
|
|
343
|
+
link.download = filename || `download-${Date.now()}`;
|
|
344
|
+
link.style.display = "none";
|
|
345
|
+
document.body.appendChild(link);
|
|
346
|
+
link.click();
|
|
347
|
+
setTimeout(() => {
|
|
348
|
+
document.body.removeChild(link);
|
|
349
|
+
URL.revokeObjectURL(url);
|
|
350
|
+
}, 100);
|
|
351
|
+
}
|
|
352
|
+
function createHttpClient(config) {
|
|
353
|
+
return new HttpClient(config);
|
|
354
|
+
}
|
|
355
|
+
var rq = new HttpClient();
|
|
356
|
+
var fetch_default = HttpClient;
|
|
357
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
358
|
+
0 && (module.exports = {
|
|
359
|
+
HttpClient,
|
|
360
|
+
HttpError,
|
|
361
|
+
createHttpClient,
|
|
362
|
+
downloadfile,
|
|
363
|
+
isFunction,
|
|
364
|
+
isObject,
|
|
365
|
+
rq
|
|
366
|
+
});
|