From 2b29f41ed17ff1cd93b5b60ad0deedd92cc1ee41 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Fri, 28 Oct 2022 17:32:33 +0200 Subject: [PATCH] fix: allow enable/disable parameter parsing by boolean for parseQuery --- src/parse/module.ts | 22 ++++++++--------- src/parse/parameter/index.ts | 1 + src/parse/parameter/utils.ts | 26 +++++++++++++++++++ src/parse/type.ts | 2 +- test/unit/parse.spec.ts | 48 ++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 src/parse/parameter/utils.ts diff --git a/src/parse/module.ts b/src/parse/module.ts index 8646aede..2ade5ca5 100644 --- a/src/parse/module.ts +++ b/src/parse/module.ts @@ -14,7 +14,7 @@ import { } from '../parameter'; import { Parameter, URLParameter } from '../constants'; import { ObjectLiteral } from '../type'; -import { parseQueryParameter } from './parameter'; +import { buildQueryParameterOptions, isQueryParameterEnabled, parseQueryParameter } from './parameter'; import { ParseInput, ParseOptions, ParseOutput } from './type'; export function parseQuery( @@ -56,11 +56,11 @@ export function parseQuery( switch (key) { case Parameter.RELATIONS: { const value = input[Parameter.RELATIONS] ?? input[URLParameter.RELATIONS]; - if (value || options[Parameter.RELATIONS]) { + if (value && isQueryParameterEnabled(options[Parameter.RELATIONS])) { relations = parseQueryParameter( key, value, - options[Parameter.RELATIONS], + buildQueryParameterOptions(options[Parameter.RELATIONS]), ); output[Parameter.RELATIONS] = relations; @@ -69,11 +69,11 @@ export function parseQuery( } case Parameter.FIELDS: { const value = input[Parameter.FIELDS] ?? input[URLParameter.FIELDS]; - if (value || options[Parameter.FIELDS]) { + if (value && isQueryParameterEnabled(options[Parameter.FIELDS])) { output[Parameter.FIELDS] = parseQueryParameter( key, value, - mergeWithGlobalOptions(options[Parameter.FIELDS]), + mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.FIELDS])), relations, ) as FieldsParseOutput; } @@ -81,11 +81,11 @@ export function parseQuery( } case Parameter.FILTERS: { const value = input[Parameter.FILTERS] ?? input[URLParameter.FILTERS]; - if (value || options[Parameter.FILTERS]) { + if (value && isQueryParameterEnabled(options[Parameter.FILTERS])) { output[Parameter.FILTERS] = parseQueryParameter( key, value, - mergeWithGlobalOptions(options[Parameter.FILTERS]), + mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.FILTERS])), relations, ) as FiltersParseOutput; } @@ -93,11 +93,11 @@ export function parseQuery( } case Parameter.PAGINATION: { const value = input[Parameter.PAGINATION] ?? input[URLParameter.PAGINATION]; - if (value || options[Parameter.PAGINATION]) { + if (value && isQueryParameterEnabled(options[Parameter.PAGINATION])) { output[Parameter.PAGINATION] = parseQueryParameter( key, value, - options[Parameter.PAGINATION], + buildQueryParameterOptions(options[Parameter.PAGINATION]), relations, ) as PaginationParseOutput; } @@ -105,11 +105,11 @@ export function parseQuery( } case Parameter.SORT: { const value = input[Parameter.SORT] ?? input[URLParameter.SORT]; - if (value || options[Parameter.SORT]) { + if (value && isQueryParameterEnabled(options[Parameter.SORT])) { output[Parameter.SORT] = parseQueryParameter( key, value, - mergeWithGlobalOptions(options[Parameter.SORT]), + mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.SORT])), relations, ) as SortParseOutput; } diff --git a/src/parse/parameter/index.ts b/src/parse/parameter/index.ts index 16059b71..d1d7f5f8 100644 --- a/src/parse/parameter/index.ts +++ b/src/parse/parameter/index.ts @@ -7,3 +7,4 @@ export * from './module'; export * from './type'; +export * from './utils'; diff --git a/src/parse/parameter/utils.ts b/src/parse/parameter/utils.ts new file mode 100644 index 00000000..a3216b9b --- /dev/null +++ b/src/parse/parameter/utils.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +export function buildQueryParameterOptions>( + input?: T | boolean, +) : T { + if (typeof input === 'boolean') { + return {} as T; + } + + return input; +} + +export function isQueryParameterEnabled>( + input?: T | boolean, +) : boolean { + if (typeof input === 'boolean') { + return input; + } + + return true; +} diff --git a/src/parse/type.ts b/src/parse/type.ts index 7c5010ac..3a2f1147 100644 --- a/src/parse/type.ts +++ b/src/parse/type.ts @@ -22,7 +22,7 @@ export type ParseOptions = { /** * On default all query keys are enabled. */ - [P in `${Parameter}`]?: ParseParameterOptions + [P in `${Parameter}`]?: boolean | ParseParameterOptions } & { defaultPath?: string }; diff --git a/test/unit/parse.spec.ts b/test/unit/parse.spec.ts index ee10406d..5b4988ff 100644 --- a/test/unit/parse.spec.ts +++ b/test/unit/parse.spec.ts @@ -50,6 +50,54 @@ describe('src/parse.ts', () => { expect(value).toEqual({ fields: [] } as ParseOutput); + + value = parseQuery({ + [Parameter.FIELDS]: ['id', 'name'], + [Parameter.FILTERS]: { id: 1}, + [Parameter.PAGINATION]: { limit: 20 }, + [Parameter.RELATIONS]: ['relation'], + [Parameter.SORT]: {id: 'DESC'}, + }, { + fields: true, + filters: true, + pagination: true, + relations: true, + sort: true + }); + expect(value).toEqual({ + fields: [ + {key: 'id'}, + {key: 'name'} + ], + filters: [ + {key: 'id', value: 1, operator: FilterComparisonOperator.EQUAL} + ], + pagination: { + limit: 20, + offset: 0 + }, + relations: [ + { key: 'relation', value: 'relation'} + ], + sort: [ + { key: 'id', value: 'DESC'} + ] + } as ParseOutput); + + value = parseQuery({ + [Parameter.FIELDS]: ['id', 'name'], + [Parameter.FILTERS]: { id: 1}, + [Parameter.PAGINATION]: { limit: 20 }, + [Parameter.RELATIONS]: ['relation'], + [Parameter.SORT]: {id: 'DESC'}, + }, { + fields: false, + filters: false, + pagination: false, + relations: false, + sort: false + }); + expect(value).toEqual({} as ParseOutput); }); it('should parse query with default path', () => {