@hzab/data-model 1.0.0 → 1.0.2

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,12 @@
1
+ # @hzab/data-model@1.0.2
2
+
3
+ - fix: getApiUrl 增加调用来源,错误提示增加入参展示
4
+
5
+ # @hzab/data-model@1.0.1
6
+
7
+ - fix: 修复 delete 入参问题
8
+ - fix: getApiUrl :xx 参数替换正则处理
9
+
1
10
  # @hzab/data-model@0.2.3
2
11
 
3
12
  - fix: axios delete 入参写法问题修复
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/data-model",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "data model",
5
5
  "main": "src",
6
6
  "scripts": {
package/src/data-model.js CHANGED
@@ -71,15 +71,18 @@ 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);
80
83
  _.each(params, (value, key) => {
81
84
  if (!_.isString(value) || !_.isNumber(value) || _.isBoolean(value)) {
82
- apiUrl = apiUrl.replace(`:${key}`, value);
85
+ apiUrl = apiUrl.replace(new RegExp(`:${key}$|:${key}(?=\/)`), value);
83
86
  }
84
87
  });
85
88
  return apiUrl;
@@ -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,
@@ -167,7 +170,7 @@ class DataModel {
167
170
  _params = this.createReqMap(_params);
168
171
  }
169
172
  return new Promise((resolve, reject) => {
170
- const apiUrl = this.getApiUrl(this.createApi, _params, ctx);
173
+ const apiUrl = this.getApiUrl(this.createApi, _params, ctx, { from: "create" });
171
174
  const opt = { ...this.axiosConf };
172
175
  if (_params instanceof FormData) {
173
176
  opt.headers = { "Content-Type": "multipart/form-data" };
@@ -196,7 +199,7 @@ class DataModel {
196
199
  _params = this.updateReqMap(_params);
197
200
  }
198
201
  return new Promise((resolve, reject) => {
199
- const apiUrl = this.getApiUrl(this.updateApi, _params, ctx);
202
+ const apiUrl = this.getApiUrl(this.updateApi, _params, ctx, { from: "update" });
200
203
  const opt = { ...this.axiosConf };
201
204
  if (_params instanceof FormData) {
202
205
  opt.headers = { "Content-Type": "multipart/form-data" };
@@ -219,15 +222,28 @@ class DataModel {
219
222
  });
220
223
  }
221
224
 
222
- delete(params, ctx) {
223
- let _params = _.cloneDeep(params);
225
+ /**
226
+ * 删除接口
227
+ * @param {*} config axios.delete config 参数,
228
+ * @param {*} config.params axios.delete url 参数,
229
+ * @param {*} config.data axios.delete data 参数,
230
+ * @param {*} ctx
231
+ * @returns
232
+ */
233
+ delete(config, ctx) {
234
+ let _config = _.cloneDeep(config) || {};
224
235
  if (this.deleteReqMap) {
225
- _params = this.deleteReqMap(_params);
236
+ _config = this.deleteReqMap(_config);
226
237
  }
227
238
  return new Promise((resolve, reject) => {
228
- const apiUrl = this.getApiUrl(this.deleteApi, _params, ctx);
239
+ const apiUrl = this.getApiUrl(
240
+ this.deleteApi,
241
+ Object.assign(_.cloneDeep(_config), _config.params, _config.data),
242
+ ctx,
243
+ { from: "delete" },
244
+ );
229
245
  this.axios
230
- .delete(apiUrl, { ...this.axiosConf, params: _params })
246
+ .delete(apiUrl, { ...this.axiosConf, ..._config })
231
247
  .then((response) => {
232
248
  this.handleRes(
233
249
  response,
@@ -244,15 +260,28 @@ class DataModel {
244
260
  });
245
261
  }
246
262
 
247
- multipleDelete(params, ctx) {
248
- let _params = _.cloneDeep(params);
263
+ /**
264
+ * 批量删除接口
265
+ * @param {*} config axios.delete config 参数,
266
+ * @param {*} config.params axios.delete url 参数,
267
+ * @param {*} config.data axios.delete data 参数,
268
+ * @param {*} ctx
269
+ * @returns
270
+ */
271
+ multipleDelete(config, ctx) {
272
+ let _config = _.cloneDeep(config) || {};
249
273
  if (this.multipleDeleteReqMap) {
250
- _params = this.multipleDeleteReqMap(_params);
274
+ _config = this.multipleDeleteReqMap(_config);
251
275
  }
252
276
  return new Promise((resolve, reject) => {
253
- const apiUrl = this.getApiUrl(this.multipleDeleteApi, _params, ctx);
277
+ const apiUrl = this.getApiUrl(
278
+ this.multipleDeleteApi,
279
+ Object.assign(_.cloneDeep(_config), _config.params, _config.data),
280
+ ctx,
281
+ { from: "multipleDelete" },
282
+ );
254
283
  this.axios
255
- .delete(apiUrl, { ...this.axiosConf, params: _params })
284
+ .delete(apiUrl, { ...this.axiosConf, ..._config })
256
285
  .then((response) => {
257
286
  this.handleRes(
258
287
  response,