From b60f9f4649a2353534e1279a2c50f08dec64c53a Mon Sep 17 00:00:00 2001 From: tada5hi Date: Tue, 18 Oct 2022 22:19:57 +0200 Subject: [PATCH] fix: parse query relations typing and allowed matching --- src/parameter/relations/parse.ts | 16 ++++------------ test/unit/relations.spec.ts | 8 -------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/parameter/relations/parse.ts b/src/parameter/relations/parse.ts index f6cfe5fe..dfb1ecac 100644 --- a/src/parameter/relations/parse.ts +++ b/src/parameter/relations/parse.ts @@ -7,15 +7,16 @@ import minimatch from 'minimatch'; import { applyMapping, hasOwnProperty } from '../../utils'; +import { isPathCoveredByParseOptionsAllowed } from '../utils'; import { RelationsParseOptions, RelationsParseOutput } from './type'; import { includeParents } from './utils'; // -------------------------------------------------- -export function parseQueryRelations( +export function parseQueryRelations = Record>( data: unknown, - options?: RelationsParseOptions, + options?: RelationsParseOptions, ): RelationsParseOutput { options ??= {}; @@ -58,16 +59,7 @@ export function parseQueryRelations( } if (options.allowed) { - items = items - .filter((item) => { - for (let i = 0; i < options.allowed.length; i++) { - if (minimatch(item, options.allowed[i])) { - return true; - } - } - - return false; - }); + items = items.filter((item) => isPathCoveredByParseOptionsAllowed(options.allowed, item)); } if (options.includeParents) { diff --git a/test/unit/relations.spec.ts b/test/unit/relations.spec.ts index 8a70ab0d..3fe51fa7 100644 --- a/test/unit/relations.spec.ts +++ b/test/unit/relations.spec.ts @@ -75,14 +75,6 @@ describe('src/relations/index.ts', () => { { key: 'profile.photos', value: 'photos' }, ] as RelationsParseOutput); - // nested data with alias - allowed = parseQueryRelations(['profile.photos', 'profile.photos.abc', 'profile.abc'], { allowed: ['profile.photos**'] }); - expect(allowed).toEqual([ - { key: 'profile', value: 'profile' }, - { key: 'profile.photos', value: 'photos' }, - { key: 'profile.photos.abc', value: 'abc' }, - ] as RelationsParseOutput); - // null data allowed = parseQueryRelations(null); expect(allowed).toEqual([]);