crud-page-react 0.0.5 → 0.0.6
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/index.d.ts +2 -0
- package/dist/index.esm.js +52 -11
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +52 -11
- package/dist/index.js.map +1 -1
- package/dist/types/schema.d.ts +2 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -150,6 +150,7 @@ interface ActionSchema {
|
|
|
150
150
|
content?: string;
|
|
151
151
|
};
|
|
152
152
|
api?: ActionApiConfig;
|
|
153
|
+
apiKey?: string;
|
|
153
154
|
}
|
|
154
155
|
interface PaginationConfig {
|
|
155
156
|
pageSize?: number;
|
|
@@ -165,6 +166,7 @@ interface CrudPageSchema {
|
|
|
165
166
|
update?: string;
|
|
166
167
|
delete?: string;
|
|
167
168
|
detail?: string;
|
|
169
|
+
[key: string]: string | ActionApiConfig | undefined;
|
|
168
170
|
};
|
|
169
171
|
fields: FieldSchema[];
|
|
170
172
|
actions?: ActionSchema[];
|
package/dist/index.esm.js
CHANGED
|
@@ -817,6 +817,23 @@ function buildUrl(template, record) {
|
|
|
817
817
|
return value !== undefined ? String(value) : match;
|
|
818
818
|
});
|
|
819
819
|
}
|
|
820
|
+
/** 处理模板数据,支持 {{fieldName}} 格式的变量替换 */
|
|
821
|
+
function processTemplateData(data, record) {
|
|
822
|
+
const result = {};
|
|
823
|
+
for (const [key, value] of Object.entries(data)) {
|
|
824
|
+
if (typeof value === 'string' && value.includes('{{') && value.includes('}}')) {
|
|
825
|
+
// 替换模板变量 {{fieldName}}
|
|
826
|
+
result[key] = value.replace(/\{\{(\w+)\}\}/g, (match, fieldName) => {
|
|
827
|
+
const fieldValue = record[fieldName];
|
|
828
|
+
return fieldValue !== undefined ? String(fieldValue) : match;
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
else {
|
|
832
|
+
result[key] = value;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return result;
|
|
836
|
+
}
|
|
820
837
|
/** 通用请求封装 */
|
|
821
838
|
async function apiRequest(url, options) {
|
|
822
839
|
const res = await fetch(url, Object.assign({ headers: { 'Content-Type': 'application/json' } }, options));
|
|
@@ -940,31 +957,55 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
940
957
|
else if (action.type === 'delete') {
|
|
941
958
|
handleDelete(record);
|
|
942
959
|
}
|
|
943
|
-
else if (action.type === 'custom'
|
|
960
|
+
else if (action.type === 'custom') {
|
|
944
961
|
// 处理自定义 action 的 API 调用
|
|
962
|
+
let apiConfig;
|
|
963
|
+
// 优先使用 apiKey 引用统一配置,向后兼容 api 直接配置
|
|
964
|
+
if (action.apiKey) {
|
|
965
|
+
const apiDef = schema.api[action.apiKey];
|
|
966
|
+
if (typeof apiDef === 'string') {
|
|
967
|
+
// 简单字符串 URL 配置
|
|
968
|
+
apiConfig = {
|
|
969
|
+
url: apiDef,
|
|
970
|
+
method: 'GET',
|
|
971
|
+
responseType: 'json'
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
else if (apiDef && typeof apiDef === 'object') {
|
|
975
|
+
// 完整的 API 配置对象
|
|
976
|
+
apiConfig = apiDef;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
else if (action.api) {
|
|
980
|
+
// 向后兼容:使用 action.api 配置
|
|
981
|
+
apiConfig = action.api;
|
|
982
|
+
}
|
|
983
|
+
if (!apiConfig) {
|
|
984
|
+
messageApi.error(`${action.label}未配置 API`);
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
945
987
|
try {
|
|
946
988
|
// 构建 URL,动态替换占位符
|
|
947
|
-
let url =
|
|
948
|
-
// 替换所有 :fieldName 格式的占位符
|
|
989
|
+
let url = apiConfig.url;
|
|
949
990
|
url = url.replace(/:(\w+)/g, (match, fieldName) => {
|
|
950
991
|
const value = record[fieldName];
|
|
951
992
|
return value !== undefined ? String(value) : match;
|
|
952
993
|
});
|
|
953
994
|
// 构建请求选项
|
|
954
995
|
const options = {
|
|
955
|
-
method:
|
|
956
|
-
headers: Object.assign({ 'Content-Type': 'application/json' },
|
|
996
|
+
method: apiConfig.method || 'GET',
|
|
997
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, apiConfig.headers)
|
|
957
998
|
};
|
|
958
|
-
//
|
|
959
|
-
if (
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
999
|
+
// 处理请求体数据
|
|
1000
|
+
if (apiConfig.data && ['POST', 'PUT', 'PATCH'].includes(apiConfig.method || 'GET')) {
|
|
1001
|
+
// 支持模板变量替换
|
|
1002
|
+
const processedData = processTemplateData(apiConfig.data, record);
|
|
1003
|
+
options.body = JSON.stringify(Object.assign(Object.assign({}, processedData), { recordId: record[rowKey], timestamp: new Date().toISOString() }));
|
|
963
1004
|
}
|
|
964
1005
|
// 调用 API
|
|
965
1006
|
const response = await request(url, options);
|
|
966
1007
|
// 处理特殊响应类型
|
|
967
|
-
if (
|
|
1008
|
+
if (apiConfig.responseType === 'blob') {
|
|
968
1009
|
// 处理文件下载
|
|
969
1010
|
const blob = new Blob([response]);
|
|
970
1011
|
const downloadUrl = URL.createObjectURL(blob);
|