Skip to content

Commit

Permalink
feat: remove parse-query-paramter fn
Browse files Browse the repository at this point in the history
BREAKING CHANGE: parseQueryParameter fn removed
  • Loading branch information
tada5hi committed Jan 14, 2024
1 parent 6dd65cc commit 6d8e1b9
Show file tree
Hide file tree
Showing 15 changed files with 5,784 additions and 4,970 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ coverage
npm-debug.log*
.idea
reports
docs/.vitepress/cache
File renamed without changes.
10,325 changes: 5,633 additions & 4,692 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,20 @@
"@rollup/plugin-node-resolve": "^15.1.0",
"@swc/core": "^1.3.100",
"@swc/jest": "^0.2.26",
"@tada5hi/commitlint-config": "^1.1.4",
"@tada5hi/eslint-config-typescript": "^1.2.0",
"@tada5hi/semantic-release": "^0.2.0",
"@tada5hi/commitlint-config": "^1.1.5",
"@tada5hi/eslint-config-typescript": "^1.2.8",
"@tada5hi/semantic-release": "^0.3.1",
"@tada5hi/tsconfig": "^0.5.0",
"@types/jest": "^29.5.11",
"@types/node": "^20.8.0",
"cross-env": "^7.0.3",
"eslint": "^8.44.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"rimraf": "^5.0.1",
"rollup": "^3.26.0",
"semantic-release": "^19.0.5",
"typescript": "^5.1.6",
"vitepress": "^1.0.0-beta.5",
"vue": "^3.3.3"
"rimraf": "^5.0.5",
"rollup": "^3.29.4",
"semantic-release": "^23.0.0",
"typescript": "^5.3.3",
"vitepress": "^1.0.0-rc.36"
}
}
31 changes: 16 additions & 15 deletions src/build/parameter/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ import type {
Parameter,
URLParameter,
} from '../../constants';
import type { ObjectLiteral } from '../../type';

export type BuildParameterInput<
P extends `${Parameter}` | `${URLParameter}`,
T extends Record<string, any> = Record<string, any>,
> =
P extends `${Parameter.FIELDS}` | `${URLParameter.FIELDS}` ?
FieldsBuildInput<T> :
P extends `${Parameter.FILTERS}` | `${URLParameter.FILTERS}` ?
FiltersBuildInput<T> :
P extends `${Parameter.RELATIONS}` | `${URLParameter.RELATIONS}` ?
RelationsBuildInput<T> :
P extends `${Parameter.PAGINATION}` | `${URLParameter.PAGINATION}` ?
PaginationBuildInput :
P extends `${Parameter.SORT}` | `${URLParameter.SORT}` ?
SortBuildInput<T> :
never;
export type BuildParametersInput<T extends ObjectLiteral = ObjectLiteral> = {
[Parameter.FIELDS]?: FieldsBuildInput<T>,
[Parameter.FILTERS]?: FiltersBuildInput<T>,
[Parameter.RELATIONS]?: RelationsBuildInput<T>,
[Parameter.PAGINATION]?: PaginationBuildInput,
[Parameter.SORT]?: SortBuildInput<T>,
};

export type BuildURLParametersInput<T extends ObjectLiteral = ObjectLiteral> = {
[URLParameter.FIELDS]?: FieldsBuildInput<T>,
[URLParameter.FILTERS]?: FiltersBuildInput<T>,
[URLParameter.RELATIONS]?: RelationsBuildInput<T>,
[URLParameter.PAGINATION]?: PaginationBuildInput,
[URLParameter.SORT]?: SortBuildInput<T>,
};
10 changes: 4 additions & 6 deletions src/build/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
* view the LICENSE file that was distributed with this source code.
*/

import type { Parameter, URLParameter } from '../constants';
import type { BuildParameterInput } from './parameter';
import type { ObjectLiteral } from '../type';
import type { BuildParametersInput, BuildURLParametersInput } from './parameter';

