Skip to content

Commit 9a0c96e

Browse files
authoredAug 14, 2022
feat(abc:st): add function of reName (#1500)
1 parent 8bebd30 commit 9a0c96e

File tree

7 files changed

+42
-16
lines changed

7 files changed

+42
-16
lines changed
 

‎packages/abc/st/index.en-US.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class TestComponent {
141141
| `[method]` | Request method | `'POST','GET','HEAD','PUT','PATCH','DELETE'` | `'GET'` ||
142142
| `[body]` | Request body (only method is POST) | `any` | - | - |
143143
| `[headers]` | Request header | `any` | - ||
144-
| `[reName]` | Map name `pi``ps` | `STReqReNameType` | `{ pi: 'pi', ps: 'ps', skip: 'skip', limit: 'limit' }` ||
144+
| `[reName]` | Map name `pi``ps` | `STReqReNameType, ((result: any, options: { pi: number; ps: number; total: number }) => { total: number; list: T[] })` | `{ pi: 'pi', ps: 'ps', skip: 'skip', limit: 'limit' }` ||
145145
| `[allInBody]` | Whether to request all parameter data into `body` (except `url` itself parameter) | `boolean` | `false` ||
146146
| `[lazyLoad]` | Whether to delay loading data in first render `st` component | `boolean` | `false` ||
147147
| `[process]` | Pre-request data processing | `(requestOptions: STRequestOptions) => STRequestOptions` | - ||

‎packages/abc/st/index.zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class TestComponent {
141141
| `[method]` | 请求方法 | `'POST','GET','HEAD','PUT','PATCH','DELETE'` | `'GET'` ||
142142
| `[body]` | 请求体 `body`,当 `method: POST` 时有效 | `any` | - | - |
143143
| `[headers]` | 请求体 `headers` | `any` | - ||
144-
| `[reName]` | 重命名请求参数 `pi``ps` | `STReqReNameType` | `{ pi: 'pi', ps: 'ps', skip: 'skip', limit: 'limit' }` ||
144+
| `[reName]` | 重命名请求参数 `pi``ps` | `STReqReNameType, ((result: any, options: { pi: number; ps: number; total: number }) => { total: number; list: T[] })` | `{ pi: 'pi', ps: 'ps', skip: 'skip', limit: 'limit' }` ||
145145
| `[allInBody]` | 是否将请求所有参数数据都放入 `body` 当中(`url` 地址本身参数除外),仅当 `method: 'POST'` 时有效 | `boolean` | `false` ||
146146
| `[lazyLoad]` | 是否延迟加载数据,即渲染结束后不会主动发起请求 | `boolean` | `false` ||
147147
| `[process]` | 请求前数据处理 | `(requestOptions: STRequestOptions) => STRequestOptions` | - ||

‎packages/abc/st/st-data-source.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,21 @@ export class STDataSource {
106106
retPs = retTotal;
107107
showPage = false;
108108
} else {
109-
// list
110-
ret = deepGet(result, res.reName!.list as string[], []);
111-
if (ret == null || !Array.isArray(ret)) {
112-
ret = [];
109+
const reName = res.reName!;
110+
if (typeof reName === 'function') {
111+
const fnRes = reName(result, { pi, ps, total });
112+
ret = fnRes.list;
113+
retTotal = fnRes.total;
114+
} else {
115+
// list
116+
ret = deepGet(result, reName.list as string[], []);
117+
if (ret == null || !Array.isArray(ret)) {
118+
ret = [];
119+
}
120+
// total
121+
const resultTotal = reName.total && deepGet(result, reName.total as string[], null);
122+
retTotal = resultTotal == null ? total || 0 : +resultTotal;
113123
}
114-
// total
115-
const resultTotal = res.reName!.total && deepGet(result, res.reName!.total as string[], null);
116-
retTotal = resultTotal == null ? total || 0 : +resultTotal;
117124
}
118125
return deepCopy(ret);
119126
})

‎packages/abc/st/st.component.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
147147
set res(value: STRes) {
148148
const item = (this._res = deepMergeKey({}, true, this.cog.res, value));
149149
const reName = item.reName!;
150-
if (!Array.isArray(reName.list)) reName.list = reName.list!.split('.');
151-
if (!Array.isArray(reName.total)) reName.total = reName.total!.split('.');
150+
if (typeof reName !== 'function') {
151+
if (!Array.isArray(reName.list)) reName.list = reName.list!.split('.');
152+
if (!Array.isArray(reName.total)) reName.total = reName.total!.split('.');
153+
}
152154
this._res = item;
153155
}
154156
@Input()

‎packages/abc/st/st.interfaces.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ export interface STRes<T extends STData = any> {
115115
* 重命名返回参数 `total`、`list`
116116
* - `{ total: 'Total' }` => Total 会被当作 `total`
117117
*/
118-
reName?: STResReNameType;
118+
reName?:
119+
| STResReNameType
120+
| ((result: any, options: { pi: number; ps: number; total: number }) => { total: number; list: T[] });
119121
/**
120122
* 数据预处理
121123
*/

‎packages/abc/st/test/st-data-source.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,16 @@ describe('abc: table: data-souce', () => {
393393
done();
394394
});
395395
});
396+
it('should be function re-name config', done => {
397+
options.res.reName = () => ({ total: 1, list: [{ a: 'L1' }] });
398+
spyOn(http, 'request').and.callFake(() => of({ L: genData(DEFAULT.ps), T: DEFAULT.ps }));
399+
srv.process(options).subscribe(res => {
400+
expect(res.total).toBe(1);
401+
expect(res.list.length).toBe(1);
402+
expect(res.list[0].a).toBe('L1');
403+
done();
404+
});
405+
});
396406
it('should be return empty when result is not array', done => {
397407
options.res.reName = { total: 'T', list: 'L' };
398408
spyOn(http, 'request').and.callFake(() => of({ L: 1, T: DEFAULT.ps }));

‎packages/util/config/abc/st.type.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ export interface AlainSTConfig {
7373
* 重命名返回参数 `total`、`list`,默认:`{ list: ['list'], total: ['total'] }`
7474
* - `{ total: 'Total' }` => Total 会被当作 `total`
7575
*/
76-
reName?: {
77-
total?: string | string[];
78-
list?: string | string[];
79-
};
76+
reName?:
77+
| {
78+
total?: string | string[];
79+
list?: string | string[];
80+
}
81+
| ((
82+
result: NzSafeAny,
83+
options: { pi: number; ps: number; total: number }
84+
) => { total: number; list: NzSafeAny[] });
8085
/**
8186
* 数据预处理
8287
*/

0 commit comments

Comments
 (0)
Please sign in to comment.