diff --git a/src/parse/module.ts b/src/parse/module.ts index 0318e25b..e3295e5f 100644 --- a/src/parse/module.ts +++ b/src/parse/module.ts @@ -56,7 +56,7 @@ export function parseQuery( switch (key) { case Parameter.RELATIONS: { const value = input[Parameter.RELATIONS] || input[URLParameter.RELATIONS]; - if (value && isQueryParameterEnabled(options[Parameter.RELATIONS])) { + if (isQueryParameterEnabled({ data: value, options: options[Parameter.RELATIONS] })) { relations = parseQueryParameter( key, value, @@ -69,7 +69,7 @@ export function parseQuery( } case Parameter.FIELDS: { const value = input[Parameter.FIELDS] || input[URLParameter.FIELDS]; - if (value && isQueryParameterEnabled(options[Parameter.FIELDS])) { + if (isQueryParameterEnabled({ data: value, options: options[Parameter.FIELDS] })) { output[Parameter.FIELDS] = parseQueryParameter( key, value, @@ -81,7 +81,7 @@ export function parseQuery( } case Parameter.FILTERS: { const value = input[Parameter.FILTERS] || input[URLParameter.FILTERS]; - if (value && isQueryParameterEnabled(options[Parameter.FILTERS])) { + if (isQueryParameterEnabled({ data: value, options: options[Parameter.FILTERS] })) { output[Parameter.FILTERS] = parseQueryParameter( key, value, @@ -93,7 +93,7 @@ export function parseQuery( } case Parameter.PAGINATION: { const value = input[Parameter.PAGINATION] || input[URLParameter.PAGINATION]; - if (value && isQueryParameterEnabled(options[Parameter.PAGINATION])) { + if (isQueryParameterEnabled({ data: value, options: options[Parameter.PAGINATION] })) { output[Parameter.PAGINATION] = parseQueryParameter( key, value, @@ -105,7 +105,7 @@ export function parseQuery( } case Parameter.SORT: { const value = input[Parameter.SORT] || input[URLParameter.SORT]; - if (value && isQueryParameterEnabled(options[Parameter.SORT])) { + if (isQueryParameterEnabled({ data: value, options: options[Parameter.SORT] })) { output[Parameter.SORT] = parseQueryParameter( key, value, diff --git a/src/parse/parameter/utils.ts b/src/parse/parameter/utils.ts index 9fe07773..f165acf8 100644 --- a/src/parse/parameter/utils.ts +++ b/src/parse/parameter/utils.ts @@ -17,12 +17,29 @@ export function buildQueryParameterOptions>( return {} as T; } -export function isQueryParameterEnabled>( - input?: T | boolean, -) : boolean { - if (typeof input === 'boolean') { - return input; +type QueryParameterEnabledContext = { + data: unknown, + options?: Record | boolean +}; +export function isQueryParameterEnabled(context: QueryParameterEnabledContext) : boolean { + if (typeof context.options === 'boolean') { + return context.options; + } + + if ( + typeof context.data !== 'undefined' && + typeof context.options === 'undefined' + ) { + return true; + } + + if (isObject(context.options)) { + if (typeof context.options.default !== 'undefined') { + return true; + } + + return typeof context.data !== 'undefined'; } - return true; + return false; } diff --git a/test/unit/parse.spec.ts b/test/unit/parse.spec.ts index 1da346e5..af6f4400 100644 --- a/test/unit/parse.spec.ts +++ b/test/unit/parse.spec.ts @@ -100,6 +100,42 @@ describe('src/parse.ts', () => { expect(value).toEqual({} as ParseOutput); }); + it('should parse query with defaults', () => { + let value = parseQuery({ + fields: ['id', 'name'], + }, { + fields: { + default: ['id'] + } + }); + expect(value).toEqual({ + fields: [ + { key: 'id' }, + ], + } as ParseOutput); + + value = parseQuery<{ id: number, name: string }>({}, { + fields: { + default: ['id', 'name'] + }, + filters: { + default: { + id: 1 + } + } + }); + + expect(value).toEqual({ + fields: [ + { key: 'id' }, + { key: 'name' } + ], + filters: [ + {key: 'id', value: 1, operator: FilterComparisonOperator.EQUAL } + ] + } as ParseOutput); + }) + it('should parse query with default path', () => { let value = parseQuery({ fields: ['id', 'name'], diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json index ca224b76..59b0f306 100644 --- a/tsconfig.eslint.json +++ b/tsconfig.eslint.json @@ -2,6 +2,7 @@ "extends": "./tsconfig.json", "include": [ "src/**/*", + "test/**/*.ts", "commitlint.config.js", "release.config.js", "rollup.config.mjs"