Skip to content

Commit

Permalink
fix: allow global default-path for parse-query method
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 19, 2022
1 parent d16a4e6 commit 2580bb9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
4 changes: 1 addition & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -275,7 +274,6 @@ export async function getUsers(req: Request, res: Response) {
maxLimit: 20
},
sort: {
defaultAlias: 'user',
allowed: ['id', 'name', 'realm.id'],
}
});
Expand Down
11 changes: 5 additions & 6 deletions docs/guide/parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,17 @@ 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'],
relations: relationsParsed
}),
// 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'],
Expand All @@ -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'],
Expand Down Expand Up @@ -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: {
Expand All @@ -235,7 +235,6 @@ export async function getUsers(req: Request, res: Response) {
maxLimit: 20
},
sort: {
defaultAlias: 'user',
allowed: ['id', 'name', 'realm.id'],
}
});
Expand Down
17 changes: 14 additions & 3 deletions src/parse/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ export function parseQuery(
) : ParseOutput {
options ??= {};

const mergeWithGlobalOptions = <T extends {[key: string]: any} & {defaultPath?: string} >(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;

Expand Down Expand Up @@ -59,7 +70,7 @@ export function parseQuery(
output[Parameter.FIELDS] = parseQueryParameter(
key,
value,
options[Parameter.FIELDS],
mergeWithGlobalOptions(options[Parameter.FIELDS]),
relations,
) as FieldsParseOutput;
}
Expand All @@ -71,7 +82,7 @@ export function parseQuery(
output[Parameter.FILTERS] = parseQueryParameter(
key,
value,
options[Parameter.FILTERS],
mergeWithGlobalOptions(options[Parameter.FILTERS]),
relations,
) as FiltersParseOutput;
}
Expand All @@ -95,7 +106,7 @@ export function parseQuery(
output[Parameter.SORT] = parseQueryParameter(
key,
value,
options[Parameter.SORT],
mergeWithGlobalOptions(options[Parameter.SORT]),
relations,
) as SortParseOutput;
}
Expand Down
4 changes: 4 additions & 0 deletions src/parse/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ export type ParseOptions = {
* On default all query keys are enabled.
*/
[K in `${Parameter}`]?: ParseParameterOptions<K>
} & {
defaultPath?: string
};

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

export type ParseOutput = {
[K in `${Parameter}`]?: ParseParameterOutput<K>
} & {
defaultPath?: string
};
19 changes: 17 additions & 2 deletions test/unit/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('src/parse.ts', () => {
allowed: ['id']
}
});

expect(value).toEqual({
fields: [
{ key: 'id' },
Expand All @@ -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([
Expand Down

0 comments on commit 2580bb9

Please sign in to comment.