From 2580bb930ec40cd1550d67fbf20a9e88bdca9505 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Wed, 19 Oct 2022 10:09:51 +0200 Subject: [PATCH] fix: allow global default-path for parse-query method --- README.MD | 4 +--- docs/guide/parse.md | 11 +++++------ src/parse/module.ts | 17 ++++++++++++++--- src/parse/type.ts | 4 ++++ test/unit/parse.spec.ts | 19 +++++++++++++++++-- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/README.MD b/README.MD index 8ccab819..dd7a4e3f 100644 --- a/README.MD +++ b/README.MD @@ -260,12 +260,11 @@ export async function getUsers(req: Request, res: Response) { // const {fields, filter, include, page, sort} = req.query; const output: ParseOutput = parseQuery(req.query, { + defaultPath: 'user', fields: { - defaultAlias: 'user', allowed: ['id', 'name', 'realm.id', 'realm.name'], }, filters: { - defaultAlias: 'user', allowed: ['id', 'name', 'realm.id'], }, relations: { @@ -275,7 +274,6 @@ export async function getUsers(req: Request, res: Response) { maxLimit: 20 }, sort: { - defaultAlias: 'user', allowed: ['id', 'name', 'realm.id'], } }); diff --git a/docs/guide/parse.md b/docs/guide/parse.md index 973fede8..3a999eee 100644 --- a/docs/guide/parse.md +++ b/docs/guide/parse.md @@ -142,8 +142,9 @@ export async function getUsers(req: Request, res: Response) { // ----------------------------------------------------- const parsed = applyQueryParseOutput(query, { + defaultPath: 'user', fields: parseQueryFields(fields, { - defaultAlias: 'user', + defaultPath: 'user', // The fields id & name of the realm entity can only be used, // if the relation 'realm' is included. allowed: ['id', 'name', 'realm.id', 'realm.name'], @@ -151,7 +152,7 @@ export async function getUsers(req: Request, res: Response) { }), // only allow filtering users by id & name filters: parseQueryFilters(filter, { - defaultAlias: 'user', + defaultPath: 'user', // realm.id can only be used as filter key, // if the relation 'realm' is included. allowed: ['id', 'name', 'realm.id'], @@ -165,7 +166,7 @@ export async function getUsers(req: Request, res: Response) { maxLimit: 20 }), sort: parseQuerySort(sort, { - defaultAlias: 'user', + defaultPath: 'user', // profile.id can only be used as sorting key, // if the relation 'realm' is included. allowed: ['id', 'name', 'realm.id'], @@ -220,12 +221,11 @@ export async function getUsers(req: Request, res: Response) { // const {fields, filter, include, page, sort} = req.query; const output: ParseOutput = parseQuery(req.query, { + defaultPath: 'user', fields: { - defaultAlias: 'user', allowed: ['id', 'name', 'realm.id', 'realm.name'], }, filters: { - defaultAlias: 'user', allowed: ['id', 'name', 'realm.id'], }, relations: { @@ -235,7 +235,6 @@ export async function getUsers(req: Request, res: Response) { maxLimit: 20 }, sort: { - defaultAlias: 'user', allowed: ['id', 'name', 'realm.id'], } }); diff --git a/src/parse/module.ts b/src/parse/module.ts index 5810c6b9..6fde785b 100644 --- a/src/parse/module.ts +++ b/src/parse/module.ts @@ -22,7 +22,18 @@ export function parseQuery( ) : ParseOutput { options ??= {}; + const mergeWithGlobalOptions = (data: T) : T => { + if (options.defaultPath) { + data.defaultPath = options.defaultPath; + } + + return data; + }; + const output : ParseOutput = {}; + if (options.defaultPath) { + output.defaultPath = options.defaultPath; + } let relations : RelationsParseOutput | undefined; @@ -59,7 +70,7 @@ export function parseQuery( output[Parameter.FIELDS] = parseQueryParameter( key, value, - options[Parameter.FIELDS], + mergeWithGlobalOptions(options[Parameter.FIELDS]), relations, ) as FieldsParseOutput; } @@ -71,7 +82,7 @@ export function parseQuery( output[Parameter.FILTERS] = parseQueryParameter( key, value, - options[Parameter.FILTERS], + mergeWithGlobalOptions(options[Parameter.FILTERS]), relations, ) as FiltersParseOutput; } @@ -95,7 +106,7 @@ export function parseQuery( output[Parameter.SORT] = parseQueryParameter( key, value, - options[Parameter.SORT], + mergeWithGlobalOptions(options[Parameter.SORT]), relations, ) as SortParseOutput; } diff --git a/src/parse/type.ts b/src/parse/type.ts index 6570fe73..1e4ed41b 100644 --- a/src/parse/type.ts +++ b/src/parse/type.ts @@ -22,10 +22,14 @@ export type ParseOptions = { * On default all query keys are enabled. */ [K in `${Parameter}`]?: ParseParameterOptions +} & { + defaultPath?: string }; //------------------------------------------------ export type ParseOutput = { [K in `${Parameter}`]?: ParseParameterOutput +} & { + defaultPath?: string }; diff --git a/test/unit/parse.spec.ts b/test/unit/parse.spec.ts index 9ba45548..b7350a65 100644 --- a/test/unit/parse.spec.ts +++ b/test/unit/parse.spec.ts @@ -24,7 +24,6 @@ describe('src/parse.ts', () => { allowed: ['id'] } }); - expect(value).toEqual({ fields: [ { key: 'id' }, @@ -34,12 +33,28 @@ describe('src/parse.ts', () => { value = parseQuery({ [Parameter.FIELDS]: ['id', 'name'], }); - expect(value).toEqual({ fields: [] } as ParseOutput); }); + it('should parse query with default path', () => { + let value = parseQuery({ + fields: ['id', 'name'], + }, { + defaultPath: 'user', + fields: { + allowed: ['id'] + } + }); + expect(value).toEqual({ + defaultPath: 'user', + fields: [ + { key: 'id', path: 'user' }, + ], + } as ParseOutput); + }) + it('should parse field query parameter', () => { let value = parseQueryParameter(Parameter.FIELDS, ['id', 'name'], {allowed: ['id', 'name']}); expect(value).toEqual([