Skip to content

Commit

Permalink
fix: apply defaults if parse input has no parameter keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 18, 2023
1 parent 978da3a commit c4ba303
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/parse/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
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,
Expand All @@ -69,7 +69,7 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
}
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,
Expand All @@ -81,7 +81,7 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
}
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,
Expand All @@ -93,7 +93,7 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
}
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,
Expand All @@ -105,7 +105,7 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
}
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,
Expand Down
29 changes: 23 additions & 6 deletions src/parse/parameter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,29 @@ export function buildQueryParameterOptions<T extends Record<string, any>>(
return {} as T;
}

export function isQueryParameterEnabled<T extends Record<string, any>>(
input?: T | boolean,
) : boolean {
if (typeof input === 'boolean') {
return input;
type QueryParameterEnabledContext = {
data: unknown,
options?: Record<string, any> | 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;
}
36 changes: 36 additions & 0 deletions test/unit/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
1 change: 1 addition & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"include": [
"src/**/*",
"test/**/*.ts",
"commitlint.config.js",
"release.config.js",
"rollup.config.mjs"
Expand Down

0 comments on commit c4ba303

Please sign in to comment.