From 82f820cf1ce4425765795be0a859a39c5a838493 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Tue, 18 Oct 2022 17:59:02 +0200 Subject: [PATCH] feat: backported defaultAlias as defaultPath --- docs/guide/fields-api-reference.md | 1 + docs/guide/filters-api-reference.md | 1 + docs/guide/relations-api-reference.md | 1 - docs/guide/sort-api-reference.md | 1 + src/build/module.ts | 3 +-- src/build/type.ts | 4 ---- src/parameter/fields/parse.ts | 9 +++++++- src/parameter/fields/type.ts | 3 ++- src/parameter/filters/parse.ts | 30 +++++++++++++++------------ src/parameter/filters/type.ts | 1 + src/parameter/sort/parse.ts | 9 +++++++- src/parameter/sort/type.ts | 1 + test/unit/fields.spec.ts | 28 +++++++++++++++++-------- test/unit/filters.spec.ts | 13 ++++++++---- test/unit/sort.spec.ts | 8 +++---- 15 files changed, 73 insertions(+), 40 deletions(-) diff --git a/docs/guide/fields-api-reference.md b/docs/guide/fields-api-reference.md index 20caaecf..554ce70a 100644 --- a/docs/guide/fields-api-reference.md +++ b/docs/guide/fields-api-reference.md @@ -91,6 +91,7 @@ type FieldsParseOptions< mapping?: Record, allowed?: ParseOptionsAllowed, default?: ParseOptionsAllowed, + defaultPath?: string, relations?: RelationsParseOutput }; ``` diff --git a/docs/guide/filters-api-reference.md b/docs/guide/filters-api-reference.md index d01d6177..0fc845cb 100644 --- a/docs/guide/filters-api-reference.md +++ b/docs/guide/filters-api-reference.md @@ -162,6 +162,7 @@ type FiltersParseOptions< allowed?: ParseOptionsAllowed, default?: FiltersParseOptionsDefault, defaultByElement?: boolean, + defaultPath?: string, relations?: RelationsParseOutput }; ``` diff --git a/docs/guide/relations-api-reference.md b/docs/guide/relations-api-reference.md index e4e09e2f..4958c6e7 100644 --- a/docs/guide/relations-api-reference.md +++ b/docs/guide/relations-api-reference.md @@ -70,7 +70,6 @@ export type RelationsParseOptions< mapping?: Record, // set alternate value for relation key. pathMapping?: Record, - defaultAlias?: string, includeParents?: boolean | string[] | string }; ``` diff --git a/docs/guide/sort-api-reference.md b/docs/guide/sort-api-reference.md index b45fb604..0d872e7e 100644 --- a/docs/guide/sort-api-reference.md +++ b/docs/guide/sort-api-reference.md @@ -87,6 +87,7 @@ type SortParseOptions< allowed?: ParseOptionsAllowed | ParseOptionsAllowed[], mapping?: Record, default?: SortParseOptionsDefault, + defaultPath?: string, relations?: RelationsParseOutput, }; ``` diff --git a/src/build/module.ts b/src/build/module.ts index 8aef8905..37ed55ce 100644 --- a/src/build/module.ts +++ b/src/build/module.ts @@ -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, @@ -24,7 +24,6 @@ import { export function buildQuery>( input?: BuildInput, - options?: BuildOptions, ) : string { if ( typeof input === 'undefined' || diff --git a/src/build/type.ts b/src/build/type.ts index 0b74b0cf..53244ab1 100644 --- a/src/build/type.ts +++ b/src/build/type.ts @@ -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, > = { diff --git a/src/parameter/fields/parse.ts b/src/parameter/fields/parse.ts index ed13513f..ef866d52 100644 --- a/src/parameter/fields/parse.ts +++ b/src/parameter/fields/parse.ts @@ -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 } : {}), }); } } diff --git a/src/parameter/fields/type.ts b/src/parameter/fields/type.ts index 264a6f6c..c4908c1e 100644 --- a/src/parameter/fields/type.ts +++ b/src/parameter/fields/type.ts @@ -50,7 +50,8 @@ export type FieldsParseOptions< mapping?: Record, allowed?: ParseOptionsAllowed, default?: ParseOptionsAllowed, - relations?: RelationsParseOutput + defaultPath?: string, + relations?: RelationsParseOutput, }; export type FieldsParseOutputElement = { diff --git a/src/parameter/filters/parse.ts b/src/parameter/filters/parse.ts index c63030c4..74133b91 100644 --- a/src/parameter/filters/parse.ts +++ b/src/parameter/filters/parse.ts @@ -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') && @@ -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]], })); @@ -116,6 +113,8 @@ export function parseQueryFilters>( options?: FiltersParseOptions, ) : FiltersParseOutput { options = options ?? {}; + options.mapping = options.mapping || {}; + options.relations = options.relations || []; // If it is an empty array nothing is allowed if ( @@ -139,8 +138,6 @@ export function parseQueryFilters>( ); } - options = buildOptions(options); - const temp : Record = {}; // transform to appreciate data format & validate input @@ -193,8 +190,15 @@ export function parseQueryFilters>( 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, }; diff --git a/src/parameter/filters/type.ts b/src/parameter/filters/type.ts index b195567e..9871c2ee 100644 --- a/src/parameter/filters/type.ts +++ b/src/parameter/filters/type.ts @@ -69,6 +69,7 @@ export type FiltersParseOptions< allowed?: ParseOptionsAllowed, default?: FiltersParseOptionsDefault, defaultByElement?: boolean, + defaultPath?: string, relations?: RelationsParseOutput }; diff --git a/src/parameter/sort/parse.ts b/src/parameter/sort/parse.ts index 104dcc15..61251856 100644 --- a/src/parameter/sort/parse.ts +++ b/src/parameter/sort/parse.ts @@ -146,9 +146,16 @@ export function parseQuerySort>( 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, }; } diff --git a/src/parameter/sort/type.ts b/src/parameter/sort/type.ts index b182480e..776b8075 100644 --- a/src/parameter/sort/type.ts +++ b/src/parameter/sort/type.ts @@ -61,6 +61,7 @@ export type SortParseOptions< allowed?: ParseOptionsAllowed | ParseOptionsAllowed[], mapping?: Record, default?: SortParseOptionsDefault, + defaultPath?: string, relations?: RelationsParseOutput, }; export type SortParseOutputElement = { diff --git a/test/unit/fields.spec.ts b/test/unit/fields.spec.ts index aabeff1c..8af7ffbe 100644 --- a/test/unit/fields.spec.ts +++ b/test/unit/fields.spec.ts @@ -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); @@ -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', @@ -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', @@ -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', diff --git a/test/unit/filters.spec.ts b/test/unit/filters.spec.ts index 2d6a25dd..68571e0d 100644 --- a/test/unit/filters.spec.ts +++ b/test/unit/filters.spec.ts @@ -74,9 +74,10 @@ 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); @@ -84,6 +85,7 @@ describe('src/filter/index.ts', () => { expect(data).toEqual([ { key: 'id', + path: 'user', value: 1 } ] as FiltersParseOutput) @@ -153,14 +155,16 @@ 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 @@ -168,10 +172,11 @@ describe('src/filter/index.ts', () => { } ] 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); diff --git a/test/unit/sort.spec.ts b/test/unit/sort.spec.ts index cda184b2..a808c5de 100644 --- a/test/unit/sort.spec.ts +++ b/test/unit/sort.spec.ts @@ -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); }); @@ -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