Skip to content

Commit

Permalink
feat: backported defaultAlias as defaultPath
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 18, 2022
1 parent ab7f5a6 commit 82f820c
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 40 deletions.
1 change: 1 addition & 0 deletions docs/guide/fields-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type FieldsParseOptions<
mapping?: Record<string, string>,
allowed?: ParseOptionsAllowed<T>,
default?: ParseOptionsAllowed<T>,
defaultPath?: string,
relations?: RelationsParseOutput
};
```
Expand Down
1 change: 1 addition & 0 deletions docs/guide/filters-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type FiltersParseOptions<
allowed?: ParseOptionsAllowed<T>,
default?: FiltersParseOptionsDefault<T>,
defaultByElement?: boolean,
defaultPath?: string,
relations?: RelationsParseOutput
};
```
Expand Down
1 change: 0 additions & 1 deletion docs/guide/relations-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export type RelationsParseOptions<
mapping?: Record<string, string>,
// set alternate value for relation key.
pathMapping?: Record<string, string>,
defaultAlias?: string,
includeParents?: boolean | string[] | string
};
```
Expand Down
1 change: 1 addition & 0 deletions docs/guide/sort-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type SortParseOptions<
allowed?: ParseOptionsAllowed<T> | ParseOptionsAllowed<T>[],
mapping?: Record<string, string>,
default?: SortParseOptionsDefault<T>,
defaultPath?: string,
relations?: RelationsParseOutput,
};
```
Expand Down
3 changes: 1 addition & 2 deletions src/build/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* view the LICENSE file that was distributed with this source code.
*/

