Skip to content

Commit

Permalink
fix: applying default fields with alias
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 20, 2022
1 parent 00f86ec commit 177f5cd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/parameter/sort/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { ObjectLiteral } from '../../type';
import {
applyMapping,
buildFieldWithPath, flattenNestedObject,
buildFieldWithPath, buildKeyPath, flattenNestedObject,
getFieldDetails,
hasOwnProperty, isFieldNonRelational,
isFieldPathAllowedByRelations,
Expand Down Expand Up @@ -44,9 +44,16 @@ function buildDefaultSortParseOutput<T extends ObjectLiteral = ObjectLiteral>(
for (let i = 0; i < keys.length; i++) {
const fieldDetails = getFieldDetails(keys[i]);

let path : string | undefined;
if (fieldDetails.path) {
path = fieldDetails.path;
} else if (options.defaultPath) {
path = options.defaultPath;
}

output.push({
key: fieldDetails.name,
...(fieldDetails.path ? { alias: fieldDetails.path } : {}),
...(path ? { path } : {}),
value: flatten[keys[i]],
});
}
Expand Down Expand Up @@ -176,10 +183,17 @@ export function parseQuerySort<T extends ObjectLiteral = ObjectLiteral>(
const keyPaths = flattenParseOptionsAllowed(options.allowed[i]);

for (let j = 0; j < keyPaths.length; j++) {
const keyWithAlias : string = keyPaths[j];
const key : string = keyWithAlias.includes('.') ?
keyWithAlias.split('.').pop() :
keyWithAlias;
let keyWithAlias : string = keyPaths[j];
let key : string;

const parts = keyWithAlias.split('.');
if (parts.length > 0) {
key = parts.pop();
} else {
key = keyWithAlias;

keyWithAlias = buildKeyPath(key, options.defaultPath);
}

if (
hasOwnProperty(items, key) ||
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
export * from './array';
export * from './mapping';
export * from './field';
export * from './key';
export * from './relation';
export * from './object';
export * from './simple';
Expand Down
7 changes: 7 additions & 0 deletions src/utils/key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function buildKeyWithPrefix(name: string, prefix?: string) {
if (prefix) {
return `${prefix}.${name}`;
}

return name;
}
17 changes: 16 additions & 1 deletion test/unit/fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ describe('src/fields/index.ts', () => {
defaultPath: 'user'
};

let data = parseQueryFields('+email', options);
let data = parseQueryFields([], options);
expect(data).toEqual([
{
key: 'id',
path: 'user'
},
{
key: 'name',
path: 'user'
},
{
key: 'email',
path: 'user'
}
] as FieldsParseOutput);

data = parseQueryFields('+email', options);
expect(data).toEqual([
{
key: 'email',
Expand Down

0 comments on commit 177f5cd

Please sign in to comment.