Skip to content

Commit

Permalink
fix: allow enable/disable parameter parsing by boolean for parseQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 28, 2022
1 parent d4ed217 commit 2b29f41
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/parse/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends ObjectLiteral = ObjectLiteral>(
Expand Down Expand Up @@ -56,11 +56,11 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
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;
Expand All @@ -69,47 +69,47 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
}
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;
}
break;
}
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;
}
break;
}
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;
}
break;
}
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;
}
Expand Down
1 change: 1 addition & 0 deletions src/parse/parameter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export * from './module';
export * from './type';
export * from './utils';
26 changes: 26 additions & 0 deletions src/parse/parameter/utils.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Record<string, any>>(
input?: T | boolean,
) : T {
if (typeof input === 'boolean') {
return {} as T;
}

return input;
}

export function isQueryParameterEnabled<T extends Record<string, any>>(
input?: T | boolean,
) : boolean {
if (typeof input === 'boolean') {
return input;
}

return true;
}
2 changes: 1 addition & 1 deletion src/parse/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type ParseOptions<T extends ObjectLiteral = ObjectLiteral> = {
/**
* On default all query keys are enabled.
*/
[P in `${Parameter}`]?: ParseParameterOptions<P, T>
[P in `${Parameter}`]?: boolean | ParseParameterOptions<P, T>
} & {
defaultPath?: string
};
Expand Down
48 changes: 48 additions & 0 deletions test/unit/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 2b29f41

Please sign in to comment.