crud-page-react 0.0.7 → 0.1.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 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';
@@ -105,6 +107,63 @@ function App() {
105
107
  export default App;
106
108
  ```
107
109
 
110
+ ### 扩展 API 配置
111
+
112
+ v0.0.7+ 版本支持更强大的 API 配置,包括自定义 HTTP 方法、请求头、请求体数据和模板变量:
113
+
114
+ ```tsx
115
+ const advancedSchema: CrudPageSchema = {
116
+ id: 'orders',
117
+ title: '订单管理',
118
+ api: {
119
+ // 简单字符串配置(向后兼容)
120
+ list: '/api/orders',
121
+
122
+ // 扩展对象配置
123
+ create: {
124
+ url: '/api/orders',
125
+ method: 'POST',
126
+ headers: {
127
+ 'X-Request-Source': 'admin-panel'
128
+ },
129
+ data: {
130
+ source: 'web',
131
+ createdBy: '{{currentUser}}',
132
+ timestamp: '{{timestamp}}'
133
+ }
134
+ },
135
+
136
+ update: {
137
+ url: '/api/orders/:id',
138
+ method: 'PUT',
139
+ data: {
140
+ updatedBy: '{{currentUser}}',
141
+ updateTime: '{{timestamp}}'
142
+ }
143
+ },
144
+
145
+ delete: {
146
+ url: '/api/orders/:orderNo',
147
+ method: 'DELETE',
148
+ data: {
149
+ deletedBy: '{{currentUser}}',
150
+ deleteReason: '{{deleteReason}}',
151
+ timestamp: '{{timestamp}}'
152
+ }
153
+ }
154
+ },
155
+ // ... 其他配置
156
+ };
157
+ ```
158
+
159
+ ### 支持的扩展配置
160
+
161
+ - **HTTP 方法**:GET、POST、PUT、PATCH、DELETE
162
+ - **自定义请求头**:添加认证、来源标识等
163
+ - **请求体数据**:固定参数和动态参数
164
+ - **模板变量**:`{{fieldName}}` 格式的动态值替换
165
+ - **URL 占位符**:`:fieldName` 格式的动态 URL 构建
166
+
108
167
  ## 动态 URL 参数
109
168
 
110
169
  支持在 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: string | ActionApiConfig;
165
- create?: string | ActionApiConfig;
166
- update?: string | ActionApiConfig;
167
- delete?: string | ActionApiConfig;
168
- detail?: string | ActionApiConfig;
169
- [key: string]: string | ActionApiConfig | undefined;
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
- const listApiConfig = parseApiConfig(schema.api.list);
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
- const deleteApiConfig = parseApiConfig(schema.api.delete);
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
- const createApiConfig = parseApiConfig(schema.api.create);
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
- const updateApiConfig = parseApiConfig(schema.api.update);
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;