crud-page-react 0.0.7 → 0.1.1
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 +102 -4
- package/dist/index.d.ts +6 -6
- package/dist/index.esm.js +8 -26
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +8 -26
- package/dist/index.js.map +1 -1
- package/dist/types/schema.d.ts +6 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,6 +36,8 @@ yarn add crud-page-react
|
|
|
36
36
|
|
|
37
37
|
## 快速开始
|
|
38
38
|
|
|
39
|
+
### 基础用法
|
|
40
|
+
|
|
39
41
|
```tsx
|
|
40
42
|
import React from 'react';
|
|
41
43
|
import { CrudPage } from 'crud-page-react';
|
|
@@ -45,10 +47,22 @@ const schema: CrudPageSchema = {
|
|
|
45
47
|
id: 'users',
|
|
46
48
|
title: '用户管理',
|
|
47
49
|
api: {
|
|
48
|
-
list:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
list: {
|
|
51
|
+
url: '/api/users',
|
|
52
|
+
method: 'GET'
|
|
53
|
+
},
|
|
54
|
+
create: {
|
|
55
|
+
url: '/api/users',
|
|
56
|
+
method: 'POST'
|
|
57
|
+
},
|
|
58
|
+
update: {
|
|
59
|
+
url: '/api/users/:id',
|
|
60
|
+
method: 'PUT'
|
|
61
|
+
},
|
|
62
|
+
delete: {
|
|
63
|
+
url: '/api/users/:id',
|
|
64
|
+
method: 'DELETE'
|
|
65
|
+
},
|
|
52
66
|
},
|
|
53
67
|
fields: [
|
|
54
68
|
{
|
|
@@ -105,6 +119,90 @@ function App() {
|
|
|
105
119
|
export default App;
|
|
106
120
|
```
|
|
107
121
|
|
|
122
|
+
### 扩展 API 配置
|
|
123
|
+
|
|
124
|
+
v0.1.0+ 版本要求使用完整的 API 配置对象,支持自定义 HTTP 方法、请求头、请求体数据和模板变量:
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
const advancedSchema: CrudPageSchema = {
|
|
128
|
+
id: 'orders',
|
|
129
|
+
title: '订单管理',
|
|
130
|
+
api: {
|
|
131
|
+
// 基础配置
|
|
132
|
+
list: {
|
|
133
|
+
url: '/api/orders',
|
|
134
|
+
method: 'GET'
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
// 扩展配置
|
|
138
|
+
create: {
|
|
139
|
+
url: '/api/orders',
|
|
140
|
+
method: 'POST',
|
|
141
|
+
headers: {
|
|
142
|
+
'X-Request-Source': 'admin-panel'
|
|
143
|
+
},
|
|
144
|
+
data: {
|
|
145
|
+
source: 'web',
|
|
146
|
+
createdBy: '{{currentUser}}',
|
|
147
|
+
timestamp: '{{timestamp}}'
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
update: {
|
|
152
|
+
url: '/api/orders/:id',
|
|
153
|
+
method: 'PUT',
|
|
154
|
+
data: {
|
|
155
|
+
updatedBy: '{{currentUser}}',
|
|
156
|
+
updateTime: '{{timestamp}}'
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
delete: {
|
|
161
|
+
url: '/api/orders/:orderNo',
|
|
162
|
+
method: 'DELETE',
|
|
163
|
+
data: {
|
|
164
|
+
deletedBy: '{{currentUser}}',
|
|
165
|
+
deleteReason: '{{deleteReason}}',
|
|
166
|
+
timestamp: '{{timestamp}}'
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
// ... 其他配置
|
|
171
|
+
};
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 支持的配置选项
|
|
175
|
+
|
|
176
|
+
- **HTTP 方法**:GET、POST、PUT、PATCH、DELETE
|
|
177
|
+
- **自定义请求头**:添加认证、来源标识等
|
|
178
|
+
- **请求体数据**:固定参数和动态参数
|
|
179
|
+
- **模板变量**:`{{fieldName}}` 格式的动态值替换
|
|
180
|
+
- **URL 占位符**:`:fieldName` 格式的动态 URL 构建
|
|
181
|
+
|
|
182
|
+
### 💥 Breaking Changes (v0.1.0)
|
|
183
|
+
|
|
184
|
+
v0.1.0 版本移除了简单字符串配置支持,所有 API 配置必须使用对象格式:
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
// ❌ 不再支持 (v0.0.x)
|
|
188
|
+
api: {
|
|
189
|
+
list: '/api/users',
|
|
190
|
+
create: '/api/users'
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ✅ 必须使用 (v0.1.0+)
|
|
194
|
+
api: {
|
|
195
|
+
list: {
|
|
196
|
+
url: '/api/users',
|
|
197
|
+
method: 'GET'
|
|
198
|
+
},
|
|
199
|
+
create: {
|
|
200
|
+
url: '/api/users',
|
|
201
|
+
method: 'POST'
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
108
206
|
## 动态 URL 参数
|
|
109
207
|
|
|
110
208
|
支持在 API 配置中使用任意字段作为 URL 参数:
|
package/dist/index.d.ts
CHANGED
|
@@ -161,12 +161,12 @@ interface CrudPageSchema {
|
|
|
161
161
|
id: string;
|
|
162
162
|
title: string;
|
|
163
163
|
api: {
|
|
164
|
-
list:
|
|
165
|
-
create?:
|
|
166
|
-
update?:
|
|
167
|
-
delete?:
|
|
168
|
-
detail?:
|
|
169
|
-
[key: string]:
|
|
164
|
+
list: ActionApiConfig;
|
|
165
|
+
create?: ActionApiConfig;
|
|
166
|
+
update?: ActionApiConfig;
|
|
167
|
+
delete?: ActionApiConfig;
|
|
168
|
+
detail?: ActionApiConfig;
|
|
169
|
+
[key: string]: ActionApiConfig | undefined;
|
|
170
170
|
};
|
|
171
171
|
fields: FieldSchema[];
|
|
172
172
|
actions?: ActionSchema[];
|
package/dist/index.esm.js
CHANGED
|
@@ -827,24 +827,6 @@ function processTemplateData(data, record) {
|
|
|
827
827
|
}
|
|
828
828
|
return result;
|
|
829
829
|
}
|
|
830
|
-
/** 解析 API 配置,支持字符串和对象两种格式 */
|
|
831
|
-
function parseApiConfig(apiDef) {
|
|
832
|
-
if (!apiDef)
|
|
833
|
-
return undefined;
|
|
834
|
-
if (typeof apiDef === 'string') {
|
|
835
|
-
// 简单字符串 URL 配置
|
|
836
|
-
return {
|
|
837
|
-
url: apiDef,
|
|
838
|
-
method: 'GET',
|
|
839
|
-
responseType: 'json'
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
else if (typeof apiDef === 'object') {
|
|
843
|
-
// 完整的 API 配置对象
|
|
844
|
-
return apiDef;
|
|
845
|
-
}
|
|
846
|
-
return undefined;
|
|
847
|
-
}
|
|
848
830
|
/** 通用请求封装 */
|
|
849
831
|
async function apiRequest(url, options) {
|
|
850
832
|
const res = await fetch(url, Object.assign({ headers: { 'Content-Type': 'application/json' } }, options));
|
|
@@ -893,12 +875,12 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
893
875
|
}, []);
|
|
894
876
|
// ---------- 获取列表 ----------
|
|
895
877
|
const fetchList = useCallback(async (params = filterParams, p = page, ps = pageSize) => {
|
|
896
|
-
|
|
897
|
-
if (!listApiConfig) {
|
|
878
|
+
if (!schema.api.list) {
|
|
898
879
|
// 没有配置 API,使用初始数据
|
|
899
880
|
initializeData();
|
|
900
881
|
return;
|
|
901
882
|
}
|
|
883
|
+
const listApiConfig = schema.api.list;
|
|
902
884
|
setLoading(true);
|
|
903
885
|
try {
|
|
904
886
|
// 构建查询参数
|
|
@@ -963,11 +945,11 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
963
945
|
}, []);
|
|
964
946
|
// ---------- 删除 ----------
|
|
965
947
|
const handleDelete = useCallback(async (record) => {
|
|
966
|
-
|
|
967
|
-
if (!deleteApiConfig) {
|
|
948
|
+
if (!schema.api.delete) {
|
|
968
949
|
messageApi.error('删除功能未配置');
|
|
969
950
|
return;
|
|
970
951
|
}
|
|
952
|
+
const deleteApiConfig = schema.api.delete;
|
|
971
953
|
try {
|
|
972
954
|
// 构建 URL,动态替换占位符
|
|
973
955
|
let url = deleteApiConfig.url;
|
|
@@ -1087,11 +1069,11 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
1087
1069
|
const handleFormOk = useCallback(async (values) => {
|
|
1088
1070
|
const isCreate = modalState.mode === 'create';
|
|
1089
1071
|
if (isCreate) {
|
|
1090
|
-
|
|
1091
|
-
if (!createApiConfig) {
|
|
1072
|
+
if (!schema.api.create) {
|
|
1092
1073
|
messageApi.error('新增功能未配置');
|
|
1093
1074
|
return;
|
|
1094
1075
|
}
|
|
1076
|
+
const createApiConfig = schema.api.create;
|
|
1095
1077
|
try {
|
|
1096
1078
|
// 构建请求选项
|
|
1097
1079
|
const options = {
|
|
@@ -1116,11 +1098,11 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
1116
1098
|
}
|
|
1117
1099
|
}
|
|
1118
1100
|
else {
|
|
1119
|
-
|
|
1120
|
-
if (!updateApiConfig) {
|
|
1101
|
+
if (!schema.api.update) {
|
|
1121
1102
|
messageApi.error('编辑功能未配置');
|
|
1122
1103
|
return;
|
|
1123
1104
|
}
|
|
1105
|
+
const updateApiConfig = schema.api.update;
|
|
1124
1106
|
try {
|
|
1125
1107
|
// 构建 URL,动态替换占位符
|
|
1126
1108
|
let url = updateApiConfig.url;
|