From 90f32bb3e521b02d3871615179aac3f4ce456c20 Mon Sep 17 00:00:00 2001 From: Tarek BAZINE Date: Fri, 12 Nov 2021 15:23:56 +0100 Subject: [PATCH 1/2] CHG - Provide options for qs url params stringify #748 --- .../crud-request/src/request-query.builder.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/crud-request/src/request-query.builder.ts b/packages/crud-request/src/request-query.builder.ts index 027b1083..f294ca6c 100644 --- a/packages/crud-request/src/request-query.builder.ts +++ b/packages/crud-request/src/request-query.builder.ts @@ -1,14 +1,7 @@ -import { - hasValue, - isObject, - isString, - isArrayFull, - isNil, - isUndefined, -} from '@nestjsx/util'; -import { stringify } from 'qs'; +import { hasValue, isArrayFull, isNil, isObject, isString, isUndefined } from '@nestjsx/util'; +import { IStringifyOptions, stringify } from 'qs'; -import { RequestQueryBuilderOptions, CreateQueryParams } from './interfaces'; +import { CreateQueryParams, RequestQueryBuilderOptions } from './interfaces'; import { validateCondition, validateFields, @@ -87,12 +80,12 @@ export class RequestQueryBuilder { }); } - query(encode = true): string { + query(encode = true, options: IStringifyOptions = { arrayFormat: 'brackets' }): string { if (this.queryObject[this.paramNames.search]) { this.queryObject[this.paramNames.filter] = undefined; this.queryObject[this.paramNames.or] = undefined; } - this.queryString = stringify(this.queryObject, { encode }); + this.queryString = stringify(this.queryObject, { encode, ...options }); return this.queryString; } From 4f9e04b8fa5d8cf1ee07e5a18f1ed78ae6ef732d Mon Sep 17 00:00:00 2001 From: Tarek BAZINE Date: Fri, 12 Nov 2021 16:14:45 +0100 Subject: [PATCH 2/2] CHG - check for duplicate join relation --- .../crud-request/src/request-query.builder.ts | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/crud-request/src/request-query.builder.ts b/packages/crud-request/src/request-query.builder.ts index f294ca6c..405ecfc7 100644 --- a/packages/crud-request/src/request-query.builder.ts +++ b/packages/crud-request/src/request-query.builder.ts @@ -22,6 +22,12 @@ import { // tslint:disable:variable-name ban-types export class RequestQueryBuilder { + public queryObject: { [key: string]: any } = {}; + public queryString: string; + private paramNames: { + [key in keyof RequestQueryBuilderOptions['paramNamesMap']]: string; + } = {}; + constructor() { this.setParamNames(); } @@ -43,11 +49,10 @@ export class RequestQueryBuilder { includeDeleted: 'include_deleted', }, }; - private paramNames: { - [key in keyof RequestQueryBuilderOptions['paramNamesMap']]: string; - } = {}; - public queryObject: { [key: string]: any } = {}; - public queryString: string; + + get options(): RequestQueryBuilderOptions { + return RequestQueryBuilder._options; + } static setOptions(options: RequestQueryBuilderOptions) { RequestQueryBuilder._options = { @@ -69,10 +74,6 @@ export class RequestQueryBuilder { return isObject(params) ? qb.createFromParams(params) : qb; } - get options(): RequestQueryBuilderOptions { - return RequestQueryBuilder._options; - } - setParamNames() { Object.keys(RequestQueryBuilder._options.paramNamesMap).forEach((key) => { const name = RequestQueryBuilder._options.paramNamesMap[key]; @@ -122,7 +123,9 @@ export class RequestQueryBuilder { ...(Array.isArray(j) && !isString(j[0]) ? (j as Array).map((o) => this.addJoin(o)) : [this.addJoin(j as QueryJoin | QueryJoinArr)]), - ]; + ].filter((item, pos) => { + return this.queryObject[param].indexOf(item) === pos; + }); } return this; }