Skip to content

Commit

Permalink
fix: parse query relations typing and allowed matching
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 18, 2022
1 parent 93ffacd commit b60f9f4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
16 changes: 4 additions & 12 deletions src/parameter/relations/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Record<string, any> = Record<string, any>>(
data: unknown,
options?: RelationsParseOptions,
options?: RelationsParseOptions<T>,
): RelationsParseOutput {
options ??= {};

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 0 additions & 8 deletions test/unit/relations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand Down

0 comments on commit b60f9f4

Please sign in to comment.