import { BuildInput, BuildOptions } from './type';
import { BuildInput } from './type';
import {
buildQueryFields,
buildQueryFilters,
Expand All @@ -24,7 +24,6 @@ import {

export function buildQuery<T extends Record<string, any>>(
input?: BuildInput<T>,
options?: BuildOptions,
) : string {
if (
typeof input === 'undefined' ||
Expand Down
4 changes: 0 additions & 4 deletions src/build/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import { Parameter, URLParameter } from '../constants';
import { BuildParameterInput } from './parameter';

export type BuildOptions = {
// empty type for now :)
};

export type BuildInput<
T extends Record<string, any>,
> = {
Expand Down
9 changes: 8 additions & 1 deletion src/parameter/fields/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,16 @@ export function parseQueryFields(

if (transformed.default.length > 0) {
for (let j = 0; j < transformed.default.length; j++) {
let path : string | undefined;
if (domainKey !== DEFAULT_ID) {
path = domainKey;
} else if (options.defaultPath) {
path = options.defaultPath;
}

output.push({
key: transformed.default[j],
...(domainKey !== DEFAULT_ID ? { path: domainKey } : {}),
...(path ? { path } : {}),
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/parameter/fields/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export type FieldsParseOptions<
mapping?: Record<string, string>,
allowed?: ParseOptionsAllowed<T>,
default?: ParseOptionsAllowed<T>,
relations?: RelationsParseOutput
defaultPath?: string,
relations?: RelationsParseOutput,
};

export type FieldsParseOutputElement = {
Expand Down
30 changes: 17 additions & 13 deletions src/parameter/filters/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ import { determineFilterOperatorLabelsByValue, transformFilterValue } from './ut

// --------------------------------------------------

function buildOptions(options?: FiltersParseOptions) : FiltersParseOptions {
options ??= {};

options.mapping = options.mapping || {};
options.relations = options.relations || [];

return options;
}

function transformFiltersParseOutputElement(element: FiltersParseOutputElement) : FiltersParseOutputElement {
if (
hasOwnProperty(element, 'path') &&
Expand Down Expand Up @@ -97,8 +88,14 @@ function buildDefaultFiltersParseOutput(
}

if (options.defaultByElement || inputKeys.length === 0) {
let path : string | undefined;
if (fieldDetails.path) {
path = fieldDetails.path;
} else if (options.defaultPath) {
path = options.defaultPath;
}
output.push(transformFiltersParseOutputElement({
...(fieldDetails.path ? { path: fieldDetails.path } : {}),
...(path ? { path } : {}),
key: fieldDetails.name,
value: flatten[keys[i]],
}));
Expand All @@ -116,6 +113,8 @@ export function parseQueryFilters<T extends Record<string, any>>(
options?: FiltersParseOptions<T>,
) : FiltersParseOutput {
options = options ?? {};
options.mapping = options.mapping || {};
options.relations = options.relations || [];

// If it is an empty array nothing is allowed
if (
Expand All @@ -139,8 +138,6 @@ export function parseQueryFilters<T extends Record<string, any>>(
);
}

options = buildOptions(options);

const temp : Record<string, FiltersParseOutputElement> = {};

// transform to appreciate data format & validate input
Expand Down Expand Up @@ -193,8 +190,15 @@ export function parseQueryFilters<T extends Record<string, any>>(
continue;
}

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

temp[fullKey] = {
...(fieldDetails.path ? { path: fieldDetails.path } : {}),
...(path ? { path } : {}),
key: fieldDetails.name,
value: value as string | boolean | number,
};
Expand Down
1 change: 1 addition & 0 deletions src/parameter/filters/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type FiltersParseOptions<
allowed?: ParseOptionsAllowed<T>,
default?: FiltersParseOptionsDefault<T>,
defaultByElement?: boolean,
defaultPath?: string,
relations?: RelationsParseOutput
};

Expand Down
9 changes: 8 additions & 1 deletion src/parameter/sort/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,16 @@ export function parseQuerySort<T extends Record<string, any>>(

matched = true;

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

items[keyWithAlias] = {
key: fieldDetails.name,
...(fieldDetails.path ? { path: fieldDetails.path } : {}),
...(path ? { path } : {}),
value: direction,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/parameter/sort/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type SortParseOptions<
allowed?: ParseOptionsAllowed<T> | ParseOptionsAllowed<T>[],
mapping?: Record<string, string>,
default?: SortParseOptionsDefault<T>,
defaultPath?: string,
relations?: RelationsParseOutput,
};
export type SortParseOutputElement = {
Expand Down
28 changes: 19 additions & 9 deletions test/unit/fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ describe('src/fields/index.ts', () => {
expect(transformedFields).toEqual({});
});

it('should transform fields with defaultAlias', () => {
it('should transform fields with defaultPath', () => {
let options : FieldsParseOptions = {
allowed: ['id', 'name', 'email']
allowed: ['id', 'name', 'email'],
defaultPath: 'user'
};

let data = parseQueryFields('+email', options);

expect(data).toEqual([
{
key: 'email'
key: 'email',
path: 'user'
}
] as FieldsParseOutput);

Expand All @@ -47,13 +49,15 @@ describe('src/fields/index.ts', () => {
{
domain: ['extra']
}
]
],
defaultPath: 'user'
}

data = parseQueryFields('+email', options);
expect(data).toEqual([
{
key: 'email',
path: 'user'
},
{
key: 'extra',
Expand All @@ -64,13 +68,16 @@ describe('src/fields/index.ts', () => {
data = parseQueryFields('+extra', options);
expect(data).toEqual([
{
key: 'id'
key: 'id',
path: 'user'
},
{
key: 'name'
key: 'name',
path: 'user'
},
{
key: 'email'
key: 'email',
path: 'user'
},
{
key: 'extra',
Expand All @@ -83,13 +90,16 @@ describe('src/fields/index.ts', () => {
}, options);
expect(data).toEqual([
{
key: 'id'
key: 'id',
path: 'user'
},
{
key: 'name',
path: 'user'
},
{
key: 'email'
key: 'email',
path: 'user'
},
{
key: 'extra',
Expand Down
13 changes: 9 additions & 4 deletions test/unit/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ describe('src/filter/index.ts', () => {
expect(allowedFilter).toEqual([] as FiltersParseOutput);
});

it('should transform fields with defaultAlias', () => {
it('should transform fields with default path', () => {
const options : FiltersParseOptions= {
allowed: ['id']
allowed: ['id'],
defaultPath: 'user'
};

const data = parseQueryFilters({ id: 1 }, options);

expect(data).toEqual([
{
key: 'id',
path: 'user',
value: 1
}
] as FiltersParseOutput)
Expand Down Expand Up @@ -153,25 +155,28 @@ describe('src/filter/index.ts', () => {
}
] as FiltersParseOutput);

data = parseQueryFilters({id: 5}, options);
data = parseQueryFilters({id: 5}, {...options, defaultPath: 'user'});
expect(data).toEqual([
{
key: 'id',
path: 'user',
value: 5,
},
{
key: 'age',
path: 'user',
value: 18,
operator: {
lessThan: true
}
}
] as FiltersParseOutput);

data = parseQueryFilters({id: 5}, {...options, defaultByElement: false});
data = parseQueryFilters({id: 5}, {...options, defaultByElement: false, defaultPath: 'user'});
expect(data).toEqual([
{
key: 'id',
path: 'user',
value: 5,
}
] as FiltersParseOutput);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ describe('src/sort/index.ts', () => {
transformed = parseQuerySort('-id', { allowed: ['id'] });
expect(transformed).toEqual([{ key: 'id', value: SortDirection.DESC }] as SortParseOutput);

// with alias mapping
// with mapping
transformed = parseQuerySort('-pit', { mapping: { pit: 'id' }, allowed: ['id'] });
expect(transformed).toEqual([{ key: 'id', value: SortDirection.DESC }] as SortParseOutput);

// with alias mapping & query alias
// with mapping & query alias
transformed = parseQuerySort('-pit', { mapping: { pit: 'id' }, allowed: ['id'] });
expect(transformed).toEqual([{ key: 'id', value: SortDirection.DESC }] as SortParseOutput);
});
Expand Down Expand Up @@ -109,9 +109,9 @@ describe('src/sort/index.ts', () => {
] as SortParseOutput);

// incomplete match
transformed = parseQuerySort(['email', 'id'], options);
transformed = parseQuerySort(['email', 'id'], {...options, defaultPath: 'user'});
expect(transformed).toStrictEqual([
{ key: 'id', value: SortDirection.ASC },
{ key: 'id', path: 'user', value: SortDirection.ASC },
] as SortParseOutput);

// no match
Expand Down

0 comments on commit 82f820c

Please sign in to comment.