Skip to content

Commit 4747bcd

Browse files
authoredAug 25, 2022
feat(abc:st): add whether to ignore null or undefined value (#1515)
1 parent 923d4f4 commit 4747bcd

File tree

7 files changed

+32
-1
lines changed

7 files changed

+32
-1
lines changed
 

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

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class TestComponent {
138138
|----------|-------------|------|---------|---------------|
139139
| `[type]` | Pagination type, `page` used `pi`, `ps`; `skip` used `skip`, `limit` | `page,skip` | `page` ||
140140
| `[params]` | Request parameters, default to auto append `pi`, `ps` to URL | `any` | - | - |
141+
| `[ignoreParamNull]` | Whether to ignore `null` or `unfind` values in parameters | `Boolean` |`false` ||
141142
| `[method]` | Request method | `'POST','GET','HEAD','PUT','PATCH','DELETE'` | `'GET'` ||
142143
| `[body]` | Request body (only method is POST) | `any` | - | - |
143144
| `[headers]` | Request header | `any` | - ||

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

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class TestComponent {
138138
|----|----|----|-----|------|
139139
| `[type]` | 分页类型,`page` 使用 `pi``ps` 组合;`skip` 使用 `skip``limit` 组合 | `page,skip` | `page` ||
140140
| `[params]` | 额外请求参数,默认自动附加 `pi``ps` 至URL | `any` | - | - |
141+
| `[ignoreParamNull]` | 是否忽略参数中 `null``undefind`| `Boolean` |`false` ||
141142
| `[method]` | 请求方法 | `'POST','GET','HEAD','PUT','PATCH','DELETE'` | `'GET'` ||
142143
| `[body]` | 请求体 `body`,当 `method: POST` 时有效 | `any` | - | - |
143144
| `[headers]` | 请求体 `headers` | `any` | - ||

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ export class STDataSource {
273273
private getByRemote(url: string, options: STDataSourceOptions): Observable<unknown> {
274274
const { req, page, paginator, pi, ps, singleSort, multiSort, columns } = options;
275275
const method = (req.method || 'GET').toUpperCase();
276-
let params = {};
276+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
277+
let params: { [param: string]: any } = {};
277278
const reName = req.reName as STReqReNameType;
278279
if (paginator) {
279280
if (req.type === 'page') {
@@ -294,6 +295,11 @@ export class STDataSource {
294295
...this.getReqSortMap(singleSort, multiSort, columns),
295296
...this.getReqFilterMap(columns)
296297
};
298+
if (options.req.ignoreParamNull == true) {
299+
Object.keys(params).forEach(key => {
300+
if (params[key] == null) delete params[key];
301+
});
302+
}
297303

298304
let reqOptions: STRequestOptions = {
299305
params,

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

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const ST_DEFAULT_CONFIG: AlainSTConfig = {
1111
method: 'GET',
1212
allInBody: false,
1313
lazyLoad: false,
14+
ignoreParamNull: false,
1415
reName: { pi: 'pi', ps: 'ps', skip: 'skip', limit: 'limit' }
1516
},
1617
res: {

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

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export interface STReq {
6060
* - `{ status: 'new' }` => `url?pi=1&ps=10&status=new`
6161
*/
6262
params?: any;
63+
/**
64+
* Whether to ignore `null` or `unfind` values in parameters
65+
*
66+
* 是否忽略参数中 `null` 或 `undefind` 值
67+
*/
68+
ignoreParamNull?: Boolean;
6369
/** 请求方法,默认:`GET` */
6470
method?: string;
6571
/** 请求体 `body` */

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

+10
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ describe('abc: table: data-souce', () => {
367367
});
368368
});
369369
});
370+
it('should be ignoreParamNull', done => {
371+
options.req.ignoreParamNull = true;
372+
options.req.params = { a: null, b: 1 };
373+
options.req.process = res => {
374+
expect(Object.keys(res.params!!)).not.toContain(`a`);
375+
expect(Object.keys(res.params!!)).toContain(`b`);
376+
return res;
377+
};
378+
srv.process(options).subscribe(() => done());
379+
});
370380
});
371381
describe('[response]', () => {
372382
beforeEach(() => {

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

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ export interface AlainSTConfig {
4040
* - `skip` 使用 `skip`,`limit` 组合
4141
*/
4242
type?: 'page' | 'skip';
43+
/**
44+
* Whether to ignore `null` or `unfind` values in parameters
45+
*
46+
* 是否忽略参数中 `null` 或 `undefind` 值
47+
*/
48+
ignoreParamNull?: Boolean;
4349
/** 请求方法,默认:`GET` */
4450
method?: string;
4551
/** 请求体 `Header` */

0 commit comments

Comments
 (0)
Please sign in to comment.