Skip to content

Commit

Permalink
fix: only apply query parameter if options are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 28, 2022
1 parent 27d270f commit 49b1d0a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dependencies": {
"@faker-js/faker": "^7.6.0",
"locter": "^0.3.2",
"rapiq": "^0.3.1",
"rapiq": "^0.4.1",
"reflect-metadata": "^0.1.13",
"yargs": "^17.6.0"
},
Expand Down
37 changes: 37 additions & 0 deletions src/query/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
applyQuerySortParseOutput,
} from './parameter';
import { QueryApplyOptions, QueryApplyOutput } from './type';
import { isQueryOptionDefined } from './utils';

export function applyQueryParseOutput<T extends ObjectLiteral = ObjectLiteral>(
query: SelectQueryBuilder<T>,
Expand Down Expand Up @@ -49,10 +50,46 @@ export function applyQuery<T extends ObjectLiteral = ObjectLiteral>(
input: unknown,
options?: QueryApplyOptions<T>,
) : QueryApplyOutput {
options = options || {};

if (options.defaultAlias) {
options.defaultPath = options.defaultAlias;
}

if (
typeof options.fields === 'undefined' ||
!isQueryOptionDefined(options.fields, ['allowed', 'default'])
) {
options.fields = false;
}

if (
typeof options.filters === 'undefined' ||
!isQueryOptionDefined(options.fields, ['allowed', 'default'])
) {
options.filters = false;
}

if (
typeof options.pagination === 'undefined'
) {
options.pagination = false;
}

if (
typeof options.relations === 'undefined' ||
!isQueryOptionDefined(options.relations, ['allowed'])
) {
options.relations = false;
}

if (
typeof options.sort === 'undefined' ||
!isQueryOptionDefined(options.sort, ['allowed', 'default'])
) {
options.sort = false;
}

const output = applyQueryParseOutput(query, parseQuery(input, options));

return {
Expand Down
1 change: 1 addition & 0 deletions src/query/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './alias';
export * from './key';
export * from './option';
20 changes: 20 additions & 0 deletions src/query/utils/option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { hasOwnProperty } from '../../utils';

export function isQueryOptionDefined(
input: Record<string, any> | boolean,
option: string | string[],
) {
if (typeof input === 'boolean') {
return false;
}

const options = Array.isArray(option) ? option : [option];

for (let i = 0; i < options.length; i++) {
if (hasOwnProperty(input, options[i])) {
return true;
}
}

return false;
}

0 comments on commit 49b1d0a

Please sign in to comment.