@hzab/data-model 2.0.0-alpha.0 → 2.0.0-alpha.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,6 +1,7 @@
1
- # @hzab/data-model@1.9.0
1
+ # @hzab/data-model@2.0.0
2
2
 
3
3
  break: axios 默认导出实例化后的 axios 实例,axios 源对象通过 axiosDef 导出
4
+ refactor: js 文件改为 js 文件
4
5
 
5
6
  # @hzab/data-model@1.8.8
6
7
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/data-model",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "data model",
5
5
  "main": "src",
6
6
  "scripts": {
package/src/ArrayUtils.ts CHANGED
@@ -133,7 +133,7 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
133
133
  * @returns 匹配的子项或 null
134
134
  */
135
135
  findItemById(id: string | number): T | null {
136
- if (this._unuseabled(id)) {
136
+ if (this._isInvalidId(id)) {
137
137
  return null;
138
138
  }
139
139
  const { _idKey } = this;
@@ -152,7 +152,7 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
152
152
  const id = query[this._idKey];
153
153
 
154
154
  // 如果有 ID 且 ID 有效,优先通过 ID 查询
155
- if (!this._unuseabled(id)) {
155
+ if (!this._isInvalidId(id)) {
156
156
  return this.findItemById(id);
157
157
  }
158
158
 
@@ -428,7 +428,7 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
428
428
  replaceItem(data: T): StateCode {
429
429
  const { _idKey, _list } = this;
430
430
  const id = data[_idKey];
431
- if (this._unuseabled(id)) {
431
+ if (this._isInvalidId(id)) {
432
432
  return STATE_CODE.ERR_INPUT_ID;
433
433
  }
434
434
  const idx = _list.findIndex((it) => it[_idKey] === id);
@@ -448,13 +448,14 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
448
448
  updateItemValue(data: Partial<T> & { [key: string]: any }): StateCode {
449
449
  const { _idKey, _list } = this;
450
450
  const id = data[_idKey];
451
- if (this._unuseabled(id)) {
451
+ if (this._isInvalidId(id)) {
452
452
  return STATE_CODE.ERR_INPUT_ID;
453
453
  }
454
- const item = cloneDeep(_list.find((it) => it[_idKey] === id));
455
- if (!item) {
454
+ const index = _list.findIndex((it) => it[_idKey] === id);
455
+ if (index < 0) {
456
456
  return STATE_CODE.NOT_FOUNT;
457
457
  }
458
+ const item = _list[index];
458
459
  Object.keys(data).forEach((key) => {
459
460
  if (key !== _idKey) {
460
461
  (item as any)[key] = data[key];
@@ -471,7 +472,7 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
471
472
  */
472
473
  delItem(id: any): StateCode {
473
474
  const { _idKey, _list } = this;
474
- if (this._unuseabled(id)) {
475
+ if (this._isInvalidId(id)) {
475
476
  return STATE_CODE.ERR_INPUT_ID;
476
477
  }
477
478
  const idx = _list.findIndex((it) => it[_idKey] === id);
@@ -482,6 +483,49 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
482
483
  return STATE_CODE.SUC;
483
484
  }
484
485
 
486
+ /**
487
+ * 清空所有数据
488
+ * @returns 状态码
489
+ */
490
+ clearAll(): StateCode {
491
+ this._list = [];
492
+ return STATE_CODE.SUC;
493
+ }
494
+
495
+ /**
496
+ * 批量删除
497
+ * @param ids 要删除的 ID 数组
498
+ * @returns 删除成功的数量
499
+ */
500
+ deleteItems(ids: any[]): number {
501
+ let successCount = 0;
502
+ ids.forEach((id) => {
503
+ const res = this.delItem(id);
504
+ if (res === STATE_CODE.SUC) {
505
+ successCount++;
506
+ }
507
+ });
508
+ return successCount;
509
+ }
510
+
511
+ /**
512
+ * 判断列表是否为空
513
+ * @returns 是否为空
514
+ */
515
+ isEmpty(): boolean {
516
+ return this._list.length === 0;
517
+ }
518
+
519
+ /**
520
+ * 获取指定范围的列表
521
+ * @param start 起始索引
522
+ * @param end 结束索引
523
+ * @returns 切片后的列表
524
+ */
525
+ slice(start?: number, end?: number): T[] {
526
+ return cloneDeep(this._list.slice(start, end));
527
+ }
528
+
485
529
  /**
486
530
  * 检查单个 item 是否匹配所有 query 条件
487
531
  * @param item - 待检查的列表项
@@ -624,11 +668,11 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
624
668
  }
625
669
 
626
670
  /**
627
- * 检查 ID 是否有效
671
+ * 检查 ID 是否无效
628
672
  * @param id 要检查的 ID
629
673
  * @returns ID 是否无效(为空)
630
674
  */
631
- protected _unuseabled(id: any): boolean {
675
+ protected _isInvalidId(id: any): boolean {
632
676
  return isNil(id) || id === "";
633
677
  }
634
678
 
@@ -638,7 +682,7 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
638
682
  * @returns 设置 ID 后的子项
639
683
  */
640
684
  protected _setId(item: T): T {
641
- if (isObject(item) && this._unuseabled((item as any)[this._idKey])) {
685
+ if (isObject(item) && this._isInvalidId((item as any)[this._idKey])) {
642
686
  (item as any)[this._idKey] = nanoid();
643
687
  }
644
688
  return item;
@@ -649,7 +693,7 @@ export class ArrayUtils<T extends Record<string, any> = Record<string, any>> {
649
693
  * @param item 要设置创建时间的子项
650
694
  */
651
695
  protected _setCreateTime(item: T): void {
652
- if (isObject(item) && this._unuseabled((item as any).createTime)) {
696
+ if (isObject(item) && this._isInvalidId((item as any).createTime)) {
653
697
  (item as any).createTime = Date.now();
654
698
  }
655
699
  }
@@ -481,10 +481,10 @@ class ArrayDataModel<T = any> extends ArrayUtils<T> {
481
481
  handleInputParams(
482
482
  params: any,
483
483
  reqMapFn?: (params: Record<string, any>, originalParams?: any) => Record<string, any>,
484
- ): Record<string, any> {
485
- let _params = cloneDeep(params);
484
+ ): Partial<T> & Record<string, any> {
485
+ let _params = cloneDeep(params) as Partial<T> & Record<string, any>;
486
486
  if (reqMapFn) {
487
- _params = reqMapFn.bind(this)(_params, params);
487
+ _params = reqMapFn(_params, params) as Partial<T> & Record<string, any>;
488
488
  }
489
489
  // TODO: FormData 文件存储?
490
490
  // if (params instanceof FormData) {
package/src/data-model.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import _ from "lodash";
2
- import { axios, axiosDef } from "./axios";
2
+ import { axios, isCancel } from "./axios";
3
3
  import {
4
4
  objToFormData,
5
5
  formDataToObj,
@@ -406,7 +406,7 @@ class DataModel<T = any> {
406
406
  const params = _.merge({}, record, ctx);
407
407
  _.each(params, (value, key) => {
408
408
  if (!_.isString(value) || !_.isNumber(value) || _.isBoolean(value)) {
409
- apiUrl = apiUrl.replace(new RegExp(`:${key}$|:${key}(?=\/)`), value);
409
+ apiUrl = apiUrl.replace(new RegExp(`:${key}$|:${key}(?=/)`), value);
410
410
  }
411
411
  });
412
412
  return apiUrl;
@@ -513,7 +513,7 @@ class DataModel<T = any> {
513
513
  * @param axiosConf axios 配置
514
514
  * @returns Promise<ResponseData>
515
515
  */
516
- create(params: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
516
+ create(params?: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
517
517
  return new Promise((resolve, reject) => {
518
518
  const opt: AxiosConfig = {
519
519
  ...this.axiosConf,
@@ -554,7 +554,7 @@ class DataModel<T = any> {
554
554
  * @param axiosConf axios 配置
555
555
  * @returns Promise<ResponseData>
556
556
  */
557
- update(params: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
557
+ update(params?: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
558
558
  return new Promise((resolve, reject) => {
559
559
  const opt: AxiosConfig = { ...this.axiosConf, ...this.updateAxiosConf, ...axiosConf };
560
560
  let _params = _.cloneDeep(formDataToObj(params));
@@ -591,7 +591,7 @@ class DataModel<T = any> {
591
591
  * @param axiosConf axios 配置
592
592
  * @returns Promise<ResponseData>
593
593
  */
594
- patch(params: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
594
+ patch(params?: RequestData | FormData, ctx?: JSONObject, axiosConf?: AxiosConfig): Promise<ResponseData> {
595
595
  return new Promise((resolve, reject) => {
596
596
  const opt: AxiosConfig = { ...this.axiosConf, ...this.patchAxiosConf, ...axiosConf };
597
597
  let _params = _.cloneDeep(formDataToObj(params));
@@ -704,8 +704,8 @@ class DataModel<T = any> {
704
704
  * @param reject 失败回调
705
705
  */
706
706
  handleRes(response: any, resolve: (res: any) => void, reject: (err: ApiError) => void) {
707
- if (axiosDef.isCancel(response)) {
708
- console.info("errorHandler axiosDef.isCancel", response);
707
+ if (isCancel(response)) {
708
+ console.info("errorHandler isCancel", response);
709
709
  return;
710
710
  }
711
711
  if (this.isResponse) {
@@ -755,8 +755,8 @@ class DataModel<T = any> {
755
755
  * @param reject Promise reject 回调
756
756
  */
757
757
  errorHandler(err: any = {}, reject: (err: ApiError) => void) {
758
- if (axiosDef.isCancel(err)) {
759
- console.info("errorHandler axiosDef.isCancel", err);
758
+ if (isCancel(err)) {
759
+ console.info("errorHandler isCancel", err);
760
760
  return;
761
761
  }
762
762
  if (this.isResponse) {
@@ -17,7 +17,7 @@ export function getPublicUrl(url: string | any, opt?: GetPublicUrlOptions): stri
17
17
  return url;
18
18
  }
19
19
  const { isFullPath } = opt || {};
20
- let baseURL = (process.env.WEBPACK_PUBLIC_PATH || ".")?.replace(/^\//, "")?.replace(/\/$/, "");
20
+ const baseURL = (process.env.WEBPACK_PUBLIC_PATH || ".")?.replace(/^\//, "")?.replace(/\/$/, "");
21
21
  if (isFullPath) {
22
22
  const fullPathname = (location.origin + location.pathname).replace(/[^\/]+.html\/?$/, "").replace(/\/$/, "");
23
23
  return `${fullPathname}/${baseURL?.replace(/^\./, "")}/${url?.replace(/^\//, "")}`;