@hzab/data-model 1.0.1 → 1.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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # @hzab/data-model@1.1.0
2
+
3
+ - formData 问题修复
4
+
5
+ # @hzab/data-model@1.0.2
6
+
7
+ - fix: getApiUrl 增加调用来源,错误提示增加入参展示
8
+
1
9
  # @hzab/data-model@1.0.1
2
10
 
3
11
  - fix: 修复 delete 入参问题
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/data-model",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "data model",
5
5
  "main": "src",
6
6
  "scripts": {
package/src/data-model.js CHANGED
@@ -71,9 +71,12 @@ class DataModel {
71
71
  this.multipleDeleteResMap = multipleDeleteResMap;
72
72
  }
73
73
 
74
- getApiUrl(api, record, ctx = this.ctx) {
74
+ getApiUrl(api, record, ctx = this.ctx, opt) {
75
+ const { from } = opt ?? {};
75
76
  if (!api) {
76
- throw new Error("Error api 不能为空", api, record, ctx);
77
+ const errMsg = `${from ?? ""} api 不能为空。`;
78
+ console.error(`Error ${errMsg} 具体入参:api、record、ctx、opt:`, api, record, ctx, opt);
79
+ throw new Error(errMsg);
77
80
  }
78
81
  let apiUrl = api;
79
82
  const params = _.merge({}, record, ctx);
@@ -94,7 +97,7 @@ class DataModel {
94
97
  }
95
98
 
96
99
  return new Promise((resolve, reject) => {
97
- const apiUrl = this.getApiUrl(this.getApi, query, ctx);
100
+ const apiUrl = this.getApiUrl(this.getApi, query, ctx, { from: "get" });
98
101
  this.axios
99
102
  .get(apiUrl, {
100
103
  ...this.axiosConf,
@@ -132,7 +135,7 @@ class DataModel {
132
135
  resultList = await this.getListFunc(query);
133
136
  } else {
134
137
  const getPro = new Promise((resolve, reject) => {
135
- const apiUrl = this.getApiUrl(this.getListApi, q, ctx);
138
+ const apiUrl = this.getApiUrl(this.getListApi, q, ctx, { from: "getList" });
136
139
  this.axios
137
140
  .get(apiUrl, {
138
141
  ...this.axiosConf,
@@ -162,15 +165,16 @@ class DataModel {
162
165
  }
163
166
 
164
167
  create(params, ctx) {
165
- let _params = _.cloneDeep(params);
166
- if (this.createReqMap) {
167
- _params = this.createReqMap(_params);
168
- }
169
168
  return new Promise((resolve, reject) => {
170
- const apiUrl = this.getApiUrl(this.createApi, _params, ctx);
171
169
  const opt = { ...this.axiosConf };
172
- if (_params instanceof FormData) {
170
+ let _params = _.cloneDeep(formDataToObj(params));
171
+ if (this.createReqMap) {
172
+ _params = this.createReqMap(_params, params);
173
+ }
174
+ const apiUrl = this.getApiUrl(this.createApi, _params, ctx, { from: "create" });
175
+ if (params instanceof FormData) {
173
176
  opt.headers = { "Content-Type": "multipart/form-data" };
177
+ _params = objToFormData(_params);
174
178
  }
175
179
  this.axios
176
180
  .post(apiUrl, _params, opt)
@@ -191,15 +195,16 @@ class DataModel {
191
195
  }
192
196
 
193
197
  update(params, ctx) {
194
- let _params = _.cloneDeep(params);
195
- if (this.updateReqMap) {
196
- _params = this.updateReqMap(_params);
197
- }
198
198
  return new Promise((resolve, reject) => {
199
- const apiUrl = this.getApiUrl(this.updateApi, _params, ctx);
200
199
  const opt = { ...this.axiosConf };
201
- if (_params instanceof FormData) {
200
+ let _params = _.cloneDeep(formDataToObj(params));
201
+ if (this.updateReqMap) {
202
+ _params = this.updateReqMap(_params, params);
203
+ }
204
+ const apiUrl = this.getApiUrl(this.updateApi, _params, ctx, { from: "update" });
205
+ if (params instanceof FormData) {
202
206
  opt.headers = { "Content-Type": "multipart/form-data" };
207
+ _params = objToFormData(_params);
203
208
  }
204
209
  this.axios
205
210
  .put(apiUrl, _params, opt)
@@ -237,6 +242,7 @@ class DataModel {
237
242
  this.deleteApi,
238
243
  Object.assign(_.cloneDeep(_config), _config.params, _config.data),
239
244
  ctx,
245
+ { from: "delete" },
240
246
  );
241
247
  this.axios
242
248
  .delete(apiUrl, { ...this.axiosConf, ..._config })
@@ -274,6 +280,7 @@ class DataModel {
274
280
  this.multipleDeleteApi,
275
281
  Object.assign(_.cloneDeep(_config), _config.params, _config.data),
276
282
  ctx,
283
+ { from: "multipleDelete" },
277
284
  );
278
285
  this.axios
279
286
  .delete(apiUrl, { ...this.axiosConf, ..._config })
@@ -357,6 +364,28 @@ export function setDefaultErrMsg(msg) {
357
364
  }
358
365
  }
359
366
 
367
+ export function objToFormData(data) {
368
+ if (data instanceof FormData) {
369
+ return data;
370
+ }
371
+ const formData = new FormData();
372
+ Object.keys(data)?.forEach((key) => {
373
+ formData.set(key, data[key]);
374
+ });
375
+ return formData;
376
+ }
377
+
378
+ export function formDataToObj(formData) {
379
+ if (!(formData instanceof FormData)) {
380
+ return formData;
381
+ }
382
+ const tempData = {};
383
+ for (const [key, value] of formData.entries()) {
384
+ tempData[key] = value;
385
+ }
386
+ return tempData;
387
+ }
388
+
360
389
  export { axios, DataModel, setDefaultAxios };
361
390
 
362
391
  export default DataModel;