Skip to content

Commit

Permalink
fix: add missing generic type argument for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 20, 2022
1 parent 4725ccb commit 01f1065
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/parameter/fields/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

import { createMerger } from 'smob';
import { ObjectLiteral } from '../../type';
import { FieldsBuildInput } from './type';
import { flattenToKeyPathArray, groupArrayByKeyPath } from '../../utils';

export function buildQueryFields<T>(
export function buildQueryFields<T extends ObjectLiteral = ObjectLiteral>(
input?: FieldsBuildInput<T>,
) : Record<string, string[]> | string[] {
if (typeof input === 'undefined') {
Expand All @@ -26,7 +27,7 @@ export function buildQueryFields<T>(
return data;
}

export function mergeQueryFields<T>(
export function mergeQueryFields(
target: Record<string, string[]> | string[],
source: Record<string, string[]> | string[],
): Record<string, string[]> | string[] {
Expand Down
5 changes: 3 additions & 2 deletions src/parameter/fields/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { merge } from 'smob';
import { ObjectLiteral } from '../../type';
import {
applyMapping, buildFieldWithPath, groupArrayByKeyPath, hasOwnProperty, isFieldPathAllowedByRelations,
} from '../../utils';
Expand Down Expand Up @@ -34,9 +35,9 @@ function buildReverseRecord(
return output;
}

export function parseQueryFields(
export function parseQueryFields<T extends ObjectLiteral = ObjectLiteral>(
data: unknown,
options?: FieldsParseOptions,
options?: FieldsParseOptions<T>,
) : FieldsParseOutput {
options ??= {};

Expand Down
3 changes: 2 additions & 1 deletion src/parameter/filters/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { merge } from 'smob';
import { ObjectLiteral } from '../../type';
import { FiltersBuildInput } from './type';
import { FilterOperator } from './constants';
import { isFilterOperatorConfig } from './utils';
Expand All @@ -21,7 +22,7 @@ const OperatorWeight = {
[FilterOperator.IN]: 13105,
};

export function buildQueryFilters<T>(
export function buildQueryFilters<T extends ObjectLiteral = ObjectLiteral>(
data?: FiltersBuildInput<T>,
) : Record<string, any> {
if (typeof data === 'undefined') {
Expand Down
7 changes: 4 additions & 3 deletions src/parameter/filters/parse.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 {
FieldDetails,
applyMapping,
Expand Down Expand Up @@ -55,8 +56,8 @@ function transformFiltersParseOutput(output: FiltersParseOutput) {
return output;
}

function buildDefaultFiltersParseOutput(
options: FiltersParseOptions,
function buildDefaultFiltersParseOutput<T extends ObjectLiteral = ObjectLiteral>(
options: FiltersParseOptions<T>,
input?: Record<string, FiltersParseOutputElement>,
) : FiltersParseOutput {
const inputKeys = Object.keys(input || {});
Expand Down Expand Up @@ -108,7 +109,7 @@ function buildDefaultFiltersParseOutput(
return input ? Object.values(input) : [];
}

export function parseQueryFilters<T extends Record<string, any>>(
export function parseQueryFilters<T extends ObjectLiteral = ObjectLiteral>(
data: unknown,
options?: FiltersParseOptions<T>,
) : FiltersParseOutput {
Expand Down
3 changes: 2 additions & 1 deletion src/parameter/pagination/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/

import { merge } from 'smob';
import { ObjectLiteral } from '../../type';
import { PaginationBuildInput } from './type';

export function mergeQueryPagination<T>(
export function mergeQueryPagination<T extends ObjectLiteral = ObjectLiteral>(
target?: PaginationBuildInput<T>,
source?: PaginationBuildInput<T>,
) : PaginationBuildInput<T> {
Expand Down
5 changes: 4 additions & 1 deletion src/parameter/pagination/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { PaginationParseOptions, PaginationParseOutput } from './type';

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

function finalizePagination(data: PaginationParseOutput, options: PaginationParseOptions) : PaginationParseOutput {
function finalizePagination(
data: PaginationParseOutput,
options: PaginationParseOptions,
) : PaginationParseOutput {
if (typeof options.maxLimit !== 'undefined') {
if (
typeof data.limit === 'undefined' ||
Expand Down
5 changes: 3 additions & 2 deletions src/parameter/relations/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

import { mergeArrays } from 'smob';
import { ObjectLiteral } from '../../type';
import { RelationsBuildInput } from './type';
import { flattenToKeyPathArray } from '../../utils';

export function buildQueryRelations<T>(
export function buildQueryRelations<T extends ObjectLiteral = ObjectLiteral>(
input?: RelationsBuildInput<T>,
) : string[] {
if (typeof input === 'undefined') {
Expand All @@ -19,7 +20,7 @@ export function buildQueryRelations<T>(
return flattenToKeyPathArray(input);
}

export function mergeQueryRelations<T>(
export function mergeQueryRelations(
target?: string[],
source?: string[],
) : string[] {
Expand Down
3 changes: 2 additions & 1 deletion src/parameter/relations/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import minimatch from 'minimatch';
import { ObjectLiteral } from '../../type';
import { applyMapping, hasOwnProperty } from '../../utils';
import { isPathCoveredByParseOptionsAllowed } from '../utils';

Expand All @@ -14,7 +15,7 @@ import { includeParents } from './utils';

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

export function parseQueryRelations<T extends Record<string, any> = Record<string, any>>(
export function parseQueryRelations<T extends ObjectLiteral = ObjectLiteral>(
data: unknown,
options?: RelationsParseOptions<T>,
): RelationsParseOutput {
Expand Down
7 changes: 5 additions & 2 deletions src/parameter/sort/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
*/

import { mergeArrays } from 'smob';
import { ObjectLiteral } from '../../type';
import { SortBuildInput, SortDirection } from './type';
import { flattenToKeyPathArray } from '../../utils';

export function buildQuerySort<T>(data?: SortBuildInput<T>) {
export function buildQuerySort<T extends ObjectLiteral = ObjectLiteral>(
data?: SortBuildInput<T>,
) {
if (typeof data === 'undefined') {
return [];
}
Expand Down Expand Up @@ -42,7 +45,7 @@ export function buildQuerySort<T>(data?: SortBuildInput<T>) {
});
}

export function mergeQuerySort<T>(
export function mergeQuerySort(
target?: string[],
source?: string[],
) {
Expand Down
7 changes: 5 additions & 2 deletions src/parameter/sort/parse.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 {
applyMapping,
buildFieldWithPath, flattenNestedObject,
Expand All @@ -31,7 +32,9 @@ function isMultiDimensionalArray(arr: unknown) : arr is unknown[][] {
return arr.length > 0 && Array.isArray(arr[0]);
}

function buildDefaultSortParseOutput<T extends Record<string, any>>(options: SortParseOptions<T>) : SortParseOutput {
function buildDefaultSortParseOutput<T extends ObjectLiteral = ObjectLiteral>(
options: SortParseOptions<T>,
) : SortParseOutput {
if (options.default) {
const output : SortParseOutput = [];

Expand Down Expand Up @@ -59,7 +62,7 @@ function buildDefaultSortParseOutput<T extends Record<string, any>>(options: Sor
* @param data
* @param options
*/
export function parseQuerySort<T extends Record<string, any>>(
export function parseQuerySort<T extends ObjectLiteral = ObjectLiteral>(
data: unknown,
options?: SortParseOptions<T>,
) : SortParseOutput {
Expand Down
1 change: 1 addition & 0 deletions src/type.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.
*/

export type ObjectLiteral = Record<string, any>;
export type Flatten<Type> = Type extends Array<infer Item> ? Item : Type;
export type OnlyScalar<T> = T extends string | number | boolean | undefined | null ? T : never;
export type OnlySingleObject<T> = T extends { [key: string]: any } ? T : never;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('src/fields/index.ts', () => {
] as FieldsParseOutput);

// if multiple possibilities are available for request field, use default
data = parseQueryFields(['id'], {
data = parseQueryFields<Record<string, any>>(['id'], {
allowed: { domain: ['id', 'name'], domain2: ['id', 'name'] },
default: { domain: ['id'], domain2: ['name']}
});
Expand Down

0 comments on commit 01f1065

Please sign in to comment.