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.
Files changed (80) hide show
  1. package/README.md +23 -0
  2. package/es/components/Table/index.d.ts +12 -0
  3. package/es/components/Table/index.js +326 -0
  4. package/es/components/Table/style.css +3 -0
  5. package/es/components/Table/types.d.ts +101 -0
  6. package/es/components/Table/types.js +1 -0
  7. package/es/components/Table/useTable.d.ts +3 -0
  8. package/es/components/Table/useTable.js +76 -0
  9. package/es/components/TableConfig/index.d.ts +11 -0
  10. package/es/components/TableConfig/index.js +19 -0
  11. package/es/components/TableUseFetch/index.d.ts +12 -0
  12. package/es/components/TableUseFetch/index.js +283 -0
  13. package/es/components/TableUseFetch/style.css +3 -0
  14. package/es/components/TableUseFetch/types.d.ts +100 -0
  15. package/es/components/TableUseFetch/types.js +1 -0
  16. package/es/components/TableUseFetch/useTable.d.ts +3 -0
  17. package/es/components/TableUseFetch/useTable.js +78 -0
  18. package/es/components/index.d.ts +3 -0
  19. package/es/components/index.js +3 -0
  20. package/es/fetch.d.ts +137 -0
  21. package/es/fetch.js +534 -0
  22. package/es/hooks/index.d.ts +4 -0
  23. package/es/hooks/index.js +4 -0
  24. package/es/hooks/useFetch.d.ts +17 -0
  25. package/es/hooks/useFetch.js +41 -0
  26. package/es/hooks/useMutation.d.ts +14 -0
  27. package/es/hooks/useMutation.js +26 -0
  28. package/es/hooks/useQuery.d.ts +14 -0
  29. package/es/hooks/useQuery.js +40 -0
  30. package/es/hooks/useX.d.ts +1 -0
  31. package/es/hooks/useX.js +13 -0
  32. package/es/index.d.ts +4 -0
  33. package/es/index.js +4 -0
  34. package/es/utils/index.d.ts +6 -0
  35. package/es/utils/index.js +23 -0
  36. package/es/utils/table.d.ts +15 -0
  37. package/es/utils/table.js +106 -0
  38. package/es/utils/util.d.ts +2 -0
  39. package/es/utils/util.js +6 -0
  40. package/lang/en_US.json +6 -0
  41. package/lang/zh_CN.json +6 -0
  42. package/lib/components/Table/index.d.ts +12 -0
  43. package/lib/components/Table/index.js +330 -0
  44. package/lib/components/Table/style.css +3 -0
  45. package/lib/components/Table/types.d.ts +101 -0
  46. package/lib/components/Table/types.js +17 -0
  47. package/lib/components/Table/useTable.d.ts +3 -0
  48. package/lib/components/Table/useTable.js +85 -0
  49. package/lib/components/TableConfig/index.d.ts +11 -0
  50. package/lib/components/TableConfig/index.js +55 -0
  51. package/lib/components/TableUseFetch/index.d.ts +12 -0
  52. package/lib/components/TableUseFetch/index.js +303 -0
  53. package/lib/components/TableUseFetch/style.css +3 -0
  54. package/lib/components/TableUseFetch/types.d.ts +100 -0
  55. package/lib/components/TableUseFetch/types.js +17 -0
  56. package/lib/components/TableUseFetch/useTable.d.ts +3 -0
  57. package/lib/components/TableUseFetch/useTable.js +86 -0
  58. package/lib/components/index.d.ts +3 -0
  59. package/lib/components/index.js +45 -0
  60. package/lib/fetch.d.ts +137 -0
  61. package/lib/fetch.js +366 -0
  62. package/lib/hooks/index.d.ts +4 -0
  63. package/lib/hooks/index.js +48 -0
  64. package/lib/hooks/useFetch.d.ts +17 -0
  65. package/lib/hooks/useFetch.js +62 -0
  66. package/lib/hooks/useMutation.d.ts +14 -0
  67. package/lib/hooks/useMutation.js +43 -0
  68. package/lib/hooks/useQuery.d.ts +14 -0
  69. package/lib/hooks/useQuery.js +57 -0
  70. package/lib/hooks/useX.d.ts +1 -0
  71. package/lib/hooks/useX.js +37 -0
  72. package/lib/index.d.ts +4 -0
  73. package/lib/index.js +29 -0
  74. package/lib/utils/index.d.ts +6 -0
  75. package/lib/utils/index.js +60 -0
  76. package/lib/utils/table.d.ts +15 -0
  77. package/lib/utils/table.js +124 -0
  78. package/lib/utils/util.d.ts +2 -0
  79. package/lib/utils/util.js +32 -0
  80. package/package.json +46 -0