export type BuildInput<
T extends Record<string, any>,
> = {
[P in `${Parameter}` | `${URLParameter}`]?: BuildParameterInput<P, T>
};
T extends ObjectLiteral,
> = BuildParametersInput<T> & BuildURLParametersInput<T>;
33 changes: 13 additions & 20 deletions src/parameter/fields/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type {
Flatten, KeyWithOptionalPrefix, NestedKeys, OnlyObject, SimpleKeys,
Flatten, KeyWithOptionalPrefix, NestedKeys, ObjectLiteral, OnlyObject, SimpleKeys,
} from '../../type';
import type { RelationsParseOutput } from '../relations';
import type {
Expand All @@ -20,25 +20,18 @@ import type { FieldOperator } from './constants';

type FieldWithOperator<T extends string> = KeyWithOptionalPrefix<T, FieldOperator>;

export type FieldsBuildInput<T extends Record<string, any>> =
{
[K in keyof T]?: Flatten<T[K]> extends OnlyObject<T[K]> ?
FieldsBuildInput<Flatten<T[K]>> :
never
}
|
(
FieldWithOperator<SimpleKeys<T>>[]
|
{
[K in keyof T]?: Flatten<T[K]> extends OnlyObject<T[K]> ?
FieldsBuildInput<Flatten<T[K]>> :
never
}
)[]
|
FieldWithOperator<NestedKeys<T>>[] |
FieldWithOperator<NestedKeys<T>>;
export type FieldsBuildSimpleKeyInput<T extends ObjectLiteral = ObjectLiteral> = FieldWithOperator<SimpleKeys<T>>;
export type FieldsBuildNestedKeyInput<T extends ObjectLiteral = ObjectLiteral> = FieldWithOperator<NestedKeys<T>>;
export type FieldsBuildRecordInput<T extends ObjectLiteral = ObjectLiteral> = {
[K in keyof T]?: Flatten<T[K]> extends OnlyObject<T[K]> ?
FieldsBuildInput<Flatten<T[K]>> :
never
};

export type FieldsBuildInput<T extends ObjectLiteral> = FieldsBuildRecordInput<T> |
(FieldsBuildSimpleKeyInput[] | FieldsBuildRecordInput<T>)[] |
FieldsBuildNestedKeyInput[] |
FieldsBuildNestedKeyInput;

// -----------------------------------------------------------
// Parse
Expand Down
150 changes: 62 additions & 88 deletions src/parse/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
* view the LICENSE file that was distributed with this source code.
*/

import {
parseQueryFields,
parseQueryFilters,
parseQueryPagination,
parseQueryRelations,
parseQuerySort,
} from '../parameter';
import type {
FieldsParseOutput,
FiltersParseOutput,
PaginationParseOutput,
RelationsParseOutput,
SortParseOutput,
} from '../parameter';
import { Parameter, URLParameter } from '../constants';
import type { ObjectLiteral } from '../type';
import { buildQueryParameterOptions, isQueryParameterEnabled, parseQueryParameter } from './parameter';
import { buildQueryParameterOptions, isQueryParameterEnabled } from './parameter';
import type { ParseInput, ParseOptions, ParseOutput } from './type';

export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
Expand All @@ -23,21 +26,19 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(
) : ParseOutput {
options = options || {};

const mergeWithGlobalOptions = <T extends {[key: string]: any} & {
const mergeWithGlobalOptions = <T extends {
defaultPath?: string,
throwOnError?: boolean
}>(data?: T) : T => {
if (typeof data !== 'undefined') {
if (typeof data.defaultPath === 'undefined') {
data.defaultPath = options.defaultPath;
}
throwOnFailure?: boolean
}>(data: T) : T => {
if (typeof data.defaultPath === 'undefined') {
data.defaultPath = options.defaultPath;
}

if (typeof data.throwOnError === 'undefined') {
data.throwOnError = options.throwOnFailure;
}
if (typeof data.throwOnFailure === 'undefined') {
data.throwOnFailure = options.throwOnFailure;
}

return data || {} as T;
return data;
};

const output : ParseOutput = {};
Expand All @@ -47,82 +48,55 @@ export function parseQuery<T extends ObjectLiteral = ObjectLiteral>(

let relations : RelationsParseOutput | undefined;

const keys : Parameter[] = [
// relations must be first parameter
Parameter.RELATIONS,
let value = input[Parameter.RELATIONS] || input[URLParameter.RELATIONS];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.RELATIONS] })) {
relations = parseQueryRelations(
value,
buildQueryParameterOptions(options[Parameter.RELATIONS]),
);

output[Parameter.RELATIONS] = relations;
}

Parameter.FIELDS,
Parameter.FILTERS,
Parameter.PAGINATION,
Parameter.SORT,
];
value = input[Parameter.FIELDS] || input[URLParameter.FIELDS];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.FIELDS] })) {
output[Parameter.FIELDS] = parseQueryFields(
value,
{
...mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.FIELDS])),
...(relations ? { relations } : {}),
},
);
}

