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