Skip to content

Commit

Permalink
fix: typing for parse-query & parse-query-parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 21, 2022
1 parent b870863 commit 1924ea7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ import {
export async function getUsers(req: Request, res: Response) {
// const {fields, filter, include, page, sort} = req.query;

const output: ParseOutput = parseQuery(req.query, {
const output: ParseOutput = parseQuery<User>(req.query, {
defaultPath: 'user',
fields: {
allowed: ['id', 'name', 'realm.id', 'realm.name'],
Expand Down
12 changes: 5 additions & 7 deletions docs/guide/parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ import {
parseQueryRelations,
parseQueryPagination,
parseQuerySort,
Parameter
} from "rapiq";

import {
Expand Down Expand Up @@ -143,29 +142,29 @@ export async function getUsers(req: Request, res: Response) {

const parsed = applyQueryParseOutput(query, {
defaultPath: 'user',
fields: parseQueryFields(fields, {
fields: parseQueryFields<User>(fields, {
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, {
filters: parseQueryFilters<User>(filter, {
defaultPath: 'user',
// realm.id can only be used as filter key,
// if the relation 'realm' is included.
allowed: ['id', 'name', 'realm.id'],
relations: relationsParsed
}),
relations: parseQueryRelations(include, {
relations: parseQueryRelations<User>(include, {
allowed: ['items', 'realm']
}),
// only allow to select 20 items at maximum.
pagination: parseQueryPagination(page, {
maxLimit: 20
}),
sort: parseQuerySort(sort, {
sort: parseQuerySort<User>(sort, {
defaultPath: 'user',
// profile.id can only be used as sorting key,
// if the relation 'realm' is included.
Expand Down Expand Up @@ -200,7 +199,6 @@ import { Request, Response } from 'express';

import {
parseQuery,
Parameter,
ParseOutput
} from 'rapiq';

Expand All @@ -220,7 +218,7 @@ import {
export async function getUsers(req: Request, res: Response) {
// const {fields, filter, include, page, sort} = req.query;

const output: ParseOutput = parseQuery(req.query, {
const output: ParseOutput = parseQuery<User>(req.query, {
defaultPath: 'user',
fields: {
allowed: ['id', 'name', 'realm.id', 'realm.name'],
Expand Down
2 changes: 1 addition & 1 deletion src/parse/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ParseInput, ParseOptions, ParseOutput } from './type';

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

Expand Down
2 changes: 1 addition & 1 deletion src/parse/parameter/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function parseQueryParameter<
>(
key: P,
data: unknown,
options?: ParseParameterOptions<P, T extends unknown ? ObjectLiteral : T>,
options?: ParseParameterOptions<P, T>,
relations?: RelationsParseOutput,
): ParseParameterOutput<P> {
switch (key) {
Expand Down

0 comments on commit 1924ea7

Please sign in to comment.