for (let i = 0; i < keys.length; i++) {
const key : Parameter = keys[i];
value = input[Parameter.FILTERS] || input[URLParameter.FILTERS];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.FILTERS] })) {
output[Parameter.FILTERS] = parseQueryFilters(
value,
{
...mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.FILTERS])),
...(relations ? { relations } : {}),
},
);
}

switch (key) {
case Parameter.RELATIONS: {
const value = input[Parameter.RELATIONS] || input[URLParameter.RELATIONS];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.RELATIONS] })) {
relations = parseQueryParameter(
key,
value,
buildQueryParameterOptions(options[Parameter.RELATIONS]),
);
value = input[Parameter.PAGINATION] || input[URLParameter.PAGINATION];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.PAGINATION] })) {
output[Parameter.PAGINATION] = parseQueryPagination(
value,
mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.PAGINATION])),
);
}

output[Parameter.RELATIONS] = relations;
}
break;
}
case Parameter.FIELDS: {
const value = input[Parameter.FIELDS] || input[URLParameter.FIELDS];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.FIELDS] })) {
output[Parameter.FIELDS] = parseQueryParameter(
key,
value,
mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.FIELDS])),
relations,
) as FieldsParseOutput;
}
break;
}
case Parameter.FILTERS: {
const value = input[Parameter.FILTERS] || input[URLParameter.FILTERS];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.FILTERS] })) {
output[Parameter.FILTERS] = parseQueryParameter(
key,
value,
mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.FILTERS])),
relations,
) as FiltersParseOutput;
}
break;
}
case Parameter.PAGINATION: {
const value = input[Parameter.PAGINATION] || input[URLParameter.PAGINATION];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.PAGINATION] })) {
output[Parameter.PAGINATION] = parseQueryParameter(
key,
value,
buildQueryParameterOptions(options[Parameter.PAGINATION]),
relations,
) as PaginationParseOutput;
}
break;
}
case Parameter.SORT: {
const value = input[Parameter.SORT] || input[URLParameter.SORT];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.SORT] })) {
output[Parameter.SORT] = parseQueryParameter(
key,
value,
mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.SORT])),
relations,
) as SortParseOutput;
}
break;
}
}
value = input[Parameter.SORT] || input[URLParameter.SORT];
if (isQueryParameterEnabled({ data: value, options: options[Parameter.SORT] })) {
output[Parameter.SORT] = parseQuerySort(
value,
{
...mergeWithGlobalOptions(buildQueryParameterOptions(options[Parameter.SORT])),
...(relations ? { relations } : {}),
},
);
}

return output;
Expand Down
1 change: 0 additions & 1 deletion src/parse/parameter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
* view the LICENSE file that was distributed with this source code.
*/

export * from './module';
export * from './type';
export * from './utils';
Loading

0 comments on commit 6d8e1b9

Please sign in to comment.