Skip to content

Commit

Permalink
fix: add generic argument for parse-query function
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 21, 2022
1 parent e38de51 commit ac0e89f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/build/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* view the LICENSE file that was distributed with this source code.
*/

import { ObjectLiteral } from '../type';
import { BuildInput } from './type';
import {
buildQueryFields,
Expand All @@ -22,7 +23,7 @@ import {
buildURLQueryString,
} from '../utils';

export function buildQuery<T extends Record<string, any>>(
export function buildQuery<T extends ObjectLiteral = ObjectLiteral>(
input?: BuildInput<T>,
) : string {
if (
Expand Down
5 changes: 3 additions & 2 deletions src/parse/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import {
SortParseOutput,
} from '../parameter';
import { Parameter, URLParameter } from '../constants';
import { ObjectLiteral } from '../type';
import { parseQueryParameter } from './parameter';
import { ParseInput, ParseOptions, ParseOutput } from './type';

export function parseQuery(
export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
input: ParseInput,
options?: ParseOptions,
options?: ParseOptions<T extends unknown ? ObjectLiteral : T>,
) : ParseOutput {
options ??= {};

Expand Down
22 changes: 13 additions & 9 deletions src/parse/parameter/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import {
import {
Parameter, URLParameter,
} from '../../constants';
import { ObjectLiteral } from '../../type';
import { ParseParameterOptions, ParseParameterOutput } from './type';

export function parseQueryParameter<K extends `${Parameter}` | `${URLParameter}`>(
key: K,
export function parseQueryParameter<
P extends `${Parameter}` | `${URLParameter}`,
T extends ObjectLiteral = ObjectLiteral,
>(
key: P,
data: unknown,
options?: ParseParameterOptions<K>,
options?: ParseParameterOptions<P, T extends unknown ? ObjectLiteral : T>,
relations?: RelationsParseOutput,
): ParseParameterOutput<K> {
): ParseParameterOutput<P> {
switch (key) {
case Parameter.FIELDS:
case URLParameter.FIELDS:
Expand All @@ -28,7 +32,7 @@ export function parseQueryParameter<K extends `${Parameter}` | `${URLParameter}`
...(invalidToEmptyObject(options)) as ParseParameterOptions<Parameter.FIELDS>,
...(relations ? { relations } : {}),
},
) as ParseParameterOutput<K>);
) as ParseParameterOutput<P>);
case Parameter.FILTERS:
case URLParameter.FILTERS:
return (parseQueryFilters(
Expand All @@ -37,31 +41,31 @@ export function parseQueryParameter<K extends `${Parameter}` | `${URLParameter}`
...(invalidToEmptyObject(options)) as ParseParameterOptions<Parameter.FILTERS>,
...(relations ? { relations } : {}),
},
) as ParseParameterOutput<K>);
) as ParseParameterOutput<P>);
case Parameter.PAGINATION:
case URLParameter.PAGINATION:
return (parseQueryPagination(
data,
{
...(invalidToEmptyObject(options)) as ParseParameterOptions<Parameter.PAGINATION>,
},
) as ParseParameterOutput<K>);
) as ParseParameterOutput<P>);
case Parameter.RELATIONS:
case URLParameter.RELATIONS:
return (parseQueryRelations(
data,
{
...(invalidToEmptyObject(options)) as ParseParameterOptions<Parameter.RELATIONS>,
},
) as ParseParameterOutput<K>);
) as ParseParameterOutput<P>);
default:
return (parseQuerySort(
data,
{
...(invalidToEmptyObject(options)) as ParseParameterOptions<Parameter.SORT>,
...(relations ? { relations } : {}),
},
) as ParseParameterOutput<K>);
) as ParseParameterOutput<P>);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/parse/parameter/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import {
} from '../../parameter';

import { Parameter, URLParameter } from '../../constants';
import { ObjectLiteral } from '../../type';

export type ParseParameterOptions<
P extends `${Parameter}` | `${URLParameter}`,
T extends Record<string, any> = Record<string, any>,
T extends ObjectLiteral = ObjectLiteral,
> =
P extends `${Parameter.FIELDS}` | `${URLParameter.FIELDS}` ?
FieldsParseOptions<T> :
Expand Down
5 changes: 3 additions & 2 deletions src/parse/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { Parameter, URLParameter } from '../constants';
import { ObjectLiteral } from '../type';

import { ParseParameterOptions, ParseParameterOutput } from './parameter';

Expand All @@ -17,11 +18,11 @@ export type ParseInput = {

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

export type ParseOptions = {
export type ParseOptions<T extends ObjectLiteral = ObjectLiteral> = {
/**
* On default all query keys are enabled.
*/
[K in `${Parameter}`]?: ParseParameterOptions<K>
[P in `${Parameter}`]?: ParseParameterOptions<P, T>
} & {
defaultPath?: string
};
Expand Down

0 comments on commit ac0e89f

Please sign in to comment.