@@ -0,0 +1,283 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3
+ var _excluded = ["classNames", "styles", "table", "locale", "dataKey", "totalKey", "manual", "nostyle", "request", "columns", "form", "alert", "toolbar", "pageSizeOptions", "pagination", "loadingDelay", "scroll"],
4
+ _excluded2 = ["title", "extra", "right", "formItem", "items", "reset", "dataForm", "handleValues", "onResetBefore"];
5
+ import { useShallow } from "zustand/react/shallow";
6
+ import { useEffect, useMemo } from "react";
7
+ import { Table, Form, Button, Space } from "antd";
8
+ import { getDataSource, getQuery, getTotal, formatDate, removeEmpty } from "../../utils/table";
9
+ import { isObject } from "../../utils/util";
10
+ import useFetch from "../../hooks/useFetch";
11
+ import useX from "../../hooks/useX";
12
+ import useTable from "./useTable";
13
+ import "./style.css";
14
+ import { jsx as _jsx } from "react/jsx-runtime";
15
+ import { jsxs as _jsxs } from "react/jsx-runtime";
16
+ var defaultClassNames = {
17
+ root: "main-container",
18
+ form: "search-form",
19
+ table: "main-table"
20
+ };
21
+ var defaultStyles = {
22
+ root: {},
23
+ form: {
24
+ display: "flex",
25
+ justifyContent: "space-between"
26
+ },
27
+ table: {},
28
+ toolbar: {
29
+ marginBottom: 15
30
+ }
31
+ };
32
+ var ProTable = function ProTable(props) {
33
+ var _classNames$root, _classNames$form, _classNames$table;
34
+ var _props$classNames = props.classNames,
35
+ classNames = _props$classNames === void 0 ? defaultClassNames : _props$classNames,
36
+ _props$styles = props.styles,
37
+ styles = _props$styles === void 0 ? defaultStyles : _props$styles,
38
+ table = props.table,
39
+ locale = props.locale,
40
+ _props$dataKey = props.dataKey,
41
+ dataKey = _props$dataKey === void 0 ? "data" : _props$dataKey,
42
+ _props$totalKey = props.totalKey,
43
+ totalKey = _props$totalKey === void 0 ? "total" : _props$totalKey,
44
+ _props$manual = props.manual,
45
+ manual = _props$manual === void 0 ? false : _props$manual,
46
+ nostyle = props.nostyle,
47
+ _props$request = props.request,
48
+ request = _props$request === void 0 ? {} : _props$request,
49
+ columns = props.columns,
50
+ _props$form = props.form,
51
+ form = _props$form === void 0 ? {} : _props$form,
52
+ alert = props.alert,
53
+ _props$toolbar = props.toolbar,
54
+ toolbar = _props$toolbar === void 0 ? null : _props$toolbar,
55
+ _props$pageSizeOption = props.pageSizeOptions,
56
+ pageSizeOptions = _props$pageSizeOption === void 0 ? [10, 20, 50, 100] : _props$pageSizeOption,
57
+ pagination = props.pagination,
58
+ _props$loadingDelay = props.loadingDelay,
59
+ loadingDelay = _props$loadingDelay === void 0 ? 300 : _props$loadingDelay,
60
+ scroll = props.scroll,
61
+ prop = _objectWithoutProperties(props, _excluded);
62
+ var formTitle = form.title,
63
+ formExtra = form.extra,
64
+ formRight = form.right,
65
+ formItem = form.formItem,
66
+ items = form.items,
67
+ formReset = form.reset,
68
+ dataForm = form.dataForm,
69
+ formHandleValues = form.handleValues,
70
+ formOnResetBefore = form.onResetBefore,
71
+ otherFormProps = _objectWithoutProperties(form, _excluded2);
72
+ var formItems = formItem || items;
73
+ var _table$useStore = table.useStore(useShallow(function (state) {
74
+ return {
75
+ data: state.data,
76
+ page: state.page,
77
+ size: state.size,
78
+ sorter: state.sorter,
79
+ search: state.search,
80
+ ready: state.ready,
81
+ setState: state.setState
82
+ };
83
+ })),
84
+ data = _table$useStore.data,
85
+ page = _table$useStore.page,
86
+ size = _table$useStore.size,
87
+ sorter = _table$useStore.sorter,
88
+ search = _table$useStore.search,
89
+ ready = _table$useStore.ready,
90
+ setState = _table$useStore.setState;
91
+ var _useFetch = useFetch(request.url, {
92
+ method: request.method,
93
+ onBefore: request.onBefore,
94
+ json: ProTable.getQuery({
95
+ page: page,
96
+ size: size,
97
+ sorter: sorter,
98
+ search: search,
99
+ params: request.params
100
+ }),
101
+ ready: ready,
102
+ onFinally: function onFinally() {
103
+ setState({
104
+ ready: false
105
+ });
106
+ },
107
+ onSuccess: function onSuccess(data) {
108
+ setState({
109
+ data: data
110
+ });
111
+ },
112
+ loadingDelay: loadingDelay
113
+ }),
114
+ loading = _useFetch.loading;
115
+ var _useMemo = useMemo(function () {
116
+ return {
117
+ column: typeof columns === "function" ? columns(data) : columns,
118
+ dataSource: getDataSource(data, dataKey),
119
+ renderAlert: typeof alert === "function" ? alert(data) : alert,
120
+ total: getTotal(totalKey, data)
121
+ };
122
+ }, [columns, data, dataKey, totalKey]),
123
+ dataSource = _useMemo.dataSource,
124
+ total = _useMemo.total,
125
+ column = _useMemo.column,
126
+ renderAlert = _useMemo.renderAlert;
127
+ var onSearch = function onSearch() {
128
+ if (formItems) {
129
+ table.form.submit();
130
+ } else {
131
+ setState({
132
+ ready: true
133
+ });
134
+ }
135
+ };
136
+ var onReset = function onReset() {
137
+ setState({
138
+ size: 10,
139
+ sorter: {}
140
+ });
141
+ if (formItems) {
142
+ if ((formOnResetBefore === null || formOnResetBefore === void 0 ? void 0 : formOnResetBefore()) === false) return;
143
+ table.form.resetFields();
144
+ if (formReset === undefined || formReset === true) {
145
+ table.form.submit();
146
+ }
147
+ }
148
+ };
149
+ if (table) {
150
+ table.run = onSearch;
151
+ table.reset = function () {
152
+ if (formItems) {
153
+ onReset();
154
+ }
155
+ };
156
+ }
157
+ useEffect(function () {
158
+ if (manual) return;
159
+ if (formItems) {
160
+ table.form.submit();
161
+ } else {
162
+ setState({
163
+ ready: true
164
+ });
165
+ }
166
+ }, [table]);
167
+ var onFinish = function onFinish(values) {
168
+ if (formHandleValues) {
169
+ values = formHandleValues(values);
170
+ }
171
+ if (!values) return;
172
+ setState({
173
+ page: 1,
174
+ search: values,
175
+ ready: true
176
+ });
177
+ };
178
+ var tableChange = function tableChange(pagination, sorter) {
179
+ setState({
180
+ page: pagination.current,
181
+ size: pagination.pageSize,
182
+ sorter: sorter,
183
+ ready: true
184
+ });
185
+ };
186
+ var x = useX(column);
187
+ var y = scroll === null || scroll === void 0 ? void 0 : scroll.y;
188
+ var renderTable = function renderTable() {
189
+ return /*#__PURE__*/_jsx(Table, _objectSpread({
190
+ columns: column,
191
+ loading: loading,
192
+ scroll: {
193
+ x: x,
194
+ y: y
195
+ },
196
+ locale: locale,
197
+ onChange: function onChange(p, _, sorter) {
198
+ return tableChange(p, sorter);
199
+ },
200
+ pagination: {
201
+ current: page,
202
+ pageSize: size,
203
+ showQuickJumper: pagination ? pagination.showQuickJumper : true,
204
+ showSizeChanger: pagination ? pagination.showSizeChanger : true,
205
+ hideOnSinglePage: pagination ? pagination.hideOnSinglePage : false,
206
+ pageSizeOptions: pageSizeOptions,
207
+ total: total,
208
+ showTotal: function showTotal(total) {
209
+ return "\u5171 ".concat(total, " \u6761\u8BB0\u5F55");
210
+ }
211
+ },
212
+ dataSource: dataSource
213
+ }, prop));
214
+ };
215
+ return /*#__PURE__*/_jsxs("div", {
216
+ className: (_classNames$root = classNames === null || classNames === void 0 ? void 0 : classNames.root) !== null && _classNames$root !== void 0 ? _classNames$root : defaultClassNames.root,
217
+ style: styles.root,
218
+ children: [!!formItems && /*#__PURE__*/_jsxs("div", {
219
+ className: (_classNames$form = classNames === null || classNames === void 0 ? void 0 : classNames.form) !== null && _classNames$form !== void 0 ? _classNames$form : defaultClassNames.form,
220
+ style: styles.form,
221
+ children: [/*#__PURE__*/_jsxs(Form, _objectSpread(_objectSpread({
222
+ form: table.form,
223
+ layout: "inline",
224
+ onFinish: onFinish
225
+ }, otherFormProps), {}, {
226
+ children: [formTitle && /*#__PURE__*/_jsx(Form.Item, {
227
+ children: formTitle
228
+ }), formItems, /*#__PURE__*/_jsx(Form.Item, {
229
+ children: /*#__PURE__*/_jsxs(Space, {
230
+ children: [/*#__PURE__*/_jsx(Button, {
231
+ type: "primary",
232
+ loading: loading,
233
+ htmlType: "submit",
234
+ children: "\u67E5\u8BE2"
235
+ }), /*#__PURE__*/_jsx(Button, {
236
+ onClick: onReset,
237
+ disabled: loading,
238
+ children: "\u91CD\u7F6E"
239
+ }), formExtra]
240
+ })
241
+ })]
242
+ })), formRight]
243
+ }), /*#__PURE__*/_jsxs("div", {
244
+ className: (_classNames$table = classNames === null || classNames === void 0 ? void 0 : classNames.table) !== null && _classNames$table !== void 0 ? _classNames$table : defaultClassNames.table,
245
+ style: styles.table,
246
+ children: [toolbar && /*#__PURE__*/_jsx("div", {
247
+ style: styles.toolbar,
248
+ children: toolbar
249
+ }), renderAlert, !!dataForm ? /*#__PURE__*/_jsx(Form, _objectSpread(_objectSpread({}, dataForm), {}, {
250
+ children: renderTable()
251
+ })) : renderTable()]
252
+ })]
253
+ });
254
+ };
255
+ ProTable.useTable = useTable;
256
+ ProTable.getQuery = function (options) {
257
+ var page = options.page,
258
+ size = options.size,
259
+ sorter = options.sorter,
260
+ search = options.search,
261
+ params = options.params;
262
+ return getQuery({
263
+ page: page,
264
+ size: size,
265
+ sorter: sorter,
266
+ search: search,
267
+ params: params
268
+ });
269
+ };
270
+ ProTable.formatDate = formatDate;
271
+ ProTable.removeEmpty = removeEmpty;
272
+ ProTable.pageSizeOptions = [10, 20, 50, 100];
273
+ //自定义配置参数组合方式. 默认提供 page,size,orderField,isAsc,...urlParams,...search
274
+ ProTable.config = function (options) {
275
+ if (!options || !isObject(options)) return;
276
+ if (options.getQuery) {
277
+ ProTable.getQuery = options.getQuery;
278
+ }
279
+ if (options.pageSizeOptions) {
280
+ ProTable.pageSizeOptions = options.pageSizeOptions;
281
+ }
282
+ };
283
+ export default ProTable;
@@ -0,0 +1,3 @@
1
+ .search-form .ant-form-item {
2
+ margin-bottom: 20px!important;
3
+ }
@@ -0,0 +1,100 @@
1
+ /// <reference types="react" />
2
+ import type { FormInstance, TableColumnType, TableProps, FormProps } from "antd";
3
+ import type { HttpMethod } from "../../fetch";
4
+ import type { UseBoundStore, StoreApi } from "zustand";
5
+ import type { GetQueryProps } from "../../utils/table";
6
+ type RecordType = Record<string, any>;
7
+ interface SorterType {
8
+ field?: string;
9
+ order?: "ascend" | "descend";
10
+ }
11
+ export interface TableState<TData = any> {
12
+ page: number;
13
+ size: number;
14
+ sorter: SorterType;
15
+ data: TData;
16
+ search: RecordType | null | undefined;
17
+ params: any[] | Record<string, any> | null | undefined;
18
+ setState: (values: Partial<TableState>) => void;
19
+ ready: boolean;
20
+ }
21
+ type UseStoreType<TData> = UseBoundStore<StoreApi<TableState<TData>>>;
22
+ export interface TableInstance<TData = any> {
23
+ useStore: UseStoreType<TData>;
24
+ run: () => void;
25
+ clear: () => void;
26
+ refresh: () => void;
27
+ reset: () => void;
28
+ sortOrder: (key: string) => any;
29
+ update: () => void;
30
+ form?: FormInstance;
31
+ }
32
+ interface FormOptions extends Omit<FormProps, "form" | "title"> {
33
+ title?: React.ReactNode;
34
+ /** @deprecated 此属性已废弃,请使用新的formItem属性代替 */
35
+ items?: React.ReactNode | React.ReactNode[];
36
+ formItem?: React.ReactNode | React.ReactNode[];
37
+ extra?: React.ReactNode;
38
+ right?: React.ReactNode;
39
+ handleValues?: (values: Record<string, any>) => any;
40
+ reset?: boolean;
41
+ onResetBefore?: () => void | boolean;
42
+ dataForm?: FormProps;
43
+ }
44
+ export interface ProTableProps<Tdata = any> extends Omit<TableProps<Tdata>, "columns"> {
45
+ classNames?: {
46
+ root?: string;
47
+ form?: string;
48
+ table?: string;
49
+ };
50
+ styles?: {
51
+ root?: React.CSSProperties;
52
+ form?: React.CSSProperties;
53
+ table?: React.CSSProperties;
54
+ toolbar?: React.CSSProperties;
55
+ };
56
+ request?: {
57
+ url?: string;
58
+ method?: HttpMethod;
59
+ params?: RecordType;
60
+ onBefore?: () => any;
61
+ onSuccess?: (data: Tdata) => any;
62
+ };
63
+ table: TableInstance<Tdata> | null;
64
+ locale?: Record<string, any>;
65
+ dataKey?: string;
66
+ totalKey?: string;
67
+ manual?: boolean;
68
+ nostyle?: boolean;
69
+ columns: ((data: Tdata) => TableColumnType<unknown>[]) | TableColumnType<unknown>[];
70
+ form?: FormOptions;
71
+ alert?: React.ReactNode | ((data: Tdata) => React.ReactNode);
72
+ toolbar?: React.ReactNode;
73
+ pageSizeOptions?: number[];
74
+ pagination?: {
75
+ showQuickJumper?: boolean;
76
+ showSizeChanger?: boolean;
77
+ hideOnSinglePage?: boolean;
78
+ };
79
+ loadingDelay?: number;
80
+ }
81
+ export interface UseTableProps {
82
+ page?: number;
83
+ size?: number;
84
+ sorter?: SorterType;
85
+ }
86
+ export interface TableRef {
87
+ form: FormInstance;
88
+ useStore: UseStoreType<any>;
89
+ run: () => void;
90
+ clear: () => void;
91
+ refresh: () => void;
92
+ reset: () => void;
93
+ sortOrder: (key: string) => "ascend" | "descend" | null;
94
+ update: () => void;
95
+ }
96
+ export interface ProTableConfigOptions {
97
+ getQuery?: (data: GetQueryProps) => Record<string, unknown>;
98
+ pageSizeOptions?: number[];
99
+ }
100
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { UseTableProps, TableRef } from "./types";
2
+ declare const useTable: (options?: UseTableProps) => TableRef[];
3
+ export default useTable;
@@ -0,0 +1,78 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
+ import { Form } from "antd";
4
+ import { useRef, useState } from "react";
5
+ import { create } from "zustand";
6
+ var useTable = function useTable() {
7
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
8
+ var _useState = useState(0),
9
+ _useState2 = _slicedToArray(_useState, 2),
10
+ _ = _useState2[0],
11
+ _update = _useState2[1];
12
+ var _Form$useForm = Form.useForm(),
13
+ _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
14
+ form = _Form$useForm2[0];
15
+ var tableRef = useRef(null);
16
+ if (!tableRef.current) {
17
+ var useStore = create(function (set) {
18
+ var _options$page, _options$size, _options$page2, _options$size2;
19
+ return {
20
+ page: (_options$page = options.page) !== null && _options$page !== void 0 ? _options$page : 1,
21
+ size: (_options$size = options.size) !== null && _options$size !== void 0 ? _options$size : 10,
22
+ sorter: options.sorter || {},
23
+ search: {},
24
+ params: {
25
+ page: (_options$page2 = options.page) !== null && _options$page2 !== void 0 ? _options$page2 : 1,
26
+ size: (_options$size2 = options.size) !== null && _options$size2 !== void 0 ? _options$size2 : 10,
27
+ sorter: options.sorter || {},
28
+ form: {}
29
+ },
30
+ data: {},
31
+ ready: false,
32
+ setState: function setState() {
33
+ var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
34
+ set(values);
35
+ }
36
+ };
37
+ });
38
+ tableRef.current = {
39
+ form: form,
40
+ useStore: useStore,
41
+ run: function run() {},
42
+ clear: function clear() {
43
+ useStore.setState({
44
+ data: {}
45
+ });
46
+ },
47
+ refresh: function refresh() {
48
+ useStore.setState({
49
+ ready: true
50
+ });
51
+ },
52
+ reset: function reset() {},
53
+ sortOrder: function sortOrder(key) {
54
+ var sorter = useStore.getState().sorter;
55
+ if (sorter && sorter.field === key) {
56
+ return sorter.order;
57
+ }
58
+ return null;
59
+ },
60
+ update: function update() {
61
+ return _update(function (v) {
62
+ return v + 1;
63
+ });
64
+ }
65
+ };
66
+ useStore.subscribe(function (state, prevState) {
67
+ var _state$sorter, _prevState$sorter;
68
+ if (state.sorter !== prevState.sorter && ((_state$sorter = state.sorter) === null || _state$sorter === void 0 ? void 0 : _state$sorter.order) !== ((_prevState$sorter = prevState.sorter) === null || _prevState$sorter === void 0 ? void 0 : _prevState$sorter.order)) {
69
+ tableRef.current = _objectSpread({}, tableRef.current);
70
+ _update(function (v) {
71
+ return v + 1;
72
+ });
73
+ }
74
+ });
75
+ }
76
+ return [tableRef.current];
77
+ };
78
+ export default useTable;
@@ -0,0 +1,3 @@
1
+ export { default as TableConfig } from "./TableConfig";
2
+ export { default as Table } from "./Table";
3
+ export { default as TableUseFetch } from "./TableUseFetch";
@@ -0,0 +1,3 @@
1
+ export { default as TableConfig } from "./TableConfig";
2
+ export { default as Table } from "./Table";
3
+ export { default as TableUseFetch } from "./TableUseFetch";
package/es/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;